1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * Initialize the database
22 * @author ExpressionEngine Dev Team
23 * @link http://codeigniter.com/user_guide/database/
25 function &DB($params = '', $active_record_override = FALSE)
27 // Load the DB config file if a DSN string wasn't passed
28 if (is_string($params) AND strpos($params, '://') === FALSE)
30 include(APPPATH.'config/database'.EXT);
32 if ( ! isset($db) OR count($db) == 0)
34 show_error('No database connection settings were found in the database config file.');
39 $active_group = $params;
42 if ( ! isset($active_group) OR ! isset($db[$active_group]))
44 show_error('You have specified an invalid database connection group.');
47 $params = $db[$active_group];
49 elseif (is_string($params))
52 /* parse the URL from the DSN string
53 * Database settings can be passed as discreet
54 * parameters or as a data source name in the first
55 * parameter. DSNs must have this prototype:
56 * $dsn = 'driver://username:password@hostname/database';
59 if (($dns = @parse_url($params)) === FALSE)
61 show_error('Invalid DB Connection String');
65 'dbdriver' => $dns['scheme'],
66 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
67 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
68 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
69 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
72 // were additional config items set?
73 if (isset($dns['query']))
75 parse_str($dns['query'], $extra);
77 foreach($extra as $key => $val)
80 if (strtoupper($val) == "TRUE")
84 elseif (strtoupper($val) == "FALSE")
94 // No DB specified yet? Beat them senseless...
95 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
97 show_error('You have not selected a database type to connect to.');
100 // Load the DB classes. Note: Since the active record class is optional
101 // we need to dynamically create a class that extends proper parent class
102 // based on whether we're using the active record class or not.
103 // Kudos to Paul for discovering this clever use of eval()
105 if ($active_record_override == TRUE)
107 $active_record = TRUE;
110 require_once(BASEPATH.'database/DB_driver'.EXT);
112 if ( ! isset($active_record) OR $active_record == TRUE)
114 require_once(BASEPATH.'database/DB_active_rec'.EXT);
116 if ( ! class_exists('CI_DB'))
118 eval('class CI_DB extends CI_DB_active_record { }');
123 if ( ! class_exists('CI_DB'))
125 eval('class CI_DB extends CI_DB_driver { }');
129 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
131 // Instantiate the DB adapter
132 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
133 $DB =& instantiate_class(new $driver($params));
135 if ($DB->autoinit == TRUE)
145 /* End of file DB.php */
146 /* Location: ./system/database/DB.php */