blob: 8314d3b97dca2a858e6b0193be54c9f89e2076db [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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Initialize the database
20 *
21 * @category Database
22 * @author ExpressionEngine Dev Team
23 * @link http://codeigniter.com/user_guide/database/
David Behler07b53422011-08-15 00:25:06 +020024 * @param string
25 * @param bool Determines if active record should be used or not
Derek Allard2067d1a2008-11-13 22:59:24 +000026 */
Derek Jonesffca6c22009-12-05 15:31:44 +000027function &DB($params = '', $active_record_override = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000028{
29 // Load the DB config file if a DSN string wasn't passed
30 if (is_string($params) AND strpos($params, '://') === FALSE)
31 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +010032 // Is the config file in the environment folder?
Greg Aker3a746652011-04-19 10:59:47 -050033 if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php'))
joelcoxe3da4282011-01-16 15:00:04 +010034 {
Greg Aker3a746652011-04-19 10:59:47 -050035 if ( ! file_exists($file_path = APPPATH.'config/database.php'))
joelcoxe3da4282011-01-16 15:00:04 +010036 {
Greg Aker3a746652011-04-19 10:59:47 -050037 show_error('The configuration file database.php does not exist.');
joelcoxe3da4282011-01-16 15:00:04 +010038 }
39 }
David Behler07b53422011-08-15 00:25:06 +020040
joelcoxe3da4282011-01-16 15:00:04 +010041 include($file_path);
Barry Mienydd671972010-10-04 16:33:58 +020042
Derek Allard2067d1a2008-11-13 22:59:24 +000043 if ( ! isset($db) OR count($db) == 0)
44 {
45 show_error('No database connection settings were found in the database config file.');
46 }
Barry Mienydd671972010-10-04 16:33:58 +020047
Derek Allard2067d1a2008-11-13 22:59:24 +000048 if ($params != '')
49 {
50 $active_group = $params;
51 }
Barry Mienydd671972010-10-04 16:33:58 +020052
Derek Allard2067d1a2008-11-13 22:59:24 +000053 if ( ! isset($active_group) OR ! isset($db[$active_group]))
54 {
55 show_error('You have specified an invalid database connection group.');
56 }
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 $params = $db[$active_group];
59 }
60 elseif (is_string($params))
61 {
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Allard2067d1a2008-11-13 22:59:24 +000063 /* parse the URL from the DSN string
Derek Jones37f4b9c2011-07-01 17:56:50 -050064 * Database settings can be passed as discreet
65 * parameters or as a data source name in the first
66 * parameter. DSNs must have this prototype:
67 * $dsn = 'driver://username:password@hostname/database';
Barry Mienydd671972010-10-04 16:33:58 +020068 */
69
Derek Allard2067d1a2008-11-13 22:59:24 +000070 if (($dns = @parse_url($params)) === FALSE)
71 {
72 show_error('Invalid DB Connection String');
73 }
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 $params = array(
76 'dbdriver' => $dns['scheme'],
77 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
78 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
79 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
80 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
81 );
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Allard2067d1a2008-11-13 22:59:24 +000083 // were additional config items set?
84 if (isset($dns['query']))
85 {
86 parse_str($dns['query'], $extra);
87
Pascal Krietec3a4a8d2011-02-14 13:40:08 -050088 foreach ($extra as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
90 // booleans please
91 if (strtoupper($val) == "TRUE")
92 {
93 $val = TRUE;
94 }
95 elseif (strtoupper($val) == "FALSE")
96 {
97 $val = FALSE;
98 }
99
100 $params[$key] = $val;
101 }
102 }
103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Jones37f4b9c2011-07-01 17:56:50 -0500105 // No DB specified yet? Beat them senseless...
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
107 {
108 show_error('You have not selected a database type to connect to.');
109 }
110
Derek Jones37f4b9c2011-07-01 17:56:50 -0500111 // Load the DB classes. Note: Since the active record class is optional
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 // we need to dynamically create a class that extends proper parent class
113 // based on whether we're using the active record class or not.
114 // Kudos to Paul for discovering this clever use of eval()
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Jonesffca6c22009-12-05 15:31:44 +0000116 if ($active_record_override !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Derek Jonesffca6c22009-12-05 15:31:44 +0000118 $active_record = $active_record_override;
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Greg Aker3a746652011-04-19 10:59:47 -0500121 require_once(BASEPATH.'database/DB_driver.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000122
123 if ( ! isset($active_record) OR $active_record == TRUE)
124 {
Greg Aker3a746652011-04-19 10:59:47 -0500125 require_once(BASEPATH.'database/DB_active_rec.php');
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 if ( ! class_exists('CI_DB'))
128 {
129 eval('class CI_DB extends CI_DB_active_record { }');
130 }
131 }
132 else
133 {
134 if ( ! class_exists('CI_DB'))
135 {
136 eval('class CI_DB extends CI_DB_driver { }');
137 }
138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Greg Aker3a746652011-04-19 10:59:47 -0500140 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000141
142 // Instantiate the DB adapter
143 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Pascal Kriete58560022010-11-10 16:01:20 -0500144 $DB = new $driver($params);
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 if ($DB->autoinit == TRUE)
147 {
148 $DB->initialize();
149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Jonesc34e5782010-03-02 17:35:55 -0600151 if (isset($params['stricton']) && $params['stricton'] == TRUE)
152 {
Barry Mienydd671972010-10-04 16:33:58 +0200153 $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
Derek Jonesc34e5782010-03-02 17:35:55 -0600154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 return $DB;
Barry Mienydd671972010-10-04 16:33:58 +0200157}
Derek Allard2067d1a2008-11-13 22:59:24 +0000158
159
160
161/* End of file DB.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000162/* Location: ./system/database/DB.php */