11<?php
2+
23/**
34 * CodeIgniter
45 *
56 * An open source application development framework for PHP
67 *
78 * This content is released under the MIT License (MIT)
89 *
9- * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
10+ * Copyright (c) 2019 - 2022, CodeIgniter Foundation
1011 *
1112 * Permission is hereby granted, free of charge, to any person obtaining a copy
1213 * of this software and associated documentation files (the "Software"), to deal
3031 * @author EllisLab Dev Team
3132 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
3233 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
34+ * @copyright Copyright (c) 2019 - 2022, CodeIgniter Foundation (https://codeigniter.com/)
3335 * @license https://opensource.org/licenses/MIT MIT License
3436 * @link https://codeigniter.com
3537 * @since Version 1.0.0
3638 * @filesource
3739 */
38- defined ('BASEPATH ' ) OR exit ('No direct script access allowed ' );
40+ defined ('BASEPATH ' ) or exit ('No direct script access allowed ' );
3941
4042/**
4143 * Initialize the database
4547 * @link https://codeigniter.com/userguide3/database/
4648 *
4749 * @param string|string[] $params
48- * @param bool $query_builder_override
49- * Determines if query builder should be used or not
5050 */
51- function &DB ($ params = '' , $ query_builder_override = NULL )
51+ function &DB ($ params = '' )
5252{
5353 // Load the DB config file if a DSN string wasn't passed
54- if (is_string ($ params ) && strpos ($ params , ':// ' ) === FALSE )
55- {
54+ if (is_string ($ params ) && strpos ($ params , ':// ' ) === false ) {
5655 // Is the config file in the environment folder?
57- if ( ! file_exists ($ file_path = APPPATH .'config/ ' .ENVIRONMENT .'/database.php ' )
58- && ! file_exists ($ file_path = APPPATH .'config/database.php ' ))
59- {
56+ if (
57+ !file_exists ($ file_path = APPPATH . 'config/ ' . ENVIRONMENT . '/database.php ' )
58+ && !file_exists ($ file_path = APPPATH . 'config/database.php ' )
59+ ) {
6060 show_error ('The configuration file database.php does not exist. ' );
6161 }
6262
6363 include ($ file_path );
6464
6565 // Make packages contain database config files,
6666 // given that the controller instance already exists
67- if (class_exists ('CI_Controller ' , FALSE ))
68- {
69- foreach (get_instance ()->load ->get_package_paths () as $ path )
70- {
71- if ($ path !== APPPATH )
72- {
73- if (file_exists ($ file_path = $ path .'config/ ' .ENVIRONMENT .'/database.php ' ))
74- {
67+ if (class_exists ('CI_Controller ' , false )) {
68+ foreach (get_instance ()->load ->get_package_paths () as $ path ) {
69+ if ($ path !== APPPATH ) {
70+ if (file_exists ($ file_path = $ path . 'config/ ' . ENVIRONMENT . '/database.php ' )) {
7571 include ($ file_path );
76- }
77- elseif (file_exists ($ file_path = $ path .'config/database.php ' ))
78- {
72+ } elseif (file_exists ($ file_path = $ path . 'config/database.php ' )) {
7973 include ($ file_path );
8074 }
8175 }
8276 }
8377 }
8478
85- if ( ! isset ($ db ) OR count ($ db ) === 0 )
86- {
79+ if (empty ($ db )) {
8780 show_error ('No database connection settings were found in the database config file. ' );
8881 }
8982
90- if ($ params !== '' )
91- {
83+ if ($ params !== '' ) {
9284 $ active_group = $ params ;
9385 }
9486
95- if ( ! isset ($ active_group ))
96- {
87+ if (!isset ($ active_group )) {
9788 show_error ('You have not specified a database connection group via $active_group in your config/database.php file. ' );
98- }
99- elseif ( ! isset ($ db [$ active_group ]))
100- {
101- show_error ('You have specified an invalid database connection group ( ' .$ active_group .') in your config/database.php file. ' );
89+ } elseif (!isset ($ db [$ active_group ])) {
90+ show_error ('You have specified an invalid database connection group ( ' . $ active_group . ') in your config/database.php file. ' );
10291 }
10392
10493 $ params = $ db [$ active_group ];
105- }
106- elseif (is_string ($ params ))
107- {
94+ } elseif (is_string ($ params )) {
10895 /**
10996 * Parse the URL from the DSN string
11097 * Database settings can be passed as discreet
11198 * parameters or as a data source name in the first
11299 * parameter. DSNs must have this prototype:
113100 * $dsn = 'driver://username:password@hostname/database';
114101 */
115- if (($ dsn = @parse_url ($ params )) === FALSE )
116- {
102+ if (($ dsn = @parse_url ($ params )) === false ) {
117103 show_error ('Invalid DB Connection String ' );
118104 }
119105
@@ -127,15 +113,12 @@ function &DB($params = '', $query_builder_override = NULL)
127113 ];
128114
129115 // Were additional config items set?
130- if (isset ($ dsn ['query ' ]))
131- {
116+ if (isset ($ dsn ['query ' ])) {
132117 parse_str ($ dsn ['query ' ], $ extra );
133118
134- foreach ($ extra as $ key => $ val )
135- {
136- if (is_string ($ val ) && in_array (strtoupper ($ val ), ['TRUE ' , 'FALSE ' , 'NULL ' ]))
137- {
138- $ val = var_export ($ val , TRUE );
119+ foreach ($ extra as $ key => $ val ) {
120+ if (is_string ($ val ) && in_array (strtoupper ($ val ), ['TRUE ' , 'FALSE ' , 'NULL ' ])) {
121+ $ val = var_export ($ val , true );
139122 }
140123
141124 $ params [$ key ] = $ val ;
@@ -144,71 +127,46 @@ function &DB($params = '', $query_builder_override = NULL)
144127 }
145128
146129 // No DB specified yet? Beat them senseless...
147- if (empty ($ params ['dbdriver ' ]))
148- {
130+ if (empty ($ params ['dbdriver ' ])) {
149131 show_error ('You have not selected a database type to connect to. ' );
150132 }
151133
152- // Load the DB classes. Note: Since the query builder class is optional
153- // we need to dynamically create a class that extends proper parent class
154- // based on whether we're using the query builder class or not.
155- if ($ query_builder_override !== NULL )
156- {
157- $ query_builder = $ query_builder_override ;
158- }
159- // Backwards compatibility work-around for keeping the
160- // $active_record config variable working. Should be
161- // removed in v3.1
162- elseif ( ! isset ($ query_builder ) && isset ($ active_record ))
163- {
164- $ query_builder = $ active_record ;
165- }
166-
167- require_once (BASEPATH .'database/DB_driver.php ' );
168-
169- if ( ! isset ($ query_builder ) OR $ query_builder === TRUE )
170- {
171- require_once (BASEPATH .'database/DB_query_builder.php ' );
172- if ( ! class_exists ('CI_DB ' , FALSE ))
173- {
174- /**
175- * CI_DB
176- *
177- * Acts as an alias for both CI_DB_driver and CI_DB_query_builder.
178- *
179- * @see CI_DB_query_builder
180- * @see CI_DB_driver
181- */
182- class CI_DB extends CI_DB_query_builder { }
183- }
184- }
185- elseif ( ! class_exists ('CI_DB ' , FALSE ))
186- {
134+ require_once (BASEPATH . 'database/DB_driver.php ' );
135+ require_once (BASEPATH . 'database/DB_query_builder.php ' );
136+ if (!class_exists ('CI_DB ' , false )) {
187137 /**
188- * @ignore
138+ * CI_DB
139+ *
140+ * Acts as an alias for both CI_DB_driver and CI_DB_query_builder.
141+ *
142+ * @see CI_DB_query_builder
143+ * @see CI_DB_driver
189144 */
190- class CI_DB extends CI_DB_driver { }
145+ class CI_DB extends CI_DB_query_builder
146+ {
147+ }
191148 }
192149
193150 // Load the DB driver
194- $ driver_file = BASEPATH .'database/drivers/ ' .$ params ['dbdriver ' ].'/ ' .$ params ['dbdriver ' ].'_driver.php ' ;
195-
196- file_exists ($ driver_file ) OR show_error ('Invalid DB driver ' );
151+ $ driver_file = BASEPATH . 'database/drivers/ ' . $ params ['dbdriver ' ] . '/ ' . $ params ['dbdriver ' ] . '_driver.php ' ;
152+ file_exists ($ driver_file ) or show_error ('Invalid DB driver ' );
197153 require_once ($ driver_file );
198154
155+ // Load the result classes as well
156+ require_once (BASEPATH . 'database/DB_result.php ' );
157+ require_once (BASEPATH . 'database/drivers/ ' . $ params ['dbdriver ' ] . '/ ' . $ params ['dbdriver ' ] . '_result.php ' );
158+
199159 // Instantiate the DB adapter
200- $ driver = 'CI_DB_ ' . $ params ['dbdriver ' ]. '_driver ' ;
160+ $ driver = 'CI_DB_ ' . $ params ['dbdriver ' ] . '_driver ' ;
201161 $ DB = new $ driver ($ params );
202162
203163 // Check for a subdriver
204- if ( ! empty ($ DB ->subdriver ))
205- {
206- $ driver_file = BASEPATH .'database/drivers/ ' .$ DB ->dbdriver .'/subdrivers/ ' .$ DB ->dbdriver .'_ ' .$ DB ->subdriver .'_driver.php ' ;
164+ if (!empty ($ DB ->subdriver )) {
165+ $ driver_file = BASEPATH . 'database/drivers/ ' . $ DB ->dbdriver . '/subdrivers/ ' . $ DB ->dbdriver . '_ ' . $ DB ->subdriver . '_driver.php ' ;
207166
208- if (file_exists ($ driver_file ))
209- {
167+ if (file_exists ($ driver_file )) {
210168 require_once ($ driver_file );
211- $ driver = 'CI_DB_ ' . $ DB ->dbdriver . '_ ' . $ DB ->subdriver . '_driver ' ;
169+ $ driver = 'CI_DB_ ' . $ DB ->dbdriver . '_ ' . $ DB ->subdriver . '_driver ' ;
212170 $ DB = new $ driver ($ params );
213171 }
214172 }
0 commit comments