blob: 60a67e8214a2276c31e97f1f14eec50b943f31ac [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, 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/
24 */
Derek Jonesffca6c22009-12-05 15:31:44 +000025function &DB($params = '', $active_record_override = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +000026{
27 // Load the DB config file if a DSN string wasn't passed
28 if (is_string($params) AND strpos($params, '://') === FALSE)
29 {
30 include(APPPATH.'config/database'.EXT);
Barry Mienydd671972010-10-04 16:33:58 +020031
Derek Allard2067d1a2008-11-13 22:59:24 +000032 if ( ! isset($db) OR count($db) == 0)
33 {
34 show_error('No database connection settings were found in the database config file.');
35 }
Barry Mienydd671972010-10-04 16:33:58 +020036
Derek Allard2067d1a2008-11-13 22:59:24 +000037 if ($params != '')
38 {
39 $active_group = $params;
40 }
Barry Mienydd671972010-10-04 16:33:58 +020041
Derek Allard2067d1a2008-11-13 22:59:24 +000042 if ( ! isset($active_group) OR ! isset($db[$active_group]))
43 {
44 show_error('You have specified an invalid database connection group.');
45 }
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 $params = $db[$active_group];
48 }
49 elseif (is_string($params))
50 {
Barry Mienydd671972010-10-04 16:33:58 +020051
Derek Allard2067d1a2008-11-13 22:59:24 +000052 /* parse the URL from the DSN string
Barry Mienydd671972010-10-04 16:33:58 +020053 * Database settings can be passed as discreet
54 * parameters or as a data source name in the first
55 * parameter. DSNs must have this prototype:
56 * $dsn = 'driver://username:password@hostname/database';
57 */
58
Derek Allard2067d1a2008-11-13 22:59:24 +000059 if (($dns = @parse_url($params)) === FALSE)
60 {
61 show_error('Invalid DB Connection String');
62 }
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064 $params = array(
65 'dbdriver' => $dns['scheme'],
66 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
67 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
68 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
69 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
70 );
Barry Mienydd671972010-10-04 16:33:58 +020071
Derek Allard2067d1a2008-11-13 22:59:24 +000072 // were additional config items set?
73 if (isset($dns['query']))
74 {
75 parse_str($dns['query'], $extra);
76
77 foreach($extra as $key => $val)
78 {
79 // booleans please
80 if (strtoupper($val) == "TRUE")
81 {
82 $val = TRUE;
83 }
84 elseif (strtoupper($val) == "FALSE")
85 {
86 $val = FALSE;
87 }
88
89 $params[$key] = $val;
90 }
91 }
92 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // No DB specified yet? Beat them senseless...
95 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
96 {
97 show_error('You have not selected a database type to connect to.');
98 }
99
100 // Load the DB classes. Note: Since the active record class is optional
101 // we need to dynamically create a class that extends proper parent class
102 // based on whether we're using the active record class or not.
103 // Kudos to Paul for discovering this clever use of eval()
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Jonesffca6c22009-12-05 15:31:44 +0000105 if ($active_record_override !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 {
Derek Jonesffca6c22009-12-05 15:31:44 +0000107 $active_record = $active_record_override;
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 require_once(BASEPATH.'database/DB_driver'.EXT);
111
112 if ( ! isset($active_record) OR $active_record == TRUE)
113 {
114 require_once(BASEPATH.'database/DB_active_rec'.EXT);
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 if ( ! class_exists('CI_DB'))
117 {
118 eval('class CI_DB extends CI_DB_active_record { }');
119 }
120 }
121 else
122 {
123 if ( ! class_exists('CI_DB'))
124 {
125 eval('class CI_DB extends CI_DB_driver { }');
126 }
127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
130
131 // Instantiate the DB adapter
132 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Pascal Kriete58560022010-11-10 16:01:20 -0500133 $DB = new $driver($params);
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 if ($DB->autoinit == TRUE)
136 {
137 $DB->initialize();
138 }
Barry Mienydd671972010-10-04 16:33:58 +0200139
Derek Jonesc34e5782010-03-02 17:35:55 -0600140 if (isset($params['stricton']) && $params['stricton'] == TRUE)
141 {
Barry Mienydd671972010-10-04 16:33:58 +0200142 $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
Derek Jonesc34e5782010-03-02 17:35:55 -0600143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 return $DB;
Barry Mienydd671972010-10-04 16:33:58 +0200146}
Derek Allard2067d1a2008-11-13 22:59:24 +0000147
148
149
150/* End of file DB.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000151/* Location: ./system/database/DB.php */