blob: b0bd7075f05226d6b55d3625434fcd5da742ec4a [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
176 * @return resource
177 */
178 function _execute($sql)
179 {
180 $sql = $this->_prep_query($sql);
Timothy Warrenab347582011-08-23 12:29:29 -0400181 return $this->conn_id->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Prep the query
188 *
189 * If needed, each database adapter can prep the query string
190 *
191 * @access private called by execute()
192 * @param string an SQL query
193 * @return string
194 */
195 function _prep_query($sql)
196 {
197 return $sql;
198 }
199
200 // --------------------------------------------------------------------
201
202 /**
203 * Begin Transaction
204 *
205 * @access public
206 * @return bool
207 */
208 function trans_begin($test_mode = FALSE)
209 {
210 if ( ! $this->trans_enabled)
211 {
212 return TRUE;
213 }
214
215 // When transactions are nested we only begin/commit/rollback the outermost ones
216 if ($this->_trans_depth > 0)
217 {
218 return TRUE;
219 }
220
221 // Reset the transaction failure flag.
222 // If the $test_mode flag is set to TRUE transactions will be rolled back
223 // even if the queries produce a successful result.
224 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
225
Timothy Warrenab347582011-08-23 12:29:29 -0400226 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Commit Transaction
233 *
234 * @access public
235 * @return bool
236 */
237 function trans_commit()
238 {
239 if ( ! $this->trans_enabled)
240 {
241 return TRUE;
242 }
243
244 // When transactions are nested we only begin/commit/rollback the outermost ones
245 if ($this->_trans_depth > 0)
246 {
247 return TRUE;
248 }
249
Timothy Warrenab347582011-08-23 12:29:29 -0400250 $ret = $this->conn->commit();
Timothy Warren80ab8162011-08-22 18:26:12 -0400251 return $ret;
252 }
253
254 // --------------------------------------------------------------------
255
256 /**
257 * Rollback Transaction
258 *
259 * @access public
260 * @return bool
261 */
262 function trans_rollback()
263 {
264 if ( ! $this->trans_enabled)
265 {
266 return TRUE;
267 }
268
269 // When transactions are nested we only begin/commit/rollback the outermost ones
270 if ($this->_trans_depth > 0)
271 {
272 return TRUE;
273 }
274
Timothy Warrenab347582011-08-23 12:29:29 -0400275 $ret = $this->conn_id->rollBack();
Timothy Warren80ab8162011-08-22 18:26:12 -0400276 return $ret;
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Escape String
283 *
284 * @access public
285 * @param string
286 * @param bool whether or not the string will be used in a LIKE condition
287 * @return string
288 */
289 function escape_str($str, $like = FALSE)
290 {
291 if (is_array($str))
292 {
293 foreach ($str as $key => $val)
294 {
295 $str[$key] = $this->escape_str($val, $like);
296 }
297
298 return $str;
299 }
300
301 // PDO doesn't require escaping
302 $str = remove_invisible_characters($str);
303
304 // escape LIKE condition wildcards
305 if ($like === TRUE)
306 {
307 $str = str_replace( array('%', '_', $this->_like_escape_chr),
308 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
309 $str);
310 }
311
312 return $str;
313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Affected Rows
319 *
320 * @access public
321 * @return integer
322 */
323 function affected_rows()
324 {
Timothy Warrenc7ba6642011-09-14 12:25:14 -0400325 if ($this->db_debug)
Timothy Warrena6c65332011-09-14 12:03:27 -0400326 {
Timothy Warrenc7ba6642011-09-14 12:25:14 -0400327 return $this->display_error('db_unsuported_feature');
Timothy Warrena6c65332011-09-14 12:03:27 -0400328 }
329 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400330 }
331
332 // --------------------------------------------------------------------
333
334 /**
335 * Insert ID
336 *
337 * @access public
338 * @return integer
339 */
340 function insert_id()
341 {
Timothy Warrenab347582011-08-23 12:29:29 -0400342 return $this->conn_id->lastInsertId();
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 {
645 // Does PDO doesn't use the LIMIT clause?
646 return $sql;
647 }
648
649 // --------------------------------------------------------------------
650
651 /**
652 * Close DB Connection
653 *
654 * @access public
655 * @param resource
656 * @return void
657 */
658 function _close($conn_id)
659 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400660 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400661 }
662
663
664}
665
666
667
668/* End of file pdo_driver.php */
669/* Location: ./system/database/drivers/pdo/pdo_driver.php */