blob: fea54e502c15a470b9df6f7100d0977b2051380a [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).
Timothy Warren928d83c2012-02-13 14:07:57 -0500119 $dsnarray = explode(':', $this->hostname);
120 $this->pdodriver = $dsnarray[0];
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.
Timothy Warrenf4524692012-02-13 14:13:28 -0500124 if ( ! empty($this->database))
Timothy Warren25dc7552012-02-13 14:12:35 -0500125 {
126 $this->dsn .= rtrim($this->hostname, ';').';';
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
Timothy Warren928d83c2012-02-13 14:07:57 -0500142 if ( ! empty($this->hostname)
143 && strpos($this->hostname, ':') === FALSE
144 && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid')))
Taufan Aditya18209332012-02-09 16:07:27 +0700145 {
146 $this->dsn .= 'host='.$this->hostname.';';
147 }
148
149 // Add a port to the DSN for databases that can use it
150 if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid')))
151 {
152 $this->dsn .= 'port='.$this->port.';';
153 }
154 }
155
156 // Add the database name to the DSN, if needed
157 if (stripos($this->dsn, 'dbname') === FALSE
158 && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
159 {
160 $this->dsn .= 'dbname='.$this->database.';';
161 }
162 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv')))
163 {
164 if (stripos($this->dsn, 'dsn') === FALSE)
165 {
166 $this->dsn .= 'database='.$this->database.';';
167 }
168 }
169 elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:')
170 {
171 if ($this->database !== ':memory')
172 {
173 if ( ! file_exists($this->database))
174 {
175 show_error('Invalid DB Connection string for PDO SQLite');
176 }
177
178 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
179 }
180
181 $this->dsn .= $this->database;
182 }
183
184 // Add charset to the DSN, if needed
185 if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
186 {
187 $this->dsn .= 'charset='.$this->char_set.';';
188 }
189 }
190
191 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400192 * Non-persistent database connection
193 *
194 * @access private called by the base class
195 * @return resource
196 */
197 function db_connect()
198 {
Taufan Aditya18209332012-02-09 16:07:27 +0700199 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
200
201 return $this->pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Persistent database connection
208 *
209 * @access private called by the base class
210 * @return resource
211 */
212 function db_pconnect()
213 {
Taufan Aditya18209332012-02-09 16:07:27 +0700214 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
215 $this->options[PDO::ATTR_PERSISTENT] = TRUE;
Timothy Warrend93393f2011-10-26 11:31:49 -0400216
Taufan Aditya18209332012-02-09 16:07:27 +0700217 return $this->pdo_connect();
218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * PDO connection
224 *
225 * @access private called by the PDO driver class
226 * @return resource
227 */
228 function pdo_connect()
229 {
Taufan Aditya62b8cae2012-02-09 16:40:39 +0700230 // Refer : http://php.net/manual/en/ref.pdo-mysql.connection.php
Taufan Aditya18209332012-02-09 16:07:27 +0700231 if ($this->pdodriver == 'mysql' && is_php('5.3.6'))
232 {
Taufan Aditya55bb97e2012-02-09 16:43:26 +0700233 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES $this->char_set COLLATE '$this->dbcollat'";
Taufan Aditya18209332012-02-09 16:07:27 +0700234 }
235
236 // Connecting...
237 try
238 {
239 $db = new PDO($this->dsn, $this->username, $this->password, $this->options);
240 }
241 catch (PDOException $e)
242 {
243 if ($this->db_debug && empty($this->failover))
244 {
245 $this->display_error($e->getMessage(), '', TRUE);
246 }
247
248 return FALSE;
249 }
250
251 return $db;
Timothy Warren80ab8162011-08-22 18:26:12 -0400252 }
253
254 // --------------------------------------------------------------------
255
256 /**
257 * Reconnect
258 *
259 * Keep / reestablish the db connection if no queries have been
260 * sent for a length of time exceeding the server's idle timeout
261 *
262 * @access public
263 * @return void
264 */
265 function reconnect()
266 {
Timothy Warren02615962011-08-24 08:21:36 -0400267 if ($this->db->db_debug)
268 {
269 return $this->db->display_error('db_unsuported_feature');
270 }
Taufan Aditya18209332012-02-09 16:07:27 +0700271
Timothy Warren02615962011-08-24 08:21:36 -0400272 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400273 }
274
275 // --------------------------------------------------------------------
276
277 /**
278 * Select the database
279 *
280 * @access private called by the base class
281 * @return resource
282 */
283 function db_select()
284 {
285 // Not needed for PDO
286 return TRUE;
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400292 * Version number query string
293 *
294 * @access public
295 * @return string
296 */
297 function _version()
298 {
Timothy Warren36fb8de2011-08-24 08:29:05 -0400299 return $this->conn_id->getAttribute(PDO::ATTR_CLIENT_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
Timothy Warren57cea512011-09-14 14:26:28 -0400498 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400499 * @access public
500 * @return integer
501 */
Timothy Warren51a48882011-09-14 13:47:06 -0400502 function insert_id($name=NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400503 {
Taufan Aditya18209332012-02-09 16:07:27 +0700504 if ($this->pdodriver == 'pgsql')
Timothy Warren33512752011-10-07 09:51:49 -0400505 {
Taufan Aditya18209332012-02-09 16:07:27 +0700506 //Convenience method for postgres insertid
Timothy Warren33512752011-10-07 09:51:49 -0400507 $v = $this->_version();
508
509 $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
510
511 if ($table == NULL && $v >= '8.1')
512 {
513 $sql='SELECT LASTVAL() as ins_id';
514 }
Taufan Aditya18209332012-02-09 16:07:27 +0700515
Timothy Warren33512752011-10-07 09:51:49 -0400516 $query = $this->query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700517 $row = $query->row();
518
Timothy Warren33512752011-10-07 09:51:49 -0400519 return $row->ins_id;
520 }
521 else
522 {
523 return $this->conn_id->lastInsertId($name);
524 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400525 }
526
527 // --------------------------------------------------------------------
528
529 /**
530 * "Count All" query
531 *
532 * Generates a platform-specific query string that counts all records in
533 * the specified database
534 *
535 * @access public
536 * @param string
537 * @return string
538 */
539 function count_all($table = '')
540 {
541 if ($table == '')
542 {
543 return 0;
544 }
545
Taufan Aditya18209332012-02-09 16:07:27 +0700546 $sql = $this->_count_string.$this->_protect_identifiers('numrows').' FROM ';
547 $sql .= $this->_protect_identifiers($table, TRUE, NULL, FALSE);
548 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400549
550 if ($query->num_rows() == 0)
551 {
552 return 0;
553 }
554
555 $row = $query->row();
556 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700557
Timothy Warren80ab8162011-08-22 18:26:12 -0400558 return (int) $row->numrows;
559 }
560
561 // --------------------------------------------------------------------
562
563 /**
564 * Show table query
565 *
566 * Generates a platform-specific query string so that the table names can be fetched
567 *
568 * @access private
569 * @param boolean
570 * @return string
571 */
572 function _list_tables($prefix_limit = FALSE)
573 {
Taufan Aditya18209332012-02-09 16:07:27 +0700574 if ($this->pdodriver == 'pgsql')
575 {
576 // Analog function to show all tables in postgre
577 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
578 }
579 else
580 {
581 $sql = "SHOW TABLES FROM `".$this->database."`";
582 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400583
584 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
585 {
Taufan Aditya18209332012-02-09 16:07:27 +0700586 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400587 }
588
589 return $sql;
590 }
591
592 // --------------------------------------------------------------------
593
594 /**
595 * Show column query
596 *
597 * Generates a platform-specific query string so that the column names can be fetched
598 *
599 * @access public
600 * @param string the table name
601 * @return string
602 */
603 function _list_columns($table = '')
604 {
Taufan Aditya18209332012-02-09 16:07:27 +0700605 return 'SHOW COLUMNS FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400606 }
607
608 // --------------------------------------------------------------------
609
610 /**
611 * Field data query
612 *
613 * Generates a platform-specific query so that the column data can be retrieved
614 *
615 * @access public
616 * @param string the table name
617 * @return object
618 */
619 function _field_data($table)
620 {
Taufan Aditya18209332012-02-09 16:07:27 +0700621 return 'SELECT TOP 1 FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400622 }
623
624 // --------------------------------------------------------------------
625
626 /**
627 * The error message string
628 *
629 * @access private
630 * @return string
631 */
632 function _error_message()
633 {
Timothy Warrenab347582011-08-23 12:29:29 -0400634 $error_array = $this->conn_id->errorInfo();
Taufan Aditya18209332012-02-09 16:07:27 +0700635
Timothy Warrenab347582011-08-23 12:29:29 -0400636 return $error_array[2];
Timothy Warren80ab8162011-08-22 18:26:12 -0400637 }
638
639 // --------------------------------------------------------------------
640
641 /**
642 * The error message number
643 *
644 * @access private
645 * @return integer
646 */
647 function _error_number()
648 {
Timothy Warrenab347582011-08-23 12:29:29 -0400649 return $this->conn_id->errorCode();
Timothy Warren80ab8162011-08-22 18:26:12 -0400650 }
651
652 // --------------------------------------------------------------------
653
654 /**
655 * Escape the SQL Identifiers
656 *
657 * This function escapes column and table names
658 *
659 * @access private
660 * @param string
661 * @return string
662 */
663 function _escape_identifiers($item)
664 {
665 if ($this->_escape_char == '')
666 {
667 return $item;
668 }
669
670 foreach ($this->_reserved_identifiers as $id)
671 {
672 if (strpos($item, '.'.$id) !== FALSE)
673 {
674 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
675
676 // remove duplicates if the user already included the escape
677 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
678 }
679 }
680
681 if (strpos($item, '.') !== FALSE)
682 {
Taufan Aditya18209332012-02-09 16:07:27 +0700683 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
684 $str .= $this->_escape_char;
Timothy Warren80ab8162011-08-22 18:26:12 -0400685 }
686 else
687 {
688 $str = $this->_escape_char.$item.$this->_escape_char;
689 }
690
691 // remove duplicates if the user already included the escape
692 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
693 }
694
695 // --------------------------------------------------------------------
696
697 /**
698 * From Tables
699 *
700 * This function implicitly groups FROM tables so there is no confusion
701 * about operator precedence in harmony with SQL standards
702 *
703 * @access public
704 * @param type
705 * @return type
706 */
707 function _from_tables($tables)
708 {
709 if ( ! is_array($tables))
710 {
711 $tables = array($tables);
712 }
713
Taufan Aditya18209332012-02-09 16:07:27 +0700714 return (count($tables) == 1) ? '`'.$tables[0].'`' : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400715 }
716
717 // --------------------------------------------------------------------
718
719 /**
720 * Insert statement
721 *
722 * Generates a platform-specific insert string from the supplied data
723 *
724 * @access public
725 * @param string the table name
726 * @param array the insert keys
727 * @param array the insert values
728 * @return string
729 */
730 function _insert($table, $keys, $values)
731 {
Taufan Aditya18209332012-02-09 16:07:27 +0700732 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400733 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400734
735 // --------------------------------------------------------------------
736
737 /**
738 * Insert_batch statement
739 *
740 * Generates a platform-specific insert string from the supplied data
741 *
742 * @access public
743 * @param string the table name
744 * @param array the insert keys
745 * @param array the insert values
746 * @return string
747 */
748 function _insert_batch($table, $keys, $values)
749 {
Taufan Aditya18209332012-02-09 16:07:27 +0700750 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400751 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400752
753 // --------------------------------------------------------------------
754
755 /**
756 * Update statement
757 *
758 * Generates a platform-specific update string from the supplied data
759 *
760 * @access public
761 * @param string the table name
762 * @param array the update data
763 * @param array the where clause
764 * @param array the orderby clause
765 * @param array the limit clause
766 * @return string
767 */
768 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
769 {
770 foreach ($values as $key => $val)
771 {
772 $valstr[] = $key." = ".$val;
773 }
774
Taufan Aditya18209332012-02-09 16:07:27 +0700775 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
776 $orderby = (count($orderby) >= 1) ? ' ORDER BY '.implode(', ', $orderby) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400777
Taufan Aditya18209332012-02-09 16:07:27 +0700778 $sql = 'UPDATE '.$this->_from_tables($table).' SET '.implode(', ', $valstr);
779 $sql .= ($where != '' && count($where) >= 1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400780 $sql .= $orderby.$limit;
781
782 return $sql;
783 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400784
785 // --------------------------------------------------------------------
786
787 /**
788 * Update_Batch statement
789 *
790 * Generates a platform-specific batch update string from the supplied data
791 *
792 * @access public
793 * @param string the table name
794 * @param array the update data
795 * @param array the where clause
796 * @return string
797 */
798 function _update_batch($table, $values, $index, $where = NULL)
799 {
Taufan Aditya18209332012-02-09 16:07:27 +0700800 $ids = array();
801 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400802
803 foreach ($values as $key => $val)
804 {
805 $ids[] = $val[$index];
806
807 foreach (array_keys($val) as $field)
808 {
809 if ($field != $index)
810 {
811 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
812 }
813 }
814 }
815
Taufan Aditya18209332012-02-09 16:07:27 +0700816 $sql = 'UPDATE '.$this->_from_tables($table).' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400817 $cases = '';
818
819 foreach ($final as $k => $v)
820 {
821 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700822
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400823 foreach ($v as $row)
824 {
825 $cases .= $row."\n";
826 }
827
828 $cases .= 'ELSE '.$k.' END, ';
829 }
830
831 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400832 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
833
834 return $sql;
835 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400836
837
838 // --------------------------------------------------------------------
839
840 /**
841 * Truncate statement
842 *
843 * Generates a platform-specific truncate string from the supplied data
844 * If the database does not support the truncate() command
845 * This function maps to "DELETE FROM table"
846 *
847 * @access public
848 * @param string the table name
849 * @return string
850 */
851 function _truncate($table)
852 {
853 return $this->_delete($table);
854 }
855
856 // --------------------------------------------------------------------
857
858 /**
859 * Delete statement
860 *
861 * Generates a platform-specific delete string from the supplied data
862 *
863 * @access public
864 * @param string the table name
865 * @param array the where clause
866 * @param string the limit clause
867 * @return string
868 */
869 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
870 {
871 $conditions = '';
872
873 if (count($where) > 0 OR count($like) > 0)
874 {
Taufan Aditya18209332012-02-09 16:07:27 +0700875 $conditions = "\nWHERE ";
Timothy Warren80ab8162011-08-22 18:26:12 -0400876 $conditions .= implode("\n", $this->ar_where);
877
878 if (count($where) > 0 && count($like) > 0)
879 {
880 $conditions .= " AND ";
881 }
Taufan Aditya18209332012-02-09 16:07:27 +0700882
Timothy Warren80ab8162011-08-22 18:26:12 -0400883 $conditions .= implode("\n", $like);
884 }
885
886 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
887
Taufan Aditya18209332012-02-09 16:07:27 +0700888 return 'DELETE FROM '.$this->_from_tables($table).$conditions.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400889 }
890
891 // --------------------------------------------------------------------
892
893 /**
894 * Limit string
895 *
896 * Generates a platform-specific LIMIT clause
897 *
898 * @access public
899 * @param string the sql query string
900 * @param integer the number of rows to limit the query to
901 * @param integer the offset value
902 * @return string
903 */
904 function _limit($sql, $limit, $offset)
905 {
Taufan Aditya18209332012-02-09 16:07:27 +0700906 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400907 {
Taufan Aditya18209332012-02-09 16:07:27 +0700908 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400909
Taufan Aditya18209332012-02-09 16:07:27 +0700910 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400911 }
912 else
913 {
Taufan Aditya18209332012-02-09 16:07:27 +0700914 $sql .= 'LIMIT '.$limit;
915 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400916
917 return $sql;
918 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400919 }
920
921 // --------------------------------------------------------------------
922
923 /**
924 * Close DB Connection
925 *
926 * @access public
927 * @param resource
928 * @return void
929 */
930 function _close($conn_id)
931 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400932 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400933 }
934
Timothy Warren80ab8162011-08-22 18:26:12 -0400935}
936
Timothy Warren80ab8162011-08-22 18:26:12 -0400937/* End of file pdo_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200938/* Location: ./system/database/drivers/pdo/pdo_driver.php */