blob: 5991586c790a46615532898eb1a84e07b43a085a [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 *
Timothy Warrend1a5ba22011-10-21 04:45:28 -04007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Timothy Warren80ab8162011-08-22 18:26:12 -040019 * @package CodeIgniter
Timothy Warrend1a5ba22011-10-21 04:45:28 -040020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Timothy Warrend1a5ba22011-10-21 04:45:28 -040022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Timothy Warren80ab8162011-08-22 18:26:12 -040023 * @link http://codeigniter.com
Timothy Warren018af7a2011-09-07 12:07:35 -040024 * @since Version 2.1.0
Timothy Warren80ab8162011-08-22 18:26:12 -040025 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
Timothy Warren02615962011-08-24 08:21:36 -040031 * PDO Database Adapter Class
Timothy Warren80ab8162011-08-22 18:26:12 -040032 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Timothy Warrend1a5ba22011-10-21 04:45:28 -040040 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_pdo_driver extends CI_DB {
44
45 var $dbdriver = 'pdo';
46
47 // the character used to excape - not necessary for PDO
48 var $_escape_char = '';
Taufan Aditya18209332012-02-09 16:07:27 +070049
50 // clause and character used for LIKE escape sequences
Timothy Warrenc7ba6642011-09-14 12:25:14 -040051 var $_like_escape_str;
52 var $_like_escape_chr;
Timothy Warren80ab8162011-08-22 18:26:12 -040053
54 /**
55 * The syntax to count rows is slightly different across different
56 * database engines, so this string appears in each driver and is
57 * used for the count_all() and count_all_results() functions.
58 */
59 var $_count_string = "SELECT COUNT(*) AS ";
60 var $_random_keyword;
Taufan Aditya18209332012-02-09 16:07:27 +070061
62 // need to track the pdo DSN, driver and options
63 var $dsn;
64 var $pdodriver;
Timothy Warren7aae3682011-10-25 10:37:31 -040065 var $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040066
Timothy Warren51b0e642011-09-13 13:30:27 -040067 function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040068 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040069 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070070
71 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) == 2)
72 {
73 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070074 // This is for general PDO users, who tend to have a full DSN string.
Taufan Aditya18209332012-02-09 16:07:27 +070075 $this->pdodriver = end($match);
76 }
77 else
78 {
79 // Try to build a complete DSN string from params
80 $this->_connect_string($params);
81 }
82
Timothy Warrenc7ba6642011-09-14 12:25:14 -040083 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070084 // this one depends on the driver being used
85 if ($this->pdodriver == 'mysql')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040086 {
87 $this->_like_escape_str = '';
88 $this->_like_escape_chr = '';
89 }
Taufan Aditya18209332012-02-09 16:07:27 +070090 elseif ($this->pdodriver == 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040091 {
92 $this->_like_escape_str = " {escape '%s'} ";
93 $this->_like_escape_chr = '!';
94 }
95 else
96 {
97 $this->_like_escape_str = " ESCAPE '%s' ";
98 $this->_like_escape_chr = '!';
99 }
100
Taufan Aditya18209332012-02-09 16:07:27 +0700101 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400102 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
103 }
104
105 /**
Taufan Aditya18209332012-02-09 16:07:27 +0700106 * Connection String
107 *
108 * @access private
109 * @param array
110 * @return void
111 */
112 function _connect_string($params)
113 {
114 if (strpos($this->hostname, ':'))
115 {
116 // hostname generally would have this prototype
117 // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);';
118 // We need to get the prefix (pdodriver used by PDO).
119 $this->dsn = $this->hostname;
Taufan Aditya9c2947a2012-02-13 04:04:56 +0700120 $this->pdodriver = current(explode(':', $this->hostname));
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700121
122 // End dsn with a semicolon for extra backward compability
123 // if database property was not empty.
124 if ( ! empty($this->database))
125 {
126 $this->dsn .= rtrim($this->dsn, ';').';';
127 }
Taufan Aditya18209332012-02-09 16:07:27 +0700128 }
129 else
130 {
131 // Invalid DSN, display an error
132 if ( ! array_key_exists('pdodriver', $params))
133 {
134 show_error('Invalid DB Connection String for PDO');
135 }
136
137 // Assuming that the following DSN string format is used:
138 // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql';
139 $this->dsn = $this->pdodriver.':';
140
141 // Add hostname to the DSN for databases that need it
142 if ( ! empty($this->hostname) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid')))
143 {
144 $this->dsn .= 'host='.$this->hostname.';';
145 }
146
147 // Add a port to the DSN for databases that can use it
148 if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid')))
149 {
150 $this->dsn .= 'port='.$this->port.';';
151 }
152 }
153
154 // Add the database name to the DSN, if needed
155 if (stripos($this->dsn, 'dbname') === FALSE
156 && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
157 {
158 $this->dsn .= 'dbname='.$this->database.';';
159 }
160 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv')))
161 {
162 if (stripos($this->dsn, 'dsn') === FALSE)
163 {
164 $this->dsn .= 'database='.$this->database.';';
165 }
166 }
167 elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:')
168 {
169 if ($this->database !== ':memory')
170 {
171 if ( ! file_exists($this->database))
172 {
173 show_error('Invalid DB Connection string for PDO SQLite');
174 }
175
176 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
177 }
178
179 $this->dsn .= $this->database;
180 }
181
182 // Add charset to the DSN, if needed
183 if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
184 {
185 $this->dsn .= 'charset='.$this->char_set.';';
186 }
187 }
188
189 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400190 * Non-persistent database connection
191 *
192 * @access private called by the base class
193 * @return resource
194 */
195 function db_connect()
196 {
Taufan Aditya18209332012-02-09 16:07:27 +0700197 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
198
199 return $this->pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Persistent database connection
206 *
207 * @access private called by the base class
208 * @return resource
209 */
210 function db_pconnect()
211 {
Taufan Aditya18209332012-02-09 16:07:27 +0700212 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
213 $this->options[PDO::ATTR_PERSISTENT] = TRUE;
Timothy Warrend93393f2011-10-26 11:31:49 -0400214
Taufan Aditya18209332012-02-09 16:07:27 +0700215 return $this->pdo_connect();
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * PDO connection
222 *
223 * @access private called by the PDO driver class
224 * @return resource
225 */
226 function pdo_connect()
227 {
Taufan Aditya62b8cae2012-02-09 16:40:39 +0700228 // Refer : http://php.net/manual/en/ref.pdo-mysql.connection.php
Taufan Aditya18209332012-02-09 16:07:27 +0700229 if ($this->pdodriver == 'mysql' && is_php('5.3.6'))
230 {
Taufan Aditya55bb97e2012-02-09 16:43:26 +0700231 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES $this->char_set COLLATE '$this->dbcollat'";
Taufan Aditya18209332012-02-09 16:07:27 +0700232 }
233
234 // Connecting...
235 try
236 {
237 $db = new PDO($this->dsn, $this->username, $this->password, $this->options);
238 }
239 catch (PDOException $e)
240 {
241 if ($this->db_debug && empty($this->failover))
242 {
243 $this->display_error($e->getMessage(), '', TRUE);
244 }
245
246 return FALSE;
247 }
248
249 return $db;
Timothy Warren80ab8162011-08-22 18:26:12 -0400250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Reconnect
256 *
257 * Keep / reestablish the db connection if no queries have been
258 * sent for a length of time exceeding the server's idle timeout
259 *
260 * @access public
261 * @return void
262 */
263 function reconnect()
264 {
Timothy Warren02615962011-08-24 08:21:36 -0400265 if ($this->db->db_debug)
266 {
267 return $this->db->display_error('db_unsuported_feature');
268 }
Taufan Aditya18209332012-02-09 16:07:27 +0700269
Timothy Warren02615962011-08-24 08:21:36 -0400270 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Select the database
277 *
278 * @access private called by the base class
279 * @return resource
280 */
281 function db_select()
282 {
283 // Not needed for PDO
284 return TRUE;
285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * Set client character set
291 *
292 * @access public
293 * @param string
294 * @param string
295 * @return resource
296 */
297 function db_set_charset($charset, $collation)
298 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400299 return TRUE;
300 }
301
302 // --------------------------------------------------------------------
303
304 /**
305 * Version number query string
306 *
307 * @access public
308 * @return string
309 */
310 function _version()
311 {
Timothy Warren36fb8de2011-08-24 08:29:05 -0400312 return $this->conn_id->getAttribute(PDO::ATTR_CLIENT_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * Execute the query
319 *
320 * @access private called by the base class
321 * @param string an SQL query
Timothy Warren51a48882011-09-14 13:47:06 -0400322 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400323 */
324 function _execute($sql)
325 {
326 $sql = $this->_prep_query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700327
Timothy Warren51a48882011-09-14 13:47:06 -0400328 $result_id = $this->conn_id->query($sql);
329
Timothy Warrend6691532011-10-07 10:03:01 -0400330 if (is_object($result_id))
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400331 {
332 $this->affect_rows = $result_id->rowCount();
333 }
334 else
335 {
336 $this->affect_rows = 0;
337 }
Timothy Warren51a48882011-09-14 13:47:06 -0400338
339 return $result_id;
Timothy Warren80ab8162011-08-22 18:26:12 -0400340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Prep the query
346 *
347 * If needed, each database adapter can prep the query string
348 *
349 * @access private called by execute()
350 * @param string an SQL query
351 * @return string
352 */
353 function _prep_query($sql)
354 {
Taufan Aditya18209332012-02-09 16:07:27 +0700355 if ($this->pdodriver === 'pgsql')
356 {
357 // Change the backtick(s) for Postgre
358 $sql = str_replace('`', '"', $sql);
359 }
360 elseif ($this->pdodriver === 'sqlite')
361 {
362 // Change the backtick(s) for SQLite
363 $sql = str_replace('`', '', $sql);
364 }
365
Timothy Warren80ab8162011-08-22 18:26:12 -0400366 return $sql;
367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Begin Transaction
373 *
374 * @access public
375 * @return bool
376 */
377 function trans_begin($test_mode = FALSE)
378 {
379 if ( ! $this->trans_enabled)
380 {
381 return TRUE;
382 }
383
384 // When transactions are nested we only begin/commit/rollback the outermost ones
385 if ($this->_trans_depth > 0)
386 {
387 return TRUE;
388 }
389
390 // Reset the transaction failure flag.
391 // If the $test_mode flag is set to TRUE transactions will be rolled back
392 // even if the queries produce a successful result.
Timothy Warrend019fd62011-10-26 11:26:17 -0400393 $this->_trans_failure = (bool) ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400394
Timothy Warrenab347582011-08-23 12:29:29 -0400395 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Commit Transaction
402 *
403 * @access public
404 * @return bool
405 */
406 function trans_commit()
407 {
408 if ( ! $this->trans_enabled)
409 {
410 return TRUE;
411 }
412
413 // When transactions are nested we only begin/commit/rollback the outermost ones
414 if ($this->_trans_depth > 0)
415 {
416 return TRUE;
417 }
418
Timothy Warrenab347582011-08-23 12:29:29 -0400419 $ret = $this->conn->commit();
Taufan Aditya18209332012-02-09 16:07:27 +0700420
Timothy Warren80ab8162011-08-22 18:26:12 -0400421 return $ret;
422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Rollback Transaction
428 *
429 * @access public
430 * @return bool
431 */
432 function trans_rollback()
433 {
434 if ( ! $this->trans_enabled)
435 {
436 return TRUE;
437 }
438
439 // When transactions are nested we only begin/commit/rollback the outermost ones
440 if ($this->_trans_depth > 0)
441 {
442 return TRUE;
443 }
444
Timothy Warrenab347582011-08-23 12:29:29 -0400445 $ret = $this->conn_id->rollBack();
Taufan Aditya18209332012-02-09 16:07:27 +0700446
Timothy Warren80ab8162011-08-22 18:26:12 -0400447 return $ret;
448 }
449
450 // --------------------------------------------------------------------
451
452 /**
453 * Escape String
454 *
455 * @access public
456 * @param string
457 * @param bool whether or not the string will be used in a LIKE condition
458 * @return string
459 */
460 function escape_str($str, $like = FALSE)
461 {
462 if (is_array($str))
463 {
464 foreach ($str as $key => $val)
465 {
466 $str[$key] = $this->escape_str($val, $like);
467 }
468
469 return $str;
470 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400471
Timothy Warren47663972011-10-05 16:44:50 -0400472 //Escape the string
473 $str = $this->conn_id->quote($str);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400474
Timothy Warren47663972011-10-05 16:44:50 -0400475 //If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400476 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400477 {
478 $str = substr($str, 1, -1);
479 }
480
Timothy Warren80ab8162011-08-22 18:26:12 -0400481 // escape LIKE condition wildcards
482 if ($like === TRUE)
483 {
484 $str = str_replace( array('%', '_', $this->_like_escape_chr),
Taufan Aditya18209332012-02-09 16:07:27 +0700485 array($this->_like_escape_chr.'%',
486 $this->_like_escape_chr.'_',
487 $this->_like_escape_chr.$this->_like_escape_chr),
Timothy Warren80ab8162011-08-22 18:26:12 -0400488 $str);
489 }
490
491 return $str;
492 }
493
494 // --------------------------------------------------------------------
495
496 /**
497 * Affected Rows
498 *
499 * @access public
500 * @return integer
501 */
502 function affected_rows()
503 {
Timothy Warren51a48882011-09-14 13:47:06 -0400504 return $this->affect_rows;
Timothy Warren80ab8162011-08-22 18:26:12 -0400505 }
506
507 // --------------------------------------------------------------------
508
509 /**
510 * Insert ID
Timothy Warren57cea512011-09-14 14:26:28 -0400511 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400512 * @access public
513 * @return integer
514 */
Timothy Warren51a48882011-09-14 13:47:06 -0400515 function insert_id($name=NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400516 {
Taufan Aditya18209332012-02-09 16:07:27 +0700517 if ($this->pdodriver == 'pgsql')
Timothy Warren33512752011-10-07 09:51:49 -0400518 {
Taufan Aditya18209332012-02-09 16:07:27 +0700519 //Convenience method for postgres insertid
Timothy Warren33512752011-10-07 09:51:49 -0400520 $v = $this->_version();
521
522 $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
523
524 if ($table == NULL && $v >= '8.1')
525 {
526 $sql='SELECT LASTVAL() as ins_id';
527 }
Taufan Aditya18209332012-02-09 16:07:27 +0700528
Timothy Warren33512752011-10-07 09:51:49 -0400529 $query = $this->query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700530 $row = $query->row();
531
Timothy Warren33512752011-10-07 09:51:49 -0400532 return $row->ins_id;
533 }
534 else
535 {
536 return $this->conn_id->lastInsertId($name);
537 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400538 }
539
540 // --------------------------------------------------------------------
541
542 /**
543 * "Count All" query
544 *
545 * Generates a platform-specific query string that counts all records in
546 * the specified database
547 *
548 * @access public
549 * @param string
550 * @return string
551 */
552 function count_all($table = '')
553 {
554 if ($table == '')
555 {
556 return 0;
557 }
558
Taufan Aditya18209332012-02-09 16:07:27 +0700559 $sql = $this->_count_string.$this->_protect_identifiers('numrows').' FROM ';
560 $sql .= $this->_protect_identifiers($table, TRUE, NULL, FALSE);
561 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400562
563 if ($query->num_rows() == 0)
564 {
565 return 0;
566 }
567
568 $row = $query->row();
569 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700570
Timothy Warren80ab8162011-08-22 18:26:12 -0400571 return (int) $row->numrows;
572 }
573
574 // --------------------------------------------------------------------
575
576 /**
577 * Show table query
578 *
579 * Generates a platform-specific query string so that the table names can be fetched
580 *
581 * @access private
582 * @param boolean
583 * @return string
584 */
585 function _list_tables($prefix_limit = FALSE)
586 {
Taufan Aditya18209332012-02-09 16:07:27 +0700587 if ($this->pdodriver == 'pgsql')
588 {
589 // Analog function to show all tables in postgre
590 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
591 }
592 else
593 {
594 $sql = "SHOW TABLES FROM `".$this->database."`";
595 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400596
597 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
598 {
Taufan Aditya18209332012-02-09 16:07:27 +0700599 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400600 }
601
602 return $sql;
603 }
604
605 // --------------------------------------------------------------------
606
607 /**
608 * Show column query
609 *
610 * Generates a platform-specific query string so that the column names can be fetched
611 *
612 * @access public
613 * @param string the table name
614 * @return string
615 */
616 function _list_columns($table = '')
617 {
Taufan Aditya18209332012-02-09 16:07:27 +0700618 return 'SHOW COLUMNS FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400619 }
620
621 // --------------------------------------------------------------------
622
623 /**
624 * Field data query
625 *
626 * Generates a platform-specific query so that the column data can be retrieved
627 *
628 * @access public
629 * @param string the table name
630 * @return object
631 */
632 function _field_data($table)
633 {
Taufan Aditya18209332012-02-09 16:07:27 +0700634 return 'SELECT TOP 1 FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400635 }
636
637 // --------------------------------------------------------------------
638
639 /**
640 * The error message string
641 *
642 * @access private
643 * @return string
644 */
645 function _error_message()
646 {
Timothy Warrenab347582011-08-23 12:29:29 -0400647 $error_array = $this->conn_id->errorInfo();
Taufan Aditya18209332012-02-09 16:07:27 +0700648
Timothy Warrenab347582011-08-23 12:29:29 -0400649 return $error_array[2];
Timothy Warren80ab8162011-08-22 18:26:12 -0400650 }
651
652 // --------------------------------------------------------------------
653
654 /**
655 * The error message number
656 *
657 * @access private
658 * @return integer
659 */
660 function _error_number()
661 {
Timothy Warrenab347582011-08-23 12:29:29 -0400662 return $this->conn_id->errorCode();
Timothy Warren80ab8162011-08-22 18:26:12 -0400663 }
664
665 // --------------------------------------------------------------------
666
667 /**
668 * Escape the SQL Identifiers
669 *
670 * This function escapes column and table names
671 *
672 * @access private
673 * @param string
674 * @return string
675 */
676 function _escape_identifiers($item)
677 {
678 if ($this->_escape_char == '')
679 {
680 return $item;
681 }
682
683 foreach ($this->_reserved_identifiers as $id)
684 {
685 if (strpos($item, '.'.$id) !== FALSE)
686 {
687 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
688
689 // remove duplicates if the user already included the escape
690 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
691 }
692 }
693
694 if (strpos($item, '.') !== FALSE)
695 {
Taufan Aditya18209332012-02-09 16:07:27 +0700696 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
697 $str .= $this->_escape_char;
Timothy Warren80ab8162011-08-22 18:26:12 -0400698 }
699 else
700 {
701 $str = $this->_escape_char.$item.$this->_escape_char;
702 }
703
704 // remove duplicates if the user already included the escape
705 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
706 }
707
708 // --------------------------------------------------------------------
709
710 /**
711 * From Tables
712 *
713 * This function implicitly groups FROM tables so there is no confusion
714 * about operator precedence in harmony with SQL standards
715 *
716 * @access public
717 * @param type
718 * @return type
719 */
720 function _from_tables($tables)
721 {
722 if ( ! is_array($tables))
723 {
724 $tables = array($tables);
725 }
726
Taufan Aditya18209332012-02-09 16:07:27 +0700727 return (count($tables) == 1) ? '`'.$tables[0].'`' : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400728 }
729
730 // --------------------------------------------------------------------
731
732 /**
733 * Insert statement
734 *
735 * Generates a platform-specific insert string from the supplied data
736 *
737 * @access public
738 * @param string the table name
739 * @param array the insert keys
740 * @param array the insert values
741 * @return string
742 */
743 function _insert($table, $keys, $values)
744 {
Taufan Aditya18209332012-02-09 16:07:27 +0700745 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400746 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400747
748 // --------------------------------------------------------------------
749
750 /**
751 * Insert_batch statement
752 *
753 * Generates a platform-specific insert string from the supplied data
754 *
755 * @access public
756 * @param string the table name
757 * @param array the insert keys
758 * @param array the insert values
759 * @return string
760 */
761 function _insert_batch($table, $keys, $values)
762 {
Taufan Aditya18209332012-02-09 16:07:27 +0700763 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400764 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400765
766 // --------------------------------------------------------------------
767
768 /**
769 * Update statement
770 *
771 * Generates a platform-specific update string from the supplied data
772 *
773 * @access public
774 * @param string the table name
775 * @param array the update data
776 * @param array the where clause
777 * @param array the orderby clause
778 * @param array the limit clause
779 * @return string
780 */
781 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
782 {
783 foreach ($values as $key => $val)
784 {
785 $valstr[] = $key." = ".$val;
786 }
787
Taufan Aditya18209332012-02-09 16:07:27 +0700788 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
789 $orderby = (count($orderby) >= 1) ? ' ORDER BY '.implode(', ', $orderby) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400790
Taufan Aditya18209332012-02-09 16:07:27 +0700791 $sql = 'UPDATE '.$this->_from_tables($table).' SET '.implode(', ', $valstr);
792 $sql .= ($where != '' && count($where) >= 1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400793 $sql .= $orderby.$limit;
794
795 return $sql;
796 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400797
798 // --------------------------------------------------------------------
799
800 /**
801 * Update_Batch statement
802 *
803 * Generates a platform-specific batch update string from the supplied data
804 *
805 * @access public
806 * @param string the table name
807 * @param array the update data
808 * @param array the where clause
809 * @return string
810 */
811 function _update_batch($table, $values, $index, $where = NULL)
812 {
Taufan Aditya18209332012-02-09 16:07:27 +0700813 $ids = array();
814 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400815
816 foreach ($values as $key => $val)
817 {
818 $ids[] = $val[$index];
819
820 foreach (array_keys($val) as $field)
821 {
822 if ($field != $index)
823 {
824 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
825 }
826 }
827 }
828
Taufan Aditya18209332012-02-09 16:07:27 +0700829 $sql = 'UPDATE '.$this->_from_tables($table).' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400830 $cases = '';
831
832 foreach ($final as $k => $v)
833 {
834 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700835
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400836 foreach ($v as $row)
837 {
838 $cases .= $row."\n";
839 }
840
841 $cases .= 'ELSE '.$k.' END, ';
842 }
843
844 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400845 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
846
847 return $sql;
848 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400849
850
851 // --------------------------------------------------------------------
852
853 /**
854 * Truncate statement
855 *
856 * Generates a platform-specific truncate string from the supplied data
857 * If the database does not support the truncate() command
858 * This function maps to "DELETE FROM table"
859 *
860 * @access public
861 * @param string the table name
862 * @return string
863 */
864 function _truncate($table)
865 {
866 return $this->_delete($table);
867 }
868
869 // --------------------------------------------------------------------
870
871 /**
872 * Delete statement
873 *
874 * Generates a platform-specific delete string from the supplied data
875 *
876 * @access public
877 * @param string the table name
878 * @param array the where clause
879 * @param string the limit clause
880 * @return string
881 */
882 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
883 {
884 $conditions = '';
885
886 if (count($where) > 0 OR count($like) > 0)
887 {
Taufan Aditya18209332012-02-09 16:07:27 +0700888 $conditions = "\nWHERE ";
Timothy Warren80ab8162011-08-22 18:26:12 -0400889 $conditions .= implode("\n", $this->ar_where);
890
891 if (count($where) > 0 && count($like) > 0)
892 {
893 $conditions .= " AND ";
894 }
Taufan Aditya18209332012-02-09 16:07:27 +0700895
Timothy Warren80ab8162011-08-22 18:26:12 -0400896 $conditions .= implode("\n", $like);
897 }
898
899 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
900
Taufan Aditya18209332012-02-09 16:07:27 +0700901 return 'DELETE FROM '.$this->_from_tables($table).$conditions.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400902 }
903
904 // --------------------------------------------------------------------
905
906 /**
907 * Limit string
908 *
909 * Generates a platform-specific LIMIT clause
910 *
911 * @access public
912 * @param string the sql query string
913 * @param integer the number of rows to limit the query to
914 * @param integer the offset value
915 * @return string
916 */
917 function _limit($sql, $limit, $offset)
918 {
Taufan Aditya18209332012-02-09 16:07:27 +0700919 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400920 {
Taufan Aditya18209332012-02-09 16:07:27 +0700921 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400922
Taufan Aditya18209332012-02-09 16:07:27 +0700923 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400924 }
925 else
926 {
Taufan Aditya18209332012-02-09 16:07:27 +0700927 $sql .= 'LIMIT '.$limit;
928 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400929
930 return $sql;
931 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400932 }
933
934 // --------------------------------------------------------------------
935
936 /**
937 * Close DB Connection
938 *
939 * @access public
940 * @param resource
941 * @return void
942 */
943 function _close($conn_id)
944 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400945 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400946 }
947
Timothy Warren80ab8162011-08-22 18:26:12 -0400948}
949
Timothy Warren80ab8162011-08-22 18:26:12 -0400950/* End of file pdo_driver.php */
951/* Location: ./system/database/drivers/pdo/pdo_driver.php */