blob: fc9f4d0dc53746105873ae4fff600016c1d0dae8 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev24276a32012-01-08 02:44:38 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreev24276a32012-01-08 02:44:38 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Initialize the database
42 *
43 * @category Database
Andrey Andreev75546112012-07-05 11:01:29 +030044 * @author EllisLab Dev Team
45 * @link http://codeigniter.com/user_guide/database/
Andrey Andreevae85eb42012-11-02 01:42:31 +020046 *
47 * @param string|string[] $params
48 * @param bool $query_builder_override
49 * Determines if query builder should be used or not
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000051function &DB($params = '', $query_builder_override = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000052{
53 // Load the DB config file if a DSN string wasn't passed
Andrey Andreevc2697db2012-03-26 13:05:24 +030054 if (is_string($params) && strpos($params, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +010056 // Is the config file in the environment folder?
Andrey Andreevdb529ca2013-01-28 11:00:02 +020057 if ( ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php')
Andrey Andreevc2697db2012-03-26 13:05:24 +030058 && ! file_exists($file_path = APPPATH.'config/database.php'))
joelcoxe3da4282011-01-16 15:00:04 +010059 {
Andrey Andreev24276a32012-01-08 02:44:38 +020060 show_error('The configuration file database.php does not exist.');
joelcoxe3da4282011-01-16 15:00:04 +010061 }
David Behler07b53422011-08-15 00:25:06 +020062
joelcoxe3da4282011-01-16 15:00:04 +010063 include($file_path);
vkeranovd008c972012-11-12 14:29:49 +020064 // Make packages contain database config files
65 foreach (get_instance()->load->get_package_paths() as $path)
Rene Brokholmf5a85302012-07-05 09:05:32 +020066 {
67 if ($path !== APPPATH)
68 {
vkeranovd008c972012-11-12 14:29:49 +020069 if (file_exists($file_path = $path.'config/'.ENVIRONMENT.'/database.php'))
Rene Brokholmf5a85302012-07-05 09:05:32 +020070 {
vkeranovd008c972012-11-12 14:29:49 +020071 include($file_path);
Rene Brokholmf5a85302012-07-05 09:05:32 +020072 }
vkeranovd008c972012-11-12 14:29:49 +020073 elseif (file_exists($file_path = $path.'config/database.php'))
Rene Brokholmf5a85302012-07-05 09:05:32 +020074 {
vkeranovd008c972012-11-12 14:29:49 +020075 include($file_path);
Rene Brokholmf5a85302012-07-05 09:05:32 +020076 }
77 }
78 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Andrey Andreev24276a32012-01-08 02:44:38 +020080 if ( ! isset($db) OR count($db) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 show_error('No database connection settings were found in the database config file.');
83 }
Barry Mienydd671972010-10-04 16:33:58 +020084
Alex Bilbie48a2baf2012-06-02 11:09:54 +010085 if ($params !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +000086 {
87 $active_group = $params;
88 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Andrey Andreevf46b16b2013-07-18 19:36:24 +030090 if ( ! isset($active_group))
Derek Allard2067d1a2008-11-13 22:59:24 +000091 {
Andrey Andreevf46b16b2013-07-18 19:36:24 +030092 show_error('You have not specified a database connection group via $active_group in your config/database.php file.');
93 }
94 elseif ( ! isset($db[$active_group]))
95 {
96 show_error('You have specified an invalid database connection group ('.$active_group.') in your config/database.php file.');
Derek Allard2067d1a2008-11-13 22:59:24 +000097 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 $params = $db[$active_group];
100 }
101 elseif (is_string($params))
102 {
vkeranovd008c972012-11-12 14:29:49 +0200103 /**
104 * Parse the URL from the DSN string
105 * Database settings can be passed as discreet
106 * parameters or as a data source name in the first
107 * parameter. DSNs must have this prototype:
108 * $dsn = 'driver://username:password@hostname/database';
Barry Mienydd671972010-10-04 16:33:58 +0200109 */
Andrey Andreevc2697db2012-03-26 13:05:24 +0300110 if (($dsn = @parse_url($params)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
112 show_error('Invalid DB Connection String');
113 }
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 $params = array(
Andrey Andreev7e963512014-02-27 15:43:24 +0200116 'dbdriver' => $dsn['scheme'],
117 'hostname' => isset($dsn['host']) ? rawurldecode($dsn['host']) : '',
118 'port' => isset($dsn['port']) ? rawurldecode($dsn['port']) : '',
119 'username' => isset($dsn['user']) ? rawurldecode($dsn['user']) : '',
120 'password' => isset($dsn['pass']) ? rawurldecode($dsn['pass']) : '',
121 'database' => isset($dsn['path']) ? rawurldecode(substr($dsn['path'], 1)) : ''
122 );
Barry Mienydd671972010-10-04 16:33:58 +0200123
vkeranovd008c972012-11-12 14:29:49 +0200124 // Were additional config items set?
Andrey Andreevc2697db2012-03-26 13:05:24 +0300125 if (isset($dsn['query']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreevc2697db2012-03-26 13:05:24 +0300127 parse_str($dsn['query'], $extra);
Taufan Adityaee2f5d02012-03-30 06:29:11 +0700128
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500129 foreach ($extra as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Taufan Adityaee2f5d02012-03-30 06:29:11 +0700131 if (is_string($val) && in_array(strtoupper($val), array('TRUE', 'FALSE', 'NULL')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
Taufan Adityaee2f5d02012-03-30 06:29:11 +0700133 $val = var_export($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
135
136 $params[$key] = $val;
137 }
138 }
139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Andrey Andreev24276a32012-01-08 02:44:38 +0200141 // No DB specified yet? Beat them senseless...
Andrey Andreeve4c30192012-06-04 15:08:24 +0300142 if (empty($params['dbdriver']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
144 show_error('You have not selected a database type to connect to.');
145 }
146
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000147 // Load the DB classes. Note: Since the query builder class is optional
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // we need to dynamically create a class that extends proper parent class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000149 // based on whether we're using the query builder class or not.
150 if ($query_builder_override !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000152 $query_builder = $query_builder_override;
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
Andrey Andreevd45f9502012-05-24 19:31:39 +0300154 // Backwards compatibility work-around for keeping the
155 // $active_record config variable working. Should be
156 // removed in v3.1
157 elseif ( ! isset($query_builder) && isset($active_record))
158 {
159 $query_builder = $active_record;
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Greg Aker3a746652011-04-19 10:59:47 -0500162 require_once(BASEPATH.'database/DB_driver.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000163
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100164 if ( ! isset($query_builder) OR $query_builder === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000166 require_once(BASEPATH.'database/DB_query_builder.php');
Andrey Andreev49e68de2013-02-21 16:30:55 +0200167 if ( ! class_exists('CI_DB', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreevae85eb42012-11-02 01:42:31 +0200169 /**
170 * CI_DB
171 *
172 * Acts as an alias for both CI_DB_driver and CI_DB_query_builder.
173 *
174 * @see CI_DB_query_builder
175 * @see CI_DB_driver
176 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000177 class CI_DB extends CI_DB_query_builder { }
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
179 }
Andrey Andreev49e68de2013-02-21 16:30:55 +0200180 elseif ( ! class_exists('CI_DB', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
Andrey Andreevae85eb42012-11-02 01:42:31 +0200182 /**
183 * @ignore
184 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200185 class CI_DB extends CI_DB_driver { }
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700188 // Load the DB driver
189 $driver_file = BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php';
190
Andrey Andreev7e963512014-02-27 15:43:24 +0200191 file_exists($driver_file) OR show_error('Invalid DB driver');
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700192 require_once($driver_file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000193
194 // Instantiate the DB adapter
195 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Pascal Kriete58560022010-11-10 16:01:20 -0500196 $DB = new $driver($params);
Barry Mienydd671972010-10-04 16:33:58 +0200197
Andrey Andreev3345ee52012-06-23 20:51:47 +0300198 // Check for a subdriver
Andrey Andreev754e2212012-06-23 20:54:06 +0300199 if ( ! empty($DB->subdriver))
Andrey Andreev3345ee52012-06-23 20:51:47 +0300200 {
Andrey Andreev44107772012-06-25 18:38:34 +0300201 $driver_file = BASEPATH.'database/drivers/'.$DB->dbdriver.'/subdrivers/'.$DB->dbdriver.'_'.$DB->subdriver.'_driver.php';
Andrey Andreev3345ee52012-06-23 20:51:47 +0300202
203 if (file_exists($driver_file))
204 {
205 require_once($driver_file);
Andrey Andreev44107772012-06-25 18:38:34 +0300206 $driver = 'CI_DB_'.$DB->dbdriver.'_'.$DB->subdriver.'_driver';
Andrey Andreev3345ee52012-06-23 20:51:47 +0300207 $DB = new $driver($params);
208 }
209 }
210
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100211 if ($DB->autoinit === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
213 $DB->initialize();
214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 return $DB;
Barry Mienydd671972010-10-04 16:33:58 +0200217}
Derek Allard2067d1a2008-11-13 22:59:24 +0000218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219/* End of file DB.php */
Andrey Andreev7f57a012012-05-24 18:40:50 +0300220/* Location: ./system/database/DB.php */