blob: 739e9d08e5cf5fe2c8896a67e2c155f877b2ac1b [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Initialize the database
20 *
21 * @category Database
22 * @author ExpressionEngine Dev Team
23 * @link http://codeigniter.com/user_guide/database/
24 */
Derek Jonesffca6c22009-12-05 15:31:44 +000025function &DB($params = '', $active_record_override = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000026{
27 // Load the DB config file if a DSN string wasn't passed
28 if (is_string($params) AND strpos($params, '://') === FALSE)
29 {
joelcoxe3da4282011-01-16 15:00:04 +010030
31 $file_path = APPPATH.'config/'.ENVIRONMENT.'/database'.EXT;
32
33 if ( ! file_exists($file_path))
34 {
35 log_message('debug', 'Database config for '.ENVIRONMENT.' environment is not found. Trying global config.');
36 $file_path = APPPATH.'config/database'.EXT;
37
38 if ( ! file_exists($file_path))
39 {
40 continue;
41 }
42 }
43
44 include($file_path);
Barry Mienydd671972010-10-04 16:33:58 +020045
Derek Allard2067d1a2008-11-13 22:59:24 +000046 if ( ! isset($db) OR count($db) == 0)
47 {
48 show_error('No database connection settings were found in the database config file.');
49 }
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 if ($params != '')
52 {
53 $active_group = $params;
54 }
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 if ( ! isset($active_group) OR ! isset($db[$active_group]))
57 {
58 show_error('You have specified an invalid database connection group.');
59 }
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 $params = $db[$active_group];
62 }
63 elseif (is_string($params))
64 {
Barry Mienydd671972010-10-04 16:33:58 +020065
Derek Allard2067d1a2008-11-13 22:59:24 +000066 /* parse the URL from the DSN string
Barry Mienydd671972010-10-04 16:33:58 +020067 * Database settings can be passed as discreet
68 * parameters or as a data source name in the first
69 * parameter. DSNs must have this prototype:
70 * $dsn = 'driver://username:password@hostname/database';
71 */
72
Derek Allard2067d1a2008-11-13 22:59:24 +000073 if (($dns = @parse_url($params)) === FALSE)
74 {
75 show_error('Invalid DB Connection String');
76 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 $params = array(
79 'dbdriver' => $dns['scheme'],
80 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
81 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
82 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
83 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
84 );
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 // were additional config items set?
87 if (isset($dns['query']))
88 {
89 parse_str($dns['query'], $extra);
90
91 foreach($extra as $key => $val)
92 {
93 // booleans please
94 if (strtoupper($val) == "TRUE")
95 {
96 $val = TRUE;
97 }
98 elseif (strtoupper($val) == "FALSE")
99 {
100 $val = FALSE;
101 }
102
103 $params[$key] = $val;
104 }
105 }
106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // No DB specified yet? Beat them senseless...
109 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
110 {
111 show_error('You have not selected a database type to connect to.');
112 }
113
114 // Load the DB classes. Note: Since the active record class is optional
115 // we need to dynamically create a class that extends proper parent class
116 // based on whether we're using the active record class or not.
117 // Kudos to Paul for discovering this clever use of eval()
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Jonesffca6c22009-12-05 15:31:44 +0000119 if ($active_record_override !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 {
Derek Jonesffca6c22009-12-05 15:31:44 +0000121 $active_record = $active_record_override;
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 require_once(BASEPATH.'database/DB_driver'.EXT);
125
126 if ( ! isset($active_record) OR $active_record == TRUE)
127 {
128 require_once(BASEPATH.'database/DB_active_rec'.EXT);
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 if ( ! class_exists('CI_DB'))
131 {
132 eval('class CI_DB extends CI_DB_active_record { }');
133 }
134 }
135 else
136 {
137 if ( ! class_exists('CI_DB'))
138 {
139 eval('class CI_DB extends CI_DB_driver { }');
140 }
141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
144
145 // Instantiate the DB adapter
146 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Pascal Kriete58560022010-11-10 16:01:20 -0500147 $DB = new $driver($params);
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 if ($DB->autoinit == TRUE)
150 {
151 $DB->initialize();
152 }
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Jonesc34e5782010-03-02 17:35:55 -0600154 if (isset($params['stricton']) && $params['stricton'] == TRUE)
155 {
Barry Mienydd671972010-10-04 16:33:58 +0200156 $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
Derek Jonesc34e5782010-03-02 17:35:55 -0600157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 return $DB;
Barry Mienydd671972010-10-04 16:33:58 +0200160}
Derek Allard2067d1a2008-11-13 22:59:24 +0000161
162
163
164/* End of file DB.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000165/* Location: ./system/database/DB.php */