blob: 4032557990f5f1161e246539ea870e3048c8e5d0 [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 = '';
adminc2e3cd72006-10-05 04:22:30 +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 {
adminc2e3cd72006-10-05 04:22:30 +0000237 if (FALSE !== ($cache = $this->CACHE->read($sql)))
admin0c405652006-10-04 18:37:57 +0000238 {
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
admin0c405652006-10-04 18:37:57 +0000290 if ($this->cache_on == TRUE AND $this->_cache_init())
admindb8a3742006-10-04 07:43:35 +0000291 {
adminc2e3cd72006-10-05 04:22:30 +0000292 $this->CACHE->delete();
admindb8a3742006-10-04 07:43:35 +0000293 }
294
admin7b613c72006-09-24 18:05:17 +0000295 return TRUE;
296 }
297
298 // Return TRUE if we don't need to create a result object
299 // Currently only the Oracle driver uses this when stored
300 // procedures are used
301 if ($return_object !== TRUE)
302 {
303 return TRUE;
304 }
admindb8a3742006-10-04 07:43:35 +0000305
306 // Load and instantiate the result driver
admindb8a3742006-10-04 07:43:35 +0000307
adminc2e3cd72006-10-05 04:22:30 +0000308 $driver = $this->load_rdriver();
309 $RES = new $driver();
admin6871d952006-10-04 00:36:18 +0000310 $RES->conn_id = $this->conn_id;
admin6871d952006-10-04 00:36:18 +0000311 $RES->result_id = $this->result_id;
312
313 if ($this->dbdriver == 'oci8')
314 {
adminc2e3cd72006-10-05 04:22:30 +0000315 $RES->stmt_id = $this->stmt_id;
316 $RES->curs_id = NULL;
317 $RES->limit_used = $this->limit_used;
admin6871d952006-10-04 00:36:18 +0000318 }
admindb8a3742006-10-04 07:43:35 +0000319
320 // Is query caching enabled? If so, we'll serialize the
adminc2e3cd72006-10-05 04:22:30 +0000321 // result object and save it to a cache file.
admin0c405652006-10-04 18:37:57 +0000322 if ($this->cache_on == TRUE AND $this->_cache_init())
adminc2e3cd72006-10-05 04:22:30 +0000323 {
324 // We'll create a new instance of the result object
325 // only without the platform specific driver since
326 // we can't use it with cached data (the query result
327 // resource ID won't be any good once we've cached the
328 // result object, so we'll have to compile the data
329 // and save it)
330 $CR = new CI_DB_result();
331 $CR->num_rows = $RES->num_rows();
332 $CR->result_object = $RES->result_object();
333 $CR->result_array = $RES->result_array();
334
335 $this->CACHE->write($sql, $CR);
admindb8a3742006-10-04 07:43:35 +0000336 }
337
admin7b613c72006-09-24 18:05:17 +0000338 return $RES;
339 }
admindb8a3742006-10-04 07:43:35 +0000340
341 // --------------------------------------------------------------------
342
343 /**
344 * Load the result drivers
345 *
346 * @access public
347 * @return string the name of the result class
348 */
349 function load_rdriver()
350 {
351 $driver = 'CI_DB_'.$this->dbdriver.'_result';
352
353 if ( ! class_exists($driver))
354 {
355 include_once(BASEPATH.'database/DB_result'.EXT);
356 include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result'.EXT);
357 }
358
359 return $driver;
360 }
admin7b613c72006-09-24 18:05:17 +0000361
362 // --------------------------------------------------------------------
363
364 /**
365 * Simple Query
366 * This is a simiplified version of the query() function. Internally
367 * we only use it when running transaction commands since they do
368 * not require all the features of the main query() function.
369 *
370 * @access public
371 * @param string the sql query
372 * @return mixed
373 */
374 function simple_query($sql)
375 {
376 if ( ! $this->conn_id)
377 {
378 $this->initialize();
379 }
380
381 return $this->_execute($sql, $this->conn_id);
382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * Disable Transactions
388 * This permits transactions to be disabled at run-time.
389 *
390 * @access public
391 * @return void
392 */
393 function trans_off()
394 {
395 $this->trans_enabled = FALSE;
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Start Transaction
402 *
403 * @access public
404 * @return void
405 */
406 function trans_start($test_mode = FALSE)
407 {
408 if ( ! $this->trans_enabled)
409 {
410 return FALSE;
411 }
412
413 // When transactions are nested we only begin/commit/rollback the outermost ones
414 if ($this->_trans_depth > 0)
415 {
416 $this->_trans_depth += 1;
417 return;
418 }
419
420 $this->trans_begin($test_mode);
421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Complete Transaction
427 *
428 * @access public
429 * @return bool
430 */
431 function trans_complete()
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 > 1)
440 {
441 $this->_trans_depth -= 1;
442 return TRUE;
443 }
444
445 // The query() function will set this flag to TRUE in the event that a query failed
446 if ($this->_trans_failure === TRUE)
447 {
448 $this->trans_rollback();
449
450 if ($this->db_debug)
451 {
452 return $this->display_error('db_transaction_failure');
453 }
454 return FALSE;
455 }
456
457 $this->trans_commit();
458 return TRUE;
459 }
460
461 // --------------------------------------------------------------------
462
463 /**
464 * Lets you retrieve the transaction flag to determine if it has failed
465 *
466 * @access public
467 * @return bool
468 */
469 function trans_status()
470 {
471 return $this->_trans_failure;
472 }
473
474 // --------------------------------------------------------------------
475
476 /**
477 * Compile Bindings
478 *
479 * @access public
480 * @param string the sql statement
481 * @param array an array of bind data
482 * @return string
483 */
484 function compile_binds($sql, $binds)
485 {
486 if (FALSE === strpos($sql, $this->bind_marker))
487 {
488 return $sql;
489 }
490
491 if ( ! is_array($binds))
492 {
493 $binds = array($binds);
494 }
495
496 foreach ($binds as $val)
497 {
498 $val = $this->escape($val);
499
500 // Just in case the replacement string contains the bind
501 // character we'll temporarily replace it with a marker
502 $val = str_replace($this->bind_marker, '{%bind_marker%}', $val);
503 $sql = preg_replace("#".preg_quote($this->bind_marker, '#')."#", str_replace('$', '\$', $val), $sql, 1);
504 }
505
506 return str_replace('{%bind_marker%}', $this->bind_marker, $sql);
507 }
508
509 // --------------------------------------------------------------------
510
511 /**
512 * Determines if a query is a "write" type.
513 *
514 * @access public
515 * @param string An SQL query string
516 * @return boolean
517 */
518 function is_write_type($sql)
519 {
520 if ( ! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|LOAD DATA|COPY|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s+/i', $sql))
521 {
522 return FALSE;
523 }
524 return TRUE;
525 }
526
527 // --------------------------------------------------------------------
528
529 /**
530 * Calculate the aggregate query elapsed time
531 *
532 * @access public
533 * @param intiger The number of decimal places
534 * @return integer
535 */
536 function elapsed_time($decimals = 6)
537 {
538 return number_format($this->benchmark, $decimals);
539 }
540
541 // --------------------------------------------------------------------
542
543 /**
544 * Returns the total number of queries
545 *
546 * @access public
547 * @return integer
548 */
549 function total_queries()
550 {
551 return $this->query_count;
552 }
553
554 // --------------------------------------------------------------------
555
556 /**
557 * Returns the last query that was executed
558 *
559 * @access public
560 * @return void
561 */
562 function last_query()
563 {
564 return end($this->queries);
565 }
566
567 // --------------------------------------------------------------------
568
569 /**
570 * "Smart" Escape String
571 *
572 * Escapes data based on type
573 * Sets boolean and null types
574 *
575 * @access public
576 * @param string
577 * @return integer
578 */
579 function escape($str)
580 {
581 switch (gettype($str))
582 {
583 case 'string' : $str = "'".$this->escape_str($str)."'";
584 break;
585 case 'boolean' : $str = ($str === FALSE) ? 0 : 1;
586 break;
587 default : $str = ($str === NULL) ? 'NULL' : $str;
588 break;
589 }
590
591 return $str;
adminbb1d4392006-09-24 20:14:38 +0000592 }
593
admin9cd4e8e2006-09-25 23:26:25 +0000594
595
596 // --------------------------------------------------------------------
597
598 /**
599 * Primary
600 *
601 * Retrieves the primary key. It assumes that the row in the first
602 * position is the primary key
603 *
604 * @access public
605 * @param string the table name
606 * @return string
607 */
608 function primary($table = '')
609 {
610 $fields = $this->field_names($table);
611
612 if ( ! is_array($fields))
613 {
614 return FALSE;
615 }
616
617 return current($fields);
618 }
619
620 // --------------------------------------------------------------------
621
admin3dd978f2006-09-30 19:24:45 +0000622 /**
623 * Returns an array of table names
624 *
625 * @access public
626 * @return array
627 */
628 function list_tables()
629 {
630 // Is there a cached result?
admine8f6eb62006-10-02 06:46:16 +0000631 if (isset($this->data_cache['table_names']))
admin3dd978f2006-09-30 19:24:45 +0000632 {
admine8f6eb62006-10-02 06:46:16 +0000633 return $this->data_cache['table_names'];
admin3dd978f2006-09-30 19:24:45 +0000634 }
635
636 if (FALSE === ($sql = $this->_list_tables()))
637 {
admin6871d952006-10-04 00:36:18 +0000638 if ($this->db_debug)
639 {
admin3dd978f2006-09-30 19:24:45 +0000640 return $this->display_error('db_unsupported_function');
admin6871d952006-10-04 00:36:18 +0000641 }
642 return FALSE;
admin3dd978f2006-09-30 19:24:45 +0000643 }
644
645 $retval = array();
646 $query = $this->query($sql);
647
648 if ($query->num_rows() > 0)
649 {
650 foreach($query->result_array() as $row)
651 {
652 if (isset($row['TABLE_NAME']))
653 {
654 $retval[] = $row['TABLE_NAME'];
655 }
656 else
657 {
658 $retval[] = array_shift($row);
659 }
660 }
661 }
662
admine8f6eb62006-10-02 06:46:16 +0000663 return $this->data_cache['table_names'] =& $retval;
admin3dd978f2006-09-30 19:24:45 +0000664 }
665
666 // --------------------------------------------------------------------
667
668 /**
669 * Determine if a particular table exists
670 * @access public
671 * @return boolean
672 */
673 function table_exists($table_name)
674 {
675 return ( ! in_array($this->dbprefix.$table_name, $this->list_tables())) ? FALSE : TRUE;
676 }
677
678 // --------------------------------------------------------------------
679
admin9cd4e8e2006-09-25 23:26:25 +0000680 /**
681 * Fetch MySQL Field Names
682 *
683 * @access public
684 * @param string the table name
685 * @return array
686 */
admin6871d952006-10-04 00:36:18 +0000687 function list_fields($table = '')
688 {
admin3ed8c512006-09-29 23:26:28 +0000689 // Is there a cached result?
admine8f6eb62006-10-02 06:46:16 +0000690 if (isset($this->data_cache['field_names'][$table]))
admin3ed8c512006-09-29 23:26:28 +0000691 {
admine8f6eb62006-10-02 06:46:16 +0000692 return $this->data_cache['field_names'][$table];
admin3ed8c512006-09-29 23:26:28 +0000693 }
admin6871d952006-10-04 00:36:18 +0000694
695 if ($table == '')
696 {
admin9cd4e8e2006-09-25 23:26:25 +0000697 if ($this->db_debug)
698 {
699 return $this->display_error('db_field_param_missing');
700 }
701 return FALSE;
admin6871d952006-10-04 00:36:18 +0000702 }
703
admin9cd4e8e2006-09-25 23:26:25 +0000704 if (FALSE === ($sql = $this->_list_columns($this->dbprefix.$table)))
705 {
admin6871d952006-10-04 00:36:18 +0000706 if ($this->db_debug)
707 {
admin9cd4e8e2006-09-25 23:26:25 +0000708 return $this->display_error('db_unsupported_function');
admin6871d952006-10-04 00:36:18 +0000709 }
710 return FALSE;
admin9cd4e8e2006-09-25 23:26:25 +0000711 }
admin6871d952006-10-04 00:36:18 +0000712
713 $query = $this->query($sql);
714
715 $retval = array();
admin9cd4e8e2006-09-25 23:26:25 +0000716 foreach($query->result_array() as $row)
717 {
718 if (isset($row['COLUMN_NAME']))
719 {
720 $retval[] = $row['COLUMN_NAME'];
721 }
722 else
723 {
724 $retval[] = current($row);
admin6871d952006-10-04 00:36:18 +0000725 }
admin9cd4e8e2006-09-25 23:26:25 +0000726 }
admin6871d952006-10-04 00:36:18 +0000727
admine8f6eb62006-10-02 06:46:16 +0000728 return $this->data_cache['field_names'][$table] =& $retval;
admin6871d952006-10-04 00:36:18 +0000729 }
adminb2a9cec2006-10-01 03:38:04 +0000730
731 // --------------------------------------------------------------------
732
733 /**
734 * Determine if a particular field exists
735 * @access public
736 * @param string
737 * @param string
738 * @return boolean
739 */
740 function field_exists($field_name, $table_name)
741 {
742 return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE;
743 }
admin6871d952006-10-04 00:36:18 +0000744
admin3dd978f2006-09-30 19:24:45 +0000745 // --------------------------------------------------------------------
746
747 /**
748 * DEPRECATED - use list_fields()
749 */
admin6871d952006-10-04 00:36:18 +0000750 function field_names($table = '')
751 {
752 return $this->list_fields($table);
753 }
admin9cd4e8e2006-09-25 23:26:25 +0000754
755 // --------------------------------------------------------------------
756
757 /**
758 * Returns an object with field data
759 *
760 * @access public
761 * @param string the table name
762 * @return object
763 */
764 function field_data($table = '')
765 {
admin6871d952006-10-04 00:36:18 +0000766 if ($table == '')
767 {
admin9cd4e8e2006-09-25 23:26:25 +0000768 if ($this->db_debug)
769 {
770 return $this->display_error('db_field_param_missing');
771 }
772 return FALSE;
admin6871d952006-10-04 00:36:18 +0000773 }
774
admin9cd4e8e2006-09-25 23:26:25 +0000775 $query = $this->query($this->_field_data($this->dbprefix.$table));
776 return $query->field_data();
777 }
778
adminbb1d4392006-09-24 20:14:38 +0000779 // --------------------------------------------------------------------
780
781 /**
782 * Generate an insert string
783 *
784 * @access public
785 * @param string the table upon which the query will be performed
786 * @param array an associative array data of key/values
787 * @return string
788 */
789 function insert_string($table, $data)
790 {
admin6871d952006-10-04 00:36:18 +0000791 $fields = array();
adminbb1d4392006-09-24 20:14:38 +0000792 $values = array();
793
794 foreach($data as $key => $val)
795 {
796 $fields[] = $key;
797 $values[] = $this->escape($val);
798 }
799
800 return $this->_insert($this->dbprefix.$table, $fields, $values);
801 }
802
803 // --------------------------------------------------------------------
804
805 /**
806 * Generate an update string
807 *
808 * @access public
809 * @param string the table upon which the query will be performed
810 * @param array an associative array data of key/values
811 * @param mixed the "where" statement
812 * @return string
813 */
814 function update_string($table, $data, $where)
815 {
816 if ($where == '')
817 return false;
818
819 $fields = array();
820 foreach($data as $key => $val)
821 {
822 $fields[$key] = $this->escape($val);
823 }
824
825 if ( ! is_array($where))
826 {
827 $dest = array($where);
828 }
829 else
830 {
831 $dest = array();
832 foreach ($where as $key => $val)
833 {
834 $prefix = (count($dest) == 0) ? '' : ' AND ';
835
836 if ($val != '')
837 {
838 if ( ! $this->_has_operator($key))
839 {
840 $key .= ' =';
841 }
842
843 $val = ' '.$this->escape($val);
844 }
845
846 $dest[] = $prefix.$key.$val;
847 }
848 }
849
850 return $this->_update($this->dbprefix.$table, $fields, $dest);
admin6871d952006-10-04 00:36:18 +0000851 }
admin7b613c72006-09-24 18:05:17 +0000852
853 // --------------------------------------------------------------------
854
855 /**
856 * Enables a native PHP function to be run, using a platform agnostic wrapper.
857 *
858 * @access public
859 * @param string the function name
860 * @param mixed any parameters needed by the function
861 * @return mixed
862 */
863 function call_function($function)
864 {
865 $driver = ($this->dbdriver == 'postgre') ? 'pg_' : $this->dbdriver.'_';
866
867 if (FALSE === strpos($driver, $function))
868 {
869 $function = $driver.$function;
870 }
871
872 if ( ! function_exists($function))
873 {
874 if ($this->db_debug)
875 {
876 return $this->display_error('db_unsupported_function');
877 }
878 return FALSE;
879 }
880 else
881 {
adminee54c112006-09-28 17:13:38 +0000882 $args = (func_num_args() > 1) ? array_splice(func_get_args(), 1) : null;
883
admin7b613c72006-09-24 18:05:17 +0000884 return call_user_func_array($function, $args);
885 }
886 }
admine8f6eb62006-10-02 06:46:16 +0000887
888 // --------------------------------------------------------------------
889
890 /**
891 * Enable Query Caching
892 *
893 * @access public
894 * @return void
895 */
896 function cache_on()
897 {
adminc2e3cd72006-10-05 04:22:30 +0000898 return $this->cache_on = TRUE;
admine8f6eb62006-10-02 06:46:16 +0000899 }
900
901 // --------------------------------------------------------------------
902
903 /**
904 * Disable Query Caching
905 *
906 * @access public
907 * @return void
908 */
909 function cache_off()
910 {
adminc2e3cd72006-10-05 04:22:30 +0000911 return $this->cache_on = FALSE;
admine8f6eb62006-10-02 06:46:16 +0000912 }
adminfa708172006-10-04 18:42:42 +0000913
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 }
admin0c405652006-10-04 18:37:57 +0000927
admin6871d952006-10-04 00:36:18 +0000928 // --------------------------------------------------------------------
929
930 /**
admin0c405652006-10-04 18:37:57 +0000931 * Initialize the Cache Class
admin6871d952006-10-04 00:36:18 +0000932 *
admin0c405652006-10-04 18:37:57 +0000933 * @access private
934 * @return void
935 */
936 function _cache_init()
admin6871d952006-10-04 00:36:18 +0000937 {
adminc2e3cd72006-10-05 04:22:30 +0000938 if (is_object($this->CACHE) AND class_exists('CI_DB_Cache'))
admindb8a3742006-10-04 07:43:35 +0000939 {
admin0c405652006-10-04 18:37:57 +0000940 return TRUE;
admindb8a3742006-10-04 07:43:35 +0000941 }
942
adminfa708172006-10-04 18:42:42 +0000943 if ( ! @include_once(BASEPATH.'database/DB_cache'.EXT))
admin6871d952006-10-04 00:36:18 +0000944 {
adminfa708172006-10-04 18:42:42 +0000945 return $this->cache_off();
admin6871d952006-10-04 00:36:18 +0000946 }
947
adminc2e3cd72006-10-05 04:22:30 +0000948 $this->CACHE = new CI_DB_Cache;
admin6871d952006-10-04 00:36:18 +0000949 return TRUE;
950 }
951
admindb8a3742006-10-04 07:43:35 +0000952
953 // --------------------------------------------------------------------
954
955 /**
admin7b613c72006-09-24 18:05:17 +0000956 * Close DB Connection
957 *
958 * @access public
959 * @return void
960 */
admin6871d952006-10-04 00:36:18 +0000961 function close()
962 {
963 if (is_resource($this->conn_id))
964 {
965 $this->_close($this->conn_id);
admin7b613c72006-09-24 18:05:17 +0000966 }
967 $this->conn_id = FALSE;
admin6871d952006-10-04 00:36:18 +0000968 }
admin7b613c72006-09-24 18:05:17 +0000969
970 // --------------------------------------------------------------------
971
972 /**
973 * Display an error message
974 *
975 * @access public
976 * @param string the error message
977 * @param string any "swap" values
978 * @param boolean whether to localize the message
979 * @return string sends the application/errror_db.php template
980 */
admin6871d952006-10-04 00:36:18 +0000981 function display_error($error = '', $swap = '', $native = FALSE)
982 {
admin7b613c72006-09-24 18:05:17 +0000983 $LANG = new CI_Language();
984 $LANG->load('db');
985
986 $heading = 'MySQL Error';
987
988 if ($native == TRUE)
989 {
990 $message = $error;
991 }
992 else
993 {
994 $message = ( ! is_array($error)) ? array(str_replace('%s', $swap, $LANG->line($error))) : $error;
995 }
996
997 if ( ! class_exists('CI_Exceptions'))
998 {
999 include_once(BASEPATH.'libraries/Exceptions'.EXT);
1000 }
1001
1002 $error = new CI_Exceptions();
1003 echo $error->show_error('An Error Was Encountered', $message, 'error_db');
1004 exit;
1005
admin6871d952006-10-04 00:36:18 +00001006 }
admin7b613c72006-09-24 18:05:17 +00001007
1008}
1009
1010?>