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