blob: a7f03e3e58a0d790b3f93b8ea435ba39e1dcd947 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
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, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000011 * @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 = '?';
Rick Ellis40990462007-07-17 21:40:44 +000047 var $save_queries = TRUE;
Derek Allardd2df9bc2007-04-15 17:41:17 +000048 var $queries = array();
49 var $data_cache = array();
50 var $trans_enabled = TRUE;
51 var $_trans_depth = 0;
Rick Ellis28239ad2007-06-11 04:26:39 +000052 var $_trans_status = TRUE; // Used with transactions to determine if a rollback should occur
Derek Allardd2df9bc2007-04-15 17:41:17 +000053 var $cache_on = FALSE;
54 var $cachedir = '';
55 var $cache_autodel = FALSE;
56 var $CACHE; // The cache class object
57
58
59 // These are use with Oracle
60 var $stmt_id;
61 var $curs_id;
62 var $limit_used;
63
64
65
66 /**
67 * Constructor. Accepts one parameter containing the database
68 * connection settings.
69 *
70 * Database settings can be passed as discreet
71 * parameters or as a data source name in the first
72 * parameter. DSNs must have this prototype:
73 * $dsn = 'driver://username:password@hostname/database';
74 *
75 * @param mixed. Can be an array or a DSN string
76 */
77 function CI_DB_driver($params)
78 {
79 $this->initialize($params);
80 log_message('debug', 'Database Driver Class Initialized');
81 }
82
83 // --------------------------------------------------------------------
84
85 /**
86 * Initialize Database Settings
87 *
88 * @access private Called by the constructor
89 * @param mixed
90 * @return void
91 */
92 function initialize($params = '')
93 {
94 if (is_array($params))
95 {
96 $defaults = array(
97 'hostname' => '',
98 'username' => '',
99 'password' => '',
100 'database' => '',
101 'conn_id' => FALSE,
102 'dbdriver' => 'mysql',
103 'dbprefix' => '',
104 'port' => '',
105 'pconnect' => FALSE,
106 'db_debug' => FALSE,
107 'cachedir' => '',
108 'cache_on' => FALSE
109 );
110
111 foreach ($defaults as $key => $val)
112 {
113 $this->$key = ( ! isset($params[$key])) ? $val : $params[$key];
114 }
115 }
116 elseif (strpos($params, '://'))
117 {
118 if (FALSE === ($dsn = @parse_url($params)))
119 {
120 log_message('error', 'Invalid DB Connection String');
121
122 if ($this->db_debug)
123 {
124 return $this->display_error('db_invalid_connection_str');
125 }
126 return FALSE;
127 }
128
129 $this->hostname = ( ! isset($dsn['host'])) ? '' : rawurldecode($dsn['host']);
130 $this->username = ( ! isset($dsn['user'])) ? '' : rawurldecode($dsn['user']);
131 $this->password = ( ! isset($dsn['pass'])) ? '' : rawurldecode($dsn['pass']);
132 $this->database = ( ! isset($dsn['path'])) ? '' : rawurldecode(substr($dsn['path'], 1));
133 }
134
135 // If an existing DB connection resource is supplied
136 // there is no need to connect and select the database
137 if (is_resource($this->conn_id))
138 {
139 return TRUE;
140 }
141
142 // Connect to the database
143 $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
144
145 // No connection? Throw an error
146 if ( ! $this->conn_id)
147 {
148 log_message('error', 'Unable to connect to the database');
149
150 if ($this->db_debug)
151 {
152 $this->display_error('db_unable_to_connect');
153 }
154 return FALSE;
155 }
156
157 // Select the database
158 if ($this->database != '')
159 {
160 if ( ! $this->db_select())
161 {
162 log_message('error', 'Unable to select database: '.$this->database);
163
164 if ($this->db_debug)
165 {
166 $this->display_error('db_unable_to_select', $this->database);
167 }
168 return FALSE;
169 }
170 }
171
172 return TRUE;
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
178 * The name of the platform in use (mysql, mssql, etc...)
179 *
180 * @access public
181 * @return string
182 */
183 function platform()
184 {
185 return $this->dbdriver;
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Database Version Number. Returns a string containing the
192 * version of the database being used
193 *
194 * @access public
195 * @return string
196 */
197 function version()
198 {
199 if (FALSE === ($sql = $this->_version()))
200 {
201 if ($this->db_debug)
202 {
203 return $this->display_error('db_unsupported_function');
204 }
205 return FALSE;
206 }
207
208 if ($this->dbdriver == 'oci8')
209 {
210 return $sql;
211 }
212
213 $query = $this->query($sql);
214 $row = $query->row();
215 return $row->ver;
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Execute the query
222 *
223 * Accepts an SQL string as input and returns a result object upon
224 * successful execution of a "read" type query. Returns boolean TRUE
225 * upon successful execution of a "write" type query. Returns boolean
226 * FALSE upon failure, and if the $db_debug variable is set to TRUE
227 * will raise an error.
228 *
229 * @access public
230 * @param string An SQL query string
231 * @param array An array of binding data
232 * @return mixed
233 */
234 function query($sql, $binds = FALSE, $return_object = TRUE)
235 {
236 if ($sql == '')
237 {
238 if ($this->db_debug)
239 {
240 log_message('error', 'Invalid query: '.$sql);
241 return $this->display_error('db_invalid_query');
242 }
243 return FALSE;
244 }
245
246 // Is query caching enabled? If the query is a "read type"
247 // we will load the caching class and return the previously
248 // cached query if it exists
249 if ($this->cache_on == TRUE AND stristr($sql, 'SELECT'))
250 {
251 if ($this->_cache_init())
252 {
253 $this->load_rdriver();
254 if (FALSE !== ($cache = $this->CACHE->read($sql)))
255 {
256 return $cache;
257 }
258 }
259 }
260
261 // Compile binds if needed
262 if ($binds !== FALSE)
263 {
264 $sql = $this->compile_binds($sql, $binds);
265 }
266
267 // Save the query for debugging
Rick Ellis40990462007-07-17 21:40:44 +0000268 if ($this->save_queries == TRUE)
269 {
270 $this->queries[] = $sql;
271 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000272
273 // Start the Query Timer
274 $time_start = list($sm, $ss) = explode(' ', microtime());
275
276 // Run the Query
277 if (FALSE === ($this->result_id = $this->simple_query($sql)))
278 {
279 // This will trigger a rollback if transactions are being used
Rick Ellis28239ad2007-06-11 04:26:39 +0000280 $this->_trans_status = FALSE;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000281
282 if ($this->db_debug)
283 {
284 log_message('error', 'Query error: '.$this->_error_message());
285 return $this->display_error(
286 array(
287 'Error Number: '.$this->_error_number(),
288 $this->_error_message(),
289 $sql
290 )
291 );
292 }
293
294 return FALSE;
295 }
296
297 // Stop and aggregate the query time results
298 $time_end = list($em, $es) = explode(' ', microtime());
299 $this->benchmark += ($em + $es) - ($sm + $ss);
300
301 // Increment the query counter
302 $this->query_count++;
303
304 // Was the query a "write" type?
305 // If so we'll simply return true
306 if ($this->is_write_type($sql) === TRUE)
307 {
308 // If caching is enabled we'll auto-cleanup any
309 // existing files related to this particular URI
310 if ($this->cache_on == TRUE AND $this->cache_autodel == TRUE AND $this->_cache_init())
311 {
312 $this->CACHE->delete();
313 }
314
315 return TRUE;
316 }
317
318 // Return TRUE if we don't need to create a result object
319 // Currently only the Oracle driver uses this when stored
320 // procedures are used
321 if ($return_object !== TRUE)
322 {
323 return TRUE;
324 }
325
326 // Load and instantiate the result driver
327
328 $driver = $this->load_rdriver();
329 $RES = new $driver();
330 $RES->conn_id = $this->conn_id;
331 $RES->result_id = $this->result_id;
Derek Allard060052d2007-07-14 14:26:13 +0000332
Derek Allardd2df9bc2007-04-15 17:41:17 +0000333 if ($this->dbdriver == 'oci8')
334 {
335 $RES->stmt_id = $this->stmt_id;
336 $RES->curs_id = NULL;
337 $RES->limit_used = $this->limit_used;
338 }
Derek Allard6e848ed2008-01-15 14:26:23 +0000339
340 $RES->num_rows = $RES->num_rows();
341
Derek Allardd2df9bc2007-04-15 17:41:17 +0000342 // Is query caching enabled? If so, we'll serialize the
343 // result object and save it to a cache file.
344 if ($this->cache_on == TRUE AND $this->_cache_init())
345 {
346 // We'll create a new instance of the result object
347 // only without the platform specific driver since
348 // we can't use it with cached data (the query result
349 // resource ID won't be any good once we've cached the
350 // result object, so we'll have to compile the data
351 // and save it)
352 $CR = new CI_DB_result();
353 $CR->num_rows = $RES->num_rows();
354 $CR->result_object = $RES->result_object();
355 $CR->result_array = $RES->result_array();
356
357 // Reset these since cached objects can not utilize resource IDs.
358 $CR->conn_id = NULL;
359 $CR->result_id = NULL;
360
361 $this->CACHE->write($sql, $CR);
362 }
363
364 return $RES;
365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * Load the result drivers
371 *
372 * @access public
373 * @return string the name of the result class
374 */
375 function load_rdriver()
376 {
377 $driver = 'CI_DB_'.$this->dbdriver.'_result';
378
379 if ( ! class_exists($driver))
380 {
381 include_once(BASEPATH.'database/DB_result'.EXT);
382 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT);
383 }
384
385 return $driver;
386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Simple Query
392 * This is a simplified version of the query() function. Internally
393 * we only use it when running transaction commands since they do
394 * not require all the features of the main query() function.
395 *
396 * @access public
397 * @param string the sql query
398 * @return mixed
399 */
400 function simple_query($sql)
401 {
402 if ( ! $this->conn_id)
403 {
404 $this->initialize();
405 }
406
407 return $this->_execute($sql);
408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Disable Transactions
414 * This permits transactions to be disabled at run-time.
415 *
416 * @access public
417 * @return void
418 */
419 function trans_off()
420 {
421 $this->trans_enabled = FALSE;
422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Start Transaction
428 *
429 * @access public
430 * @return void
431 */
432 function trans_start($test_mode = FALSE)
433 {
434 if ( ! $this->trans_enabled)
435 {
436 return FALSE;
437 }
438
439 // When transactions are nested we only begin/commit/rollback the outermost ones
440 if ($this->_trans_depth > 0)
441 {
442 $this->_trans_depth += 1;
443 return;
444 }
445
446 $this->trans_begin($test_mode);
447 }
448
449 // --------------------------------------------------------------------
450
451 /**
452 * Complete Transaction
453 *
454 * @access public
455 * @return bool
456 */
457 function trans_complete()
458 {
459 if ( ! $this->trans_enabled)
460 {
461 return FALSE;
462 }
463
464 // When transactions are nested we only begin/commit/rollback the outermost ones
465 if ($this->_trans_depth > 1)
466 {
467 $this->_trans_depth -= 1;
468 return TRUE;
469 }
470
471 // The query() function will set this flag to TRUE in the event that a query failed
Rick Ellis28239ad2007-06-11 04:26:39 +0000472 if ($this->_trans_status === FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000473 {
474 $this->trans_rollback();
475
476 if ($this->db_debug)
477 {
478 return $this->display_error('db_transaction_failure');
479 }
480 return FALSE;
481 }
482
483 $this->trans_commit();
484 return TRUE;
485 }
486
487 // --------------------------------------------------------------------
488
489 /**
490 * Lets you retrieve the transaction flag to determine if it has failed
491 *
492 * @access public
493 * @return bool
494 */
495 function trans_status()
496 {
Rick Ellis28239ad2007-06-11 04:26:39 +0000497 return $this->_trans_status;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Compile Bindings
504 *
505 * @access public
506 * @param string the sql statement
507 * @param array an array of bind data
508 * @return string
509 */
510 function compile_binds($sql, $binds)
511 {
512 if (FALSE === strpos($sql, $this->bind_marker))
513 {
514 return $sql;
515 }
516
517 if ( ! is_array($binds))
518 {
519 $binds = array($binds);
520 }
521
522 foreach ($binds as $val)
523 {
524 $val = $this->escape($val);
525
526 // Just in case the replacement string contains the bind
527 // character we'll temporarily replace it with a marker
528 $val = str_replace($this->bind_marker, '{%bind_marker%}', $val);
529 $sql = preg_replace("#".preg_quote($this->bind_marker, '#')."#", str_replace('$', '\$', $val), $sql, 1);
530 }
531
532 return str_replace('{%bind_marker%}', $this->bind_marker, $sql);
533 }
534
535 // --------------------------------------------------------------------
536
537 /**
538 * Determines if a query is a "write" type.
539 *
540 * @access public
541 * @param string An SQL query string
542 * @return boolean
543 */
544 function is_write_type($sql)
545 {
546 if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
547 {
548 return FALSE;
549 }
550 return TRUE;
551 }
552
553 // --------------------------------------------------------------------
554
555 /**
556 * Calculate the aggregate query elapsed time
557 *
558 * @access public
559 * @param integer The number of decimal places
560 * @return integer
561 */
562 function elapsed_time($decimals = 6)
563 {
564 return number_format($this->benchmark, $decimals);
565 }
566
567 // --------------------------------------------------------------------
568
569 /**
570 * Returns the total number of queries
571 *
572 * @access public
573 * @return integer
574 */
575 function total_queries()
576 {
577 return $this->query_count;
578 }
579
580 // --------------------------------------------------------------------
581
582 /**
583 * Returns the last query that was executed
584 *
585 * @access public
586 * @return void
587 */
588 function last_query()
589 {
590 return end($this->queries);
591 }
592
593 // --------------------------------------------------------------------
594
595 /**
596 * "Smart" Escape String
597 *
598 * Escapes data based on type
599 * Sets boolean and null types
600 *
601 * @access public
602 * @param string
603 * @return integer
604 */
605 function escape($str)
606 {
607 switch (gettype($str))
608 {
609 case 'string' : $str = "'".$this->escape_str($str)."'";
610 break;
611 case 'boolean' : $str = ($str === FALSE) ? 0 : 1;
612 break;
613 default : $str = ($str === NULL) ? 'NULL' : $str;
614 break;
615 }
616
617 return $str;
618 }
619
620 // --------------------------------------------------------------------
621
622 /**
623 * Primary
624 *
625 * Retrieves the primary key. It assumes that the row in the first
626 * position is the primary key
627 *
628 * @access public
629 * @param string the table name
630 * @return string
631 */
632 function primary($table = '')
633 {
634 $fields = $this->list_fields($table);
635
636 if ( ! is_array($fields))
637 {
638 return FALSE;
639 }
640
641 return current($fields);
642 }
643
644 // --------------------------------------------------------------------
645
646 /**
647 * Returns an array of table names
648 *
649 * @access public
650 * @return array
651 */
652 function list_tables()
653 {
654 // Is there a cached result?
655 if (isset($this->data_cache['table_names']))
656 {
657 return $this->data_cache['table_names'];
658 }
659
660 if (FALSE === ($sql = $this->_list_tables()))
661 {
662 if ($this->db_debug)
663 {
664 return $this->display_error('db_unsupported_function');
665 }
666 return FALSE;
667 }
668
669 $retval = array();
670 $query = $this->query($sql);
671
672 if ($query->num_rows() > 0)
673 {
674 foreach($query->result_array() as $row)
675 {
676 if (isset($row['TABLE_NAME']))
677 {
678 $retval[] = $row['TABLE_NAME'];
679 }
680 else
681 {
682 $retval[] = array_shift($row);
683 }
684 }
685 }
686
687 $this->data_cache['table_names'] = $retval;
688 return $this->data_cache['table_names'];
689 }
690
691 // --------------------------------------------------------------------
692
693 /**
694 * Determine if a particular table exists
695 * @access public
696 * @return boolean
697 */
698 function table_exists($table_name)
699 {
700 return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE;
701 }
702
703 // --------------------------------------------------------------------
704
705 /**
706 * Fetch MySQL Field Names
707 *
708 * @access public
709 * @param string the table name
710 * @return array
711 */
712 function list_fields($table = '')
713 {
714 // Is there a cached result?
715 if (isset($this->data_cache['field_names'][$table]))
716 {
717 return $this->data_cache['field_names'][$table];
718 }
719
720 if ($table == '')
721 {
722 if ($this->db_debug)
723 {
724 return $this->display_error('db_field_param_missing');
725 }
726 return FALSE;
727 }
728
729 if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table)))
730 {
731 if ($this->db_debug)
732 {
733 return $this->display_error('db_unsupported_function');
734 }
735 return FALSE;
736 }
737
738 $query = $this->query($sql);
739
740 $retval = array();
741 foreach($query->result_array() as $row)
742 {
743 if (isset($row['COLUMN_NAME']))
744 {
745 $retval[] = $row['COLUMN_NAME'];
746 }
747 else
748 {
749 $retval[] = current($row);
750 }
751 }
752
753 $this->data_cache['field_names'][$table] = $retval;
754 return $this->data_cache['field_names'][$table];
755 }
756
757 // --------------------------------------------------------------------
758
759 /**
760 * Determine if a particular field exists
761 * @access public
762 * @param string
763 * @param string
764 * @return boolean
765 */
766 function field_exists($field_name, $table_name)
767 {
768 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
769 }
770
771 // --------------------------------------------------------------------
772
773 /**
774 * DEPRECATED - use list_fields()
775 */
776 function field_names($table = '')
777 {
778 return $this->list_fields($table);
779 }
780
781 // --------------------------------------------------------------------
782
783 /**
784 * Returns an object with field data
785 *
786 * @access public
787 * @param string the table name
788 * @return object
789 */
790 function field_data($table = '')
791 {
792 if ($table == '')
793 {
794 if ($this->db_debug)
795 {
796 return $this->display_error('db_field_param_missing');
797 }
798 return FALSE;
799 }
800
801 $query = $this->query($this->_field_data($this->dbprefix.$table));
802 return $query->field_data();
803 }
804
805 // --------------------------------------------------------------------
806
807 /**
808 * Generate an insert string
809 *
810 * @access public
811 * @param string the table upon which the query will be performed
812 * @param array an associative array data of key/values
813 * @return string
814 */
815 function insert_string($table, $data)
816 {
817 $fields = array();
818 $values = array();
819
820 foreach($data as $key => $val)
821 {
822 $fields[] = $key;
823 $values[] = $this->escape($val);
824 }
825
826 return $this->_insert($this->dbprefix.$table, $fields, $values);
827 }
828
829 // --------------------------------------------------------------------
830
831 /**
832 * Generate an update string
833 *
834 * @access public
835 * @param string the table upon which the query will be performed
836 * @param array an associative array data of key/values
837 * @param mixed the "where" statement
838 * @return string
839 */
840 function update_string($table, $data, $where)
841 {
842 if ($where == '')
843 return false;
844
845 $fields = array();
846 foreach($data as $key => $val)
847 {
848 $fields[$key] = $this->escape($val);
849 }
850
851 if ( ! is_array($where))
852 {
853 $dest = array($where);
854 }
855 else
856 {
857 $dest = array();
858 foreach ($where as $key => $val)
859 {
860 $prefix = (count($dest) == 0) ? '' : ' AND ';
861
862 if ($val != '')
863 {
864 if ( ! $this->_has_operator($key))
865 {
866 $key .= ' =';
867 }
868
869 $val = ' '.$this->escape($val);
870 }
871
872 $dest[] = $prefix.$key.$val;
873 }
874 }
875
876 return $this->_update($this->dbprefix.$table, $fields, $dest);
877 }
878
879 // --------------------------------------------------------------------
880
881 /**
882 * Enables a native PHP function to be run, using a platform agnostic wrapper.
883 *
884 * @access public
885 * @param string the function name
886 * @param mixed any parameters needed by the function
887 * @return mixed
888 */
889 function call_function($function)
890 {
891 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
892
893 if (FALSE === strpos($driver, $function))
894 {
895 $function = $driver.$function;
896 }
897
898 if ( ! function_exists($function))
899 {
900 if ($this->db_debug)
901 {
902 return $this->display_error('db_unsupported_function');
903 }
904 return FALSE;
905 }
906 else
907 {
908 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
909
910 return call_user_func_array($function, $args);
911 }
912 }
913
914 // --------------------------------------------------------------------
915
916 /**
917 * Set Cache Directory Path
918 *
919 * @access public
920 * @param string the path to the cache directory
921 * @return void
922 */
923 function cache_set_path($path = '')
924 {
925 $this->cachedir = $path;
926 }
927
928 // --------------------------------------------------------------------
929
930 /**
931 * Enable Query Caching
932 *
933 * @access public
934 * @return void
935 */
936 function cache_on()
937 {
938 $this->cache_on = TRUE;
939 return TRUE;
940 }
941
942 // --------------------------------------------------------------------
943
944 /**
945 * Disable Query Caching
946 *
947 * @access public
948 * @return void
949 */
950 function cache_off()
951 {
952 $this->cache_on = FALSE;
953 return FALSE;
954 }
955
956
957 // --------------------------------------------------------------------
958
959 /**
960 * Delete the cache files associated with a particular URI
961 *
962 * @access public
963 * @return void
964 */
965 function cache_delete($segment_one = '', $segment_two = '')
966 {
967 if ( ! $this->_cache_init())
968 {
969 return FALSE;
970 }
971 return $this->CACHE->delete($segment_one, $segment_two);
972 }
973
974 // --------------------------------------------------------------------
975
976 /**
977 * Delete All cache files
978 *
979 * @access public
980 * @return void
981 */
982 function cache_delete_all()
983 {
984 if ( ! $this->_cache_init())
985 {
986 return FALSE;
987 }
988
989 return $this->CACHE->delete_all();
990 }
991
992 // --------------------------------------------------------------------
993
994 /**
995 * Initialize the Cache Class
996 *
997 * @access private
998 * @return void
999 */
1000 function _cache_init()
1001 {
1002 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1003 {
1004 return TRUE;
1005 }
1006
1007 if ( ! @include(BASEPATH.'database/DB_cache'.EXT))
1008 {
1009 return $this->cache_off();
1010 }
1011
1012 $this->CACHE = new CI_DB_Cache;
1013 return TRUE;
1014 }
1015
1016
1017 // --------------------------------------------------------------------
1018
1019 /**
1020 * Close DB Connection
1021 *
1022 * @access public
1023 * @return void
1024 */
1025 function close()
1026 {
1027 if (is_resource($this->conn_id))
1028 {
1029 $this->_close($this->conn_id);
1030 }
1031 $this->conn_id = FALSE;
1032 }
1033
1034 // --------------------------------------------------------------------
1035
1036 /**
1037 * Display an error message
1038 *
1039 * @access public
1040 * @param string the error message
1041 * @param string any "swap" values
1042 * @param boolean whether to localize the message
1043 * @return string sends the application/error_db.php template
1044 */
1045 function display_error($error = '', $swap = '', $native = FALSE)
1046 {
1047 $LANG = new CI_Language();
1048 $LANG->load('db');
1049
1050 $heading = 'MySQL Error';
1051
1052 if ($native == TRUE)
1053 {
1054 $message = $error;
1055 }
1056 else
1057 {
1058 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1059 }
1060
1061 if ( ! class_exists('CI_Exceptions'))
1062 {
1063 include(BASEPATH.'libraries/Exceptions'.EXT);
1064 }
1065
1066 $error = new CI_Exceptions();
1067 echo $error->show_error('An Error Was Encountered', $message, 'error_db');
1068 exit;
1069 }
1070
1071}
1072
admin7b613c72006-09-24 18:05:17 +00001073?>