blob: 8bf1ba8ba0da078c13ca929d528cb564dda0ea3d [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Initialize the database
20 *
21 * @category Database
22 * @author ExpressionEngine Dev Team
23 * @link http://codeigniter.com/user_guide/database/
24 */
Derek Jonesffca6c22009-12-05 15:31:44 +000025function &DB($params = '', $active_record_override = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000026{
27 // Load the DB config file if a DSN string wasn't passed
28 if (is_string($params) AND strpos($params, '://') === FALSE)
29 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +010030 // Is the config file in the environment folder?
31 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database'.EXT))
joelcoxe3da4282011-01-16 15:00:04 +010032 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +010033 if ( ! file_exists($file_path = APPPATH.'config/database'.EXT))
joelcoxe3da4282011-01-16 15:00:04 +010034 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +010035 show_error('The configuration file database'.EXT.' does not exist.');
joelcoxe3da4282011-01-16 15:00:04 +010036 }
37 }
38
39 include($file_path);
Barry Mienydd671972010-10-04 16:33:58 +020040
Derek Allard2067d1a2008-11-13 22:59:24 +000041 if ( ! isset($db) OR count($db) == 0)
42 {
43 show_error('No database connection settings were found in the database config file.');
44 }
Barry Mienydd671972010-10-04 16:33:58 +020045
Derek Allard2067d1a2008-11-13 22:59:24 +000046 if ($params != '')
47 {
48 $active_group = $params;
49 }
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 if ( ! isset($active_group) OR ! isset($db[$active_group]))
52 {
53 show_error('You have specified an invalid database connection group.');
54 }
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 $params = $db[$active_group];
57 }
58 elseif (is_string($params))
59 {
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 /* parse the URL from the DSN string
Barry Mienydd671972010-10-04 16:33:58 +020062 * Database settings can be passed as discreet
63 * parameters or as a data source name in the first
64 * parameter. DSNs must have this prototype:
65 * $dsn = 'driver://username:password@hostname/database';
66 */
67
Derek Allard2067d1a2008-11-13 22:59:24 +000068 if (($dns = @parse_url($params)) === FALSE)
69 {
70 show_error('Invalid DB Connection String');
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 $params = array(
74 'dbdriver' => $dns['scheme'],
75 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
76 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
77 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
78 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
79 );
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 // were additional config items set?
82 if (isset($dns['query']))
83 {
84 parse_str($dns['query'], $extra);
85
Pascal Krietec3a4a8d2011-02-14 13:40:08 -050086 foreach ($extra as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
88 // booleans please
89 if (strtoupper($val) == "TRUE")
90 {
91 $val = TRUE;
92 }
93 elseif (strtoupper($val) == "FALSE")
94 {
95 $val = FALSE;
96 }
97
98 $params[$key] = $val;
99 }
100 }
101 }
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // No DB specified yet? Beat them senseless...
104 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
105 {
106 show_error('You have not selected a database type to connect to.');
107 }
108
109 // Load the DB classes. Note: Since the active record class is optional
110 // we need to dynamically create a class that extends proper parent class
111 // based on whether we're using the active record class or not.
112 // Kudos to Paul for discovering this clever use of eval()
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Jonesffca6c22009-12-05 15:31:44 +0000114 if ($active_record_override !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Derek Jonesffca6c22009-12-05 15:31:44 +0000116 $active_record = $active_record_override;
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 require_once(BASEPATH.'database/DB_driver'.EXT);
120
121 if ( ! isset($active_record) OR $active_record == TRUE)
122 {
123 require_once(BASEPATH.'database/DB_active_rec'.EXT);
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 if ( ! class_exists('CI_DB'))
126 {
127 eval('class CI_DB extends CI_DB_active_record { }');
128 }
129 }
130 else
131 {
132 if ( ! class_exists('CI_DB'))
133 {
134 eval('class CI_DB extends CI_DB_driver { }');
135 }
136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
139
140 // Instantiate the DB adapter
141 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Pascal Kriete58560022010-11-10 16:01:20 -0500142 $DB = new $driver($params);
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 if ($DB->autoinit == TRUE)
145 {
146 $DB->initialize();
147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Jonesc34e5782010-03-02 17:35:55 -0600149 if (isset($params['stricton']) && $params['stricton'] == TRUE)
150 {
Barry Mienydd671972010-10-04 16:33:58 +0200151 $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
Derek Jonesc34e5782010-03-02 17:35:55 -0600152 }
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 return $DB;
Barry Mienydd671972010-10-04 16:33:58 +0200155}
Derek Allard2067d1a2008-11-13 22:59:24 +0000156
157
158
159/* End of file DB.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000160/* Location: ./system/database/DB.php */