blob: c8732ac3c24b2e553e96901aa6b56244d506c7a7 [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
Andrey Andreev738f5342012-03-06 20:43:40 +020062 // need to track the pdo driver and options
Taufan Aditya18209332012-02-09 16:07:27 +070063 var $pdodriver;
Timothy Warren7aae3682011-10-25 10:37:31 -040064 var $options = array();
Timothy Warren80ab8162011-08-22 18:26:12 -040065
Timothy Warren51b0e642011-09-13 13:30:27 -040066 function __construct($params)
Timothy Warren80ab8162011-08-22 18:26:12 -040067 {
Timothy Warrenb5a43b02011-10-04 17:26:04 -040068 parent::__construct($params);
Taufan Aditya18209332012-02-09 16:07:27 +070069
70 if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) == 2)
71 {
72 // If there is a minimum valid dsn string pattern found, we're done
Taufan Adityafdd6ad02012-02-10 13:10:27 +070073 // This is for general PDO users, who tend to have a full DSN string.
Taufan Aditya18209332012-02-09 16:07:27 +070074 $this->pdodriver = end($match);
75 }
76 else
77 {
78 // Try to build a complete DSN string from params
79 $this->_connect_string($params);
80 }
81
Timothy Warrenc7ba6642011-09-14 12:25:14 -040082 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070083 // this one depends on the driver being used
84 if ($this->pdodriver == 'mysql')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040085 {
86 $this->_like_escape_str = '';
87 $this->_like_escape_chr = '';
88 }
Taufan Aditya18209332012-02-09 16:07:27 +070089 elseif ($this->pdodriver == 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040090 {
91 $this->_like_escape_str = " {escape '%s'} ";
92 $this->_like_escape_chr = '!';
93 }
94 else
95 {
96 $this->_like_escape_str = " ESCAPE '%s' ";
97 $this->_like_escape_chr = '!';
98 }
99
Taufan Aditya18209332012-02-09 16:07:27 +0700100 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400101 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
102 }
103
104 /**
Taufan Aditya18209332012-02-09 16:07:27 +0700105 * Connection String
106 *
107 * @access private
108 * @param array
109 * @return void
110 */
111 function _connect_string($params)
112 {
113 if (strpos($this->hostname, ':'))
114 {
115 // hostname generally would have this prototype
116 // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);';
117 // We need to get the prefix (pdodriver used by PDO).
Timothy Warren928d83c2012-02-13 14:07:57 -0500118 $dsnarray = explode(':', $this->hostname);
119 $this->pdodriver = $dsnarray[0];
Taufan Aditya5dcdbc32012-02-13 21:15:56 +0700120
121 // End dsn with a semicolon for extra backward compability
122 // if database property was not empty.
Timothy Warrenf4524692012-02-13 14:13:28 -0500123 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500124 {
125 $this->dsn .= rtrim($this->hostname, ';').';';
126 }
Taufan Aditya18209332012-02-09 16:07:27 +0700127 }
128 else
129 {
130 // Invalid DSN, display an error
131 if ( ! array_key_exists('pdodriver', $params))
132 {
133 show_error('Invalid DB Connection String for PDO');
134 }
135
136 // Assuming that the following DSN string format is used:
137 // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql';
138 $this->dsn = $this->pdodriver.':';
139
140 // Add hostname to the DSN for databases that need it
Timothy Warren928d83c2012-02-13 14:07:57 -0500141 if ( ! empty($this->hostname)
142 && strpos($this->hostname, ':') === FALSE
143 && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700144 {
145 $this->dsn .= 'host='.$this->hostname.';';
146 }
147
148 // Add a port to the DSN for databases that can use it
149 if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid')))
150 {
151 $this->dsn .= 'port='.$this->port.';';
152 }
153 }
154
155 // Add the database name to the DSN, if needed
156 if (stripos($this->dsn, 'dbname') === FALSE
157 && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
158 {
159 $this->dsn .= 'dbname='.$this->database.';';
160 }
161 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv')))
162 {
163 if (stripos($this->dsn, 'dsn') === FALSE)
164 {
165 $this->dsn .= 'database='.$this->database.';';
166 }
167 }
168 elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:')
169 {
170 if ($this->database !== ':memory')
171 {
172 if ( ! file_exists($this->database))
173 {
174 show_error('Invalid DB Connection string for PDO SQLite');
175 }
176
177 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
178 }
179
180 $this->dsn .= $this->database;
181 }
182
183 // Add charset to the DSN, if needed
184 if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
185 {
186 $this->dsn .= 'charset='.$this->char_set.';';
187 }
188 }
189
190 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400191 * Non-persistent database connection
192 *
193 * @access private called by the base class
194 * @return resource
195 */
196 function db_connect()
197 {
Taufan Aditya18209332012-02-09 16:07:27 +0700198 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
199
200 return $this->pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Persistent database connection
207 *
208 * @access private called by the base class
209 * @return resource
210 */
211 function db_pconnect()
212 {
Taufan Aditya18209332012-02-09 16:07:27 +0700213 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
214 $this->options[PDO::ATTR_PERSISTENT] = TRUE;
Timothy Warrend93393f2011-10-26 11:31:49 -0400215
Taufan Aditya18209332012-02-09 16:07:27 +0700216 return $this->pdo_connect();
217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * PDO connection
223 *
224 * @access private called by the PDO driver class
225 * @return resource
226 */
227 function pdo_connect()
228 {
Taufan Aditya62b8cae2012-02-09 16:40:39 +0700229 // Refer : http://php.net/manual/en/ref.pdo-mysql.connection.php
Taufan Aditya18209332012-02-09 16:07:27 +0700230 if ($this->pdodriver == 'mysql' && is_php('5.3.6'))
231 {
Taufan Aditya55bb97e2012-02-09 16:43:26 +0700232 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES $this->char_set COLLATE '$this->dbcollat'";
Taufan Aditya18209332012-02-09 16:07:27 +0700233 }
234
235 // Connecting...
236 try
237 {
238 $db = new PDO($this->dsn, $this->username, $this->password, $this->options);
239 }
240 catch (PDOException $e)
241 {
242 if ($this->db_debug && empty($this->failover))
243 {
244 $this->display_error($e->getMessage(), '', TRUE);
245 }
246
247 return FALSE;
248 }
249
250 return $db;
Timothy Warren80ab8162011-08-22 18:26:12 -0400251 }
252
253 // --------------------------------------------------------------------
254
255 /**
256 * Reconnect
257 *
258 * Keep / reestablish the db connection if no queries have been
259 * sent for a length of time exceeding the server's idle timeout
260 *
261 * @access public
262 * @return void
263 */
264 function reconnect()
265 {
Timothy Warren02615962011-08-24 08:21:36 -0400266 if ($this->db->db_debug)
267 {
268 return $this->db->display_error('db_unsuported_feature');
269 }
Taufan Aditya18209332012-02-09 16:07:27 +0700270
Timothy Warren02615962011-08-24 08:21:36 -0400271 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Select the database
278 *
279 * @access private called by the base class
280 * @return resource
281 */
282 function db_select()
283 {
284 // Not needed for PDO
285 return TRUE;
286 }
287
288 // --------------------------------------------------------------------
289
290 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200291 * Database version number
Timothy Warren80ab8162011-08-22 18:26:12 -0400292 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400293 * @return string
294 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200295 public function version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400296 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200297 return isset($this->data_cache['version'])
298 ? $this->data_cache['version']
299 : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400300 }
301
302 // --------------------------------------------------------------------
303
304 /**
305 * Execute the query
306 *
307 * @access private called by the base class
308 * @param string an SQL query
Timothy Warren51a48882011-09-14 13:47:06 -0400309 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400310 */
311 function _execute($sql)
312 {
313 $sql = $this->_prep_query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700314
Timothy Warren51a48882011-09-14 13:47:06 -0400315 $result_id = $this->conn_id->query($sql);
316
Timothy Warrend6691532011-10-07 10:03:01 -0400317 if (is_object($result_id))
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400318 {
319 $this->affect_rows = $result_id->rowCount();
320 }
321 else
322 {
323 $this->affect_rows = 0;
324 }
Timothy Warren51a48882011-09-14 13:47:06 -0400325
326 return $result_id;
Timothy Warren80ab8162011-08-22 18:26:12 -0400327 }
328
329 // --------------------------------------------------------------------
330
331 /**
332 * Prep the query
333 *
334 * If needed, each database adapter can prep the query string
335 *
336 * @access private called by execute()
337 * @param string an SQL query
338 * @return string
339 */
340 function _prep_query($sql)
341 {
Taufan Aditya18209332012-02-09 16:07:27 +0700342 if ($this->pdodriver === 'pgsql')
343 {
344 // Change the backtick(s) for Postgre
345 $sql = str_replace('`', '"', $sql);
346 }
347 elseif ($this->pdodriver === 'sqlite')
348 {
349 // Change the backtick(s) for SQLite
350 $sql = str_replace('`', '', $sql);
351 }
352
Timothy Warren80ab8162011-08-22 18:26:12 -0400353 return $sql;
354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Begin Transaction
360 *
361 * @access public
362 * @return bool
363 */
364 function trans_begin($test_mode = FALSE)
365 {
366 if ( ! $this->trans_enabled)
367 {
368 return TRUE;
369 }
370
371 // When transactions are nested we only begin/commit/rollback the outermost ones
372 if ($this->_trans_depth > 0)
373 {
374 return TRUE;
375 }
376
377 // Reset the transaction failure flag.
378 // If the $test_mode flag is set to TRUE transactions will be rolled back
379 // even if the queries produce a successful result.
Timothy Warrend019fd62011-10-26 11:26:17 -0400380 $this->_trans_failure = (bool) ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400381
Timothy Warrenab347582011-08-23 12:29:29 -0400382 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Commit Transaction
389 *
390 * @access public
391 * @return bool
392 */
393 function trans_commit()
394 {
395 if ( ! $this->trans_enabled)
396 {
397 return TRUE;
398 }
399
400 // When transactions are nested we only begin/commit/rollback the outermost ones
401 if ($this->_trans_depth > 0)
402 {
403 return TRUE;
404 }
405
Timothy Warrenab347582011-08-23 12:29:29 -0400406 $ret = $this->conn->commit();
Taufan Aditya18209332012-02-09 16:07:27 +0700407
Timothy Warren80ab8162011-08-22 18:26:12 -0400408 return $ret;
409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Rollback Transaction
415 *
416 * @access public
417 * @return bool
418 */
419 function trans_rollback()
420 {
421 if ( ! $this->trans_enabled)
422 {
423 return TRUE;
424 }
425
426 // When transactions are nested we only begin/commit/rollback the outermost ones
427 if ($this->_trans_depth > 0)
428 {
429 return TRUE;
430 }
431
Timothy Warrenab347582011-08-23 12:29:29 -0400432 $ret = $this->conn_id->rollBack();
Taufan Aditya18209332012-02-09 16:07:27 +0700433
Timothy Warren80ab8162011-08-22 18:26:12 -0400434 return $ret;
435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * Escape String
441 *
442 * @access public
443 * @param string
444 * @param bool whether or not the string will be used in a LIKE condition
445 * @return string
446 */
447 function escape_str($str, $like = FALSE)
448 {
449 if (is_array($str))
450 {
451 foreach ($str as $key => $val)
452 {
453 $str[$key] = $this->escape_str($val, $like);
454 }
455
456 return $str;
457 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400458
Timothy Warren47663972011-10-05 16:44:50 -0400459 //Escape the string
460 $str = $this->conn_id->quote($str);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400461
Timothy Warren47663972011-10-05 16:44:50 -0400462 //If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400463 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400464 {
465 $str = substr($str, 1, -1);
466 }
467
Timothy Warren80ab8162011-08-22 18:26:12 -0400468 // escape LIKE condition wildcards
469 if ($like === TRUE)
470 {
471 $str = str_replace( array('%', '_', $this->_like_escape_chr),
Taufan Aditya18209332012-02-09 16:07:27 +0700472 array($this->_like_escape_chr.'%',
473 $this->_like_escape_chr.'_',
474 $this->_like_escape_chr.$this->_like_escape_chr),
Timothy Warren80ab8162011-08-22 18:26:12 -0400475 $str);
476 }
477
478 return $str;
479 }
480
481 // --------------------------------------------------------------------
482
483 /**
484 * Affected Rows
485 *
486 * @access public
487 * @return integer
488 */
489 function affected_rows()
490 {
Timothy Warren51a48882011-09-14 13:47:06 -0400491 return $this->affect_rows;
Timothy Warren80ab8162011-08-22 18:26:12 -0400492 }
493
494 // --------------------------------------------------------------------
495
496 /**
497 * Insert ID
Andrey Andreeva39d6992012-03-01 19:11:39 +0200498 *
499 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400500 */
Andrey Andreeva39d6992012-03-01 19:11:39 +0200501 public function insert_id($name = NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400502 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200503 if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1')
Timothy Warren33512752011-10-07 09:51:49 -0400504 {
Andrey Andreeva39d6992012-03-01 19:11:39 +0200505 $query = $this->query('SELECT LASTVAL() AS ins_id');
506 $query = $query->row();
507 return $query->ins_id;
Timothy Warren33512752011-10-07 09:51:49 -0400508 }
Andrey Andreeva39d6992012-03-01 19:11:39 +0200509
510 return $this->conn_id->lastInsertId($name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400511 }
512
513 // --------------------------------------------------------------------
514
515 /**
516 * "Count All" query
517 *
518 * Generates a platform-specific query string that counts all records in
519 * the specified database
520 *
521 * @access public
522 * @param string
523 * @return string
524 */
525 function count_all($table = '')
526 {
527 if ($table == '')
528 {
529 return 0;
530 }
531
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200532 $sql = $this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE);
Taufan Aditya18209332012-02-09 16:07:27 +0700533 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400534
535 if ($query->num_rows() == 0)
536 {
537 return 0;
538 }
539
540 $row = $query->row();
541 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700542
Timothy Warren80ab8162011-08-22 18:26:12 -0400543 return (int) $row->numrows;
544 }
545
546 // --------------------------------------------------------------------
547
548 /**
549 * Show table query
550 *
551 * Generates a platform-specific query string so that the table names can be fetched
552 *
553 * @access private
554 * @param boolean
555 * @return string
556 */
557 function _list_tables($prefix_limit = FALSE)
558 {
Taufan Aditya18209332012-02-09 16:07:27 +0700559 if ($this->pdodriver == 'pgsql')
560 {
561 // Analog function to show all tables in postgre
562 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
563 }
564 else
565 {
566 $sql = "SHOW TABLES FROM `".$this->database."`";
567 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400568
569 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
570 {
Taufan Aditya18209332012-02-09 16:07:27 +0700571 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400572 }
573
574 return $sql;
575 }
576
577 // --------------------------------------------------------------------
578
579 /**
580 * Show column query
581 *
582 * Generates a platform-specific query string so that the column names can be fetched
583 *
584 * @access public
585 * @param string the table name
586 * @return string
587 */
588 function _list_columns($table = '')
589 {
Taufan Aditya18209332012-02-09 16:07:27 +0700590 return 'SHOW COLUMNS FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400591 }
592
593 // --------------------------------------------------------------------
594
595 /**
596 * Field data query
597 *
598 * Generates a platform-specific query so that the column data can be retrieved
599 *
600 * @access public
601 * @param string the table name
602 * @return object
603 */
604 function _field_data($table)
605 {
Taufan Aditya18209332012-02-09 16:07:27 +0700606 return 'SELECT TOP 1 FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400607 }
608
609 // --------------------------------------------------------------------
610
611 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200612 * Error
Timothy Warren80ab8162011-08-22 18:26:12 -0400613 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200614 * Returns an array containing code and message of the last
615 * database error that has occured.
Timothy Warren80ab8162011-08-22 18:26:12 -0400616 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200617 * @return array
Timothy Warren80ab8162011-08-22 18:26:12 -0400618 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200619 public function error()
Timothy Warren80ab8162011-08-22 18:26:12 -0400620 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200621 $error = array('code' => '00000', 'message' => '');
622 $pdo_error = $this->conn_id->errorInfo();
623
624 if (empty($pdo_error[0]))
625 {
626 return $error;
627 }
628
629 $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
630 if (isset($pdo_error[2]))
631 {
632 $error['message'] = $pdo_error[2];
633 }
634
635 return $error;
Timothy Warren80ab8162011-08-22 18:26:12 -0400636 }
637
638 // --------------------------------------------------------------------
639
640 /**
641 * Escape the SQL Identifiers
642 *
643 * This function escapes column and table names
644 *
645 * @access private
646 * @param string
647 * @return string
648 */
649 function _escape_identifiers($item)
650 {
651 if ($this->_escape_char == '')
652 {
653 return $item;
654 }
655
656 foreach ($this->_reserved_identifiers as $id)
657 {
658 if (strpos($item, '.'.$id) !== FALSE)
659 {
660 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
661
662 // remove duplicates if the user already included the escape
663 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
664 }
665 }
666
667 if (strpos($item, '.') !== FALSE)
668 {
Taufan Aditya18209332012-02-09 16:07:27 +0700669 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
670 $str .= $this->_escape_char;
Timothy Warren80ab8162011-08-22 18:26:12 -0400671 }
672 else
673 {
674 $str = $this->_escape_char.$item.$this->_escape_char;
675 }
676
677 // remove duplicates if the user already included the escape
678 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
679 }
680
681 // --------------------------------------------------------------------
682
683 /**
684 * From Tables
685 *
686 * This function implicitly groups FROM tables so there is no confusion
687 * about operator precedence in harmony with SQL standards
688 *
689 * @access public
690 * @param type
691 * @return type
692 */
693 function _from_tables($tables)
694 {
695 if ( ! is_array($tables))
696 {
697 $tables = array($tables);
698 }
699
Taufan Aditya18209332012-02-09 16:07:27 +0700700 return (count($tables) == 1) ? '`'.$tables[0].'`' : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400701 }
702
703 // --------------------------------------------------------------------
704
705 /**
706 * Insert statement
707 *
708 * Generates a platform-specific insert string from the supplied data
709 *
710 * @access public
711 * @param string the table name
712 * @param array the insert keys
713 * @param array the insert values
714 * @return string
715 */
716 function _insert($table, $keys, $values)
717 {
Taufan Aditya18209332012-02-09 16:07:27 +0700718 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400719 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400720
721 // --------------------------------------------------------------------
722
723 /**
724 * Insert_batch statement
725 *
726 * Generates a platform-specific insert string from the supplied data
727 *
728 * @access public
729 * @param string the table name
730 * @param array the insert keys
731 * @param array the insert values
732 * @return string
733 */
734 function _insert_batch($table, $keys, $values)
735 {
Taufan Aditya18209332012-02-09 16:07:27 +0700736 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400737 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400738
739 // --------------------------------------------------------------------
740
741 /**
742 * Update statement
743 *
744 * Generates a platform-specific update string from the supplied data
745 *
746 * @access public
747 * @param string the table name
748 * @param array the update data
749 * @param array the where clause
750 * @param array the orderby clause
751 * @param array the limit clause
752 * @return string
753 */
754 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
755 {
756 foreach ($values as $key => $val)
757 {
758 $valstr[] = $key." = ".$val;
759 }
760
Taufan Aditya18209332012-02-09 16:07:27 +0700761 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
762 $orderby = (count($orderby) >= 1) ? ' ORDER BY '.implode(', ', $orderby) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400763
Taufan Aditya18209332012-02-09 16:07:27 +0700764 $sql = 'UPDATE '.$this->_from_tables($table).' SET '.implode(', ', $valstr);
765 $sql .= ($where != '' && count($where) >= 1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400766 $sql .= $orderby.$limit;
767
768 return $sql;
769 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400770
771 // --------------------------------------------------------------------
772
773 /**
774 * Update_Batch statement
775 *
776 * Generates a platform-specific batch update string from the supplied data
777 *
778 * @access public
779 * @param string the table name
780 * @param array the update data
781 * @param array the where clause
782 * @return string
783 */
784 function _update_batch($table, $values, $index, $where = NULL)
785 {
Taufan Aditya18209332012-02-09 16:07:27 +0700786 $ids = array();
787 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400788
789 foreach ($values as $key => $val)
790 {
791 $ids[] = $val[$index];
792
793 foreach (array_keys($val) as $field)
794 {
795 if ($field != $index)
796 {
797 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
798 }
799 }
800 }
801
Taufan Aditya18209332012-02-09 16:07:27 +0700802 $sql = 'UPDATE '.$this->_from_tables($table).' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400803 $cases = '';
804
805 foreach ($final as $k => $v)
806 {
807 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700808
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400809 foreach ($v as $row)
810 {
811 $cases .= $row."\n";
812 }
813
814 $cases .= 'ELSE '.$k.' END, ';
815 }
816
817 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400818 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
819
820 return $sql;
821 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400822
823
824 // --------------------------------------------------------------------
825
826 /**
827 * Truncate statement
828 *
829 * Generates a platform-specific truncate string from the supplied data
830 * If the database does not support the truncate() command
831 * This function maps to "DELETE FROM table"
832 *
833 * @access public
834 * @param string the table name
835 * @return string
836 */
837 function _truncate($table)
838 {
839 return $this->_delete($table);
840 }
841
842 // --------------------------------------------------------------------
843
844 /**
845 * Delete statement
846 *
847 * Generates a platform-specific delete string from the supplied data
848 *
849 * @access public
850 * @param string the table name
851 * @param array the where clause
852 * @param string the limit clause
853 * @return string
854 */
855 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
856 {
857 $conditions = '';
858
859 if (count($where) > 0 OR count($like) > 0)
860 {
Taufan Aditya18209332012-02-09 16:07:27 +0700861 $conditions = "\nWHERE ";
Timothy Warren80ab8162011-08-22 18:26:12 -0400862 $conditions .= implode("\n", $this->ar_where);
863
864 if (count($where) > 0 && count($like) > 0)
865 {
866 $conditions .= " AND ";
867 }
Taufan Aditya18209332012-02-09 16:07:27 +0700868
Timothy Warren80ab8162011-08-22 18:26:12 -0400869 $conditions .= implode("\n", $like);
870 }
871
872 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
873
Taufan Aditya18209332012-02-09 16:07:27 +0700874 return 'DELETE FROM '.$this->_from_tables($table).$conditions.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400875 }
876
877 // --------------------------------------------------------------------
878
879 /**
880 * Limit string
881 *
882 * Generates a platform-specific LIMIT clause
883 *
884 * @access public
885 * @param string the sql query string
886 * @param integer the number of rows to limit the query to
887 * @param integer the offset value
888 * @return string
889 */
890 function _limit($sql, $limit, $offset)
891 {
Taufan Aditya18209332012-02-09 16:07:27 +0700892 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400893 {
Taufan Aditya18209332012-02-09 16:07:27 +0700894 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400895
Taufan Aditya18209332012-02-09 16:07:27 +0700896 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400897 }
898 else
899 {
Taufan Aditya18209332012-02-09 16:07:27 +0700900 $sql .= 'LIMIT '.$limit;
901 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400902
903 return $sql;
904 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400905 }
906
907 // --------------------------------------------------------------------
908
909 /**
910 * Close DB Connection
911 *
912 * @access public
913 * @param resource
914 * @return void
915 */
916 function _close($conn_id)
917 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400918 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400919 }
920
Timothy Warren80ab8162011-08-22 18:26:12 -0400921}
922
Timothy Warren80ab8162011-08-22 18:26:12 -0400923/* End of file pdo_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200924/* Location: ./system/database/drivers/pdo/pdo_driver.php */