DB.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Initialize the database
00020  *
00021  * @category    Database
00022  * @author              ExpressionEngine Dev Team
00023  * @link                http://codeigniter.com/user_guide/database/
00024  */
00025 function &DB($params = '', $active_record_override = FALSE)
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 }       
00142 
00143 
00144 
00145 /* End of file DB.php */
00146 /* Location: ./system/database/DB.php */