Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 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 |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 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 |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 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 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Initialize the database |
| 32 | * |
| 33 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/database/ |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 36 | * @param string |
| 37 | * @param bool Determines if active record should be used or not |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | */ |
Derek Jones | ffca6c2 | 2009-12-05 15:31:44 +0000 | [diff] [blame] | 39 | function &DB($params = '', $active_record_override = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | { |
| 41 | // Load the DB config file if a DSN string wasn't passed |
| 42 | if (is_string($params) AND strpos($params, '://') === FALSE) |
| 43 | { |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 44 | // Is the config file in the environment folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 45 | if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php')) |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 46 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 47 | if ( ! file_exists($file_path = APPPATH.'config/database.php')) |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 48 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [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 | } |
| 51 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 52 | |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 53 | include($file_path); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 54 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | if ( ! isset($db) OR count($db) == 0) |
| 56 | { |
| 57 | show_error('No database connection settings were found in the database config file.'); |
| 58 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 59 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | if ($params != '') |
| 61 | { |
| 62 | $active_group = $params; |
| 63 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | if ( ! isset($active_group) OR ! isset($db[$active_group])) |
| 66 | { |
| 67 | show_error('You have specified an invalid database connection group.'); |
| 68 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 69 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | $params = $db[$active_group]; |
| 71 | } |
| 72 | elseif (is_string($params)) |
| 73 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 74 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 75 | /* parse the URL from the DSN string |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 76 | * Database settings can be passed as discreet |
| 77 | * parameters or as a data source name in the first |
| 78 | * parameter. DSNs must have this prototype: |
| 79 | * $dsn = 'driver://username:password@hostname/database'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 80 | */ |
| 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | if (($dns = @parse_url($params)) === FALSE) |
| 83 | { |
| 84 | show_error('Invalid DB Connection String'); |
| 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 | $params = array( |
| 88 | 'dbdriver' => $dns['scheme'], |
| 89 | 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '', |
| 90 | 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '', |
| 91 | 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '', |
| 92 | 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : '' |
| 93 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | // were additional config items set? |
| 96 | if (isset($dns['query'])) |
| 97 | { |
| 98 | parse_str($dns['query'], $extra); |
| 99 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 100 | foreach ($extra as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | { |
| 102 | // booleans please |
| 103 | if (strtoupper($val) == "TRUE") |
| 104 | { |
| 105 | $val = TRUE; |
| 106 | } |
| 107 | elseif (strtoupper($val) == "FALSE") |
| 108 | { |
| 109 | $val = FALSE; |
| 110 | } |
| 111 | |
| 112 | $params[$key] = $val; |
| 113 | } |
| 114 | } |
| 115 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 116 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 117 | // No DB specified yet? Beat them senseless... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '') |
| 119 | { |
| 120 | show_error('You have not selected a database type to connect to.'); |
| 121 | } |
| 122 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 123 | // Load the DB classes. Note: Since the active record class is optional |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | // we need to dynamically create a class that extends proper parent class |
| 125 | // based on whether we're using the active record class or not. |
| 126 | // Kudos to Paul for discovering this clever use of eval() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 127 | |
Derek Jones | ffca6c2 | 2009-12-05 15:31:44 +0000 | [diff] [blame] | 128 | if ($active_record_override !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | { |
Derek Jones | ffca6c2 | 2009-12-05 15:31:44 +0000 | [diff] [blame] | 130 | $active_record = $active_record_override; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 133 | require_once(BASEPATH.'database/DB_driver.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | |
| 135 | if ( ! isset($active_record) OR $active_record == TRUE) |
| 136 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 137 | require_once(BASEPATH.'database/DB_active_rec.php'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | if ( ! class_exists('CI_DB')) |
| 140 | { |
| 141 | eval('class CI_DB extends CI_DB_active_record { }'); |
| 142 | } |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | if ( ! class_exists('CI_DB')) |
| 147 | { |
| 148 | eval('class CI_DB extends CI_DB_driver { }'); |
| 149 | } |
| 150 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 152 | require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | |
| 154 | // Instantiate the DB adapter |
| 155 | $driver = 'CI_DB_'.$params['dbdriver'].'_driver'; |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 156 | $DB = new $driver($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 157 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | if ($DB->autoinit == TRUE) |
| 159 | { |
| 160 | $DB->initialize(); |
| 161 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Derek Jones | c34e578 | 2010-03-02 17:35:55 -0600 | [diff] [blame] | 163 | if (isset($params['stricton']) && $params['stricton'] == TRUE) |
| 164 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 165 | $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"'); |
Derek Jones | c34e578 | 2010-03-02 17:35:55 -0600 | [diff] [blame] | 166 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | return $DB; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | |
| 171 | |
| 172 | |
| 173 | /* End of file DB.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 174 | /* Location: ./system/database/DB.php */ |