/** * Database Loader * * @param string the DB credentials * @param bool whether to return the DB object * @param bool whether to enable active record (this allows us to override the config setting) * @return object */ publicfunctiondatabase($params = '', $return = FALSE, $active_record = NULL) { // Grab the super object $CI =& get_instance();
// Do we even need to load the database class? if (class_exists('CI_DB') AND $return == FALSE AND $active_record == NULL AND isset($CI->db) AND is_object($CI->db)) { returnFALSE; }
require_once(BASEPATH.'database/DB.php');
if ($return === TRUE) { returnDB($params, $active_record); }
// Initialize the db variable. Needed to prevent // reference errors with some configurations $CI->db = '';
// Load the DB class $CI->db =& DB($params, $active_record); }
/** * Initialize the database * * @category Database * @author EllisLab Dev Team * @link http://codeigniter.com/user_guide/database/ * @param string * @param bool Determines if active record should be used or not */ function &DB($params = '', $active_record_override = NULL) { // Load the DB config file if a DSN string wasn't passed // ... some code here require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');
// Instantiate the DB adapter $driver = 'CI_DB_'.$params['dbdriver'].'_driver'; $DB = new$driver($params);
if ($DB->autoinit == TRUE) { $DB->initialize(); } // some code here ... return$DB; }
/** * Initialize Database Settings * * @access private Called by the constructor * @param mixed * @return void */ functioninitialize() { // If an existing connection resource is available // there is no need to connect and select the database if (is_resource($this->conn_id) OR is_object($this->conn_id)) { returnTRUE; }