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