Go to the source code of this file.
Functions | |
| & | DB ($params= '', $active_record_override=FALSE) |
| & DB | ( | $ | params = '', |
|
| $ | active_record_override = FALSE | |||
| ) |
Definition at line 25 of file DB.php.
References $active_group, $active_record, $db, and show_error().
Referenced by CI_Loader::database().
00026 { 00027 // Load the DB config file if a DSN string wasn't passed 00028 if (is_string($params) AND strpos($params, '://') === FALSE) 00029 { 00030 include(APPPATH.'config/database'.EXT); 00031 00032 if ( ! isset($db) OR count($db) == 0) 00033 { 00034 show_error('No database connection settings were found in the database config file.'); 00035 } 00036 00037 if ($params != '') 00038 { 00039 $active_group = $params; 00040 } 00041 00042 if ( ! isset($active_group) OR ! isset($db[$active_group])) 00043 { 00044 show_error('You have specified an invalid database connection group.'); 00045 } 00046 00047 $params = $db[$active_group]; 00048 } 00049 elseif (is_string($params)) 00050 { 00051 00052 /* parse the URL from the DSN string 00053 * Database settings can be passed as discreet 00054 * parameters or as a data source name in the first 00055 * parameter. DSNs must have this prototype: 00056 * $dsn = 'driver://username:password@hostname/database'; 00057 */ 00058 00059 if (($dns = @parse_url($params)) === FALSE) 00060 { 00061 show_error('Invalid DB Connection String'); 00062 } 00063 00064 $params = array( 00065 'dbdriver' => $dns['scheme'], 00066 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '', 00067 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '', 00068 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '', 00069 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : '' 00070 ); 00071 00072 // were additional config items set? 00073 if (isset($dns['query'])) 00074 { 00075 parse_str($dns['query'], $extra); 00076 00077 foreach($extra as $key => $val) 00078 { 00079 // booleans please 00080 if (strtoupper($val) == "TRUE") 00081 { 00082 $val = TRUE; 00083 } 00084 elseif (strtoupper($val) == "FALSE") 00085 { 00086 $val = FALSE; 00087 } 00088 00089 $params[$key] = $val; 00090 } 00091 } 00092 } 00093 00094 // No DB specified yet? Beat them senseless... 00095 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '') 00096 { 00097 show_error('You have not selected a database type to connect to.'); 00098 } 00099 00100 // Load the DB classes. Note: Since the active record class is optional 00101 // we need to dynamically create a class that extends proper parent class 00102 // based on whether we're using the active record class or not. 00103 // Kudos to Paul for discovering this clever use of eval() 00104 00105 if ($active_record_override == TRUE) 00106 { 00107 $active_record = TRUE; 00108 } 00109 00110 require_once(BASEPATH.'database/DB_driver'.EXT); 00111 00112 if ( ! isset($active_record) OR $active_record == TRUE) 00113 { 00114 require_once(BASEPATH.'database/DB_active_rec'.EXT); 00115 00116 if ( ! class_exists('CI_DB')) 00117 { 00118 eval('class CI_DB extends CI_DB_active_record { }'); 00119 } 00120 } 00121 else 00122 { 00123 if ( ! class_exists('CI_DB')) 00124 { 00125 eval('class CI_DB extends CI_DB_driver { }'); 00126 } 00127 } 00128 00129 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT); 00130 00131 // Instantiate the DB adapter 00132 $driver = 'CI_DB_'.$params['dbdriver'].'_driver'; 00133 $DB =& new $driver($params); 00134 00135 if ($DB->autoinit == TRUE) 00136 { 00137 $DB->initialize(); 00138 } 00139 00140 return $DB; 00141 }

