blob: 96e49551581812ca2fcc8774c38d93ce86b8e231 [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
Derek Jonesf4a4bd82011-10-20 12:18:42 -050032 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * @link http://codeigniter.com/user_guide/database/
David Behler07b53422011-08-15 00:25:06 +020034 * @param string
35 * @param bool Determines if active record should be used or not
Derek Allard2067d1a2008-11-13 22:59:24 +000036 */
Derek Jonesffca6c22009-12-05 15:31:44 +000037function &DB($params = '', $active_record_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);
Barry Mienydd671972010-10-04 16:33:58 +020050
Andrey Andreev24276a32012-01-08 02:44:38 +020051 if ( ! isset($db) OR count($db) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +000052 {
53 show_error('No database connection settings were found in the database config file.');
54 }
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 if ($params != '')
57 {
58 $active_group = $params;
59 }
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 if ( ! isset($active_group) OR ! isset($db[$active_group]))
62 {
63 show_error('You have specified an invalid database connection group.');
64 }
Barry Mienydd671972010-10-04 16:33:58 +020065
Derek Allard2067d1a2008-11-13 22:59:24 +000066 $params = $db[$active_group];
67 }
68 elseif (is_string($params))
69 {
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 /* parse the URL from the DSN string
Derek Jones37f4b9c2011-07-01 17:56:50 -050072 * Database settings can be passed as discreet
73 * parameters or as a data source name in the first
74 * parameter. DSNs must have this prototype:
75 * $dsn = 'driver://username:password@hostname/database';
Barry Mienydd671972010-10-04 16:33:58 +020076 */
Andrey Andreevc2697db2012-03-26 13:05:24 +030077 if (($dsn = @parse_url($params)) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000078 {
79 show_error('Invalid DB Connection String');
80 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 $params = array(
Andrey Andreevc2697db2012-03-26 13:05:24 +030083 'dbdriver' => $dsn['scheme'],
84 'hostname' => isset($dsn['host']) ? rawurldecode($dsn['host']) : '',
85 'port' => isset($dsn['port']) ? rawurldecode($dsn['port']) : '',
86 'username' => isset($dsn['user']) ? rawurldecode($dsn['user']) : '',
87 'password' => isset($dsn['pass']) ? rawurldecode($dsn['pass']) : '',
88 'database' => isset($dsn['path']) ? rawurldecode(substr($dsn['path'], 1)) : ''
Andrey Andreev24276a32012-01-08 02:44:38 +020089 );
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 // were additional config items set?
Andrey Andreevc2697db2012-03-26 13:05:24 +030092 if (isset($dsn['query']))
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
Andrey Andreevc2697db2012-03-26 13:05:24 +030094 parse_str($dsn['query'], $extra);
Pascal Krietec3a4a8d2011-02-14 13:40:08 -050095 foreach ($extra as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
97 // booleans please
Andrey Andreev24276a32012-01-08 02:44:38 +020098 if (strtoupper($val) === 'TRUE')
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
100 $val = TRUE;
101 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200102 elseif (strtoupper($val) === 'FALSE')
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
104 $val = FALSE;
105 }
106
107 $params[$key] = $val;
108 }
109 }
110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Andrey Andreev24276a32012-01-08 02:44:38 +0200112 // No DB specified yet? Beat them senseless...
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
114 {
115 show_error('You have not selected a database type to connect to.');
116 }
117
Andrey Andreev24276a32012-01-08 02:44:38 +0200118 // Load the DB classes. Note: Since the active record class is optional
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 // we need to dynamically create a class that extends proper parent class
120 // based on whether we're using the active record class or not.
Derek Jonesffca6c22009-12-05 15:31:44 +0000121 if ($active_record_override !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Derek Jonesffca6c22009-12-05 15:31:44 +0000123 $active_record = $active_record_override;
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Greg Aker3a746652011-04-19 10:59:47 -0500126 require_once(BASEPATH.'database/DB_driver.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000127
128 if ( ! isset($active_record) OR $active_record == TRUE)
129 {
Greg Aker3a746652011-04-19 10:59:47 -0500130 require_once(BASEPATH.'database/DB_active_rec.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 if ( ! class_exists('CI_DB'))
132 {
GDmac92f019c2011-12-26 14:55:41 +0100133 class CI_DB extends CI_DB_active_record { }
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 }
135 }
Andrey Andreev24276a32012-01-08 02:44:38 +0200136 elseif ( ! class_exists('CI_DB'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Andrey Andreev24276a32012-01-08 02:44:38 +0200138 class CI_DB extends CI_DB_driver { }
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Greg Aker3a746652011-04-19 10:59:47 -0500141 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000142
143 // Instantiate the DB adapter
144 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Pascal Kriete58560022010-11-10 16:01:20 -0500145 $DB = new $driver($params);
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 if ($DB->autoinit == TRUE)
148 {
149 $DB->initialize();
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Jonesc34e5782010-03-02 17:35:55 -0600152 if (isset($params['stricton']) && $params['stricton'] == TRUE)
153 {
Barry Mienydd671972010-10-04 16:33:58 +0200154 $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
Derek Jonesc34e5782010-03-02 17:35:55 -0600155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 return $DB;
Barry Mienydd671972010-10-04 16:33:58 +0200158}
Derek Allard2067d1a2008-11-13 22:59:24 +0000159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160/* End of file DB.php */
Andrey Andreevc2697db2012-03-26 13:05:24 +0300161/* Location: ./system/database/DB.php */