blob: 5c90f44f37bdee46cd04eaef134a5e2bfb22725b [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @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 Allard2067d1a2008-11-13 22:59:24 +000023 * @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 Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/database/
David Behler07b53422011-08-15 00:25:06 +020036 * @param string
37 * @param bool Determines if active record should be used or not
Derek Allard2067d1a2008-11-13 22:59:24 +000038 */
Derek Jonesffca6c22009-12-05 15:31:44 +000039function &DB($params = '', $active_record_override = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000040{
41 // Load the DB config file if a DSN string wasn't passed
42 if (is_string($params) AND strpos($params, '://') === FALSE)
43 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +010044 // Is the config file in the environment folder?
Greg Aker3a746652011-04-19 10:59:47 -050045 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php'))
joelcoxe3da4282011-01-16 15:00:04 +010046 {
Greg Aker3a746652011-04-19 10:59:47 -050047 if ( ! file_exists($file_path = APPPATH.'config/database.php'))
joelcoxe3da4282011-01-16 15:00:04 +010048 {
Greg Aker3a746652011-04-19 10:59:47 -050049 show_error('The configuration file database.php does not exist.');
joelcoxe3da4282011-01-16 15:00:04 +010050 }
51 }
David Behler07b53422011-08-15 00:25:06 +020052
joelcoxe3da4282011-01-16 15:00:04 +010053 include($file_path);
Barry Mienydd671972010-10-04 16:33:58 +020054
Derek Allard2067d1a2008-11-13 22:59:24 +000055 if ( ! isset($db) OR count($db) == 0)
56 {
57 show_error('No database connection settings were found in the database config file.');
58 }
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 if ($params != '')
61 {
62 $active_group = $params;
63 }
Barry Mienydd671972010-10-04 16:33:58 +020064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 if ( ! isset($active_group) OR ! isset($db[$active_group]))
66 {
67 show_error('You have specified an invalid database connection group.');
68 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 $params = $db[$active_group];
71 }
72 elseif (is_string($params))
73 {
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 /* parse the URL from the DSN string
Derek Jones37f4b9c2011-07-01 17:56:50 -050076 * 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 Mienydd671972010-10-04 16:33:58 +020080 */
81
Derek Allard2067d1a2008-11-13 22:59:24 +000082 if (($dns = @parse_url($params)) === FALSE)
83 {
84 show_error('Invalid DB Connection String');
85 }
Barry Mienydd671972010-10-04 16:33:58 +020086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 $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 Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 // were additional config items set?
96 if (isset($dns['query']))
97 {
98 parse_str($dns['query'], $extra);
99
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500100 foreach ($extra as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
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 Mienydd671972010-10-04 16:33:58 +0200116
Derek Jones37f4b9c2011-07-01 17:56:50 -0500117 // No DB specified yet? Beat them senseless...
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
119 {
120 show_error('You have not selected a database type to connect to.');
121 }
122
Derek Jones37f4b9c2011-07-01 17:56:50 -0500123 // Load the DB classes. Note: Since the active record class is optional
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // 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 Mienydd671972010-10-04 16:33:58 +0200127
Derek Jonesffca6c22009-12-05 15:31:44 +0000128 if ($active_record_override !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
Derek Jonesffca6c22009-12-05 15:31:44 +0000130 $active_record = $active_record_override;
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Greg Aker3a746652011-04-19 10:59:47 -0500133 require_once(BASEPATH.'database/DB_driver.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000134
135 if ( ! isset($active_record) OR $active_record == TRUE)
136 {
Greg Aker3a746652011-04-19 10:59:47 -0500137 require_once(BASEPATH.'database/DB_active_rec.php');
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 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 Mienydd671972010-10-04 16:33:58 +0200151
Greg Aker3a746652011-04-19 10:59:47 -0500152 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000153
154 // Instantiate the DB adapter
155 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Pascal Kriete58560022010-11-10 16:01:20 -0500156 $DB = new $driver($params);
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 if ($DB->autoinit == TRUE)
159 {
160 $DB->initialize();
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Jonesc34e5782010-03-02 17:35:55 -0600163 if (isset($params['stricton']) && $params['stricton'] == TRUE)
164 {
Barry Mienydd671972010-10-04 16:33:58 +0200165 $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
Derek Jonesc34e5782010-03-02 17:35:55 -0600166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 return $DB;
Barry Mienydd671972010-10-04 16:33:58 +0200169}
Derek Allard2067d1a2008-11-13 22:59:24 +0000170
171
172
173/* End of file DB.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000174/* Location: ./system/database/DB.php */