blob: 46b5e543dcb9bea66f9dc502cc03e9849221cd1f [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
74 // This for general PDO users, who tend to have full DSN string.
75 $this->pdodriver = end($match);
76 }
77 else
78 {
79 // Try to build a complete DSN string from params
80 $this->_connect_string($params);
81 }
82
Timothy Warrenc7ba6642011-09-14 12:25:14 -040083 // clause and character used for LIKE escape sequences
Taufan Aditya18209332012-02-09 16:07:27 +070084 // this one depends on the driver being used
85 if ($this->pdodriver == 'mysql')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040086 {
87 $this->_like_escape_str = '';
88 $this->_like_escape_chr = '';
89 }
Taufan Aditya18209332012-02-09 16:07:27 +070090 elseif ($this->pdodriver == 'odbc')
Timothy Warrenc7ba6642011-09-14 12:25:14 -040091 {
92 $this->_like_escape_str = " {escape '%s'} ";
93 $this->_like_escape_chr = '!';
94 }
95 else
96 {
97 $this->_like_escape_str = " ESCAPE '%s' ";
98 $this->_like_escape_chr = '!';
99 }
100
Taufan Aditya18209332012-02-09 16:07:27 +0700101 $this->trans_enabled = FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400102 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
103 }
104
105 /**
Taufan Aditya18209332012-02-09 16:07:27 +0700106 * Connection String
107 *
108 * @access private
109 * @param array
110 * @return void
111 */
112 function _connect_string($params)
113 {
114 if (strpos($this->hostname, ':'))
115 {
116 // hostname generally would have this prototype
117 // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);';
118 // We need to get the prefix (pdodriver used by PDO).
119 $this->dsn = $this->hostname;
120 $this->pdodriver = substr($this->hostname, 0, strpos($this->hostname, ':'));
121 }
122 else
123 {
124 // Invalid DSN, display an error
125 if ( ! array_key_exists('pdodriver', $params))
126 {
127 show_error('Invalid DB Connection String for PDO');
128 }
129
130 // Assuming that the following DSN string format is used:
131 // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql';
132 $this->dsn = $this->pdodriver.':';
133
134 // Add hostname to the DSN for databases that need it
135 if ( ! empty($this->hostname) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid')))
136 {
137 $this->dsn .= 'host='.$this->hostname.';';
138 }
139
140 // Add a port to the DSN for databases that can use it
141 if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid')))
142 {
143 $this->dsn .= 'port='.$this->port.';';
144 }
145 }
146
147 // Add the database name to the DSN, if needed
148 if (stripos($this->dsn, 'dbname') === FALSE
149 && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid')))
150 {
151 $this->dsn .= 'dbname='.$this->database.';';
152 }
153 elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv')))
154 {
155 if (stripos($this->dsn, 'dsn') === FALSE)
156 {
157 $this->dsn .= 'database='.$this->database.';';
158 }
159 }
160 elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:')
161 {
162 if ($this->database !== ':memory')
163 {
164 if ( ! file_exists($this->database))
165 {
166 show_error('Invalid DB Connection string for PDO SQLite');
167 }
168
169 $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : '';
170 }
171
172 $this->dsn .= $this->database;
173 }
174
175 // Add charset to the DSN, if needed
176 if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci')))
177 {
178 $this->dsn .= 'charset='.$this->char_set.';';
179 }
180 }
181
182 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400183 * Non-persistent database connection
184 *
185 * @access private called by the base class
186 * @return resource
187 */
188 function db_connect()
189 {
Taufan Aditya18209332012-02-09 16:07:27 +0700190 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
191
192 return $this->pdo_connect();
Timothy Warren80ab8162011-08-22 18:26:12 -0400193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Persistent database connection
199 *
200 * @access private called by the base class
201 * @return resource
202 */
203 function db_pconnect()
204 {
Taufan Aditya18209332012-02-09 16:07:27 +0700205 $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
206 $this->options[PDO::ATTR_PERSISTENT] = TRUE;
Timothy Warrend93393f2011-10-26 11:31:49 -0400207
Taufan Aditya18209332012-02-09 16:07:27 +0700208 return $this->pdo_connect();
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * PDO connection
215 *
216 * @access private called by the PDO driver class
217 * @return resource
218 */
219 function pdo_connect()
220 {
Taufan Aditya62b8cae2012-02-09 16:40:39 +0700221 // Refer : http://php.net/manual/en/ref.pdo-mysql.connection.php
Taufan Aditya18209332012-02-09 16:07:27 +0700222 if ($this->pdodriver == 'mysql' && is_php('5.3.6'))
223 {
Taufan Aditya62b8cae2012-02-09 16:40:39 +0700224 $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES $this->char_set COLLATE '.$this->dbcollat.'";
Taufan Aditya18209332012-02-09 16:07:27 +0700225 }
226
227 // Connecting...
228 try
229 {
230 $db = new PDO($this->dsn, $this->username, $this->password, $this->options);
231 }
232 catch (PDOException $e)
233 {
234 if ($this->db_debug && empty($this->failover))
235 {
236 $this->display_error($e->getMessage(), '', TRUE);
237 }
238
239 return FALSE;
240 }
241
242 return $db;
Timothy Warren80ab8162011-08-22 18:26:12 -0400243 }
244
245 // --------------------------------------------------------------------
246
247 /**
248 * Reconnect
249 *
250 * Keep / reestablish the db connection if no queries have been
251 * sent for a length of time exceeding the server's idle timeout
252 *
253 * @access public
254 * @return void
255 */
256 function reconnect()
257 {
Timothy Warren02615962011-08-24 08:21:36 -0400258 if ($this->db->db_debug)
259 {
260 return $this->db->display_error('db_unsuported_feature');
261 }
Taufan Aditya18209332012-02-09 16:07:27 +0700262
Timothy Warren02615962011-08-24 08:21:36 -0400263 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400264 }
265
266 // --------------------------------------------------------------------
267
268 /**
269 * Select the database
270 *
271 * @access private called by the base class
272 * @return resource
273 */
274 function db_select()
275 {
276 // Not needed for PDO
277 return TRUE;
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Set client character set
284 *
285 * @access public
286 * @param string
287 * @param string
288 * @return resource
289 */
290 function db_set_charset($charset, $collation)
291 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400292 return TRUE;
293 }
294
295 // --------------------------------------------------------------------
296
297 /**
298 * Version number query string
299 *
300 * @access public
301 * @return string
302 */
303 function _version()
304 {
Timothy Warren36fb8de2011-08-24 08:29:05 -0400305 return $this->conn_id->getAttribute(PDO::ATTR_CLIENT_VERSION);
Timothy Warren80ab8162011-08-22 18:26:12 -0400306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * Execute the query
312 *
313 * @access private called by the base class
314 * @param string an SQL query
Timothy Warren51a48882011-09-14 13:47:06 -0400315 * @return object
Timothy Warren80ab8162011-08-22 18:26:12 -0400316 */
317 function _execute($sql)
318 {
319 $sql = $this->_prep_query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700320
Timothy Warren51a48882011-09-14 13:47:06 -0400321 $result_id = $this->conn_id->query($sql);
322
Timothy Warrend6691532011-10-07 10:03:01 -0400323 if (is_object($result_id))
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400324 {
325 $this->affect_rows = $result_id->rowCount();
326 }
327 else
328 {
329 $this->affect_rows = 0;
330 }
Timothy Warren51a48882011-09-14 13:47:06 -0400331
332 return $result_id;
Timothy Warren80ab8162011-08-22 18:26:12 -0400333 }
334
335 // --------------------------------------------------------------------
336
337 /**
338 * Prep the query
339 *
340 * If needed, each database adapter can prep the query string
341 *
342 * @access private called by execute()
343 * @param string an SQL query
344 * @return string
345 */
346 function _prep_query($sql)
347 {
Taufan Aditya18209332012-02-09 16:07:27 +0700348 if ($this->pdodriver === 'pgsql')
349 {
350 // Change the backtick(s) for Postgre
351 $sql = str_replace('`', '"', $sql);
352 }
353 elseif ($this->pdodriver === 'sqlite')
354 {
355 // Change the backtick(s) for SQLite
356 $sql = str_replace('`', '', $sql);
357 }
358
Timothy Warren80ab8162011-08-22 18:26:12 -0400359 return $sql;
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * Begin Transaction
366 *
367 * @access public
368 * @return bool
369 */
370 function trans_begin($test_mode = FALSE)
371 {
372 if ( ! $this->trans_enabled)
373 {
374 return TRUE;
375 }
376
377 // When transactions are nested we only begin/commit/rollback the outermost ones
378 if ($this->_trans_depth > 0)
379 {
380 return TRUE;
381 }
382
383 // Reset the transaction failure flag.
384 // If the $test_mode flag is set to TRUE transactions will be rolled back
385 // even if the queries produce a successful result.
Timothy Warrend019fd62011-10-26 11:26:17 -0400386 $this->_trans_failure = (bool) ($test_mode === TRUE);
Timothy Warren80ab8162011-08-22 18:26:12 -0400387
Timothy Warrenab347582011-08-23 12:29:29 -0400388 return $this->conn_id->beginTransaction();
Timothy Warren80ab8162011-08-22 18:26:12 -0400389 }
390
391 // --------------------------------------------------------------------
392
393 /**
394 * Commit Transaction
395 *
396 * @access public
397 * @return bool
398 */
399 function trans_commit()
400 {
401 if ( ! $this->trans_enabled)
402 {
403 return TRUE;
404 }
405
406 // When transactions are nested we only begin/commit/rollback the outermost ones
407 if ($this->_trans_depth > 0)
408 {
409 return TRUE;
410 }
411
Timothy Warrenab347582011-08-23 12:29:29 -0400412 $ret = $this->conn->commit();
Taufan Aditya18209332012-02-09 16:07:27 +0700413
Timothy Warren80ab8162011-08-22 18:26:12 -0400414 return $ret;
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Rollback Transaction
421 *
422 * @access public
423 * @return bool
424 */
425 function trans_rollback()
426 {
427 if ( ! $this->trans_enabled)
428 {
429 return TRUE;
430 }
431
432 // When transactions are nested we only begin/commit/rollback the outermost ones
433 if ($this->_trans_depth > 0)
434 {
435 return TRUE;
436 }
437
Timothy Warrenab347582011-08-23 12:29:29 -0400438 $ret = $this->conn_id->rollBack();
Taufan Aditya18209332012-02-09 16:07:27 +0700439
Timothy Warren80ab8162011-08-22 18:26:12 -0400440 return $ret;
441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * Escape String
447 *
448 * @access public
449 * @param string
450 * @param bool whether or not the string will be used in a LIKE condition
451 * @return string
452 */
453 function escape_str($str, $like = FALSE)
454 {
455 if (is_array($str))
456 {
457 foreach ($str as $key => $val)
458 {
459 $str[$key] = $this->escape_str($val, $like);
460 }
461
462 return $str;
463 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400464
Timothy Warren47663972011-10-05 16:44:50 -0400465 //Escape the string
466 $str = $this->conn_id->quote($str);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400467
Timothy Warren47663972011-10-05 16:44:50 -0400468 //If there are duplicated quotes, trim them away
Timothy Warrend6691532011-10-07 10:03:01 -0400469 if (strpos($str, "'") === 0)
Timothy Warrenec193322011-10-07 09:53:35 -0400470 {
471 $str = substr($str, 1, -1);
472 }
473
Timothy Warren80ab8162011-08-22 18:26:12 -0400474 // escape LIKE condition wildcards
475 if ($like === TRUE)
476 {
477 $str = str_replace( array('%', '_', $this->_like_escape_chr),
Taufan Aditya18209332012-02-09 16:07:27 +0700478 array($this->_like_escape_chr.'%',
479 $this->_like_escape_chr.'_',
480 $this->_like_escape_chr.$this->_like_escape_chr),
Timothy Warren80ab8162011-08-22 18:26:12 -0400481 $str);
482 }
483
484 return $str;
485 }
486
487 // --------------------------------------------------------------------
488
489 /**
490 * Affected Rows
491 *
492 * @access public
493 * @return integer
494 */
495 function affected_rows()
496 {
Timothy Warren51a48882011-09-14 13:47:06 -0400497 return $this->affect_rows;
Timothy Warren80ab8162011-08-22 18:26:12 -0400498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Insert ID
Timothy Warren57cea512011-09-14 14:26:28 -0400504 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400505 * @access public
506 * @return integer
507 */
Timothy Warren51a48882011-09-14 13:47:06 -0400508 function insert_id($name=NULL)
Timothy Warren80ab8162011-08-22 18:26:12 -0400509 {
Taufan Aditya18209332012-02-09 16:07:27 +0700510 if ($this->pdodriver == 'pgsql')
Timothy Warren33512752011-10-07 09:51:49 -0400511 {
Taufan Aditya18209332012-02-09 16:07:27 +0700512 //Convenience method for postgres insertid
Timothy Warren33512752011-10-07 09:51:49 -0400513 $v = $this->_version();
514
515 $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
516
517 if ($table == NULL && $v >= '8.1')
518 {
519 $sql='SELECT LASTVAL() as ins_id';
520 }
Taufan Aditya18209332012-02-09 16:07:27 +0700521
Timothy Warren33512752011-10-07 09:51:49 -0400522 $query = $this->query($sql);
Taufan Aditya18209332012-02-09 16:07:27 +0700523 $row = $query->row();
524
Timothy Warren33512752011-10-07 09:51:49 -0400525 return $row->ins_id;
526 }
527 else
528 {
529 return $this->conn_id->lastInsertId($name);
530 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * "Count All" query
537 *
538 * Generates a platform-specific query string that counts all records in
539 * the specified database
540 *
541 * @access public
542 * @param string
543 * @return string
544 */
545 function count_all($table = '')
546 {
547 if ($table == '')
548 {
549 return 0;
550 }
551
Taufan Aditya18209332012-02-09 16:07:27 +0700552 $sql = $this->_count_string.$this->_protect_identifiers('numrows').' FROM ';
553 $sql .= $this->_protect_identifiers($table, TRUE, NULL, FALSE);
554 $query = $this->query($sql);
Timothy Warren80ab8162011-08-22 18:26:12 -0400555
556 if ($query->num_rows() == 0)
557 {
558 return 0;
559 }
560
561 $row = $query->row();
562 $this->_reset_select();
Taufan Aditya18209332012-02-09 16:07:27 +0700563
Timothy Warren80ab8162011-08-22 18:26:12 -0400564 return (int) $row->numrows;
565 }
566
567 // --------------------------------------------------------------------
568
569 /**
570 * Show table query
571 *
572 * Generates a platform-specific query string so that the table names can be fetched
573 *
574 * @access private
575 * @param boolean
576 * @return string
577 */
578 function _list_tables($prefix_limit = FALSE)
579 {
Taufan Aditya18209332012-02-09 16:07:27 +0700580 if ($this->pdodriver == 'pgsql')
581 {
582 // Analog function to show all tables in postgre
583 $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'";
584 }
585 else
586 {
587 $sql = "SHOW TABLES FROM `".$this->database."`";
588 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400589
590 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
591 {
Taufan Aditya18209332012-02-09 16:07:27 +0700592 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400593 }
594
595 return $sql;
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
601 * Show column query
602 *
603 * Generates a platform-specific query string so that the column names can be fetched
604 *
605 * @access public
606 * @param string the table name
607 * @return string
608 */
609 function _list_columns($table = '')
610 {
Taufan Aditya18209332012-02-09 16:07:27 +0700611 return 'SHOW COLUMNS FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400612 }
613
614 // --------------------------------------------------------------------
615
616 /**
617 * Field data query
618 *
619 * Generates a platform-specific query so that the column data can be retrieved
620 *
621 * @access public
622 * @param string the table name
623 * @return object
624 */
625 function _field_data($table)
626 {
Taufan Aditya18209332012-02-09 16:07:27 +0700627 return 'SELECT TOP 1 FROM '.$this->_from_tables($table);
Timothy Warren80ab8162011-08-22 18:26:12 -0400628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * The error message string
634 *
635 * @access private
636 * @return string
637 */
638 function _error_message()
639 {
Timothy Warrenab347582011-08-23 12:29:29 -0400640 $error_array = $this->conn_id->errorInfo();
Taufan Aditya18209332012-02-09 16:07:27 +0700641
Timothy Warrenab347582011-08-23 12:29:29 -0400642 return $error_array[2];
Timothy Warren80ab8162011-08-22 18:26:12 -0400643 }
644
645 // --------------------------------------------------------------------
646
647 /**
648 * The error message number
649 *
650 * @access private
651 * @return integer
652 */
653 function _error_number()
654 {
Timothy Warrenab347582011-08-23 12:29:29 -0400655 return $this->conn_id->errorCode();
Timothy Warren80ab8162011-08-22 18:26:12 -0400656 }
657
658 // --------------------------------------------------------------------
659
660 /**
661 * Escape the SQL Identifiers
662 *
663 * This function escapes column and table names
664 *
665 * @access private
666 * @param string
667 * @return string
668 */
669 function _escape_identifiers($item)
670 {
671 if ($this->_escape_char == '')
672 {
673 return $item;
674 }
675
676 foreach ($this->_reserved_identifiers as $id)
677 {
678 if (strpos($item, '.'.$id) !== FALSE)
679 {
680 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
681
682 // remove duplicates if the user already included the escape
683 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
684 }
685 }
686
687 if (strpos($item, '.') !== FALSE)
688 {
Taufan Aditya18209332012-02-09 16:07:27 +0700689 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
690 $str .= $this->_escape_char;
Timothy Warren80ab8162011-08-22 18:26:12 -0400691 }
692 else
693 {
694 $str = $this->_escape_char.$item.$this->_escape_char;
695 }
696
697 // remove duplicates if the user already included the escape
698 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
699 }
700
701 // --------------------------------------------------------------------
702
703 /**
704 * From Tables
705 *
706 * This function implicitly groups FROM tables so there is no confusion
707 * about operator precedence in harmony with SQL standards
708 *
709 * @access public
710 * @param type
711 * @return type
712 */
713 function _from_tables($tables)
714 {
715 if ( ! is_array($tables))
716 {
717 $tables = array($tables);
718 }
719
Taufan Aditya18209332012-02-09 16:07:27 +0700720 return (count($tables) == 1) ? '`'.$tables[0].'`' : '('.implode(', ', $tables).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400721 }
722
723 // --------------------------------------------------------------------
724
725 /**
726 * Insert statement
727 *
728 * Generates a platform-specific insert string from the supplied data
729 *
730 * @access public
731 * @param string the table name
732 * @param array the insert keys
733 * @param array the insert values
734 * @return string
735 */
736 function _insert($table, $keys, $values)
737 {
Taufan Aditya18209332012-02-09 16:07:27 +0700738 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren80ab8162011-08-22 18:26:12 -0400739 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400740
741 // --------------------------------------------------------------------
742
743 /**
744 * Insert_batch statement
745 *
746 * Generates a platform-specific insert string from the supplied data
747 *
748 * @access public
749 * @param string the table name
750 * @param array the insert keys
751 * @param array the insert values
752 * @return string
753 */
754 function _insert_batch($table, $keys, $values)
755 {
Taufan Aditya18209332012-02-09 16:07:27 +0700756 return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400757 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400758
759 // --------------------------------------------------------------------
760
761 /**
762 * Update statement
763 *
764 * Generates a platform-specific update string from the supplied data
765 *
766 * @access public
767 * @param string the table name
768 * @param array the update data
769 * @param array the where clause
770 * @param array the orderby clause
771 * @param array the limit clause
772 * @return string
773 */
774 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
775 {
776 foreach ($values as $key => $val)
777 {
778 $valstr[] = $key." = ".$val;
779 }
780
Taufan Aditya18209332012-02-09 16:07:27 +0700781 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
782 $orderby = (count($orderby) >= 1) ? ' ORDER BY '.implode(', ', $orderby) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400783
Taufan Aditya18209332012-02-09 16:07:27 +0700784 $sql = 'UPDATE '.$this->_from_tables($table).' SET '.implode(', ', $valstr);
785 $sql .= ($where != '' && count($where) >= 1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren80ab8162011-08-22 18:26:12 -0400786 $sql .= $orderby.$limit;
787
788 return $sql;
789 }
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400790
791 // --------------------------------------------------------------------
792
793 /**
794 * Update_Batch statement
795 *
796 * Generates a platform-specific batch update string from the supplied data
797 *
798 * @access public
799 * @param string the table name
800 * @param array the update data
801 * @param array the where clause
802 * @return string
803 */
804 function _update_batch($table, $values, $index, $where = NULL)
805 {
Taufan Aditya18209332012-02-09 16:07:27 +0700806 $ids = array();
807 $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : '';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400808
809 foreach ($values as $key => $val)
810 {
811 $ids[] = $val[$index];
812
813 foreach (array_keys($val) as $field)
814 {
815 if ($field != $index)
816 {
817 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
818 }
819 }
820 }
821
Taufan Aditya18209332012-02-09 16:07:27 +0700822 $sql = 'UPDATE '.$this->_from_tables($table).' SET ';
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400823 $cases = '';
824
825 foreach ($final as $k => $v)
826 {
827 $cases .= $k.' = CASE '."\n";
Taufan Aditya18209332012-02-09 16:07:27 +0700828
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400829 foreach ($v as $row)
830 {
831 $cases .= $row."\n";
832 }
833
834 $cases .= 'ELSE '.$k.' END, ';
835 }
836
837 $sql .= substr($cases, 0, -2);
Timothy Warrenb5a43b02011-10-04 17:26:04 -0400838 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
839
840 return $sql;
841 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400842
843
844 // --------------------------------------------------------------------
845
846 /**
847 * Truncate statement
848 *
849 * Generates a platform-specific truncate string from the supplied data
850 * If the database does not support the truncate() command
851 * This function maps to "DELETE FROM table"
852 *
853 * @access public
854 * @param string the table name
855 * @return string
856 */
857 function _truncate($table)
858 {
859 return $this->_delete($table);
860 }
861
862 // --------------------------------------------------------------------
863
864 /**
865 * Delete statement
866 *
867 * Generates a platform-specific delete string from the supplied data
868 *
869 * @access public
870 * @param string the table name
871 * @param array the where clause
872 * @param string the limit clause
873 * @return string
874 */
875 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
876 {
877 $conditions = '';
878
879 if (count($where) > 0 OR count($like) > 0)
880 {
Taufan Aditya18209332012-02-09 16:07:27 +0700881 $conditions = "\nWHERE ";
Timothy Warren80ab8162011-08-22 18:26:12 -0400882 $conditions .= implode("\n", $this->ar_where);
883
884 if (count($where) > 0 && count($like) > 0)
885 {
886 $conditions .= " AND ";
887 }
Taufan Aditya18209332012-02-09 16:07:27 +0700888
Timothy Warren80ab8162011-08-22 18:26:12 -0400889 $conditions .= implode("\n", $like);
890 }
891
892 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
893
Taufan Aditya18209332012-02-09 16:07:27 +0700894 return 'DELETE FROM '.$this->_from_tables($table).$conditions.$limit;
Timothy Warren80ab8162011-08-22 18:26:12 -0400895 }
896
897 // --------------------------------------------------------------------
898
899 /**
900 * Limit string
901 *
902 * Generates a platform-specific LIMIT clause
903 *
904 * @access public
905 * @param string the sql query string
906 * @param integer the number of rows to limit the query to
907 * @param integer the offset value
908 * @return string
909 */
910 function _limit($sql, $limit, $offset)
911 {
Taufan Aditya18209332012-02-09 16:07:27 +0700912 if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite')
Timothy Warren0a43ad82011-09-15 20:15:19 -0400913 {
Taufan Aditya18209332012-02-09 16:07:27 +0700914 $offset = ($offset == 0) ? '' : $offset.', ';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400915
Taufan Aditya18209332012-02-09 16:07:27 +0700916 return $sql.'LIMIT '.$offset.$limit;
Timothy Warren0a43ad82011-09-15 20:15:19 -0400917 }
918 else
919 {
Taufan Aditya18209332012-02-09 16:07:27 +0700920 $sql .= 'LIMIT '.$limit;
921 $sql .= ($offset > 0) ? ' OFFSET '.$offset : '';
Timothy Warren0a43ad82011-09-15 20:15:19 -0400922
923 return $sql;
924 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400925 }
926
927 // --------------------------------------------------------------------
928
929 /**
930 * Close DB Connection
931 *
932 * @access public
933 * @param resource
934 * @return void
935 */
936 function _close($conn_id)
937 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400938 $this->conn_id = null;
Timothy Warren80ab8162011-08-22 18:26:12 -0400939 }
940
Timothy Warren80ab8162011-08-22 18:26:12 -0400941}
942
Timothy Warren80ab8162011-08-22 18:26:12 -0400943/* End of file pdo_driver.php */
944/* Location: ./system/database/drivers/pdo/pdo_driver.php */