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 |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, 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/ |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 35 | * @param string |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 36 | * @param bool Determines if query builder should be used or not |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 38 | function &DB($params = '', $query_builder_override = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | { |
| 40 | // Load the DB config file if a DSN string wasn't passed |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 41 | if (is_string($params) && strpos($params, '://') === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | { |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 43 | // Is the config file in the environment folder? |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 44 | if (( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php')) |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 45 | && ! file_exists($file_path = APPPATH.'config/database.php')) |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 46 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 47 | show_error('The configuration file database.php does not exist.'); |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 48 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 49 | |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 50 | include($file_path); |
Rene Brokholm | d6d1858 | 2012-07-05 08:32:39 +0200 | [diff] [blame] | 51 | //make packages contain database config files |
| 52 | foreach(get_instance()->load->get_package_paths() as $path) |
Rene Brokholm | f5a8530 | 2012-07-05 09:05:32 +0200 | [diff] [blame] | 53 | { |
| 54 | if ($path !== APPPATH) |
| 55 | { |
| 56 | if (file_exists ($file_path = $path.'config/'.ENVIRONMENT.'/database.php')) |
| 57 | { |
| 58 | include ($file_path); |
| 59 | } |
| 60 | elseif ( file_exists ($file_path = $path.'config/database.php')) |
| 61 | { |
| 62 | include ($file_path); |
| 63 | } |
| 64 | } |
| 65 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 66 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 67 | if ( ! isset($db) OR count($db) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | { |
| 69 | show_error('No database connection settings were found in the database config file.'); |
| 70 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 71 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 72 | if ($params !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | { |
| 74 | $active_group = $params; |
| 75 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 76 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | if ( ! isset($active_group) OR ! isset($db[$active_group])) |
| 78 | { |
| 79 | show_error('You have specified an invalid database connection group.'); |
| 80 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | $params = $db[$active_group]; |
| 83 | } |
| 84 | elseif (is_string($params)) |
| 85 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 86 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | /* parse the URL from the DSN string |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 88 | * Database settings can be passed as discreet |
| 89 | * parameters or as a data source name in the first |
| 90 | * parameter. DSNs must have this prototype: |
| 91 | * $dsn = 'driver://username:password@hostname/database'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | */ |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 93 | if (($dsn = @parse_url($params)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | { |
| 95 | show_error('Invalid DB Connection String'); |
| 96 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | $params = array( |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 99 | 'dbdriver' => $dsn['scheme'], |
| 100 | 'hostname' => isset($dsn['host']) ? rawurldecode($dsn['host']) : '', |
| 101 | 'port' => isset($dsn['port']) ? rawurldecode($dsn['port']) : '', |
| 102 | 'username' => isset($dsn['user']) ? rawurldecode($dsn['user']) : '', |
| 103 | 'password' => isset($dsn['pass']) ? rawurldecode($dsn['pass']) : '', |
| 104 | 'database' => isset($dsn['path']) ? rawurldecode(substr($dsn['path'], 1)) : '' |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 105 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 106 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | // were additional config items set? |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 108 | if (isset($dsn['query'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | { |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 110 | parse_str($dsn['query'], $extra); |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 111 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 112 | foreach ($extra as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | { |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 114 | if (is_string($val) && in_array(strtoupper($val), array('TRUE', 'FALSE', 'NULL'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | { |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 116 | $val = var_export($val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | $params[$key] = $val; |
| 120 | } |
| 121 | } |
| 122 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 124 | // No DB specified yet? Beat them senseless... |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 125 | if (empty($params['dbdriver'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | { |
| 127 | show_error('You have not selected a database type to connect to.'); |
| 128 | } |
| 129 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 130 | // Load the DB classes. Note: Since the query builder class is optional |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | // we need to dynamically create a class that extends proper parent class |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 132 | // based on whether we're using the query builder class or not. |
| 133 | if ($query_builder_override !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 135 | $query_builder = $query_builder_override; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | } |
Andrey Andreev | d45f950 | 2012-05-24 19:31:39 +0300 | [diff] [blame] | 137 | // Backwards compatibility work-around for keeping the |
| 138 | // $active_record config variable working. Should be |
| 139 | // removed in v3.1 |
| 140 | elseif ( ! isset($query_builder) && isset($active_record)) |
| 141 | { |
| 142 | $query_builder = $active_record; |
| 143 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 145 | require_once(BASEPATH.'database/DB_driver.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 147 | if ( ! isset($query_builder) OR $query_builder === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 149 | require_once(BASEPATH.'database/DB_query_builder.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | if ( ! class_exists('CI_DB')) |
| 151 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 152 | class CI_DB extends CI_DB_query_builder { } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 155 | elseif ( ! class_exists('CI_DB')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 157 | class CI_DB extends CI_DB_driver { } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 159 | |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 160 | // Load the DB driver |
| 161 | $driver_file = BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php'; |
| 162 | |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 163 | if ( ! file_exists($driver_file)) |
| 164 | { |
| 165 | show_error('Invalid DB driver'); |
| 166 | } |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 167 | |
| 168 | require_once($driver_file); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | |
| 170 | // Instantiate the DB adapter |
| 171 | $driver = 'CI_DB_'.$params['dbdriver'].'_driver'; |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 172 | $DB = new $driver($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 174 | // Check for a subdriver |
Andrey Andreev | 754e221 | 2012-06-23 20:54:06 +0300 | [diff] [blame] | 175 | if ( ! empty($DB->subdriver)) |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 176 | { |
Andrey Andreev | 4410777 | 2012-06-25 18:38:34 +0300 | [diff] [blame] | 177 | $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] | 178 | |
| 179 | if (file_exists($driver_file)) |
| 180 | { |
| 181 | require_once($driver_file); |
Andrey Andreev | 4410777 | 2012-06-25 18:38:34 +0300 | [diff] [blame] | 182 | $driver = 'CI_DB_'.$DB->dbdriver.'_'.$DB->subdriver.'_driver'; |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 183 | $DB = new $driver($params); |
| 184 | } |
| 185 | } |
| 186 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 187 | if ($DB->autoinit === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | { |
| 189 | $DB->initialize(); |
| 190 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 192 | if ( ! empty($params['stricton'])) |
Derek Jones | c34e578 | 2010-03-02 17:35:55 -0600 | [diff] [blame] | 193 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"'); |
Derek Jones | c34e578 | 2010-03-02 17:35:55 -0600 | [diff] [blame] | 195 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | return $DB; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 198 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | /* End of file DB.php */ |
Andrey Andreev | 7f57a01 | 2012-05-24 18:40:50 +0300 | [diff] [blame] | 201 | /* Location: ./system/database/DB.php */ |