blob: 6161f149b4b6fe527bc2de6e45044ef41f154954 [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 * Database Driver Class
20 *
21 * This is the platform-independent base DB implementation class.
22 * This class will not be called directly. Rather, the adapter
23 * class for the specific database will extend and instantiate it.
24 *
25 * @package CodeIgniter
26 * @subpackage Drivers
27 * @category Database
28 * @author ExpressionEngine Dev Team
29 * @link http://codeigniter.com/user_guide/database/
30 */
31class CI_DB_driver {
32
33 var $username;
34 var $password;
35 var $hostname;
36 var $database;
37 var $dbdriver = 'mysql';
38 var $dbprefix = '';
39 var $char_set = 'utf8';
40 var $dbcollat = 'utf8_general_ci';
41 var $autoinit = TRUE; // Whether to automatically initialize the DB
42 var $swap_pre = '';
43 var $port = '';
44 var $pconnect = FALSE;
45 var $conn_id = FALSE;
46 var $result_id = FALSE;
47 var $db_debug = FALSE;
48 var $benchmark = 0;
49 var $query_count = 0;
50 var $bind_marker = '?';
51 var $save_queries = TRUE;
52 var $queries = array();
53 var $query_times = array();
54 var $data_cache = array();
55 var $trans_enabled = TRUE;
56 var $trans_strict = TRUE;
57 var $_trans_depth = 0;
58 var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
59 var $cache_on = FALSE;
60 var $cachedir = '';
61 var $cache_autodel = FALSE;
62 var $CACHE; // The cache class object
63
64 // Private variables
65 var $_protect_identifiers = TRUE;
66 var $_reserved_identifiers = array('*'); // Identifiers that should NOT be escaped
67
68 // These are use with Oracle
69 var $stmt_id;
70 var $curs_id;
71 var $limit_used;
72
73
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -050076 * Constructor. Accepts one parameter containing the database
Derek Allard2067d1a2008-11-13 22:59:24 +000077 * connection settings.
78 *
79 * @param array
Barry Mienydd671972010-10-04 16:33:58 +020080 */
Timothy Warren6f531dc2011-10-10 10:10:46 -040081 function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
83 if (is_array($params))
84 {
85 foreach ($params as $key => $val)
86 {
87 $this->$key = $val;
88 }
89 }
90
91 log_message('debug', 'Database Driver Class Initialized');
92 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // --------------------------------------------------------------------
95
96 /**
97 * Initialize Database Settings
98 *
99 * @access private Called by the constructor
100 * @param mixed
101 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200102 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 function initialize()
104 {
105 // If an existing connection resource is available
106 // there is no need to connect and select the database
107 if (is_resource($this->conn_id) OR is_object($this->conn_id))
108 {
109 return TRUE;
110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 // ----------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // Connect to the database and set the connection ID
115 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
116
Derek Jones37f4b9c2011-07-01 17:56:50 -0500117 // No connection resource? Throw an error
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 if ( ! $this->conn_id)
119 {
120 log_message('error', 'Unable to connect to the database');
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 if ($this->db_debug)
123 {
124 $this->display_error('db_unable_to_connect');
125 }
126 return FALSE;
127 }
128
129 // ----------------------------------------------------------------
130
131 // Select the DB... assuming a database name is specified in the config file
132 if ($this->database != '')
133 {
134 if ( ! $this->db_select())
135 {
136 log_message('error', 'Unable to select database: '.$this->database);
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 if ($this->db_debug)
139 {
140 $this->display_error('db_unable_to_select', $this->database);
141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
144 else
145 {
146 // We've selected the DB. Now we set the character set
147 if ( ! $this->db_set_charset($this->char_set, $this->dbcollat))
148 {
149 return FALSE;
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 return TRUE;
153 }
154 }
155
156 return TRUE;
157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 // --------------------------------------------------------------------
160
161 /**
162 * Set client character set
163 *
164 * @access public
165 * @param string
166 * @param string
167 * @return resource
168 */
169 function db_set_charset($charset, $collation)
170 {
171 if ( ! $this->_db_set_charset($this->char_set, $this->dbcollat))
172 {
173 log_message('error', 'Unable to set database connection charset: '.$this->char_set);
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 if ($this->db_debug)
176 {
177 $this->display_error('db_unable_to_set_charset', $this->char_set);
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 return FALSE;
181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 return TRUE;
184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 // --------------------------------------------------------------------
187
188 /**
189 * The name of the platform in use (mysql, mssql, etc...)
190 *
191 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200192 * @return string
193 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 function platform()
195 {
196 return $this->dbdriver;
197 }
198
199 // --------------------------------------------------------------------
200
201 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500202 * Database Version Number. Returns a string containing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * version of the database being used
204 *
205 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200206 * @return string
207 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 function version()
209 {
210 if (FALSE === ($sql = $this->_version()))
211 {
212 if ($this->db_debug)
213 {
214 return $this->display_error('db_unsupported_function');
215 }
216 return FALSE;
217 }
Derek Allard3683f772009-12-16 17:32:33 +0000218
219 // Some DBs have functions that return the version, and don't run special
220 // SQL queries per se. In these instances, just return the result.
Esen Sagynov2e087942011-08-09 23:35:01 -0700221 $driver_version_exceptions = array('oci8', 'sqlite', 'cubrid');
Derek Allard3683f772009-12-16 17:32:33 +0000222
223 if (in_array($this->dbdriver, $driver_version_exceptions))
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
225 return $sql;
226 }
Derek Allard3683f772009-12-16 17:32:33 +0000227 else
228 {
229 $query = $this->query($sql);
230 return $query->row('ver');
231 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // --------------------------------------------------------------------
235
236 /**
237 * Execute the query
238 *
239 * Accepts an SQL string as input and returns a result object upon
Derek Jones37f4b9c2011-07-01 17:56:50 -0500240 * successful execution of a "read" type query. Returns boolean TRUE
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 * upon successful execution of a "write" type query. Returns boolean
242 * FALSE upon failure, and if the $db_debug variable is set to TRUE
243 * will raise an error.
244 *
245 * @access public
246 * @param string An SQL query string
247 * @param array An array of binding data
Barry Mienydd671972010-10-04 16:33:58 +0200248 * @return mixed
249 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 function query($sql, $binds = FALSE, $return_object = TRUE)
251 {
252 if ($sql == '')
253 {
254 if ($this->db_debug)
255 {
256 log_message('error', 'Invalid query: '.$sql);
257 return $this->display_error('db_invalid_query');
258 }
259 return FALSE;
260 }
261
262 // Verify table prefix and replace if necessary
263 if ( ($this->dbprefix != '' AND $this->swap_pre != '') AND ($this->dbprefix != $this->swap_pre) )
Derek Jonese7792202010-03-02 17:24:46 -0600264 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $sql = preg_replace("/(\W)".$this->swap_pre."(\S+?)/", "\\1".$this->dbprefix."\\2", $sql);
266 }
Derek Jonese7792202010-03-02 17:24:46 -0600267
Ryan Dialef7474c2012-03-01 16:11:36 -0500268 // Compile binds if needed
269 if ($binds !== FALSE)
270 {
271 $sql = $this->compile_binds($sql, $binds);
272 }
273
Derek Jones37f4b9c2011-07-01 17:56:50 -0500274 // Is query caching enabled? If the query is a "read type"
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 // we will load the caching class and return the previously
276 // cached query if it exists
277 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
278 {
279 if ($this->_cache_init())
280 {
281 $this->load_rdriver();
282 if (FALSE !== ($cache = $this->CACHE->read($sql)))
283 {
284 return $cache;
285 }
286 }
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Jones37f4b9c2011-07-01 17:56:50 -0500289 // Save the query for debugging
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 if ($this->save_queries == TRUE)
291 {
292 $this->queries[] = $sql;
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 // Start the Query Timer
296 $time_start = list($sm, $ss) = explode(' ', microtime());
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // Run the Query
299 if (FALSE === ($this->result_id = $this->simple_query($sql)))
300 {
301 if ($this->save_queries == TRUE)
302 {
303 $this->query_times[] = 0;
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // This will trigger a rollback if transactions are being used
307 $this->_trans_status = FALSE;
308
309 if ($this->db_debug)
310 {
311 // grab the error number and message now, as we might run some
312 // additional queries before displaying the error
313 $error_no = $this->_error_number();
314 $error_msg = $this->_error_message();
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 // We call this function in order to roll-back queries
Derek Jones37f4b9c2011-07-01 17:56:50 -0500317 // if transactions are enabled. If we don't call this here
Barry Mienydd671972010-10-04 16:33:58 +0200318 // the error message will trigger an exit, causing the
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // transactions to remain in limbo.
320 $this->trans_complete();
321
322 // Log and display errors
323 log_message('error', 'Query error: '.$error_msg);
324 return $this->display_error(
325 array(
326 'Error Number: '.$error_no,
327 $error_msg,
328 $sql
329 )
330 );
331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 return FALSE;
334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // Stop and aggregate the query time results
337 $time_end = list($em, $es) = explode(' ', microtime());
338 $this->benchmark += ($em + $es) - ($sm + $ss);
339
340 if ($this->save_queries == TRUE)
341 {
342 $this->query_times[] = ($em + $es) - ($sm + $ss);
343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 // Increment the query counter
346 $this->query_count++;
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // Was the query a "write" type?
349 // If so we'll simply return true
350 if ($this->is_write_type($sql) === TRUE)
351 {
352 // If caching is enabled we'll auto-cleanup any
353 // existing files related to this particular URI
354 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
355 {
356 $this->CACHE->delete();
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 return TRUE;
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // Return TRUE if we don't need to create a result object
363 // Currently only the Oracle driver uses this when stored
364 // procedures are used
365 if ($return_object !== TRUE)
366 {
367 return TRUE;
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
370 // Load and instantiate the result driver
371
372 $driver = $this->load_rdriver();
373 $RES = new $driver();
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 $RES->conn_id = $this->conn_id;
375 $RES->result_id = $this->result_id;
376
377 if ($this->dbdriver == 'oci8')
378 {
379 $RES->stmt_id = $this->stmt_id;
380 $RES->curs_id = NULL;
381 $RES->limit_used = $this->limit_used;
382 $this->stmt_id = FALSE;
383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 // oci8 vars must be set before calling this
386 $RES->num_rows = $RES->num_rows();
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Jones37f4b9c2011-07-01 17:56:50 -0500388 // Is query caching enabled? If so, we'll serialize the
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 // result object and save it to a cache file.
390 if ($this->cache_on == TRUE AND $this->_cache_init())
391 {
392 // We'll create a new instance of the result object
393 // only without the platform specific driver since
394 // we can't use it with cached data (the query result
395 // resource ID won't be any good once we've cached the
396 // result object, so we'll have to compile the data
397 // and save it)
398 $CR = new CI_DB_result();
Barry Mienydd671972010-10-04 16:33:58 +0200399 $CR->num_rows = $RES->num_rows();
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 $CR->result_object = $RES->result_object();
401 $CR->result_array = $RES->result_array();
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 // Reset these since cached objects can not utilize resource IDs.
404 $CR->conn_id = NULL;
405 $CR->result_id = NULL;
406
407 $this->CACHE->write($sql, $CR);
408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 return $RES;
411 }
412
413 // --------------------------------------------------------------------
414
415 /**
416 * Load the result drivers
417 *
418 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200419 * @return string the name of the result class
420 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 function load_rdriver()
422 {
423 $driver = 'CI_DB_'.$this->dbdriver.'_result';
424
425 if ( ! class_exists($driver))
426 {
Greg Aker3a746652011-04-19 10:59:47 -0500427 include_once(BASEPATH.'database/DB_result.php');
428 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 return $driver;
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 // --------------------------------------------------------------------
435
436 /**
437 * Simple Query
Derek Jones37f4b9c2011-07-01 17:56:50 -0500438 * This is a simplified version of the query() function. Internally
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * we only use it when running transaction commands since they do
440 * not require all the features of the main query() function.
441 *
442 * @access public
443 * @param string the sql query
Barry Mienydd671972010-10-04 16:33:58 +0200444 * @return mixed
445 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 function simple_query($sql)
447 {
448 if ( ! $this->conn_id)
449 {
450 $this->initialize();
451 }
452
453 return $this->_execute($sql);
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 // --------------------------------------------------------------------
457
458 /**
459 * Disable Transactions
460 * This permits transactions to be disabled at run-time.
461 *
462 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200463 * @return void
464 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 function trans_off()
466 {
467 $this->trans_enabled = FALSE;
468 }
469
470 // --------------------------------------------------------------------
471
472 /**
473 * Enable/disable Transaction Strict Mode
474 * When strict mode is enabled, if you are running multiple groups of
475 * transactions, if one group fails all groups will be rolled back.
476 * If strict mode is disabled, each group is treated autonomously, meaning
477 * a failure of one group will not affect any others
478 *
479 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200480 * @return void
481 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 function trans_strict($mode = TRUE)
483 {
484 $this->trans_strict = is_bool($mode) ? $mode : TRUE;
485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 // --------------------------------------------------------------------
488
489 /**
490 * Start Transaction
491 *
492 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200493 * @return void
494 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 function trans_start($test_mode = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200496 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 if ( ! $this->trans_enabled)
498 {
499 return FALSE;
500 }
501
502 // When transactions are nested we only begin/commit/rollback the outermost ones
503 if ($this->_trans_depth > 0)
504 {
505 $this->_trans_depth += 1;
506 return;
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 $this->trans_begin($test_mode);
510 }
511
512 // --------------------------------------------------------------------
513
514 /**
515 * Complete Transaction
516 *
517 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200518 * @return bool
519 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 function trans_complete()
521 {
522 if ( ! $this->trans_enabled)
523 {
524 return FALSE;
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 // When transactions are nested we only begin/commit/rollback the outermost ones
528 if ($this->_trans_depth > 1)
529 {
530 $this->_trans_depth -= 1;
531 return TRUE;
532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 // The query() function will set this flag to FALSE in the event that a query failed
535 if ($this->_trans_status === FALSE)
536 {
537 $this->trans_rollback();
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 // If we are NOT running in strict mode, we will reset
540 // the _trans_status flag so that subsequent groups of transactions
541 // will be permitted.
542 if ($this->trans_strict === FALSE)
543 {
544 $this->_trans_status = TRUE;
545 }
546
547 log_message('debug', 'DB Transaction Failure');
548 return FALSE;
549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 $this->trans_commit();
552 return TRUE;
553 }
554
555 // --------------------------------------------------------------------
556
557 /**
558 * Lets you retrieve the transaction flag to determine if it has failed
559 *
560 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200561 * @return bool
562 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 function trans_status()
564 {
565 return $this->_trans_status;
566 }
567
568 // --------------------------------------------------------------------
569
570 /**
571 * Compile Bindings
572 *
573 * @access public
574 * @param string the sql statement
575 * @param array an array of bind data
Barry Mienydd671972010-10-04 16:33:58 +0200576 * @return string
577 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 function compile_binds($sql, $binds)
579 {
580 if (strpos($sql, $this->bind_marker) === FALSE)
581 {
582 return $sql;
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 if ( ! is_array($binds))
586 {
587 $binds = array($binds);
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 // Get the sql segments around the bind markers
591 $segments = explode($this->bind_marker, $sql);
592
593 // The count of bind should be 1 less then the count of segments
594 // If there are more bind arguments trim it down
595 if (count($binds) >= count($segments)) {
596 $binds = array_slice($binds, 0, count($segments)-1);
597 }
598
599 // Construct the binded query
600 $result = $segments[0];
601 $i = 0;
602 foreach ($binds as $bind)
603 {
604 $result .= $this->escape($bind);
605 $result .= $segments[++$i];
606 }
607
608 return $result;
609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // --------------------------------------------------------------------
612
613 /**
614 * Determines if a query is a "write" type.
615 *
616 * @access public
617 * @param string An SQL query string
Barry Mienydd671972010-10-04 16:33:58 +0200618 * @return boolean
619 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 function is_write_type($sql)
621 {
Derek Allarde37ab382009-02-03 16:13:57 +0000622 if ( ! preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
624 return FALSE;
625 }
626 return TRUE;
627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // --------------------------------------------------------------------
630
631 /**
632 * Calculate the aggregate query elapsed time
633 *
634 * @access public
635 * @param integer The number of decimal places
Barry Mienydd671972010-10-04 16:33:58 +0200636 * @return integer
637 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 function elapsed_time($decimals = 6)
639 {
640 return number_format($this->benchmark, $decimals);
641 }
Barry Mienydd671972010-10-04 16:33:58 +0200642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 // --------------------------------------------------------------------
644
645 /**
646 * Returns the total number of queries
647 *
648 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200649 * @return integer
650 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 function total_queries()
652 {
653 return $this->query_count;
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 // --------------------------------------------------------------------
657
658 /**
659 * Returns the last query that was executed
660 *
661 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200662 * @return void
663 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 function last_query()
665 {
666 return end($this->queries);
667 }
668
669 // --------------------------------------------------------------------
670
671 /**
672 * "Smart" Escape String
673 *
674 * Escapes data based on type
675 * Sets boolean and null types
676 *
677 * @access public
678 * @param string
Barry Mienydd671972010-10-04 16:33:58 +0200679 * @return mixed
680 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 function escape($str)
Barry Mienydd671972010-10-04 16:33:58 +0200682 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000683 if (is_string($str))
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 {
Derek Jonesa377bdd2009-02-11 18:55:24 +0000685 $str = "'".$this->escape_str($str)."'";
686 }
687 elseif (is_bool($str))
688 {
689 $str = ($str === FALSE) ? 0 : 1;
690 }
691 elseif (is_null($str))
692 {
693 $str = 'NULL';
694 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000695
696 return $str;
697 }
698
699 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600700
Derek Jonese4ed5832009-02-20 21:44:59 +0000701 /**
Derek Jonesbdc7fb92009-02-20 21:55:10 +0000702 * Escape LIKE String
Derek Jonese4ed5832009-02-20 21:44:59 +0000703 *
704 * Calls the individual driver for platform
705 * specific escaping for LIKE conditions
Barry Mienydd671972010-10-04 16:33:58 +0200706 *
Derek Jonese4ed5832009-02-20 21:44:59 +0000707 * @access public
708 * @param string
709 * @return mixed
710 */
Barry Mienydd671972010-10-04 16:33:58 +0200711 function escape_like_str($str)
712 {
713 return $this->escape_str($str, TRUE);
Derek Jonese4ed5832009-02-20 21:44:59 +0000714 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000715
Derek Jonese4ed5832009-02-20 21:44:59 +0000716 // --------------------------------------------------------------------
Derek Jonese7792202010-03-02 17:24:46 -0600717
Derek Allard2067d1a2008-11-13 22:59:24 +0000718 /**
719 * Primary
720 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500721 * Retrieves the primary key. It assumes that the row in the first
Derek Allard2067d1a2008-11-13 22:59:24 +0000722 * position is the primary key
723 *
724 * @access public
725 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200726 * @return string
727 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 function primary($table = '')
Barry Mienydd671972010-10-04 16:33:58 +0200729 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 $fields = $this->list_fields($table);
Barry Mienydd671972010-10-04 16:33:58 +0200731
Derek Allard2067d1a2008-11-13 22:59:24 +0000732 if ( ! is_array($fields))
733 {
734 return FALSE;
735 }
736
737 return current($fields);
738 }
739
740 // --------------------------------------------------------------------
741
742 /**
743 * Returns an array of table names
744 *
745 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200746 * @return array
747 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 function list_tables($constrain_by_prefix = FALSE)
749 {
750 // Is there a cached result?
751 if (isset($this->data_cache['table_names']))
752 {
753 return $this->data_cache['table_names'];
754 }
Barry Mienydd671972010-10-04 16:33:58 +0200755
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 if (FALSE === ($sql = $this->_list_tables($constrain_by_prefix)))
757 {
758 if ($this->db_debug)
759 {
760 return $this->display_error('db_unsupported_function');
761 }
762 return FALSE;
763 }
764
765 $retval = array();
766 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 if ($query->num_rows() > 0)
769 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500770 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000771 {
772 if (isset($row['TABLE_NAME']))
773 {
774 $retval[] = $row['TABLE_NAME'];
775 }
776 else
777 {
778 $retval[] = array_shift($row);
779 }
780 }
781 }
782
783 $this->data_cache['table_names'] = $retval;
784 return $this->data_cache['table_names'];
785 }
Barry Mienydd671972010-10-04 16:33:58 +0200786
Derek Allard2067d1a2008-11-13 22:59:24 +0000787 // --------------------------------------------------------------------
788
789 /**
790 * Determine if a particular table exists
791 * @access public
792 * @return boolean
793 */
794 function table_exists($table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200795 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000796 return ( ! in_array($this->_protect_identifiers($table_name, TRUE, FALSE, FALSE), $this->list_tables())) ? FALSE : TRUE;
797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 // --------------------------------------------------------------------
800
801 /**
802 * Fetch MySQL Field Names
803 *
804 * @access public
805 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200806 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000807 */
808 function list_fields($table = '')
809 {
810 // Is there a cached result?
811 if (isset($this->data_cache['field_names'][$table]))
812 {
813 return $this->data_cache['field_names'][$table];
814 }
Barry Mienydd671972010-10-04 16:33:58 +0200815
Derek Allard2067d1a2008-11-13 22:59:24 +0000816 if ($table == '')
817 {
818 if ($this->db_debug)
819 {
820 return $this->display_error('db_field_param_missing');
821 }
822 return FALSE;
823 }
Barry Mienydd671972010-10-04 16:33:58 +0200824
Greg Aker1edde302010-01-26 00:17:01 +0000825 if (FALSE === ($sql = $this->_list_columns($table)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000826 {
827 if ($this->db_debug)
828 {
829 return $this->display_error('db_unsupported_function');
830 }
831 return FALSE;
832 }
Barry Mienydd671972010-10-04 16:33:58 +0200833
Derek Allard2067d1a2008-11-13 22:59:24 +0000834 $query = $this->query($sql);
Barry Mienydd671972010-10-04 16:33:58 +0200835
Derek Allard2067d1a2008-11-13 22:59:24 +0000836 $retval = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500837 foreach ($query->result_array() as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000838 {
839 if (isset($row['COLUMN_NAME']))
840 {
841 $retval[] = $row['COLUMN_NAME'];
842 }
843 else
844 {
845 $retval[] = current($row);
Barry Mienydd671972010-10-04 16:33:58 +0200846 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000847 }
Barry Mienydd671972010-10-04 16:33:58 +0200848
Derek Allard2067d1a2008-11-13 22:59:24 +0000849 $this->data_cache['field_names'][$table] = $retval;
850 return $this->data_cache['field_names'][$table];
851 }
852
853 // --------------------------------------------------------------------
854
855 /**
856 * Determine if a particular field exists
857 * @access public
858 * @param string
859 * @param string
860 * @return boolean
861 */
862 function field_exists($field_name, $table_name)
Barry Mienydd671972010-10-04 16:33:58 +0200863 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000864 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
865 }
Barry Mienydd671972010-10-04 16:33:58 +0200866
Derek Allard2067d1a2008-11-13 22:59:24 +0000867 // --------------------------------------------------------------------
868
869 /**
870 * Returns an object with field data
871 *
872 * @access public
873 * @param string the table name
Barry Mienydd671972010-10-04 16:33:58 +0200874 * @return object
875 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000876 function field_data($table = '')
877 {
878 if ($table == '')
879 {
880 if ($this->db_debug)
881 {
882 return $this->display_error('db_field_param_missing');
883 }
884 return FALSE;
885 }
Barry Mienydd671972010-10-04 16:33:58 +0200886
Derek Allard2067d1a2008-11-13 22:59:24 +0000887 $query = $this->query($this->_field_data($this->_protect_identifiers($table, TRUE, NULL, FALSE)));
888
889 return $query->field_data();
Barry Mienydd671972010-10-04 16:33:58 +0200890 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000891
892 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 /**
895 * Generate an insert string
896 *
897 * @access public
898 * @param string the table upon which the query will be performed
899 * @param array an associative array data of key/values
Barry Mienydd671972010-10-04 16:33:58 +0200900 * @return string
901 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000902 function insert_string($table, $data)
903 {
904 $fields = array();
905 $values = array();
Barry Mienydd671972010-10-04 16:33:58 +0200906
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500907 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000908 {
909 $fields[] = $this->_escape_identifiers($key);
910 $values[] = $this->escape($val);
911 }
Barry Mienydd671972010-10-04 16:33:58 +0200912
Derek Allard2067d1a2008-11-13 22:59:24 +0000913 return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
Barry Mienydd671972010-10-04 16:33:58 +0200914 }
915
Derek Allard2067d1a2008-11-13 22:59:24 +0000916 // --------------------------------------------------------------------
917
918 /**
919 * Generate an update string
920 *
921 * @access public
922 * @param string the table upon which the query will be performed
923 * @param array an associative array data of key/values
924 * @param mixed the "where" statement
Barry Mienydd671972010-10-04 16:33:58 +0200925 * @return string
926 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 function update_string($table, $data, $where)
928 {
929 if ($where == '')
930 {
931 return false;
932 }
Barry Mienydd671972010-10-04 16:33:58 +0200933
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 $fields = array();
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500935 foreach ($data as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000936 {
937 $fields[$this->_protect_identifiers($key)] = $this->escape($val);
938 }
939
940 if ( ! is_array($where))
941 {
942 $dest = array($where);
943 }
944 else
945 {
946 $dest = array();
947 foreach ($where as $key => $val)
948 {
949 $prefix = (count($dest) == 0) ? '' : ' AND ';
Barry Mienydd671972010-10-04 16:33:58 +0200950
Derek Allard2067d1a2008-11-13 22:59:24 +0000951 if ($val !== '')
952 {
953 if ( ! $this->_has_operator($key))
954 {
955 $key .= ' =';
956 }
Barry Mienydd671972010-10-04 16:33:58 +0200957
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 $val = ' '.$this->escape($val);
959 }
Barry Mienydd671972010-10-04 16:33:58 +0200960
Derek Allard2067d1a2008-11-13 22:59:24 +0000961 $dest[] = $prefix.$key.$val;
962 }
Barry Mienydd671972010-10-04 16:33:58 +0200963 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000964
965 return $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $dest);
Barry Mienydd671972010-10-04 16:33:58 +0200966 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000967
968 // --------------------------------------------------------------------
969
970 /**
971 * Tests whether the string has an SQL operator
972 *
973 * @access private
974 * @param string
975 * @return bool
976 */
977 function _has_operator($str)
978 {
979 $str = trim($str);
980 if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
981 {
982 return FALSE;
983 }
984
985 return TRUE;
986 }
987
988 // --------------------------------------------------------------------
989
990 /**
991 * Enables a native PHP function to be run, using a platform agnostic wrapper.
992 *
993 * @access public
994 * @param string the function name
995 * @param mixed any parameters needed by the function
Barry Mienydd671972010-10-04 16:33:58 +0200996 * @return mixed
997 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000998 function call_function($function)
999 {
1000 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
Barry Mienydd671972010-10-04 16:33:58 +02001001
Derek Allard2067d1a2008-11-13 22:59:24 +00001002 if (FALSE === strpos($driver, $function))
1003 {
1004 $function = $driver.$function;
1005 }
Barry Mienydd671972010-10-04 16:33:58 +02001006
Derek Allard2067d1a2008-11-13 22:59:24 +00001007 if ( ! function_exists($function))
1008 {
1009 if ($this->db_debug)
1010 {
1011 return $this->display_error('db_unsupported_function');
1012 }
1013 return FALSE;
1014 }
1015 else
1016 {
1017 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
Repoxd92bd572011-12-01 10:08:52 +01001018 if (is_null($args))
1019 {
1020 return call_user_func($function);
1021 }
1022 else
1023 {
1024 return call_user_func_array($function, $args);
1025 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001026 }
1027 }
1028
1029 // --------------------------------------------------------------------
1030
1031 /**
1032 * Set Cache Directory Path
1033 *
1034 * @access public
1035 * @param string the path to the cache directory
1036 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001037 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001038 function cache_set_path($path = '')
1039 {
1040 $this->cachedir = $path;
1041 }
1042
1043 // --------------------------------------------------------------------
1044
1045 /**
1046 * Enable Query Caching
1047 *
1048 * @access public
1049 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001050 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 function cache_on()
1052 {
1053 $this->cache_on = TRUE;
1054 return TRUE;
1055 }
1056
1057 // --------------------------------------------------------------------
1058
1059 /**
1060 * Disable Query Caching
1061 *
1062 * @access public
1063 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001064 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 function cache_off()
1066 {
1067 $this->cache_on = FALSE;
1068 return FALSE;
1069 }
Barry Mienydd671972010-10-04 16:33:58 +02001070
Derek Allard2067d1a2008-11-13 22:59:24 +00001071
1072 // --------------------------------------------------------------------
1073
1074 /**
1075 * Delete the cache files associated with a particular URI
1076 *
1077 * @access public
1078 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001079 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001080 function cache_delete($segment_one = '', $segment_two = '')
1081 {
1082 if ( ! $this->_cache_init())
1083 {
1084 return FALSE;
1085 }
1086 return $this->CACHE->delete($segment_one, $segment_two);
1087 }
1088
1089 // --------------------------------------------------------------------
1090
1091 /**
1092 * Delete All cache files
1093 *
1094 * @access public
1095 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001096 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001097 function cache_delete_all()
1098 {
1099 if ( ! $this->_cache_init())
1100 {
1101 return FALSE;
1102 }
1103
1104 return $this->CACHE->delete_all();
1105 }
1106
1107 // --------------------------------------------------------------------
1108
1109 /**
1110 * Initialize the Cache Class
1111 *
1112 * @access private
1113 * @return void
Barry Mienydd671972010-10-04 16:33:58 +02001114 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001115 function _cache_init()
1116 {
1117 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1118 {
1119 return TRUE;
1120 }
Derek Allarde37ab382009-02-03 16:13:57 +00001121
1122 if ( ! class_exists('CI_DB_Cache'))
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 {
Greg Aker3a746652011-04-19 10:59:47 -05001124 if ( ! @include(BASEPATH.'database/DB_cache.php'))
Derek Allarde37ab382009-02-03 16:13:57 +00001125 {
1126 return $this->cache_off();
1127 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 }
Derek Allarde37ab382009-02-03 16:13:57 +00001129
Derek Allard2067d1a2008-11-13 22:59:24 +00001130 $this->CACHE = new CI_DB_Cache($this); // pass db object to support multiple db connections and returned db objects
1131 return TRUE;
1132 }
1133
1134 // --------------------------------------------------------------------
1135
1136 /**
1137 * Close DB Connection
1138 *
1139 * @access public
Barry Mienydd671972010-10-04 16:33:58 +02001140 * @return void
1141 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001142 function close()
1143 {
1144 if (is_resource($this->conn_id) OR is_object($this->conn_id))
1145 {
1146 $this->_close($this->conn_id);
1147 }
1148 $this->conn_id = FALSE;
1149 }
Barry Mienydd671972010-10-04 16:33:58 +02001150
Derek Allard2067d1a2008-11-13 22:59:24 +00001151 // --------------------------------------------------------------------
1152
1153 /**
1154 * Display an error message
1155 *
1156 * @access public
1157 * @param string the error message
1158 * @param string any "swap" values
1159 * @param boolean whether to localize the message
Barry Mienydd671972010-10-04 16:33:58 +02001160 * @return string sends the application/error_db.php template
1161 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001162 function display_error($error = '', $swap = '', $native = FALSE)
1163 {
Derek Jonese7792202010-03-02 17:24:46 -06001164 $LANG =& load_class('Lang', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 $LANG->load('db');
1166
1167 $heading = $LANG->line('db_error_heading');
1168
1169 if ($native == TRUE)
1170 {
1171 $message = $error;
1172 }
1173 else
1174 {
1175 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1176 }
Barry Mienydd671972010-10-04 16:33:58 +02001177
Pascal Kriete60f8c392010-08-25 18:03:28 +02001178 // Find the most likely culprit of the error by going through
1179 // the backtrace until the source file is no longer in the
1180 // database folder.
Barry Mienydd671972010-10-04 16:33:58 +02001181
Pascal Kriete60f8c392010-08-25 18:03:28 +02001182 $trace = debug_backtrace();
1183
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001184 foreach ($trace as $call)
Pascal Kriete60f8c392010-08-25 18:03:28 +02001185 {
1186 if (isset($call['file']) && strpos($call['file'], BASEPATH.'database') === FALSE)
1187 {
1188 // Found it - use a relative path for safety
1189 $message[] = 'Filename: '.str_replace(array(BASEPATH, APPPATH), '', $call['file']);
1190 $message[] = 'Line Number: '.$call['line'];
Barry Mienydd671972010-10-04 16:33:58 +02001191
Pascal Kriete60f8c392010-08-25 18:03:28 +02001192 break;
1193 }
1194 }
Barry Mienydd671972010-10-04 16:33:58 +02001195
Derek Jonese7792202010-03-02 17:24:46 -06001196 $error =& load_class('Exceptions', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +00001197 echo $error->show_error($heading, $message, 'error_db');
1198 exit;
1199 }
1200
1201 // --------------------------------------------------------------------
1202
1203 /**
1204 * Protect Identifiers
1205 *
1206 * This function adds backticks if appropriate based on db type
1207 *
1208 * @access private
1209 * @param mixed the item to escape
1210 * @return mixed the item with backticks
1211 */
1212 function protect_identifiers($item, $prefix_single = FALSE)
1213 {
1214 return $this->_protect_identifiers($item, $prefix_single);
1215 }
1216
1217 // --------------------------------------------------------------------
1218
1219 /**
1220 * Protect Identifiers
1221 *
1222 * This function is used extensively by the Active Record class, and by
Barry Mienydd671972010-10-04 16:33:58 +02001223 * a couple functions in this class.
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 * It takes a column or table name (optionally with an alias) and inserts
Derek Jones37f4b9c2011-07-01 17:56:50 -05001225 * the table prefix onto it. Some logic is necessary in order to deal with
1226 * column names that include the path. Consider a query like this:
Derek Allard2067d1a2008-11-13 22:59:24 +00001227 *
1228 * SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table
1229 *
1230 * Or a query with aliasing:
1231 *
1232 * SELECT m.member_id, m.member_name FROM members AS m
1233 *
1234 * Since the column name can include up to four segments (host, DB, table, column)
1235 * or also have an alias prefix, we need to do a bit of work to figure this out and
1236 * insert the table prefix (if it exists) in the proper position, and escape only
1237 * the correct identifiers.
1238 *
1239 * @access private
1240 * @param string
1241 * @param bool
1242 * @param mixed
1243 * @param bool
1244 * @return string
Barry Mienydd671972010-10-04 16:33:58 +02001245 */
Derek Allard2067d1a2008-11-13 22:59:24 +00001246 function _protect_identifiers($item, $prefix_single = FALSE, $protect_identifiers = NULL, $field_exists = TRUE)
1247 {
1248 if ( ! is_bool($protect_identifiers))
1249 {
1250 $protect_identifiers = $this->_protect_identifiers;
1251 }
Derek Allarde37ab382009-02-03 16:13:57 +00001252
1253 if (is_array($item))
1254 {
1255 $escaped_array = array();
1256
Pascal Krietec3a4a8d2011-02-14 13:40:08 -05001257 foreach ($item as $k => $v)
Derek Allarde37ab382009-02-03 16:13:57 +00001258 {
1259 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v);
1260 }
1261
1262 return $escaped_array;
1263 }
1264
Derek Allard2067d1a2008-11-13 22:59:24 +00001265 // Convert tabs or multiple spaces into single spaces
Derek Jones7b3b96c2009-02-10 21:01:47 +00001266 $item = preg_replace('/[\t ]+/', ' ', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001267
Derek Allard2067d1a2008-11-13 22:59:24 +00001268 // If the item has an alias declaration we remove it and set it aside.
1269 // Basically we remove everything to the right of the first space
1270 $alias = '';
1271 if (strpos($item, ' ') !== FALSE)
Derek Allard911d3e02008-12-15 14:08:35 +00001272 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001273 $alias = strstr($item, " ");
1274 $item = substr($item, 0, - strlen($alias));
1275 }
1276
Derek Allard911d3e02008-12-15 14:08:35 +00001277 // This is basically a bug fix for queries that use MAX, MIN, etc.
Barry Mienydd671972010-10-04 16:33:58 +02001278 // If a parenthesis is found we know that we do not need to
Derek Jones37f4b9c2011-07-01 17:56:50 -05001279 // escape the data or add a prefix. There's probably a more graceful
Derek Allard911d3e02008-12-15 14:08:35 +00001280 // way to deal with this, but I'm not thinking of it -- Rick
1281 if (strpos($item, '(') !== FALSE)
1282 {
1283 return $item.$alias;
1284 }
1285
Derek Allard2067d1a2008-11-13 22:59:24 +00001286 // Break the string apart if it contains periods, then insert the table prefix
1287 // in the correct location, assuming the period doesn't indicate that we're dealing
1288 // with an alias. While we're at it, we will escape the components
1289 if (strpos($item, '.') !== FALSE)
1290 {
1291 $parts = explode('.', $item);
Barry Mienydd671972010-10-04 16:33:58 +02001292
Derek Allard2067d1a2008-11-13 22:59:24 +00001293 // Does the first segment of the exploded item match
Derek Jones37f4b9c2011-07-01 17:56:50 -05001294 // one of the aliases previously identified? If so,
Derek Allard2067d1a2008-11-13 22:59:24 +00001295 // we have nothing more to do other than escape the item
1296 if (in_array($parts[0], $this->ar_aliased_tables))
Derek Allard911d3e02008-12-15 14:08:35 +00001297 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001298 if ($protect_identifiers === TRUE)
1299 {
1300 foreach ($parts as $key => $val)
1301 {
1302 if ( ! in_array($val, $this->_reserved_identifiers))
1303 {
1304 $parts[$key] = $this->_escape_identifiers($val);
1305 }
1306 }
Barry Mienydd671972010-10-04 16:33:58 +02001307
Derek Allard2067d1a2008-11-13 22:59:24 +00001308 $item = implode('.', $parts);
Barry Mienydd671972010-10-04 16:33:58 +02001309 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001310 return $item.$alias;
1311 }
Barry Mienydd671972010-10-04 16:33:58 +02001312
Derek Jones37f4b9c2011-07-01 17:56:50 -05001313 // Is there a table prefix defined in the config file? If not, no need to do anything
Derek Allard2067d1a2008-11-13 22:59:24 +00001314 if ($this->dbprefix != '')
1315 {
1316 // We now add the table prefix based on some logic.
1317 // Do we have 4 segments (hostname.database.table.column)?
1318 // If so, we add the table prefix to the column name in the 3rd segment.
1319 if (isset($parts[3]))
1320 {
1321 $i = 2;
1322 }
1323 // Do we have 3 segments (database.table.column)?
1324 // If so, we add the table prefix to the column name in 2nd position
1325 elseif (isset($parts[2]))
1326 {
1327 $i = 1;
1328 }
1329 // Do we have 2 segments (table.column)?
1330 // If so, we add the table prefix to the column name in 1st segment
1331 else
1332 {
1333 $i = 0;
1334 }
Barry Mienydd671972010-10-04 16:33:58 +02001335
Derek Allard2067d1a2008-11-13 22:59:24 +00001336 // This flag is set when the supplied $item does not contain a field name.
1337 // This can happen when this function is being called from a JOIN.
1338 if ($field_exists == FALSE)
1339 {
1340 $i++;
1341 }
Barry Mienydd671972010-10-04 16:33:58 +02001342
Derek Jones55acc8b2009-07-11 03:38:13 +00001343 // Verify table prefix and replace if necessary
1344 if ($this->swap_pre != '' && strncmp($parts[$i], $this->swap_pre, strlen($this->swap_pre)) === 0)
1345 {
1346 $parts[$i] = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $parts[$i]);
1347 }
Barry Mienydd671972010-10-04 16:33:58 +02001348
Derek Allard2067d1a2008-11-13 22:59:24 +00001349 // We only add the table prefix if it does not already exist
1350 if (substr($parts[$i], 0, strlen($this->dbprefix)) != $this->dbprefix)
1351 {
1352 $parts[$i] = $this->dbprefix.$parts[$i];
1353 }
Barry Mienydd671972010-10-04 16:33:58 +02001354
Derek Allard2067d1a2008-11-13 22:59:24 +00001355 // Put the parts back together
1356 $item = implode('.', $parts);
1357 }
Barry Mienydd671972010-10-04 16:33:58 +02001358
Derek Allard2067d1a2008-11-13 22:59:24 +00001359 if ($protect_identifiers === TRUE)
1360 {
1361 $item = $this->_escape_identifiers($item);
1362 }
Barry Mienydd671972010-10-04 16:33:58 +02001363
Derek Allard2067d1a2008-11-13 22:59:24 +00001364 return $item.$alias;
1365 }
1366
Derek Jones37f4b9c2011-07-01 17:56:50 -05001367 // Is there a table prefix? If not, no need to insert it
Derek Allard2067d1a2008-11-13 22:59:24 +00001368 if ($this->dbprefix != '')
1369 {
Derek Jones55acc8b2009-07-11 03:38:13 +00001370 // Verify table prefix and replace if necessary
1371 if ($this->swap_pre != '' && strncmp($item, $this->swap_pre, strlen($this->swap_pre)) === 0)
1372 {
1373 $item = preg_replace("/^".$this->swap_pre."(\S+?)/", $this->dbprefix."\\1", $item);
1374 }
1375
Derek Allard2067d1a2008-11-13 22:59:24 +00001376 // Do we prefix an item with no segments?
1377 if ($prefix_single == TRUE AND substr($item, 0, strlen($this->dbprefix)) != $this->dbprefix)
1378 {
1379 $item = $this->dbprefix.$item;
Barry Mienydd671972010-10-04 16:33:58 +02001380 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001381 }
1382
1383 if ($protect_identifiers === TRUE AND ! in_array($item, $this->_reserved_identifiers))
1384 {
1385 $item = $this->_escape_identifiers($item);
1386 }
Barry Mienydd671972010-10-04 16:33:58 +02001387
Derek Allard2067d1a2008-11-13 22:59:24 +00001388 return $item.$alias;
1389 }
Túbal Martín511f2252011-11-24 14:43:45 +01001390
1391 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +00001392
Túbal Martín511f2252011-11-24 14:43:45 +01001393 /**
1394 * Dummy method that allows Active Record class to be disabled
1395 *
1396 * This function is used extensively by every db driver.
1397 *
1398 * @access private
1399 * @return void
1400 */
1401 protected function _reset_select()
1402 {
1403
1404 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001405
1406}
1407
1408
1409/* End of file DB_driver.php */
Timothy Warren6f531dc2011-10-10 10:10:46 -04001410/* Location: ./system/database/DB_driver.php */