blob: ef67c80c8d665234ee02fccd6f41cba2aa50a216 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Initialize the database
20 *
21 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000022 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000023 * @link http://codeigniter.com/user_guide/database/
Derek Allardd2df9bc2007-04-15 17:41:17 +000024 */
Derek Jones75d568d2008-01-30 20:26:25 +000025function &DB($params = '', $active_record_override = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +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);
31
Derek Jones0b59f272008-05-13 04:22:33 +000032 if ( ! isset($db) OR count($db) == 0)
Derek Allardd2df9bc2007-04-15 17:41:17 +000033 {
Derek Allard39b622d2008-01-16 21:10:09 +000034 show_error('No database connection settings were found in the database config file.');
Derek Allardd2df9bc2007-04-15 17:41:17 +000035 }
36
Derek Allard39b622d2008-01-16 21:10:09 +000037 if ($params != '')
38 {
39 $active_group = $params;
40 }
41
Derek Jones0b59f272008-05-13 04:22:33 +000042 if ( ! isset($active_group) OR ! isset($db[$active_group]))
Derek Allard39b622d2008-01-16 21:10:09 +000043 {
44 show_error('You have specified an invalid database connection group.');
45 }
46
Derek Allard993925b2008-08-21 12:43:31 +000047 $params = $db[$active_group] }
Derek Jones63eac3e2008-02-14 20:23:37 +000048 elseif (is_string($params))
Derek Jones7dd38382008-02-13 04:52:58 +000049 {
50
51 /* parse the URL from the DSN string
52 * Database settings can be passed as discreet
53 * parameters or as a data source name in the first
54 * parameter. DSNs must have this prototype:
55 * $dsn = 'driver://username:password@hostname/database';
56 */
57
58 if (($dns = @parse_url($params)) === FALSE)
59 {
60 show_error('Invalid DB Connection String');
61 }
62
63 $params = array(
64 'dbdriver' => $dns['scheme'],
65 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '',
66 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '',
67 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '',
Derek Jones97bc0102008-05-14 15:01:50 +000068 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : ''
Derek Jones7dd38382008-02-13 04:52:58 +000069 );
Derek Jones97bc0102008-05-14 15:01:50 +000070
71 // were additional config items set?
72 if (isset($dns['query']))
73 {
74 parse_str($dns['query'], $extra);
75
76 foreach($extra as $key => $val)
77 {
78 // booleans please
79 if (strtoupper($val) == "TRUE")
80 {
81 $val = TRUE;
82 }
83 elseif (strtoupper($val) == "FALSE")
84 {
85 $val = FALSE;
86 }
87
88 $params[$key] = $val;
89 }
90 }
Derek Jones7dd38382008-02-13 04:52:58 +000091 }
Derek Jones97bc0102008-05-14 15:01:50 +000092
Derek Allardd2df9bc2007-04-15 17:41:17 +000093 // No DB specified yet? Beat them senseless...
Derek Jones0b59f272008-05-13 04:22:33 +000094 if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '')
Derek Allardd2df9bc2007-04-15 17:41:17 +000095 {
96 show_error('You have not selected a database type to connect to.');
97 }
98
99 // Load the DB classes. Note: Since the active record class is optional
100 // we need to dynamically create a class that extends proper parent class
101 // based on whether we're using the active record class or not.
102 // Kudos to Paul for discovering this clever use of eval()
103
Derek Jones75d568d2008-01-30 20:26:25 +0000104 if ($active_record_override == TRUE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000105 {
Derek Jones75d568d2008-01-30 20:26:25 +0000106 $active_record = TRUE;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000107 }
108
109 require_once(BASEPATH.'database/DB_driver'.EXT);
110
Derek Jones0b59f272008-05-13 04:22:33 +0000111 if ( ! isset($active_record) OR $active_record == TRUE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000112 {
113 require_once(BASEPATH.'database/DB_active_rec'.EXT);
114
Derek Jones0b59f272008-05-13 04:22:33 +0000115 if ( ! class_exists('CI_DB'))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000116 {
117 eval('class CI_DB extends CI_DB_active_record { }');
118 }
119 }
120 else
121 {
Derek Jones0b59f272008-05-13 04:22:33 +0000122 if ( ! class_exists('CI_DB'))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000123 {
124 eval('class CI_DB extends CI_DB_driver { }');
125 }
126 }
127
128 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT);
129
130 // Instantiate the DB adapter
131 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
Derek Allard39b622d2008-01-16 21:10:09 +0000132 $DB =& new $driver($params);
133
134 if ($DB->autoinit == TRUE)
135 {
136 $DB->initialize();
137 }
138
Derek Allardd2df9bc2007-04-15 17:41:17 +0000139 return $DB;
140}
141
142
Derek Jones0b59f272008-05-13 04:22:33 +0000143
144/* End of file DB.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000145/* Location: ./system/database/DB.php */