blob: fc700ec324ad544b152925689f9aa2f540d88c1d [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 $RES->num_rows = $RES->num_rows();
333
Derek Allardd2df9bc2007-04-15 17:41:17 +0000334 if ($this->dbdriver == 'oci8')
335 {
336 $RES->stmt_id = $this->stmt_id;
337 $RES->curs_id = NULL;
338 $RES->limit_used = $this->limit_used;
339 }
340
341 // Is query caching enabled? If so, we'll serialize the
342 // result object and save it to a cache file.
343 if ($this->cache_on == TRUE AND $this->_cache_init())
344 {
345 // We'll create a new instance of the result object
346 // only without the platform specific driver since
347 // we can't use it with cached data (the query result
348 // resource ID won't be any good once we've cached the
349 // result object, so we'll have to compile the data
350 // and save it)
351 $CR = new CI_DB_result();
352 $CR->num_rows = $RES->num_rows();
353 $CR->result_object = $RES->result_object();
354 $CR->result_array = $RES->result_array();
355
356 // Reset these since cached objects can not utilize resource IDs.
357 $CR->conn_id = NULL;
358 $CR->result_id = NULL;
359
360 $this->CACHE->write($sql, $CR);
361 }
362
363 return $RES;
364 }
365
366 // --------------------------------------------------------------------
367
368 /**
369 * Load the result drivers
370 *
371 * @access public
372 * @return string the name of the result class
373 */
374 function load_rdriver()
375 {
376 $driver = 'CI_DB_'.$this->dbdriver.'_result';
377
378 if ( ! class_exists($driver))
379 {
380 include_once(BASEPATH.'database/DB_result'.EXT);
381 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT);
382 }
383
384 return $driver;
385 }
386
387 // --------------------------------------------------------------------
388
389 /**
390 * Simple Query
391 * This is a simplified version of the query() function. Internally
392 * we only use it when running transaction commands since they do
393 * not require all the features of the main query() function.
394 *
395 * @access public
396 * @param string the sql query
397 * @return mixed
398 */
399 function simple_query($sql)
400 {
401 if ( ! $this->conn_id)
402 {
403 $this->initialize();
404 }
405
406 return $this->_execute($sql);
407 }
408
409 // --------------------------------------------------------------------
410
411 /**
412 * Disable Transactions
413 * This permits transactions to be disabled at run-time.
414 *
415 * @access public
416 * @return void
417 */
418 function trans_off()
419 {
420 $this->trans_enabled = FALSE;
421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Start Transaction
427 *
428 * @access public
429 * @return void
430 */
431 function trans_start($test_mode = FALSE)
432 {
433 if ( ! $this->trans_enabled)
434 {
435 return FALSE;
436 }
437
438 // When transactions are nested we only begin/commit/rollback the outermost ones
439 if ($this->_trans_depth > 0)
440 {
441 $this->_trans_depth += 1;
442 return;
443 }
444
445 $this->trans_begin($test_mode);
446 }
447
448 // --------------------------------------------------------------------
449
450 /**
451 * Complete Transaction
452 *
453 * @access public
454 * @return bool
455 */
456 function trans_complete()
457 {
458 if ( ! $this->trans_enabled)
459 {
460 return FALSE;
461 }
462
463 // When transactions are nested we only begin/commit/rollback the outermost ones
464 if ($this->_trans_depth > 1)
465 {
466 $this->_trans_depth -= 1;
467 return TRUE;
468 }
469
470 // The query() function will set this flag to TRUE in the event that a query failed
Rick Ellis28239ad2007-06-11 04:26:39 +0000471 if ($this->_trans_status === FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000472 {
473 $this->trans_rollback();
474
475 if ($this->db_debug)
476 {
477 return $this->display_error('db_transaction_failure');
478 }
479 return FALSE;
480 }
481
482 $this->trans_commit();
483 return TRUE;
484 }
485
486 // --------------------------------------------------------------------
487
488 /**
489 * Lets you retrieve the transaction flag to determine if it has failed
490 *
491 * @access public
492 * @return bool
493 */
494 function trans_status()
495 {
Rick Ellis28239ad2007-06-11 04:26:39 +0000496 return $this->_trans_status;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000497 }
498
499 // --------------------------------------------------------------------
500
501 /**
502 * Compile Bindings
503 *
504 * @access public
505 * @param string the sql statement
506 * @param array an array of bind data
507 * @return string
508 */
509 function compile_binds($sql, $binds)
510 {
511 if (FALSE === strpos($sql, $this->bind_marker))
512 {
513 return $sql;
514 }
515
516 if ( ! is_array($binds))
517 {
518 $binds = array($binds);
519 }
520
521 foreach ($binds as $val)
522 {
523 $val = $this->escape($val);
524
525 // Just in case the replacement string contains the bind
526 // character we'll temporarily replace it with a marker
527 $val = str_replace($this->bind_marker, '{%bind_marker%}', $val);
528 $sql = preg_replace("#".preg_quote($this->bind_marker, '#')."#", str_replace('$', '\$', $val), $sql, 1);
529 }
530
531 return str_replace('{%bind_marker%}', $this->bind_marker, $sql);
532 }
533
534 // --------------------------------------------------------------------
535
536 /**
537 * Determines if a query is a "write" type.
538 *
539 * @access public
540 * @param string An SQL query string
541 * @return boolean
542 */
543 function is_write_type($sql)
544 {
545 if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
546 {
547 return FALSE;
548 }
549 return TRUE;
550 }
551
552 // --------------------------------------------------------------------
553
554 /**
555 * Calculate the aggregate query elapsed time
556 *
557 * @access public
558 * @param integer The number of decimal places
559 * @return integer
560 */
561 function elapsed_time($decimals = 6)
562 {
563 return number_format($this->benchmark, $decimals);
564 }
565
566 // --------------------------------------------------------------------
567
568 /**
569 * Returns the total number of queries
570 *
571 * @access public
572 * @return integer
573 */
574 function total_queries()
575 {
576 return $this->query_count;
577 }
578
579 // --------------------------------------------------------------------
580
581 /**
582 * Returns the last query that was executed
583 *
584 * @access public
585 * @return void
586 */
587 function last_query()
588 {
589 return end($this->queries);
590 }
591
592 // --------------------------------------------------------------------
593
594 /**
595 * "Smart" Escape String
596 *
597 * Escapes data based on type
598 * Sets boolean and null types
599 *
600 * @access public
601 * @param string
602 * @return integer
603 */
604 function escape($str)
605 {
606 switch (gettype($str))
607 {
608 case 'string' : $str = "'".$this->escape_str($str)."'";
609 break;
610 case 'boolean' : $str = ($str === FALSE) ? 0 : 1;
611 break;
612 default : $str = ($str === NULL) ? 'NULL' : $str;
613 break;
614 }
615
616 return $str;
617 }
618
619 // --------------------------------------------------------------------
620
621 /**
622 * Primary
623 *
624 * Retrieves the primary key. It assumes that the row in the first
625 * position is the primary key
626 *
627 * @access public
628 * @param string the table name
629 * @return string
630 */
631 function primary($table = '')
632 {
633 $fields = $this->list_fields($table);
634
635 if ( ! is_array($fields))
636 {
637 return FALSE;
638 }
639
640 return current($fields);
641 }
642
643 // --------------------------------------------------------------------
644
645 /**
646 * Returns an array of table names
647 *
648 * @access public
649 * @return array
650 */
651 function list_tables()
652 {
653 // Is there a cached result?
654 if (isset($this->data_cache['table_names']))
655 {
656 return $this->data_cache['table_names'];
657 }
658
659 if (FALSE === ($sql = $this->_list_tables()))
660 {
661 if ($this->db_debug)
662 {
663 return $this->display_error('db_unsupported_function');
664 }
665 return FALSE;
666 }
667
668 $retval = array();
669 $query = $this->query($sql);
670
671 if ($query->num_rows() > 0)
672 {
673 foreach($query->result_array() as $row)
674 {
675 if (isset($row['TABLE_NAME']))
676 {
677 $retval[] = $row['TABLE_NAME'];
678 }
679 else
680 {
681 $retval[] = array_shift($row);
682 }
683 }
684 }
685
686 $this->data_cache['table_names'] = $retval;
687 return $this->data_cache['table_names'];
688 }
689
690 // --------------------------------------------------------------------
691
692 /**
693 * Determine if a particular table exists
694 * @access public
695 * @return boolean
696 */
697 function table_exists($table_name)
698 {
699 return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE;
700 }
701
702 // --------------------------------------------------------------------
703
704 /**
705 * Fetch MySQL Field Names
706 *
707 * @access public
708 * @param string the table name
709 * @return array
710 */
711 function list_fields($table = '')
712 {
713 // Is there a cached result?
714 if (isset($this->data_cache['field_names'][$table]))
715 {
716 return $this->data_cache['field_names'][$table];
717 }
718
719 if ($table == '')
720 {
721 if ($this->db_debug)
722 {
723 return $this->display_error('db_field_param_missing');
724 }
725 return FALSE;
726 }
727
728 if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table)))
729 {
730 if ($this->db_debug)
731 {
732 return $this->display_error('db_unsupported_function');
733 }
734 return FALSE;
735 }
736
737 $query = $this->query($sql);
738
739 $retval = array();
740 foreach($query->result_array() as $row)
741 {
742 if (isset($row['COLUMN_NAME']))
743 {
744 $retval[] = $row['COLUMN_NAME'];
745 }
746 else
747 {
748 $retval[] = current($row);
749 }
750 }
751
752 $this->data_cache['field_names'][$table] = $retval;
753 return $this->data_cache['field_names'][$table];
754 }
755
756 // --------------------------------------------------------------------
757
758 /**
759 * Determine if a particular field exists
760 * @access public
761 * @param string
762 * @param string
763 * @return boolean
764 */
765 function field_exists($field_name, $table_name)
766 {
767 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
768 }
769
770 // --------------------------------------------------------------------
771
772 /**
773 * DEPRECATED - use list_fields()
774 */
775 function field_names($table = '')
776 {
777 return $this->list_fields($table);
778 }
779
780 // --------------------------------------------------------------------
781
782 /**
783 * Returns an object with field data
784 *
785 * @access public
786 * @param string the table name
787 * @return object
788 */
789 function field_data($table = '')
790 {
791 if ($table == '')
792 {
793 if ($this->db_debug)
794 {
795 return $this->display_error('db_field_param_missing');
796 }
797 return FALSE;
798 }
799
800 $query = $this->query($this->_field_data($this->dbprefix.$table));
801 return $query->field_data();
802 }
803
804 // --------------------------------------------------------------------
805
806 /**
807 * Generate an insert string
808 *
809 * @access public
810 * @param string the table upon which the query will be performed
811 * @param array an associative array data of key/values
812 * @return string
813 */
814 function insert_string($table, $data)
815 {
816 $fields = array();
817 $values = array();
818
819 foreach($data as $key => $val)
820 {
821 $fields[] = $key;
822 $values[] = $this->escape($val);
823 }
824
825 return $this->_insert($this->dbprefix.$table, $fields, $values);
826 }
827
828 // --------------------------------------------------------------------
829
830 /**
831 * Generate an update string
832 *
833 * @access public
834 * @param string the table upon which the query will be performed
835 * @param array an associative array data of key/values
836 * @param mixed the "where" statement
837 * @return string
838 */
839 function update_string($table, $data, $where)
840 {
841 if ($where == '')
842 return false;
843
844 $fields = array();
845 foreach($data as $key => $val)
846 {
847 $fields[$key] = $this->escape($val);
848 }
849
850 if ( ! is_array($where))
851 {
852 $dest = array($where);
853 }
854 else
855 {
856 $dest = array();
857 foreach ($where as $key => $val)
858 {
859 $prefix = (count($dest) == 0) ? '' : ' AND ';
860
861 if ($val != '')
862 {
863 if ( ! $this->_has_operator($key))
864 {
865 $key .= ' =';
866 }
867
868 $val = ' '.$this->escape($val);
869 }
870
871 $dest[] = $prefix.$key.$val;
872 }
873 }
874
875 return $this->_update($this->dbprefix.$table, $fields, $dest);
876 }
877
878 // --------------------------------------------------------------------
879
880 /**
881 * Enables a native PHP function to be run, using a platform agnostic wrapper.
882 *
883 * @access public
884 * @param string the function name
885 * @param mixed any parameters needed by the function
886 * @return mixed
887 */
888 function call_function($function)
889 {
890 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
891
892 if (FALSE === strpos($driver, $function))
893 {
894 $function = $driver.$function;
895 }
896
897 if ( ! function_exists($function))
898 {
899 if ($this->db_debug)
900 {
901 return $this->display_error('db_unsupported_function');
902 }
903 return FALSE;
904 }
905 else
906 {
907 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
908
909 return call_user_func_array($function, $args);
910 }
911 }
912
913 // --------------------------------------------------------------------
914
915 /**
916 * Set Cache Directory Path
917 *
918 * @access public
919 * @param string the path to the cache directory
920 * @return void
921 */
922 function cache_set_path($path = '')
923 {
924 $this->cachedir = $path;
925 }
926
927 // --------------------------------------------------------------------
928
929 /**
930 * Enable Query Caching
931 *
932 * @access public
933 * @return void
934 */
935 function cache_on()
936 {
937 $this->cache_on = TRUE;
938 return TRUE;
939 }
940
941 // --------------------------------------------------------------------
942
943 /**
944 * Disable Query Caching
945 *
946 * @access public
947 * @return void
948 */
949 function cache_off()
950 {
951 $this->cache_on = FALSE;
952 return FALSE;
953 }
954
955
956 // --------------------------------------------------------------------
957
958 /**
959 * Delete the cache files associated with a particular URI
960 *
961 * @access public
962 * @return void
963 */
964 function cache_delete($segment_one = '', $segment_two = '')
965 {
966 if ( ! $this->_cache_init())
967 {
968 return FALSE;
969 }
970 return $this->CACHE->delete($segment_one, $segment_two);
971 }
972
973 // --------------------------------------------------------------------
974
975 /**
976 * Delete All cache files
977 *
978 * @access public
979 * @return void
980 */
981 function cache_delete_all()
982 {
983 if ( ! $this->_cache_init())
984 {
985 return FALSE;
986 }
987
988 return $this->CACHE->delete_all();
989 }
990
991 // --------------------------------------------------------------------
992
993 /**
994 * Initialize the Cache Class
995 *
996 * @access private
997 * @return void
998 */
999 function _cache_init()
1000 {
1001 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
1002 {
1003 return TRUE;
1004 }
1005
1006 if ( ! @include(BASEPATH.'database/DB_cache'.EXT))
1007 {
1008 return $this->cache_off();
1009 }
1010
1011 $this->CACHE = new CI_DB_Cache;
1012 return TRUE;
1013 }
1014
1015
1016 // --------------------------------------------------------------------
1017
1018 /**
1019 * Close DB Connection
1020 *
1021 * @access public
1022 * @return void
1023 */
1024 function close()
1025 {
1026 if (is_resource($this->conn_id))
1027 {
1028 $this->_close($this->conn_id);
1029 }
1030 $this->conn_id = FALSE;
1031 }
1032
1033 // --------------------------------------------------------------------
1034
1035 /**
1036 * Display an error message
1037 *
1038 * @access public
1039 * @param string the error message
1040 * @param string any "swap" values
1041 * @param boolean whether to localize the message
1042 * @return string sends the application/error_db.php template
1043 */
1044 function display_error($error = '', $swap = '', $native = FALSE)
1045 {
1046 $LANG = new CI_Language();
1047 $LANG->load('db');
1048
1049 $heading = 'MySQL Error';
1050
1051 if ($native == TRUE)
1052 {
1053 $message = $error;
1054 }
1055 else
1056 {
1057 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
1058 }
1059
1060 if ( ! class_exists('CI_Exceptions'))
1061 {
1062 include(BASEPATH.'libraries/Exceptions'.EXT);
1063 }
1064
1065 $error = new CI_Exceptions();
1066 echo $error->show_error('An Error Was Encountered', $message, 'error_db');
1067 exit;
1068 }
1069
1070}
1071
admin7b613c72006-09-24 18:05:17 +00001072?>