Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Initialize the database |
| 31 | * |
| 32 | * @category Database |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 33 | * @author EllisLab Dev Team |
| 34 | * @link http://codeigniter.com/user_guide/database/ |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 35 | * |
| 36 | * @param string|string[] $params |
| 37 | * @param bool $query_builder_override |
| 38 | * Determines if query builder should be used or not |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 40 | function &DB($params = '', $query_builder_override = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | { |
| 42 | // Load the DB config file if a DSN string wasn't passed |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 43 | if (is_string($params) && strpos($params, '://') === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | { |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 45 | // Is the config file in the environment folder? |
Andrey Andreev | db529ca | 2013-01-28 11:00:02 +0200 | [diff] [blame] | 46 | if ( ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php') |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 47 | && ! file_exists($file_path = APPPATH.'config/database.php')) |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 48 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 49 | show_error('The configuration file database.php does not exist.'); |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 50 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 51 | |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 52 | include($file_path); |
vkeranov | d008c97 | 2012-11-12 14:29:49 +0200 | [diff] [blame] | 53 | // Make packages contain database config files |
| 54 | foreach (get_instance()->load->get_package_paths() as $path) |
Rene Brokholm | f5a8530 | 2012-07-05 09:05:32 +0200 | [diff] [blame] | 55 | { |
| 56 | if ($path !== APPPATH) |
| 57 | { |
vkeranov | d008c97 | 2012-11-12 14:29:49 +0200 | [diff] [blame] | 58 | if (file_exists($file_path = $path.'config/'.ENVIRONMENT.'/database.php')) |
Rene Brokholm | f5a8530 | 2012-07-05 09:05:32 +0200 | [diff] [blame] | 59 | { |
vkeranov | d008c97 | 2012-11-12 14:29:49 +0200 | [diff] [blame] | 60 | include($file_path); |
Rene Brokholm | f5a8530 | 2012-07-05 09:05:32 +0200 | [diff] [blame] | 61 | } |
vkeranov | d008c97 | 2012-11-12 14:29:49 +0200 | [diff] [blame] | 62 | elseif (file_exists($file_path = $path.'config/database.php')) |
Rene Brokholm | f5a8530 | 2012-07-05 09:05:32 +0200 | [diff] [blame] | 63 | { |
vkeranov | d008c97 | 2012-11-12 14:29:49 +0200 | [diff] [blame] | 64 | include($file_path); |
Rene Brokholm | f5a8530 | 2012-07-05 09:05:32 +0200 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 68 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 69 | if ( ! isset($db) OR count($db) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | { |
| 71 | show_error('No database connection settings were found in the database config file.'); |
| 72 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 73 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 74 | if ($params !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | { |
| 76 | $active_group = $params; |
| 77 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 78 | |
Andrey Andreev | f46b16b | 2013-07-18 19:36:24 +0300 | [diff] [blame] | 79 | if ( ! isset($active_group)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | { |
Andrey Andreev | f46b16b | 2013-07-18 19:36:24 +0300 | [diff] [blame] | 81 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 87 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | $params = $db[$active_group]; |
| 89 | } |
| 90 | elseif (is_string($params)) |
| 91 | { |
vkeranov | d008c97 | 2012-11-12 14:29:49 +0200 | [diff] [blame] | 92 | /** |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 98 | */ |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 99 | if (($dsn = @parse_url($params)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | { |
| 101 | show_error('Invalid DB Connection String'); |
| 102 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | $params = array( |
Andrey Andreev | 7e96351 | 2014-02-27 15:43:24 +0200 | [diff] [blame^] | 105 | '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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 112 | |
vkeranov | d008c97 | 2012-11-12 14:29:49 +0200 | [diff] [blame] | 113 | // Were additional config items set? |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 114 | if (isset($dsn['query'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | { |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 116 | parse_str($dsn['query'], $extra); |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 117 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 118 | foreach ($extra as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | { |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 120 | if (is_string($val) && in_array(strtoupper($val), array('TRUE', 'FALSE', 'NULL'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | { |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 122 | $val = var_export($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | $params[$key] = $val; |
| 126 | } |
| 127 | } |
| 128 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 130 | // No DB specified yet? Beat them senseless... |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 131 | if (empty($params['dbdriver'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | { |
| 133 | show_error('You have not selected a database type to connect to.'); |
| 134 | } |
| 135 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 136 | // Load the DB classes. Note: Since the query builder class is optional |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | // we need to dynamically create a class that extends proper parent class |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 138 | // based on whether we're using the query builder class or not. |
| 139 | if ($query_builder_override !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 141 | $query_builder = $query_builder_override; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | } |
Andrey Andreev | d45f950 | 2012-05-24 19:31:39 +0300 | [diff] [blame] | 143 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 151 | require_once(BASEPATH.'database/DB_driver.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 153 | if ( ! isset($query_builder) OR $query_builder === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 155 | require_once(BASEPATH.'database/DB_query_builder.php'); |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 156 | if ( ! class_exists('CI_DB', FALSE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | { |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 158 | /** |
| 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 Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 166 | class CI_DB extends CI_DB_query_builder { } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 169 | elseif ( ! class_exists('CI_DB', FALSE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | { |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 171 | /** |
| 172 | * @ignore |
| 173 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 174 | class CI_DB extends CI_DB_driver { } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 176 | |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 177 | // Load the DB driver |
| 178 | $driver_file = BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php'; |
| 179 | |
Andrey Andreev | 7e96351 | 2014-02-27 15:43:24 +0200 | [diff] [blame^] | 180 | file_exists($driver_file) OR show_error('Invalid DB driver'); |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 181 | require_once($driver_file); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | |
| 183 | // Instantiate the DB adapter |
| 184 | $driver = 'CI_DB_'.$params['dbdriver'].'_driver'; |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 185 | $DB = new $driver($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 187 | // Check for a subdriver |
Andrey Andreev | 754e221 | 2012-06-23 20:54:06 +0300 | [diff] [blame] | 188 | if ( ! empty($DB->subdriver)) |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 189 | { |
Andrey Andreev | 4410777 | 2012-06-25 18:38:34 +0300 | [diff] [blame] | 190 | $driver_file = BASEPATH.'database/drivers/'.$DB->dbdriver.'/subdrivers/'.$DB->dbdriver.'_'.$DB->subdriver.'_driver.php'; |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 191 | |
| 192 | if (file_exists($driver_file)) |
| 193 | { |
| 194 | require_once($driver_file); |
Andrey Andreev | 4410777 | 2012-06-25 18:38:34 +0300 | [diff] [blame] | 195 | $driver = 'CI_DB_'.$DB->dbdriver.'_'.$DB->subdriver.'_driver'; |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 196 | $DB = new $driver($params); |
| 197 | } |
| 198 | } |
| 199 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 200 | if ($DB->autoinit === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
| 202 | $DB->initialize(); |
| 203 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | return $DB; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | /* End of file DB.php */ |
Andrey Andreev | 7f57a01 | 2012-05-24 18:40:50 +0300 | [diff] [blame] | 209 | /* Location: ./system/database/DB.php */ |