blob: f8d02049359581379f715dd4a3ba40f1f6586ec0 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev24276a32012-01-08 02:44:38 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev24276a32012-01-08 02:44:38 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Initialize the database
31 *
32 * @category Database
Andrey Andreev75546112012-07-05 11:01:29 +030033 * @author EllisLab Dev Team
34 * @link http://codeigniter.com/user_guide/database/
Andrey Andreevae85eb42012-11-02 01:42:31 +020035 *
36 * @param string|string[] $params
37 * @param bool $query_builder_override
38 * Determines if query builder should be used or not
Derek Allard2067d1a2008-11-13 22:59:24 +000039 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000040function &DB($params = '', $query_builder_override = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000041{
42 // Load the DB config file if a DSN string wasn't passed
Andrey Andreevc2697db2012-03-26 13:05:24 +030043 if (is_string($params) && strpos($params, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +010045 // Is the config file in the environment folder?
Andrey Andreevdb529ca2013-01-28 11:00:02 +020046 if ( ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php')
Andrey Andreevc2697db2012-03-26 13:05:24 +030047 && ! file_exists($file_path = APPPATH.'config/database.php'))
joelcoxe3da4282011-01-16 15:00:04 +010048 {
Andrey Andreev24276a32012-01-08 02:44:38 +020049 show_error('The configuration file database.php does not exist.');
joelcoxe3da4282011-01-16 15:00:04 +010050 }
David Behler07b53422011-08-15 00:25:06 +020051
joelcoxe3da4282011-01-16 15:00:04 +010052 include($file_path);
vkeranovd008c972012-11-12 14:29:49 +020053 // Make packages contain database config files
54 foreach (get_instance()->load->get_package_paths() as $path)
Rene Brokholmf5a85302012-07-05 09:05:32 +020055 {
56 if ($path !== APPPATH)
57 {
vkeranovd008c972012-11-12 14:29:49 +020058 if (file_exists($file_path = $path.'config/'.ENVIRONMENT.'/database.php'))
Rene Brokholmf5a85302012-07-05 09:05:32 +020059 {
vkeranovd008c972012-11-12 14:29:49 +020060 include($file_path);
Rene Brokholmf5a85302012-07-05 09:05:32 +020061 }
vkeranovd008c972012-11-12 14:29:49 +020062 elseif (file_exists($file_path = $path.'config/database.php'))
Rene Brokholmf5a85302012-07-05 09:05:32 +020063 {
vkeranovd008c972012-11-12 14:29:49 +020064 include($file_path);
Rene Brokholmf5a85302012-07-05 09:05:32 +020065 }
66 }
67 }
Barry Mienydd671972010-10-04 16:33:58 +020068
Andrey Andreev24276a32012-01-08 02:44:38 +020069 if ( ! isset($db) OR count($db) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
71 show_error('No database connection settings were found in the database config file.');
72 }
Barry Mienydd671972010-10-04 16:33:58 +020073
Alex Bilbie48a2baf2012-06-02 11:09:54 +010074 if ($params !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +000075 {
76 $active_group = $params;
77 }
Barry Mienydd671972010-10-04 16:33:58 +020078
Andrey Andreevf46b16b2013-07-18 19:36:24 +030079 if ( ! isset($active_group))
Derek Allard2067d1a2008-11-13 22:59:24 +000080 {
Andrey Andreevf46b16b2013-07-18 19:36:24 +030081 show_error('You have not specified a database connection group via $active_group in your config/database.php file.');
82 }
83 elseif ( ! isset($db[$active_group]))
84 {
85 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 +000086 }
Barry Mienydd671972010-10-04 16:33:58 +020087
Derek Allard2067d1a2008-11-13 22:59:24 +000088 $params = $db[$active_group];
89 }
90 elseif (is_string($params))
91 {
vkeranovd008c972012-11-12 14:29:49 +020092 /**
93 * Parse the URL from the DSN string
94 * Database settings can be passed as discreet
95 * parameters or as a data source name in the first
96 * parameter. DSNs must have this prototype:
97 * $dsn = 'driver://username:password@hostname/database';
Barry Mienydd671972010-10-04 16:33:58 +020098 */
Andrey Andreevc2697db2012-03-26 13:05:24 +030099 if (($dsn = @parse_url($params)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 show_error('Invalid DB Connection String');
102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 $params = array(
Andrey Andreev7e963512014-02-27 15:43:24 +0200105 'dbdriver' => $dsn['scheme'],
106 'hostname' => isset($dsn['host']) ? rawurldecode($dsn['host']) : '',
107 'port' => isset($dsn['port']) ? rawurldecode($dsn['port']) : '',
108 'username' => isset($dsn['user']) ? rawurldecode($dsn['user']) : '',
109 'password' => isset($dsn['pass']) ? rawurldecode($dsn['pass']) : '',
110 'database' => isset($dsn['path']) ? rawurldecode(substr($dsn['path'], 1)) : ''
111 );
Barry Mienydd671972010-10-04 16:33:58 +0200112
vkeranovd008c972012-11-12 14:29:49 +0200113 // Were additional config items set?
Andrey Andreevc2697db2012-03-26 13:05:24 +0300114 if (isset($dsn['query']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Andrey Andreevc2697db2012-03-26 13:05:24 +0300116 parse_str($dsn['query'], $extra);
Taufan Adityaee2f5d02012-03-30 06:29:11 +0700117
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500118 foreach ($extra as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
Taufan Adityaee2f5d02012-03-30 06:29:11 +0700120 if (is_string($val) && in_array(strtoupper($val), array('TRUE', 'FALSE', 'NULL')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Taufan Adityaee2f5d02012-03-30 06:29:11 +0700122 $val = var_export($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 }
124
125 $params[$key] = $val;
126 }
127 }
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Andrey Andreev24276a32012-01-08 02:44:38 +0200130 // No DB specified yet? Beat them senseless...
Andrey Andreeve4c30192012-06-04 15:08:24 +0300131 if (empty($params['dbdriver']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 {
133 show_error('You have not selected a database type to connect to.');
134 }
135
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000136 // Load the DB classes. Note: Since the query builder class is optional
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 // we need to dynamically create a class that extends proper parent class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000138 // based on whether we're using the query builder class or not.
139 if ($query_builder_override !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000141 $query_builder = $query_builder_override;
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Andrey Andreevd45f9502012-05-24 19:31:39 +0300143 // Backwards compatibility work-around for keeping the
144 // $active_record config variable working. Should be
145 // removed in v3.1
146 elseif ( ! isset($query_builder) && isset($active_record))
147 {
148 $query_builder = $active_record;
149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Greg Aker3a746652011-04-19 10:59:47 -0500151 require_once(BASEPATH.'database/DB_driver.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000152
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100153 if ( ! isset($query_builder) OR $query_builder === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000155 require_once(BASEPATH.'database/DB_query_builder.php');
Andrey Andreev49e68de2013-02-21 16:30:55 +0200156 if ( ! class_exists('CI_DB', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreevae85eb42012-11-02 01:42:31 +0200158 /**
159 * CI_DB
160 *
161 * Acts as an alias for both CI_DB_driver and CI_DB_query_builder.
162 *
163 * @see CI_DB_query_builder
164 * @see CI_DB_driver
165 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000166 class CI_DB extends CI_DB_query_builder { }
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
168 }
Andrey Andreev49e68de2013-02-21 16:30:55 +0200169 elseif ( ! class_exists('CI_DB', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Andrey Andreevae85eb42012-11-02 01:42:31 +0200171 /**
172 * @ignore
173 */
Andrey Andreev24276a32012-01-08 02:44:38 +0200174 class CI_DB extends CI_DB_driver { }
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700177 // Load the DB driver
178 $driver_file = BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php';
179
Andrey Andreev7e963512014-02-27 15:43:24 +0200180 file_exists($driver_file) OR show_error('Invalid DB driver');
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700181 require_once($driver_file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000182
183 // Instantiate the DB adapter
184 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Pascal Kriete58560022010-11-10 16:01:20 -0500185 $DB = new $driver($params);
Barry Mienydd671972010-10-04 16:33:58 +0200186
Andrey Andreev3345ee52012-06-23 20:51:47 +0300187 // Check for a subdriver
Andrey Andreev754e2212012-06-23 20:54:06 +0300188 if ( ! empty($DB->subdriver))
Andrey Andreev3345ee52012-06-23 20:51:47 +0300189 {
Andrey Andreev44107772012-06-25 18:38:34 +0300190 $driver_file = BASEPATH.'database/drivers/'.$DB->dbdriver.'/subdrivers/'.$DB->dbdriver.'_'.$DB->subdriver.'_driver.php';
Andrey Andreev3345ee52012-06-23 20:51:47 +0300191
192 if (file_exists($driver_file))
193 {
194 require_once($driver_file);
Andrey Andreev44107772012-06-25 18:38:34 +0300195 $driver = 'CI_DB_'.$DB->dbdriver.'_'.$DB->subdriver.'_driver';
Andrey Andreev3345ee52012-06-23 20:51:47 +0300196 $DB = new $driver($params);
197 }
198 }
199
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100200 if ($DB->autoinit === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
202 $DB->initialize();
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 return $DB;
Barry Mienydd671972010-10-04 16:33:58 +0200206}
Derek Allard2067d1a2008-11-13 22:59:24 +0000207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208/* End of file DB.php */
Andrey Andreev7f57a012012-05-24 18:40:50 +0300209/* Location: ./system/database/DB.php */