blob: 24707aaca960edfacf3c595f604e592780594b0e [file] [log] [blame]
admin7b613c72006-09-24 18:05:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.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 Rick Ellis
29 * @link http://www.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 $port = '';
40 var $pconnect = FALSE;
41 var $conn_id = FALSE;
42 var $result_id = FALSE;
43 var $db_debug = FALSE;
44 var $benchmark = 0;
45 var $query_count = 0;
46 var $bind_marker = '?';
47 var $queries = array();
admine8f6eb62006-10-02 06:46:16 +000048 var $data_cache = array();
admin7b613c72006-09-24 18:05:17 +000049 var $trans_enabled = TRUE;
50 var $_trans_depth = 0;
51 var $_trans_failure = FALSE; // Used with transactions to determine if a rollback should occur
admin6871d952006-10-04 00:36:18 +000052 var $cache_on = FALSE;
53 var $cachedir = '';
admin0c405652006-10-04 18:37:57 +000054 var $cache; // The cache class object
admin7b613c72006-09-24 18:05:17 +000055
admin6871d952006-10-04 00:36:18 +000056
57 // These are use with Oracle
58 var $stmt_id;
59 var $curs_id;
60 var $limit_used;
admin7b613c72006-09-24 18:05:17 +000061
admine8f6eb62006-10-02 06:46:16 +000062
admin7b613c72006-09-24 18:05:17 +000063
64 /**
65 * Constructor. Accepts one parameter containing the database
66 * connection settings.
67 *
68 * Database settings can be passed as discreet
69 * parameters or as a data source name in the first
70 * parameter. DSNs must have this prototype:
71 * $dsn = 'driver://username:password@hostname/database';
72 *
73 * @param mixed. Can be an array or a DSN string
74 */
75 function CI_DB_driver($params)
76 {
77 $this->initialize($params);
78 log_message('debug', 'Database Driver Class Initialized');
79 }
80
81 // --------------------------------------------------------------------
82
83 /**
84 * Initialize Database Settings
85 *
86 * @access private Called by the constructor
87 * @param mixed
88 * @return void
89 */
90 function initialize($params = '')
admin6871d952006-10-04 00:36:18 +000091 {
admin7b613c72006-09-24 18:05:17 +000092 if (is_array($params))
93 {
admin6871d952006-10-04 00:36:18 +000094 $defaults = array(
95 'hostname' => '',
96 'username' => '',
97 'password' => '',
98 'database' => '',
99 'dbdriver' => 'mysql',
100 'dbprefix' => '',
101 'port' => '',
102 'pconnect' => FALSE,
103 'db_debug' => FALSE,
104 'cachedir' => '',
105 'cache_on' => FALSE
106 );
107
108 foreach ($defaults as $key => $val)
admin7b613c72006-09-24 18:05:17 +0000109 {
110 $this->$key = ( ! isset($params[$key])) ? $val : $params[$key];
111 }
112 }
113 elseif (strpos($params, '://'))
114 {
115 if (FALSE === ($dsn = @parse_url($params)))
116 {
117 log_message('error', 'Invalid DB Connection String');
118
119 if ($this->db_debug)
120 {
121 return $this->display_error('db_invalid_connection_str');
122 }
123 return FALSE;
124 }
125
126 $this->hostname = ( ! isset($dsn['host'])) ? '' : rawurldecode($dsn['host']);
127 $this->username = ( ! isset($dsn['user'])) ? '' : rawurldecode($dsn['user']);
128 $this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']);
129 $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1));
130 }
admin6871d952006-10-04 00:36:18 +0000131
132 // Connect to the database
133 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
134
135 if ( ! $this->conn_id)
136 {
admin7b613c72006-09-24 18:05:17 +0000137 log_message('error', 'Unable to connect to the database');
138
admin6871d952006-10-04 00:36:18 +0000139 if ($this->db_debug)
140 {
admin7b613c72006-09-24 18:05:17 +0000141 $this->display_error('db_unable_to_connect');
admin6871d952006-10-04 00:36:18 +0000142 }
143 }
admin7b613c72006-09-24 18:05:17 +0000144 else
145 {
146 if ( ! $this->db_select())
147 {
148 log_message('error', 'Unable to select database: '.$this->database);
149
150 if ($this->db_debug)
151 {
152 $this->display_error('db_unable_to_select', $this->database);
153 }
154 }
admin6871d952006-10-04 00:36:18 +0000155 }
admin7b613c72006-09-24 18:05:17 +0000156 }
157
158
159 // --------------------------------------------------------------------
160
161 /**
admin9cd4e8e2006-09-25 23:26:25 +0000162 * The name of the platform in use (mysql, mssql, etc...)
163 *
164 * @access public
165 * @return string
166 */
167 function platform()
168 {
169 return $this->dbdriver;
170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Database Version Number. Returns a string containing the
176 * version of the database being used
177 *
178 * @access public
179 * @return string
180 */
181 function version()
182 {
183 if (FALSE === ($sql = $this->_version()))
184 {
admin6871d952006-10-04 00:36:18 +0000185 if ($this->db_debug)
186 {
admin9cd4e8e2006-09-25 23:26:25 +0000187 return $this->display_error('db_unsupported_function');
admin6871d952006-10-04 00:36:18 +0000188 }
189 return FALSE;
admin9cd4e8e2006-09-25 23:26:25 +0000190 }
191
admin6871d952006-10-04 00:36:18 +0000192 if ($this->dbdriver == 'oci8')
193 {
admin9cd4e8e2006-09-25 23:26:25 +0000194 return $sql;
195 }
196
197 $query = $this->query($sql);
198 $row = $query->row();
199 return $row->ver;
200 }
201
202 // --------------------------------------------------------------------
203
204 /**
admin7b613c72006-09-24 18:05:17 +0000205 * Execute the query
206 *
207 * Accepts an SQL string as input and returns a result object upon
208 * successful execution of a "read" type query. Returns boolean TRUE
209 * upon successful execution of a "write" type query. Returns boolean
210 * FALSE upon failure, and if the $db_debug variable is set to TRUE
211 * will raise an error.
212 *
213 * @access public
214 * @param string An SQL query string
215 * @param array An array of binding data
216 * @return mixed
217 */
admin6871d952006-10-04 00:36:18 +0000218 function query($sql, $binds = FALSE, $return_object = TRUE)
admindb8a3742006-10-04 07:43:35 +0000219 {
admin7b613c72006-09-24 18:05:17 +0000220 if ($sql == '')
221 {
admin6871d952006-10-04 00:36:18 +0000222 if ($this->db_debug)
223 {
admin7b613c72006-09-24 18:05:17 +0000224 log_message('error', 'Invalid query: '.$sql);
225 return $this->display_error('db_invalid_query');
admin6871d952006-10-04 00:36:18 +0000226 }
227 return FALSE;
admin7b613c72006-09-24 18:05:17 +0000228 }
229
admin0c405652006-10-04 18:37:57 +0000230 // Is query caching enabled? If the query is a "read type"
231 // we will load the caching class and return the previously
232 // cached query if it exists
admin6871d952006-10-04 00:36:18 +0000233 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
admin0c405652006-10-04 18:37:57 +0000234 {
235 if ($this->_cache_init())
admine8f6eb62006-10-02 06:46:16 +0000236 {
admin0c405652006-10-04 18:37:57 +0000237 if (FALSE !== ($cache = $this->cache->read($sql)))
238 {
239 return $cache;
240 }
admine8f6eb62006-10-02 06:46:16 +0000241 }
242 }
243
admin7b613c72006-09-24 18:05:17 +0000244 // Compile binds if needed
245 if ($binds !== FALSE)
246 {
247 $sql = $this->compile_binds($sql, $binds);
248 }
249
admin6871d952006-10-04 00:36:18 +0000250 // Save the query for debugging
251 $this->queries[] = $sql;
admin7b613c72006-09-24 18:05:17 +0000252
253 // Start the Query Timer
admin6871d952006-10-04 00:36:18 +0000254 $time_start = list($sm, $ss) = explode(' ', microtime());
255
admin7b613c72006-09-24 18:05:17 +0000256 // Run the Query
admin6871d952006-10-04 00:36:18 +0000257 if (FALSE === ($this->result_id = $this->simple_query($sql)))
258 {
259 // This will trigger a rollback if transactions are being used
260 $this->_trans_failure = TRUE;
261
262 if ($this->db_debug)
263 {
adminbb1d4392006-09-24 20:14:38 +0000264 log_message('error', 'Query error: '.$this->_error_message());
admin7b613c72006-09-24 18:05:17 +0000265 return $this->display_error(
266 array(
adminbb1d4392006-09-24 20:14:38 +0000267 'Error Number: '.$this->_error_number(),
268 $this->_error_message(),
admin7b613c72006-09-24 18:05:17 +0000269 $sql
270 )
271 );
admin6871d952006-10-04 00:36:18 +0000272 }
273
274 return FALSE;
275 }
276
admin7b613c72006-09-24 18:05:17 +0000277 // Stop and aggregate the query time results
278 $time_end = list($em, $es) = explode(' ', microtime());
279 $this->benchmark += ($em + $es) - ($sm + $ss);
280
281 // Increment the query counter
admin6871d952006-10-04 00:36:18 +0000282 $this->query_count++;
283
admin7b613c72006-09-24 18:05:17 +0000284 // Was the query a "write" type?
285 // If so we'll simply return true
286 if ($this->is_write_type($sql) === TRUE)
admindb8a3742006-10-04 07:43:35 +0000287 {
288 // If caching is enabled we'll auto-cleanup any
289 // existing files related to this particular URI
290
admin0c405652006-10-04 18:37:57 +0000291 if ($this->cache_on == TRUE AND $this->_cache_init())
admindb8a3742006-10-04 07:43:35 +0000292 {
admin0c405652006-10-04 18:37:57 +0000293 $this->cache->delete();
admindb8a3742006-10-04 07:43:35 +0000294 }
295
admin7b613c72006-09-24 18:05:17 +0000296 return TRUE;
297 }
298
299 // Return TRUE if we don't need to create a result object
300 // Currently only the Oracle driver uses this when stored
301 // procedures are used
302 if ($return_object !== TRUE)
303 {
304 return TRUE;
305 }
admindb8a3742006-10-04 07:43:35 +0000306
307 // Load and instantiate the result driver
admin7b613c72006-09-24 18:05:17 +0000308
admindb8a3742006-10-04 07:43:35 +0000309 $driver = $this->load_rdriver();
310
311 $RES = new $driver();
admin6871d952006-10-04 00:36:18 +0000312 $RES->conn_id = $this->conn_id;
313 $RES->db_debug = $this->db_debug;
314 $RES->result_id = $this->result_id;
315
316 if ($this->dbdriver == 'oci8')
317 {
admin7b613c72006-09-24 18:05:17 +0000318 $RES->stmt_id = $this->stmt_id;
319 $RES->curs_id = NULL;
320 $RES->limit_used = $this->limit_used;
admin6871d952006-10-04 00:36:18 +0000321 }
admindb8a3742006-10-04 07:43:35 +0000322
323 // Is query caching enabled? If so, we'll serialize the
324 // result object and save it to a cache file
admin0c405652006-10-04 18:37:57 +0000325 if ($this->cache_on == TRUE AND $this->_cache_init())
admindb8a3742006-10-04 07:43:35 +0000326 {
admin0c405652006-10-04 18:37:57 +0000327 $this->cache->write($sql, $RES);
admindb8a3742006-10-04 07:43:35 +0000328 }
329
admin7b613c72006-09-24 18:05:17 +0000330 return $RES;
331 }
admindb8a3742006-10-04 07:43:35 +0000332
333 // --------------------------------------------------------------------
334
335 /**
336 * Load the result drivers
337 *
338 * @access public
339 * @return string the name of the result class
340 */
341 function load_rdriver()
342 {
343 $driver = 'CI_DB_'.$this->dbdriver.'_result';
344
345 if ( ! class_exists($driver))
346 {
347 include_once(BASEPATH.'database/DB_result'.EXT);
348 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT);
349 }
350
351 return $driver;
352 }
admin7b613c72006-09-24 18:05:17 +0000353
354 // --------------------------------------------------------------------
355
356 /**
357 * Simple Query
358 * This is a simiplified version of the query() function. Internally
359 * we only use it when running transaction commands since they do
360 * not require all the features of the main query() function.
361 *
362 * @access public
363 * @param string the sql query
364 * @return mixed
365 */
366 function simple_query($sql)
367 {
368 if ( ! $this->conn_id)
369 {
370 $this->initialize();
371 }
372
373 return $this->_execute($sql, $this->conn_id);
374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Disable Transactions
380 * This permits transactions to be disabled at run-time.
381 *
382 * @access public
383 * @return void
384 */
385 function trans_off()
386 {
387 $this->trans_enabled = FALSE;
388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Start Transaction
394 *
395 * @access public
396 * @return void
397 */
398 function trans_start($test_mode = FALSE)
399 {
400 if ( ! $this->trans_enabled)
401 {
402 return FALSE;
403 }
404
405 // When transactions are nested we only begin/commit/rollback the outermost ones
406 if ($this->_trans_depth > 0)
407 {
408 $this->_trans_depth += 1;
409 return;
410 }
411
412 $this->trans_begin($test_mode);
413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Complete Transaction
419 *
420 * @access public
421 * @return bool
422 */
423 function trans_complete()
424 {
425 if ( ! $this->trans_enabled)
426 {
427 return FALSE;
428 }
429
430 // When transactions are nested we only begin/commit/rollback the outermost ones
431 if ($this->_trans_depth > 1)
432 {
433 $this->_trans_depth -= 1;
434 return TRUE;
435 }
436
437 // The query() function will set this flag to TRUE in the event that a query failed
438 if ($this->_trans_failure === TRUE)
439 {
440 $this->trans_rollback();
441
442 if ($this->db_debug)
443 {
444 return $this->display_error('db_transaction_failure');
445 }
446 return FALSE;
447 }
448
449 $this->trans_commit();
450 return TRUE;
451 }
452
453 // --------------------------------------------------------------------
454
455 /**
456 * Lets you retrieve the transaction flag to determine if it has failed
457 *
458 * @access public
459 * @return bool
460 */
461 function trans_status()
462 {
463 return $this->_trans_failure;
464 }
465
466 // --------------------------------------------------------------------
467
468 /**
469 * Compile Bindings
470 *
471 * @access public
472 * @param string the sql statement
473 * @param array an array of bind data
474 * @return string
475 */
476 function compile_binds($sql, $binds)
477 {
478 if (FALSE === strpos($sql, $this->bind_marker))
479 {
480 return $sql;
481 }
482
483 if ( ! is_array($binds))
484 {
485 $binds = array($binds);
486 }
487
488 foreach ($binds as $val)
489 {
490 $val = $this->escape($val);
491
492 // Just in case the replacement string contains the bind
493 // character we'll temporarily replace it with a marker
494 $val = str_replace($this->bind_marker, '{%bind_marker%}', $val);
495 $sql = preg_replace("#".preg_quote($this->bind_marker, '#')."#", str_replace('$', '\$', $val), $sql, 1);
496 }
497
498 return str_replace('{%bind_marker%}', $this->bind_marker, $sql);
499 }
500
501 // --------------------------------------------------------------------
502
503 /**
504 * Determines if a query is a "write" type.
505 *
506 * @access public
507 * @param string An SQL query string
508 * @return boolean
509 */
510 function is_write_type($sql)
511 {
512 if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
513 {
514 return FALSE;
515 }
516 return TRUE;
517 }
518
519 // --------------------------------------------------------------------
520
521 /**
522 * Calculate the aggregate query elapsed time
523 *
524 * @access public
525 * @param intiger The number of decimal places
526 * @return integer
527 */
528 function elapsed_time($decimals = 6)
529 {
530 return number_format($this->benchmark, $decimals);
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Returns the total number of queries
537 *
538 * @access public
539 * @return integer
540 */
541 function total_queries()
542 {
543 return $this->query_count;
544 }
545
546 // --------------------------------------------------------------------
547
548 /**
549 * Returns the last query that was executed
550 *
551 * @access public
552 * @return void
553 */
554 function last_query()
555 {
556 return end($this->queries);
557 }
558
559 // --------------------------------------------------------------------
560
561 /**
562 * "Smart" Escape String
563 *
564 * Escapes data based on type
565 * Sets boolean and null types
566 *
567 * @access public
568 * @param string
569 * @return integer
570 */
571 function escape($str)
572 {
573 switch (gettype($str))
574 {
575 case 'string' : $str = "'".$this->escape_str($str)."'";
576 break;
577 case 'boolean' : $str = ($str === FALSE) ? 0 : 1;
578 break;
579 default : $str = ($str === NULL) ? 'NULL' : $str;
580 break;
581 }
582
583 return $str;
adminbb1d4392006-09-24 20:14:38 +0000584 }
585
admin9cd4e8e2006-09-25 23:26:25 +0000586
587
588 // --------------------------------------------------------------------
589
590 /**
591 * Primary
592 *
593 * Retrieves the primary key. It assumes that the row in the first
594 * position is the primary key
595 *
596 * @access public
597 * @param string the table name
598 * @return string
599 */
600 function primary($table = '')
601 {
602 $fields = $this->field_names($table);
603
604 if ( ! is_array($fields))
605 {
606 return FALSE;
607 }
608
609 return current($fields);
610 }
611
612 // --------------------------------------------------------------------
613
admin3dd978f2006-09-30 19:24:45 +0000614 /**
615 * Returns an array of table names
616 *
617 * @access public
618 * @return array
619 */
620 function list_tables()
621 {
622 // Is there a cached result?
admine8f6eb62006-10-02 06:46:16 +0000623 if (isset($this->data_cache['table_names']))
admin3dd978f2006-09-30 19:24:45 +0000624 {
admine8f6eb62006-10-02 06:46:16 +0000625 return $this->data_cache['table_names'];
admin3dd978f2006-09-30 19:24:45 +0000626 }
627
628 if (FALSE === ($sql = $this->_list_tables()))
629 {
admin6871d952006-10-04 00:36:18 +0000630 if ($this->db_debug)
631 {
admin3dd978f2006-09-30 19:24:45 +0000632 return $this->display_error('db_unsupported_function');
admin6871d952006-10-04 00:36:18 +0000633 }
634 return FALSE;
admin3dd978f2006-09-30 19:24:45 +0000635 }
636
637 $retval = array();
638 $query = $this->query($sql);
639
640 if ($query->num_rows() > 0)
641 {
642 foreach($query->result_array() as $row)
643 {
644 if (isset($row['TABLE_NAME']))
645 {
646 $retval[] = $row['TABLE_NAME'];
647 }
648 else
649 {
650 $retval[] = array_shift($row);
651 }
652 }
653 }
654
admine8f6eb62006-10-02 06:46:16 +0000655 return $this->data_cache['table_names'] =& $retval;
admin3dd978f2006-09-30 19:24:45 +0000656 }
657
658 // --------------------------------------------------------------------
659
660 /**
661 * Determine if a particular table exists
662 * @access public
663 * @return boolean
664 */
665 function table_exists($table_name)
666 {
667 return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE;
668 }
669
670 // --------------------------------------------------------------------
671
admin9cd4e8e2006-09-25 23:26:25 +0000672 /**
673 * Fetch MySQL Field Names
674 *
675 * @access public
676 * @param string the table name
677 * @return array
678 */
admin6871d952006-10-04 00:36:18 +0000679 function list_fields($table = '')
680 {
admin3ed8c512006-09-29 23:26:28 +0000681 // Is there a cached result?
admine8f6eb62006-10-02 06:46:16 +0000682 if (isset($this->data_cache['field_names'][$table]))
admin3ed8c512006-09-29 23:26:28 +0000683 {
admine8f6eb62006-10-02 06:46:16 +0000684 return $this->data_cache['field_names'][$table];
admin3ed8c512006-09-29 23:26:28 +0000685 }
admin6871d952006-10-04 00:36:18 +0000686
687 if ($table == '')
688 {
admin9cd4e8e2006-09-25 23:26:25 +0000689 if ($this->db_debug)
690 {
691 return $this->display_error('db_field_param_missing');
692 }
693 return FALSE;
admin6871d952006-10-04 00:36:18 +0000694 }
695
admin9cd4e8e2006-09-25 23:26:25 +0000696 if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table)))
697 {
admin6871d952006-10-04 00:36:18 +0000698 if ($this->db_debug)
699 {
admin9cd4e8e2006-09-25 23:26:25 +0000700 return $this->display_error('db_unsupported_function');
admin6871d952006-10-04 00:36:18 +0000701 }
702 return FALSE;
admin9cd4e8e2006-09-25 23:26:25 +0000703 }
admin6871d952006-10-04 00:36:18 +0000704
705 $query = $this->query($sql);
706
707 $retval = array();
admin9cd4e8e2006-09-25 23:26:25 +0000708 foreach($query->result_array() as $row)
709 {
710 if (isset($row['COLUMN_NAME']))
711 {
712 $retval[] = $row['COLUMN_NAME'];
713 }
714 else
715 {
716 $retval[] = current($row);
admin6871d952006-10-04 00:36:18 +0000717 }
admin9cd4e8e2006-09-25 23:26:25 +0000718 }
admin6871d952006-10-04 00:36:18 +0000719
admine8f6eb62006-10-02 06:46:16 +0000720 return $this->data_cache['field_names'][$table] =& $retval;
admin6871d952006-10-04 00:36:18 +0000721 }
adminb2a9cec2006-10-01 03:38:04 +0000722
723 // --------------------------------------------------------------------
724
725 /**
726 * Determine if a particular field exists
727 * @access public
728 * @param string
729 * @param string
730 * @return boolean
731 */
732 function field_exists($field_name, $table_name)
733 {
734 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
735 }
admin6871d952006-10-04 00:36:18 +0000736
admin3dd978f2006-09-30 19:24:45 +0000737 // --------------------------------------------------------------------
738
739 /**
740 * DEPRECATED - use list_fields()
741 */
admin6871d952006-10-04 00:36:18 +0000742 function field_names($table = '')
743 {
744 return $this->list_fields($table);
745 }
admin9cd4e8e2006-09-25 23:26:25 +0000746
747 // --------------------------------------------------------------------
748
749 /**
750 * Returns an object with field data
751 *
752 * @access public
753 * @param string the table name
754 * @return object
755 */
756 function field_data($table = '')
757 {
admin6871d952006-10-04 00:36:18 +0000758 if ($table == '')
759 {
admin9cd4e8e2006-09-25 23:26:25 +0000760 if ($this->db_debug)
761 {
762 return $this->display_error('db_field_param_missing');
763 }
764 return FALSE;
admin6871d952006-10-04 00:36:18 +0000765 }
766
admin9cd4e8e2006-09-25 23:26:25 +0000767 $query = $this->query($this->_field_data($this->dbprefix.$table));
768 return $query->field_data();
769 }
770
adminbb1d4392006-09-24 20:14:38 +0000771 // --------------------------------------------------------------------
772
773 /**
774 * Generate an insert string
775 *
776 * @access public
777 * @param string the table upon which the query will be performed
778 * @param array an associative array data of key/values
779 * @return string
780 */
781 function insert_string($table, $data)
782 {
admin6871d952006-10-04 00:36:18 +0000783 $fields = array();
adminbb1d4392006-09-24 20:14:38 +0000784 $values = array();
785
786 foreach($data as $key => $val)
787 {
788 $fields[] = $key;
789 $values[] = $this->escape($val);
790 }
791
792 return $this->_insert($this->dbprefix.$table, $fields, $values);
793 }
794
795 // --------------------------------------------------------------------
796
797 /**
798 * Generate an update string
799 *
800 * @access public
801 * @param string the table upon which the query will be performed
802 * @param array an associative array data of key/values
803 * @param mixed the "where" statement
804 * @return string
805 */
806 function update_string($table, $data, $where)
807 {
808 if ($where == '')
809 return false;
810
811 $fields = array();
812 foreach($data as $key => $val)
813 {
814 $fields[$key] = $this->escape($val);
815 }
816
817 if ( ! is_array($where))
818 {
819 $dest = array($where);
820 }
821 else
822 {
823 $dest = array();
824 foreach ($where as $key => $val)
825 {
826 $prefix = (count($dest) == 0) ? '' : ' AND ';
827
828 if ($val != '')
829 {
830 if ( ! $this->_has_operator($key))
831 {
832 $key .= ' =';
833 }
834
835 $val = ' '.$this->escape($val);
836 }
837
838 $dest[] = $prefix.$key.$val;
839 }
840 }
841
842 return $this->_update($this->dbprefix.$table, $fields, $dest);
admin6871d952006-10-04 00:36:18 +0000843 }
admin7b613c72006-09-24 18:05:17 +0000844
845 // --------------------------------------------------------------------
846
847 /**
848 * Enables a native PHP function to be run, using a platform agnostic wrapper.
849 *
850 * @access public
851 * @param string the function name
852 * @param mixed any parameters needed by the function
853 * @return mixed
854 */
855 function call_function($function)
856 {
857 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
858
859 if (FALSE === strpos($driver, $function))
860 {
861 $function = $driver.$function;
862 }
863
864 if ( ! function_exists($function))
865 {
866 if ($this->db_debug)
867 {
868 return $this->display_error('db_unsupported_function');
869 }
870 return FALSE;
871 }
872 else
873 {
adminee54c112006-09-28 17:13:38 +0000874 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
875
admin7b613c72006-09-24 18:05:17 +0000876 return call_user_func_array($function, $args);
877 }
878 }
admine8f6eb62006-10-02 06:46:16 +0000879
880 // --------------------------------------------------------------------
881
882 /**
883 * Enable Query Caching
884 *
885 * @access public
886 * @return void
887 */
888 function cache_on()
889 {
admindb8a3742006-10-04 07:43:35 +0000890 return $this->query_caching = TRUE;
admine8f6eb62006-10-02 06:46:16 +0000891 }
892
893 // --------------------------------------------------------------------
894
895 /**
896 * Disable Query Caching
897 *
898 * @access public
899 * @return void
900 */
901 function cache_off()
902 {
admindb8a3742006-10-04 07:43:35 +0000903 return $this->query_caching = FALSE;
admine8f6eb62006-10-02 06:46:16 +0000904 }
admin0c405652006-10-04 18:37:57 +0000905
admin6871d952006-10-04 00:36:18 +0000906 // --------------------------------------------------------------------
907
908 /**
admin0c405652006-10-04 18:37:57 +0000909 * Initialize the Cache Class
admin6871d952006-10-04 00:36:18 +0000910 *
admin0c405652006-10-04 18:37:57 +0000911 * @access private
912 * @return void
913 */
914 function _cache_init()
admin6871d952006-10-04 00:36:18 +0000915 {
admin0c405652006-10-04 18:37:57 +0000916 if (is_object($this->cache))
admindb8a3742006-10-04 07:43:35 +0000917 {
admin0c405652006-10-04 18:37:57 +0000918 return TRUE;
admindb8a3742006-10-04 07:43:35 +0000919 }
920
admin0c405652006-10-04 18:37:57 +0000921 if ( ! class_exists('CI_DB_Cache'))
admin6871d952006-10-04 00:36:18 +0000922 {
admin0c405652006-10-04 18:37:57 +0000923 if ( ! @include_once(BASEPATH.'database/DB_cache'.EXT))
admin6871d952006-10-04 00:36:18 +0000924 {
admin0c405652006-10-04 18:37:57 +0000925 return $this->cache_off();
admin6871d952006-10-04 00:36:18 +0000926 }
admin6871d952006-10-04 00:36:18 +0000927 }
928
admin0c405652006-10-04 18:37:57 +0000929 $this->cache = new CI_DB_Cache;
admin6871d952006-10-04 00:36:18 +0000930 return TRUE;
931 }
932
admindb8a3742006-10-04 07:43:35 +0000933
934 // --------------------------------------------------------------------
935
936 /**
admin7b613c72006-09-24 18:05:17 +0000937 * Close DB Connection
938 *
939 * @access public
940 * @return void
941 */
admin6871d952006-10-04 00:36:18 +0000942 function close()
943 {
944 if (is_resource($this->conn_id))
945 {
946 $this->_close($this->conn_id);
admin7b613c72006-09-24 18:05:17 +0000947 }
948 $this->conn_id = FALSE;
admin6871d952006-10-04 00:36:18 +0000949 }
admin7b613c72006-09-24 18:05:17 +0000950
951 // --------------------------------------------------------------------
952
953 /**
954 * Display an error message
955 *
956 * @access public
957 * @param string the error message
958 * @param string any "swap" values
959 * @param boolean whether to localize the message
960 * @return string sends the application/errror_db.php template
961 */
admin6871d952006-10-04 00:36:18 +0000962 function display_error($error = '', $swap = '', $native = FALSE)
963 {
admin7b613c72006-09-24 18:05:17 +0000964 $LANG = new CI_Language();
965 $LANG->load('db');
966
967 $heading = 'MySQL Error';
968
969 if ($native == TRUE)
970 {
971 $message = $error;
972 }
973 else
974 {
975 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
976 }
977
978 if ( ! class_exists('CI_Exceptions'))
979 {
980 include_once(BASEPATH.'libraries/Exceptions'.EXT);
981 }
982
983 $error = new CI_Exceptions();
984 echo $error->show_error('An Error Was Encountered', $message, 'error_db');
985 exit;
986
admin6871d952006-10-04 00:36:18 +0000987 }
admin7b613c72006-09-24 18:05:17 +0000988
989}
990
991?>