DB.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 function &DB($params = '', $active_record_override = FALSE)
00026 {
00027
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
00053
00054
00055
00056
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
00073 if (isset($dns['query']))
00074 {
00075 parse_str($dns['query'], $extra);
00076
00077 foreach($extra as $key => $val)
00078 {
00079
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
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
00101
00102
00103
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
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 }
00142
00143
00144
00145
00146