blob: 44e93a0423b6b404a4b2d81e64b300ebbf615e1d [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 /**
292 * Set client character set
293 *
294 * @access public
295 * @param string
296 * @param string
297 * @return resource
298 */
299 function db_set_charset($charset, $collation)
300 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400301 return TRUE;
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Version number query string
308 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400309 * @return string
310 */
Andrey Andreeved740822012-03-01 16:37:08 +0200311 protected function _version()
Timothy Warren80ab8162011-08-22 18:26:12 -0400312 {
Andrey Andreeved740822012-03-01 16:37:08 +0200313 return $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * Execute the query
320 *
321 * @access private called by the base class
322 * @param string an SQL query
Timothy Warren51a48882011-09-14 13:47:06 -0400323 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400324 */
325 function _execute($sql)
326 {
327 $sql = $this->_prep_query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700328
Timothy Warren51a48882011-09-14 13:47:06 -0400329 $result_id = $this->conn_id->query($sql);
330
Timothy Warrend6691532011-10-07 10:03:01 -0400331 if (is_object($result_id))
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400332 {
333 $this->affect_rows = $result_id->rowCount();
334 }
335 else
336 {
337 $this->affect_rows = 0;
338 }
Timothy Warren51a48882011-09-14 13:47:06 -0400339
340 return $result_id;
Timothy Warren80ab8162011-08-22 18:26:12 -0400341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * Prep the query
347 *
348 * If needed, each database adapter can prep the query string
349 *
350 * @access private called by execute()
351 * @param string an SQL query
352 * @return string
353 */
354 function _prep_query($sql)
355 {
Taufan Aditya18209332012-02-09 16:07:27 +0700356 if ($this->pdodriver === 'pgsql')
357 {
358 // Change the backtick(s) for Postgre
359 $sql = str_replace('`', '"', $sql);
360 }
361 elseif ($this->pdodriver === 'sqlite')
362 {
363 // Change the backtick(s) for SQLite
364 $sql = str_replace('`', '', $sql);
365 }
366
Timothy Warren80ab8162011-08-22 18:26:12 -0400367 return $sql;
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * Begin Transaction
374 *
375 * @access public
376 * @return bool
377 */
378 function trans_begin($test_mode = FALSE)
379 {
380 if ( ! $this->trans_enabled)
381 {
382 return TRUE;
383 }
384
385 // When transactions are nested we only begin/commit/rollback the outermost ones
386 if ($this->_trans_depth > 0)
387 {
388 return TRUE;
389 }
390
391 // Reset the transaction failure flag.
392 // If the $test_mode flag is set to TRUE transactions will be rolled back
393 // even if the queries produce a successful result.
Timothy Warrend019fd62011-10-26 11:26:17 -0400394 $this->_trans_failure = (bool) ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400395
Timothy Warrenab347582011-08-23 12:29:29 -0400396 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Commit Transaction
403 *
404 * @access public
405 * @return bool
406 */
407 function trans_commit()
408 {
409 if ( ! $this->trans_enabled)
410 {
411 return TRUE;
412 }
413
414 // When transactions are nested we only begin/commit/rollback the outermost ones
415 if ($this->_trans_depth > 0)
416 {
417 return TRUE;
418 }
419
Timothy Warrenab347582011-08-23 12:29:29 -0400420 $ret = $this->conn->commit();
Taufan Aditya18209332012-02-09 16:07:27 +0700421
Timothy Warren80ab8162011-08-22 18:26:12 -0400422 return $ret;
423 }
424
425 // --------------------------------------------------------------------
426
427 /**
428 * Rollback Transaction
429 *
430 * @access public
431 * @return bool
432 */
433 function trans_rollback()
434 {
435 if ( ! $this->trans_enabled)
436 {
437 return TRUE;
438 }
439
440 // When transactions are nested we only begin/commit/rollback the outermost ones
441 if ($this->_trans_depth > 0)
442 {
443 return TRUE;
444 }
445
Timothy Warrenab347582011-08-23 12:29:29 -0400446 $ret = $this->conn_id->rollBack();
Taufan Aditya18209332012-02-09 16:07:27 +0700447
Timothy Warren80ab8162011-08-22 18:26:12 -0400448 return $ret;
449 }
450
451 // --------------------------------------------------------------------
452
453 /**
454 * Escape String
455 *
456 * @access public
457 * @param string
458 * @param bool whether or not the string will be used in a LIKE condition
459 * @return string
460 */
461 function escape_str($str, $like = FALSE)
462 {
463 if (is_array($str))
464 {
465 foreach ($str as $key => $val)
466 {
467 $str[$key] = $this->escape_str($val, $like);
468 }
469
470 return $str;
471 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400472
Timothy Warren47663972011-10-05 16:44:50 -0400473 //Escape the string
474 $str = $this->conn_id->quote($str);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400475
Timothy Warren47663972011-10-05 16:44:50 -0400476 //If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400477 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400478 {
479 $str = substr($str, 1, -1);
480 }
481
Timothy Warren80ab8162011-08-22 18:26:12 -0400482 // escape LIKE condition wildcards
483 if ($like === TRUE)
484 {
485 $str = str_replace( array('%', '_', $this->_like_escape_chr),
Taufan Aditya18209332012-02-09 16:07:27 +0700486 array($this->_like_escape_chr.'%',
487 $this->_like_escape_chr.'_',
488 $this->_like_escape_chr.$this->_like_escape_chr),
Timothy Warren80ab8162011-08-22 18:26:12 -0400489 $str);
490 }
491
492 return $str;
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Affected Rows
499 *
500 * @access public
501 * @return integer
502 */
503 function affected_rows()
504 {
Timothy Warren51a48882011-09-14 13:47:06 -0400505 return $this->affect_rows;
Timothy Warren80ab8162011-08-22 18:26:12 -0400506 }
507
508 // --------------------------------------------------------------------
509
510 /**
511 * Insert ID
Timothy Warren57cea512011-09-14 14:26:28 -0400512 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400513 * @access public
514 * @return integer
515 */
Timothy Warren51a48882011-09-14 13:47:06 -0400516 function insert_id($name=NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400517 {
Taufan Aditya18209332012-02-09 16:07:27 +0700518 if ($this->pdodriver == 'pgsql')
Timothy Warren33512752011-10-07 09:51:49 -0400519 {
Taufan Aditya18209332012-02-09 16:07:27 +0700520 //Convenience method for postgres insertid
Timothy Warren33512752011-10-07 09:51:49 -0400521 $v = $this->_version();
522
523 $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
524
525 if ($table == NULL && $v >= '8.1')
526 {
527 $sql='SELECT LASTVAL() as ins_id';
528 }
Taufan Aditya18209332012-02-09 16:07:27 +0700529
Timothy Warren33512752011-10-07 09:51:49 -0400530 $query = $this->query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700531 $row = $query->row();
532
Timothy Warren33512752011-10-07 09:51:49 -0400533 return $row->ins_id;
534 }
535 else
536 {
537 return $this->conn_id->lastInsertId($name);
538 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400539 }
540
541 // --------------------------------------------------------------------
542
543 /**
544 * "Count All" query
545 *
546 * Generates a platform-specific query string that counts all records in
547 * the specified database
548 *
549 * @access public
550 * @param string
551 * @return string
552 */
553 function count_all($table = '')
554 {
555 if ($table == '')
556 {
557 return 0;
558 }
559
Taufan Aditya18209332012-02-09 16:07:27 +0700560 $sql = $this->_count_string.$this->_protect_identifiers('numrows').' FROM ';
561 $sql .= $this->_protect_identifiers($table, TRUE, NULL, FALSE);
562 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400563
564 if ($query->num_rows() == 0)
565 {
566 return 0;
567 }
568
569 $row = $query->row();
570 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700571
Timothy Warren80ab8162011-08-22 18:26:12 -0400572 return (int) $row->numrows;
573 }
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Show table query
579 *
580 * Generates a platform-specific query string so that the table names can be fetched
581 *
582 * @access private
583 * @param boolean
584 * @return string
585 */
586 function _list_tables($prefix_limit = FALSE)
587 {
Taufan Aditya18209332012-02-09 16:07:27 +0700588 if ($this->pdodriver == 'pgsql')
589 {
590 // Analog function to show all tables in postgre
591 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
592 }
593 else
594 {
595 $sql = "SHOW TABLES FROM `".$this->database."`";
596 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400597
598 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
599 {
Taufan Aditya18209332012-02-09 16:07:27 +0700600 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400601 }
602
603 return $sql;
604 }
605
606 // --------------------------------------------------------------------
607
608 /**
609 * Show column query
610 *
611 * Generates a platform-specific query string so that the column names can be fetched
612 *
613 * @access public
614 * @param string the table name
615 * @return string
616 */
617 function _list_columns($table = '')
618 {
Taufan Aditya18209332012-02-09 16:07:27 +0700619 return 'SHOW COLUMNS FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400620 }
621
622 // --------------------------------------------------------------------
623
624 /**
625 * Field data query
626 *
627 * Generates a platform-specific query so that the column data can be retrieved
628 *
629 * @access public
630 * @param string the table name
631 * @return object
632 */
633 function _field_data($table)
634 {
Taufan Aditya18209332012-02-09 16:07:27 +0700635 return 'SELECT TOP 1 FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400636 }
637
638 // --------------------------------------------------------------------
639
640 /**
641 * The error message string
642 *
643 * @access private
644 * @return string
645 */
646 function _error_message()
647 {
Timothy Warrenab347582011-08-23 12:29:29 -0400648 $error_array = $this->conn_id->errorInfo();
Taufan Aditya18209332012-02-09 16:07:27 +0700649
Timothy Warrenab347582011-08-23 12:29:29 -0400650 return $error_array[2];
Timothy Warren80ab8162011-08-22 18:26:12 -0400651 }
652
653 // --------------------------------------------------------------------
654
655 /**
656 * The error message number
657 *
658 * @access private
659 * @return integer
660 */
661 function _error_number()
662 {
Timothy Warrenab347582011-08-23 12:29:29 -0400663 return $this->conn_id->errorCode();
Timothy Warren80ab8162011-08-22 18:26:12 -0400664 }
665
666 // --------------------------------------------------------------------
667
668 /**
669 * Escape the SQL Identifiers
670 *
671 * This function escapes column and table names
672 *
673 * @access private
674 * @param string
675 * @return string
676 */
677 function _escape_identifiers($item)
678 {
679 if ($this->_escape_char == '')
680 {
681 return $item;
682 }
683
684 foreach ($this->_reserved_identifiers as $id)
685 {
686 if (strpos($item, '.'.$id) !== FALSE)
687 {
688 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
689
690 // remove duplicates if the user already included the escape
691 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
692 }
693 }
694
695 if (strpos($item, '.') !== FALSE)
696 {
Taufan Aditya18209332012-02-09 16:07:27 +0700697 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
698 $str .= $this->_escape_char;
Timothy Warren80ab8162011-08-22 18:26:12 -0400699 }
700 else
701 {
702 $str = $this->_escape_char.$item.$this->_escape_char;
703 }
704
705 // remove duplicates if the user already included the escape
706 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
707 }
708
709 // --------------------------------------------------------------------
710
711 /**
712 * From Tables
713 *
714 * This function implicitly groups FROM tables so there is no confusion
715 * about operator precedence in harmony with SQL standards
716 *
717 * @access public
718 * @param type
719 * @return type
720 */
721 function _from_tables($tables)
722 {
723 if ( ! is_array($tables))
724 {
725 $tables = array($tables);
726 }
727
Taufan Aditya18209332012-02-09 16:07:27 +0700728 return (count($tables) == 1) ? '`'.$tables[0].'`' : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400729 }
730
731 // --------------------------------------------------------------------
732
733 /**
734 * Insert statement
735 *
736 * Generates a platform-specific insert string from the supplied data
737 *
738 * @access public
739 * @param string the table name
740 * @param array the insert keys
741 * @param array the insert values
742 * @return string
743 */
744 function _insert($table, $keys, $values)
745 {
Taufan Aditya18209332012-02-09 16:07:27 +0700746 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400747 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400748
749 // --------------------------------------------------------------------
750
751 /**
752 * Insert_batch statement
753 *
754 * Generates a platform-specific insert string from the supplied data
755 *
756 * @access public
757 * @param string the table name
758 * @param array the insert keys
759 * @param array the insert values
760 * @return string
761 */
762 function _insert_batch($table, $keys, $values)
763 {
Taufan Aditya18209332012-02-09 16:07:27 +0700764 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400765 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400766
767 // --------------------------------------------------------------------
768
769 /**
770 * Update statement
771 *
772 * Generates a platform-specific update string from the supplied data
773 *
774 * @access public
775 * @param string the table name
776 * @param array the update data
777 * @param array the where clause
778 * @param array the orderby clause
779 * @param array the limit clause
780 * @return string
781 */
782 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
783 {
784 foreach ($values as $key => $val)
785 {
786 $valstr[] = $key." = ".$val;
787 }
788
Taufan Aditya18209332012-02-09 16:07:27 +0700789 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
790 $orderby = (count($orderby) >= 1) ? ' ORDER BY '.implode(', ', $orderby) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400791
Taufan Aditya18209332012-02-09 16:07:27 +0700792 $sql = 'UPDATE '.$this->_from_tables($table).' SET '.implode(', ', $valstr);
793 $sql .= ($where != '' && count($where) >= 1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400794 $sql .= $orderby.$limit;
795
796 return $sql;
797 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400798
799 // --------------------------------------------------------------------
800
801 /**
802 * Update_Batch statement
803 *
804 * Generates a platform-specific batch update string from the supplied data
805 *
806 * @access public
807 * @param string the table name
808 * @param array the update data
809 * @param array the where clause
810 * @return string
811 */
812 function _update_batch($table, $values, $index, $where = NULL)
813 {
Taufan Aditya18209332012-02-09 16:07:27 +0700814 $ids = array();
815 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400816
817 foreach ($values as $key => $val)
818 {
819 $ids[] = $val[$index];
820
821 foreach (array_keys($val) as $field)
822 {
823 if ($field != $index)
824 {
825 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
826 }
827 }
828 }
829
Taufan Aditya18209332012-02-09 16:07:27 +0700830 $sql = 'UPDATE '.$this->_from_tables($table).' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400831 $cases = '';
832
833 foreach ($final as $k => $v)
834 {
835 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700836
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400837 foreach ($v as $row)
838 {
839 $cases .= $row."\n";
840 }
841
842 $cases .= 'ELSE '.$k.' END, ';
843 }
844
845 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400846 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
847
848 return $sql;
849 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400850
851
852 // --------------------------------------------------------------------
853
854 /**
855 * Truncate statement
856 *
857 * Generates a platform-specific truncate string from the supplied data
858 * If the database does not support the truncate() command
859 * This function maps to "DELETE FROM table"
860 *
861 * @access public
862 * @param string the table name
863 * @return string
864 */
865 function _truncate($table)
866 {
867 return $this->_delete($table);
868 }
869
870 // --------------------------------------------------------------------
871
872 /**
873 * Delete statement
874 *
875 * Generates a platform-specific delete string from the supplied data
876 *
877 * @access public
878 * @param string the table name
879 * @param array the where clause
880 * @param string the limit clause
881 * @return string
882 */
883 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
884 {
885 $conditions = '';
886
887 if (count($where) > 0 OR count($like) > 0)
888 {
Taufan Aditya18209332012-02-09 16:07:27 +0700889 $conditions = "\nWHERE ";
Timothy Warren80ab8162011-08-22 18:26:12 -0400890 $conditions .= implode("\n", $this->ar_where);
891
892 if (count($where) > 0 && count($like) > 0)
893 {
894 $conditions .= " AND ";
895 }
Taufan Aditya18209332012-02-09 16:07:27 +0700896
Timothy Warren80ab8162011-08-22 18:26:12 -0400897 $conditions .= implode("\n", $like);
898 }
899
900 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
901
Taufan Aditya18209332012-02-09 16:07:27 +0700902 return 'DELETE FROM '.$this->_from_tables($table).$conditions.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400903 }
904
905 // --------------------------------------------------------------------
906
907 /**
908 * Limit string
909 *
910 * Generates a platform-specific LIMIT clause
911 *
912 * @access public
913 * @param string the sql query string
914 * @param integer the number of rows to limit the query to
915 * @param integer the offset value
916 * @return string
917 */
918 function _limit($sql, $limit, $offset)
919 {
Taufan Aditya18209332012-02-09 16:07:27 +0700920 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400921 {
Taufan Aditya18209332012-02-09 16:07:27 +0700922 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400923
Taufan Aditya18209332012-02-09 16:07:27 +0700924 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400925 }
926 else
927 {
Taufan Aditya18209332012-02-09 16:07:27 +0700928 $sql .= 'LIMIT '.$limit;
929 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400930
931 return $sql;
932 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400933 }
934
935 // --------------------------------------------------------------------
936
937 /**
938 * Close DB Connection
939 *
940 * @access public
941 * @param resource
942 * @return void
943 */
944 function _close($conn_id)
945 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400946 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400947 }
948
Timothy Warren80ab8162011-08-22 18:26:12 -0400949}
950
Timothy Warren80ab8162011-08-22 18:26:12 -0400951/* End of file pdo_driver.php */
Andrey Andreeved740822012-03-01 16:37:08 +0200952/* Location: ./system/database/drivers/pdo/pdo_driver.php */