blob: c5a215b827a3919718bb715f77ea2112a94280b2 [file] [log] [blame]
Timothy Warren80ab8162011-08-22 18:26:12 -04001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Timothy Warren018af7a2011-09-07 12:07:35 -040012 * @since Version 2.1.0
Timothy Warren80ab8162011-08-22 18:26:12 -040013 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
Timothy Warren02615962011-08-24 08:21:36 -040019 * PDO Database Adapter Class
Timothy Warren80ab8162011-08-22 18:26:12 -040020 *
21 * Note: _DB is an extender class that the app controller
22 * creates dynamically based on whether the active record
23 * class is being used or not.
24 *
25 * @package CodeIgniter
26 * @subpackage Drivers
27 * @category Database
28 * @author ExpressionEngine Dev Team
29 * @link http://codeigniter.com/user_guide/database/
30 */
31class CI_DB_pdo_driver extends CI_DB {
32
33 var $dbdriver = 'pdo';
34
35 // the character used to excape - not necessary for PDO
36 var $_escape_char = '';
Timothy Warrenc7ba6642011-09-14 12:25:14 -040037 var $_like_escape_str;
38 var $_like_escape_chr;
39
Timothy Warren80ab8162011-08-22 18:26:12 -040040
41 /**
42 * The syntax to count rows is slightly different across different
43 * database engines, so this string appears in each driver and is
44 * used for the count_all() and count_all_results() functions.
45 */
46 var $_count_string = "SELECT COUNT(*) AS ";
47 var $_random_keyword;
48
49
Timothy Warren51b0e642011-09-13 13:30:27 -040050 function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040051 {
52 parent::CI_DB($params);
Timothy Warrenab347582011-08-23 12:29:29 -040053
Timothy Warrenc7ba6642011-09-14 12:25:14 -040054 // clause and character used for LIKE escape sequences
55 if(strpos($this->hostname, 'mysql') !== FALSE)
56 {
57 $this->_like_escape_str = '';
58 $this->_like_escape_chr = '';
59 }
60 else if(strpos($this->hostname, 'odbc') !== FALSE)
61 {
62 $this->_like_escape_str = " {escape '%s'} ";
63 $this->_like_escape_chr = '!';
64 }
65 else
66 {
67 $this->_like_escape_str = " ESCAPE '%s' ";
68 $this->_like_escape_chr = '!';
69 }
70
Timothy Warrenab347582011-08-23 12:29:29 -040071 $this->hostname = $this->hostname . ";dbname=".$this->database;
72 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -040073
74 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
75 }
76
77 /**
78 * Non-persistent database connection
79 *
80 * @access private called by the base class
81 * @return resource
82 */
83 function db_connect()
84 {
Timothy Warrenab347582011-08-23 12:29:29 -040085 return new PDO($this->hostname,$this->username,$this->password, array(
86 PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT
Timothy Warren80ab8162011-08-22 18:26:12 -040087 ));
88 }
89
90 // --------------------------------------------------------------------
91
92 /**
93 * Persistent database connection
94 *
95 * @access private called by the base class
96 * @return resource
97 */
98 function db_pconnect()
99 {
Timothy Warrenab347582011-08-23 12:29:29 -0400100 return new PDO($this->hostname,$this->username,$this->password, array(
101 PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
102 PDO::ATTR_PERSISTENT => true
Timothy Warren80ab8162011-08-22 18:26:12 -0400103 ));
104 }
105
106 // --------------------------------------------------------------------
107
108 /**
109 * Reconnect
110 *
111 * Keep / reestablish the db connection if no queries have been
112 * sent for a length of time exceeding the server's idle timeout
113 *
114 * @access public
115 * @return void
116 */
117 function reconnect()
118 {
Timothy Warren02615962011-08-24 08:21:36 -0400119 if ($this->db->db_debug)
120 {
121 return $this->db->display_error('db_unsuported_feature');
122 }
123 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400124 }
125
126 // --------------------------------------------------------------------
127
128 /**
129 * Select the database
130 *
131 * @access private called by the base class
132 * @return resource
133 */
134 function db_select()
135 {
136 // Not needed for PDO
137 return TRUE;
138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Set client character set
144 *
145 * @access public
146 * @param string
147 * @param string
148 * @return resource
149 */
150 function db_set_charset($charset, $collation)
151 {
152 // @todo - add support if needed
153 return TRUE;
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Version number query string
160 *
161 * @access public
162 * @return string
163 */
164 function _version()
165 {
Timothy Warren36fb8de2011-08-24 08:29:05 -0400166 return $this->conn_id->getAttribute(PDO::ATTR_CLIENT_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Execute the query
173 *
174 * @access private called by the base class
175 * @param string an SQL query
Timothy Warren51a48882011-09-14 13:47:06 -0400176 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400177 */
178 function _execute($sql)
179 {
180 $sql = $this->_prep_query($sql);
Timothy Warren51a48882011-09-14 13:47:06 -0400181 $result_id = $this->conn_id->query($sql);
182
183 $this->affect_rows = $result_id->rowCount();
184
185 return $result_id;
Timothy Warren80ab8162011-08-22 18:26:12 -0400186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Prep the query
192 *
193 * If needed, each database adapter can prep the query string
194 *
195 * @access private called by execute()
196 * @param string an SQL query
197 * @return string
198 */
199 function _prep_query($sql)
200 {
201 return $sql;
202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Begin Transaction
208 *
209 * @access public
210 * @return bool
211 */
212 function trans_begin($test_mode = FALSE)
213 {
214 if ( ! $this->trans_enabled)
215 {
216 return TRUE;
217 }
218
219 // When transactions are nested we only begin/commit/rollback the outermost ones
220 if ($this->_trans_depth > 0)
221 {
222 return TRUE;
223 }
224
225 // Reset the transaction failure flag.
226 // If the $test_mode flag is set to TRUE transactions will be rolled back
227 // even if the queries produce a successful result.
228 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
229
Timothy Warrenab347582011-08-23 12:29:29 -0400230 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * Commit Transaction
237 *
238 * @access public
239 * @return bool
240 */
241 function trans_commit()
242 {
243 if ( ! $this->trans_enabled)
244 {
245 return TRUE;
246 }
247
248 // When transactions are nested we only begin/commit/rollback the outermost ones
249 if ($this->_trans_depth > 0)
250 {
251 return TRUE;
252 }
253
Timothy Warrenab347582011-08-23 12:29:29 -0400254 $ret = $this->conn->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400255 return $ret;
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Rollback Transaction
262 *
263 * @access public
264 * @return bool
265 */
266 function trans_rollback()
267 {
268 if ( ! $this->trans_enabled)
269 {
270 return TRUE;
271 }
272
273 // When transactions are nested we only begin/commit/rollback the outermost ones
274 if ($this->_trans_depth > 0)
275 {
276 return TRUE;
277 }
278
Timothy Warrenab347582011-08-23 12:29:29 -0400279 $ret = $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400280 return $ret;
281 }
282
283 // --------------------------------------------------------------------
284
285 /**
286 * Escape String
287 *
288 * @access public
289 * @param string
290 * @param bool whether or not the string will be used in a LIKE condition
291 * @return string
292 */
293 function escape_str($str, $like = FALSE)
294 {
295 if (is_array($str))
296 {
297 foreach ($str as $key => $val)
298 {
299 $str[$key] = $this->escape_str($val, $like);
300 }
301
302 return $str;
303 }
304
305 // PDO doesn't require escaping
306 $str = remove_invisible_characters($str);
307
308 // escape LIKE condition wildcards
309 if ($like === TRUE)
310 {
311 $str = str_replace( array('%', '_', $this->_like_escape_chr),
312 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
313 $str);
314 }
315
316 return $str;
317 }
318
319 // --------------------------------------------------------------------
320
321 /**
322 * Affected Rows
323 *
324 * @access public
325 * @return integer
326 */
327 function affected_rows()
328 {
Timothy Warren51a48882011-09-14 13:47:06 -0400329 return $this->affect_rows;
Timothy Warren80ab8162011-08-22 18:26:12 -0400330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Insert ID
Timothy Warren57cea512011-09-14 14:26:28 -0400336 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400337 * @access public
338 * @return integer
339 */
Timothy Warren51a48882011-09-14 13:47:06 -0400340 function insert_id($name=NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400341 {
Timothy Warren51a48882011-09-14 13:47:06 -0400342 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * "Count All" query
349 *
350 * Generates a platform-specific query string that counts all records in
351 * the specified database
352 *
353 * @access public
354 * @param string
355 * @return string
356 */
357 function count_all($table = '')
358 {
359 if ($table == '')
360 {
361 return 0;
362 }
363
364 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
365
366 if ($query->num_rows() == 0)
367 {
368 return 0;
369 }
370
371 $row = $query->row();
372 $this->_reset_select();
373 return (int) $row->numrows;
374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Show table query
380 *
381 * Generates a platform-specific query string so that the table names can be fetched
382 *
383 * @access private
384 * @param boolean
385 * @return string
386 */
387 function _list_tables($prefix_limit = FALSE)
388 {
389 $sql = "SHOW TABLES FROM `".$this->database."`";
390
391 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
392 {
393 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
394 return FALSE; // not currently supported
395 }
396
397 return $sql;
398 }
399
400 // --------------------------------------------------------------------
401
402 /**
403 * Show column query
404 *
405 * Generates a platform-specific query string so that the column names can be fetched
406 *
407 * @access public
408 * @param string the table name
409 * @return string
410 */
411 function _list_columns($table = '')
412 {
413 return "SHOW COLUMNS FROM ".$table;
414 }
415
416 // --------------------------------------------------------------------
417
418 /**
419 * Field data query
420 *
421 * Generates a platform-specific query so that the column data can be retrieved
422 *
423 * @access public
424 * @param string the table name
425 * @return object
426 */
427 function _field_data($table)
428 {
429 return "SELECT TOP 1 FROM ".$table;
430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * The error message string
436 *
437 * @access private
438 * @return string
439 */
440 function _error_message()
441 {
Timothy Warrenab347582011-08-23 12:29:29 -0400442 $error_array = $this->conn_id->errorInfo();
443 return $error_array[2];
Timothy Warren80ab8162011-08-22 18:26:12 -0400444 }
445
446 // --------------------------------------------------------------------
447
448 /**
449 * The error message number
450 *
451 * @access private
452 * @return integer
453 */
454 function _error_number()
455 {
Timothy Warrenab347582011-08-23 12:29:29 -0400456 return $this->conn_id->errorCode();
Timothy Warren80ab8162011-08-22 18:26:12 -0400457 }
458
459 // --------------------------------------------------------------------
460
461 /**
462 * Escape the SQL Identifiers
463 *
464 * This function escapes column and table names
465 *
466 * @access private
467 * @param string
468 * @return string
469 */
470 function _escape_identifiers($item)
471 {
472 if ($this->_escape_char == '')
473 {
474 return $item;
475 }
476
477 foreach ($this->_reserved_identifiers as $id)
478 {
479 if (strpos($item, '.'.$id) !== FALSE)
480 {
481 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
482
483 // remove duplicates if the user already included the escape
484 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
485 }
486 }
487
488 if (strpos($item, '.') !== FALSE)
489 {
490 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Timothy Warren018af7a2011-09-07 12:07:35 -0400491
Timothy Warren80ab8162011-08-22 18:26:12 -0400492 }
493 else
494 {
495 $str = $this->_escape_char.$item.$this->_escape_char;
496 }
497
498 // remove duplicates if the user already included the escape
499 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
505 * From Tables
506 *
507 * This function implicitly groups FROM tables so there is no confusion
508 * about operator precedence in harmony with SQL standards
509 *
510 * @access public
511 * @param type
512 * @return type
513 */
514 function _from_tables($tables)
515 {
516 if ( ! is_array($tables))
517 {
518 $tables = array($tables);
519 }
520
Timothy Warrenab347582011-08-23 12:29:29 -0400521 return (count($tables) == 1) ? $tables[0] : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400522 }
523
524 // --------------------------------------------------------------------
525
526 /**
527 * Insert statement
528 *
529 * Generates a platform-specific insert string from the supplied data
530 *
531 * @access public
532 * @param string the table name
533 * @param array the insert keys
534 * @param array the insert values
535 * @return string
536 */
537 function _insert($table, $keys, $values)
538 {
539 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
540 }
541
542 // --------------------------------------------------------------------
543
544 /**
545 * Update statement
546 *
547 * Generates a platform-specific update string from the supplied data
548 *
549 * @access public
550 * @param string the table name
551 * @param array the update data
552 * @param array the where clause
553 * @param array the orderby clause
554 * @param array the limit clause
555 * @return string
556 */
557 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
558 {
559 foreach ($values as $key => $val)
560 {
561 $valstr[] = $key." = ".$val;
562 }
563
564 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
565
566 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
567
568 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
569
570 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
571
572 $sql .= $orderby.$limit;
573
574 return $sql;
575 }
576
577
578 // --------------------------------------------------------------------
579
580 /**
581 * Truncate statement
582 *
583 * Generates a platform-specific truncate string from the supplied data
584 * If the database does not support the truncate() command
585 * This function maps to "DELETE FROM table"
586 *
587 * @access public
588 * @param string the table name
589 * @return string
590 */
591 function _truncate($table)
592 {
593 return $this->_delete($table);
594 }
595
596 // --------------------------------------------------------------------
597
598 /**
599 * Delete statement
600 *
601 * Generates a platform-specific delete string from the supplied data
602 *
603 * @access public
604 * @param string the table name
605 * @param array the where clause
606 * @param string the limit clause
607 * @return string
608 */
609 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
610 {
611 $conditions = '';
612
613 if (count($where) > 0 OR count($like) > 0)
614 {
615 $conditions = "\nWHERE ";
616 $conditions .= implode("\n", $this->ar_where);
617
618 if (count($where) > 0 && count($like) > 0)
619 {
620 $conditions .= " AND ";
621 }
622 $conditions .= implode("\n", $like);
623 }
624
625 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
626
627 return "DELETE FROM ".$table.$conditions.$limit;
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * Limit string
634 *
635 * Generates a platform-specific LIMIT clause
636 *
637 * @access public
638 * @param string the sql query string
639 * @param integer the number of rows to limit the query to
640 * @param integer the offset value
641 * @return string
642 */
643 function _limit($sql, $limit, $offset)
644 {
Timothy Warren5fc36d82011-09-16 12:31:37 -0400645 if(strpos($this->hostname, 'cubrid') !== FALSE || strpos($this->hostname, 'sqlite') !== FALSE)
Timothy Warren0a43ad82011-09-15 20:15:19 -0400646 {
647 if ($offset == 0)
648 {
649 $offset = '';
650 }
651 else
652 {
653 $offset .= ", ";
654 }
655
656 return $sql."LIMIT ".$offset.$limit;
657 }
658 else
659 {
660 $sql .= "LIMIT ".$limit;
661
662 if ($offset > 0)
663 {
664 $sql .= " OFFSET ".$offset;
665 }
666
667 return $sql;
668 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400669 }
670
671 // --------------------------------------------------------------------
672
673 /**
674 * Close DB Connection
675 *
676 * @access public
677 * @param resource
678 * @return void
679 */
680 function _close($conn_id)
681 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400682 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400683 }
684
685
686}
687
688
689
690/* End of file pdo_driver.php */
691/* Location: ./system/database/drivers/pdo/pdo_driver.php */