blob: 032cf1b7698ca7fe8dceb2e066e477e2da5ca6cf [file] [log] [blame]
Andrey Andreev24276a32012-01-08 02:44:38 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Initialize the database
30 *
31 * @category Database
Andrey Andreev75546112012-07-05 11:01:29 +030032 * @author EllisLab Dev Team
33 * @link http://codeigniter.com/user_guide/database/
David Behler07b53422011-08-15 00:25:06 +020034 * @param string
Jamie Rumbelow7efad202012-02-19 12:37:00 +000035 * @param bool Determines if query builder should be used or not
Derek Allard2067d1a2008-11-13 22:59:24 +000036 */
Jamie Rumbelow7efad202012-02-19 12:37:00 +000037function &DB($params = '', $query_builder_override = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000038{
39 // Load the DB config file if a DSN string wasn't passed
Andrey Andreevc2697db2012-03-26 13:05:24 +030040 if (is_string($params) && strpos($params, '://') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000041 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +010042 // Is the config file in the environment folder?
Andrey Andreev24276a32012-01-08 02:44:38 +020043 if (( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php'))
Andrey Andreevc2697db2012-03-26 13:05:24 +030044 && ! file_exists($file_path = APPPATH.'config/database.php'))
joelcoxe3da4282011-01-16 15:00:04 +010045 {
Andrey Andreev24276a32012-01-08 02:44:38 +020046 show_error('The configuration file database.php does not exist.');
joelcoxe3da4282011-01-16 15:00:04 +010047 }
David Behler07b53422011-08-15 00:25:06 +020048
joelcoxe3da4282011-01-16 15:00:04 +010049 include($file_path);
Rene Brokholmd6d18582012-07-05 08:32:39 +020050 //make packages contain database config files
51 foreach(get_instance()->load->get_package_paths() as $path)
Rene Brokholmf5a85302012-07-05 09:05:32 +020052 {
53 if ($path !== APPPATH)
54 {
55 if (file_exists ($file_path = $path.'config/'.ENVIRONMENT.'/database.php'))
56 {
57 include ($file_path);
58 }
59 elseif ( file_exists ($file_path = $path.'config/database.php'))
60 {
61 include ($file_path);
62 }
63 }
64 }
Barry Mienydd671972010-10-04 16:33:58 +020065
Andrey Andreev24276a32012-01-08 02:44:38 +020066 if ( ! isset($db) OR count($db) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
68 show_error('No database connection settings were found in the database config file.');
69 }
Barry Mienydd671972010-10-04 16:33:58 +020070
Alex Bilbie48a2baf2012-06-02 11:09:54 +010071 if ($params !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +000072 {
73 $active_group = $params;
74 }
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 if ( ! isset($active_group) OR ! isset($db[$active_group]))
77 {
78 show_error('You have specified an invalid database connection group.');
79 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 $params = $db[$active_group];
82 }
83 elseif (is_string($params))
84 {
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 /* parse the URL from the DSN string
Derek Jones37f4b9c2011-07-01 17:56:50 -050087 * Database settings can be passed as discreet
88 * parameters or as a data source name in the first
89 * parameter. DSNs must have this prototype:
90 * $dsn = 'driver://username:password@hostname/database';
Barry Mienydd671972010-10-04 16:33:58 +020091 */
Andrey Andreevc2697db2012-03-26 13:05:24 +030092 if (($dsn = @parse_url($params)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
94 show_error('Invalid DB Connection String');
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 $params = array(
Andrey Andreevc2697db2012-03-26 13:05:24 +030098 'dbdriver' => $dsn['scheme'],
99 'hostname' => isset($dsn['host']) ? rawurldecode($dsn['host']) : '',
100 'port' => isset($dsn['port']) ? rawurldecode($dsn['port']) : '',
101 'username' => isset($dsn['user']) ? rawurldecode($dsn['user']) : '',
102 'password' => isset($dsn['pass']) ? rawurldecode($dsn['pass']) : '',
103 'database' => isset($dsn['path']) ? rawurldecode(substr($dsn['path'], 1)) : ''
Andrey Andreev24276a32012-01-08 02:44:38 +0200104 );
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 // were additional config items set?
Andrey Andreevc2697db2012-03-26 13:05:24 +0300107 if (isset($dsn['query']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Andrey Andreevc2697db2012-03-26 13:05:24 +0300109 parse_str($dsn['query'], $extra);
Taufan Adityaee2f5d02012-03-30 06:29:11 +0700110
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500111 foreach ($extra as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
Taufan Adityaee2f5d02012-03-30 06:29:11 +0700113 if (is_string($val) && in_array(strtoupper($val), array('TRUE', 'FALSE', 'NULL')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
Taufan Adityaee2f5d02012-03-30 06:29:11 +0700115 $val = var_export($val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
117
118 $params[$key] = $val;
119 }
120 }
121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Andrey Andreev24276a32012-01-08 02:44:38 +0200123 // No DB specified yet? Beat them senseless...
Andrey Andreeve4c30192012-06-04 15:08:24 +0300124 if (empty($params['dbdriver']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
126 show_error('You have not selected a database type to connect to.');
127 }
128
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000129 // Load the DB classes. Note: Since the query builder class is optional
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // we need to dynamically create a class that extends proper parent class
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000131 // based on whether we're using the query builder class or not.
132 if ($query_builder_override !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000134 $query_builder = $query_builder_override;
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 }
Andrey Andreevd45f9502012-05-24 19:31:39 +0300136 // Backwards compatibility work-around for keeping the
137 // $active_record config variable working. Should be
138 // removed in v3.1
139 elseif ( ! isset($query_builder) && isset($active_record))
140 {
141 $query_builder = $active_record;
142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Greg Aker3a746652011-04-19 10:59:47 -0500144 require_once(BASEPATH.'database/DB_driver.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000145
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100146 if ( ! isset($query_builder) OR $query_builder === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000148 require_once(BASEPATH.'database/DB_query_builder.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 if ( ! class_exists('CI_DB'))
150 {
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000151 class CI_DB extends CI_DB_query_builder { }
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200154 elseif ( ! class_exists('CI_DB'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200156 class CI_DB extends CI_DB_driver { }
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700159 // Load the DB driver
160 $driver_file = BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php';
161
Andrey Andreev75546112012-07-05 11:01:29 +0300162 if ( ! file_exists($driver_file))
163 {
164 show_error('Invalid DB driver');
165 }
Taufan Adityaa8a2e332012-03-29 03:56:46 +0700166
167 require_once($driver_file);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168
169 // Instantiate the DB adapter
170 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Pascal Kriete58560022010-11-10 16:01:20 -0500171 $DB = new $driver($params);
Barry Mienydd671972010-10-04 16:33:58 +0200172
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100173 if ($DB->autoinit === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
175 $DB->initialize();
176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177
Andrey Andreeve4c30192012-06-04 15:08:24 +0300178 if ( ! empty($params['stricton']))
Derek Jonesc34e5782010-03-02 17:35:55 -0600179 {
Barry Mienydd671972010-10-04 16:33:58 +0200180 $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
Derek Jonesc34e5782010-03-02 17:35:55 -0600181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 return $DB;
Barry Mienydd671972010-10-04 16:33:58 +0200184}
Derek Allard2067d1a2008-11-13 22:59:24 +0000185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186/* End of file DB.php */
Andrey Andreev75546112012-07-05 11:01:29 +0300187/* Location: ./system/database/DB.php */