Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 6 | * |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 8 | * |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 10 | * |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 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 Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 19 | * @package CodeIgniter |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 23 | * @link http://codeigniter.com |
Timothy Warren | 018af7a | 2011-09-07 12:07:35 -0400 | [diff] [blame] | 24 | * @since Version 2.1.0 |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 28 | /** |
Timothy Warren | 0261596 | 2011-08-24 08:21:36 -0400 | [diff] [blame] | 29 | * PDO Database Adapter Class |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 30 | * |
| 31 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 32 | * creates dynamically based on whether the query builder |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 33 | * class is being used or not. |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Drivers |
| 37 | * @category Database |
Timothy Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
| 41 | class CI_DB_pdo_driver extends CI_DB { |
| 42 | |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 43 | public $dbdriver = 'pdo'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 44 | |
| 45 | // the character used to excape - not necessary for PDO |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 46 | protected $_escape_char = ''; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 47 | |
| 48 | // clause and character used for LIKE escape sequences |
Andrey Andreev | e9f2095 | 2012-04-06 19:30:41 +0300 | [diff] [blame] | 49 | protected $_like_escape_str = " ESCAPE '%s' "; |
| 50 | protected $_like_escape_chr = '!'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 51 | |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 52 | protected $_random_keyword; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 53 | |
Andrey Andreev | 738f534 | 2012-03-06 20:43:40 +0200 | [diff] [blame] | 54 | // need to track the pdo driver and options |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 55 | public $pdodriver; |
| 56 | public $options = array(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 57 | |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 58 | public function __construct($params) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 59 | { |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 60 | parent::__construct($params); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 61 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 62 | if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) === 2) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 63 | { |
| 64 | // If there is a minimum valid dsn string pattern found, we're done |
Taufan Aditya | fdd6ad0 | 2012-02-10 13:10:27 +0700 | [diff] [blame] | 65 | // This is for general PDO users, who tend to have a full DSN string. |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 66 | $this->pdodriver = end($match); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // Try to build a complete DSN string from params |
| 71 | $this->_connect_string($params); |
| 72 | } |
| 73 | |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 74 | // clause and character used for LIKE escape sequences |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 75 | // this one depends on the driver being used |
Andrey Andreev | def568f | 2012-05-25 01:06:54 +0300 | [diff] [blame] | 76 | if ($this->pdodriver === 'mysql') |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 77 | { |
Andrey Andreev | e9f2095 | 2012-04-06 19:30:41 +0300 | [diff] [blame] | 78 | $this->_escape_char = '`'; |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 79 | $this->_like_escape_str = ''; |
Andrey Andreev | def568f | 2012-05-25 01:06:54 +0300 | [diff] [blame] | 80 | $this->_like_escape_chr = '\\'; |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 81 | } |
Andrey Andreev | def568f | 2012-05-25 01:06:54 +0300 | [diff] [blame] | 82 | elseif ($this->pdodriver === 'odbc') |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 83 | { |
| 84 | $this->_like_escape_str = " {escape '%s'} "; |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 85 | } |
Andrey Andreev | e9f2095 | 2012-04-06 19:30:41 +0300 | [diff] [blame] | 86 | elseif ( ! in_array($this->pdodriver, array('sqlsrv', 'mssql', 'dblib', 'sybase'))) |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 87 | { |
Andrey Andreev | e9f2095 | 2012-04-06 19:30:41 +0300 | [diff] [blame] | 88 | $this->_escape_char = '"'; |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 89 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 90 | |
| 91 | $this->trans_enabled = FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 92 | $this->_random_keyword = ' RND('.time().')'; // database specific random keyword |
| 93 | } |
| 94 | |
| 95 | /** |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 96 | * Connection String |
| 97 | * |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 98 | * @param array |
| 99 | * @return void |
| 100 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 101 | protected function _connect_string($params) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 102 | { |
| 103 | if (strpos($this->hostname, ':')) |
| 104 | { |
| 105 | // hostname generally would have this prototype |
| 106 | // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);'; |
| 107 | // We need to get the prefix (pdodriver used by PDO). |
Timothy Warren | 928d83c | 2012-02-13 14:07:57 -0500 | [diff] [blame] | 108 | $dsnarray = explode(':', $this->hostname); |
| 109 | $this->pdodriver = $dsnarray[0]; |
Taufan Aditya | 5dcdbc3 | 2012-02-13 21:15:56 +0700 | [diff] [blame] | 110 | |
| 111 | // End dsn with a semicolon for extra backward compability |
| 112 | // if database property was not empty. |
Timothy Warren | f452469 | 2012-02-13 14:13:28 -0500 | [diff] [blame] | 113 | if ( ! empty($this->database)) |
Timothy Warren | 25dc755 | 2012-02-13 14:12:35 -0500 | [diff] [blame] | 114 | { |
| 115 | $this->dsn .= rtrim($this->hostname, ';').';'; |
| 116 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 117 | } |
| 118 | else |
| 119 | { |
| 120 | // Invalid DSN, display an error |
| 121 | if ( ! array_key_exists('pdodriver', $params)) |
| 122 | { |
| 123 | show_error('Invalid DB Connection String for PDO'); |
| 124 | } |
| 125 | |
| 126 | // Assuming that the following DSN string format is used: |
| 127 | // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql'; |
| 128 | $this->dsn = $this->pdodriver.':'; |
| 129 | |
| 130 | // Add hostname to the DSN for databases that need it |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 131 | if ( ! empty($this->hostname) |
Timothy Warren | 928d83c | 2012-02-13 14:07:57 -0500 | [diff] [blame] | 132 | && strpos($this->hostname, ':') === FALSE |
| 133 | && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid'))) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 134 | { |
| 135 | $this->dsn .= 'host='.$this->hostname.';'; |
| 136 | } |
| 137 | |
| 138 | // Add a port to the DSN for databases that can use it |
| 139 | if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid'))) |
| 140 | { |
| 141 | $this->dsn .= 'port='.$this->port.';'; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // Add the database name to the DSN, if needed |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 146 | if (stripos($this->dsn, 'dbname') === FALSE |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 147 | && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid'))) |
| 148 | { |
| 149 | $this->dsn .= 'dbname='.$this->database.';'; |
| 150 | } |
| 151 | elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv'))) |
| 152 | { |
| 153 | if (stripos($this->dsn, 'dsn') === FALSE) |
| 154 | { |
| 155 | $this->dsn .= 'database='.$this->database.';'; |
| 156 | } |
| 157 | } |
| 158 | elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:') |
| 159 | { |
| 160 | if ($this->database !== ':memory') |
| 161 | { |
| 162 | if ( ! file_exists($this->database)) |
| 163 | { |
| 164 | show_error('Invalid DB Connection string for PDO SQLite'); |
| 165 | } |
| 166 | |
| 167 | $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : ''; |
| 168 | } |
| 169 | |
| 170 | $this->dsn .= $this->database; |
| 171 | } |
| 172 | |
| 173 | // Add charset to the DSN, if needed |
| 174 | if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci'))) |
| 175 | { |
| 176 | $this->dsn .= 'charset='.$this->char_set.';'; |
| 177 | } |
| 178 | } |
| 179 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 180 | // -------------------------------------------------------------------- |
| 181 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 182 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 183 | * Non-persistent database connection |
| 184 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 185 | * @return object |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 186 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 187 | public function db_connect() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 188 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 189 | return $this->_pdo_connect(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // -------------------------------------------------------------------- |
| 193 | |
| 194 | /** |
| 195 | * Persistent database connection |
| 196 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 197 | * @return object |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 198 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 199 | public function db_pconnect() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 200 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 201 | return $this->_pdo_connect(TRUE); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // -------------------------------------------------------------------- |
| 205 | |
| 206 | /** |
| 207 | * PDO connection |
| 208 | * |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 209 | * @param bool |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 210 | * @return object |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 211 | */ |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 212 | protected function _pdo_connect($persistent = FALSE) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 213 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 214 | $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT; |
| 215 | $persistent === FALSE OR $this->options[PDO::ATTR_PERSISTENT] = TRUE; |
| 216 | |
| 217 | /* Prior to PHP 5.3.6, even if the charset was supplied in the DSN |
| 218 | * on connect - it was ignored. This is a work-around for the issue. |
| 219 | * |
| 220 | * Reference: http://www.php.net/manual/en/ref.pdo-mysql.connection.php |
| 221 | */ |
Taufan Aditya | e16192c | 2012-04-27 02:13:21 +0700 | [diff] [blame] | 222 | if ($this->pdodriver === 'mysql' && ! is_php('5.3.6') && ! empty($this->char_set)) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 223 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 224 | $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES '.$this->char_set |
| 225 | .( ! empty($this->db_collat) ? " COLLATE '".$this->dbcollat."'" : ''); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // Connecting... |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 229 | try |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 230 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 231 | return new PDO($this->dsn, $this->username, $this->password, $this->options); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 232 | } |
| 233 | catch (PDOException $e) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 234 | { |
| 235 | if ($this->db_debug && empty($this->failover)) |
| 236 | { |
| 237 | $this->display_error($e->getMessage(), '', TRUE); |
| 238 | } |
| 239 | |
| 240 | return FALSE; |
| 241 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // -------------------------------------------------------------------- |
| 245 | |
| 246 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 247 | * Database version number |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 248 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 249 | * @return string |
| 250 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 251 | public function version() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 252 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 253 | return isset($this->data_cache['version']) |
| 254 | ? $this->data_cache['version'] |
| 255 | : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // -------------------------------------------------------------------- |
| 259 | |
| 260 | /** |
| 261 | * Execute the query |
| 262 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 263 | * @param string an SQL query |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 264 | * @return mixed |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 265 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 266 | protected function _execute($sql) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 267 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 268 | return $this->conn_id->query($sql); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | // -------------------------------------------------------------------- |
| 272 | |
| 273 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 274 | * Begin Transaction |
| 275 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 276 | * @return bool |
| 277 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 278 | public function trans_begin($test_mode = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 279 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 280 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 281 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 282 | { |
| 283 | return TRUE; |
| 284 | } |
| 285 | |
| 286 | // Reset the transaction failure flag. |
| 287 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 288 | // even if the queries produce a successful result. |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 289 | $this->_trans_failure = ($test_mode === TRUE); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 290 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 291 | return $this->conn_id->beginTransaction(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // -------------------------------------------------------------------- |
| 295 | |
| 296 | /** |
| 297 | * Commit Transaction |
| 298 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 299 | * @return bool |
| 300 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 301 | public function trans_commit() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 302 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 303 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 304 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 305 | { |
| 306 | return TRUE; |
| 307 | } |
| 308 | |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 309 | return $this->conn_id->commit(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // -------------------------------------------------------------------- |
| 313 | |
| 314 | /** |
| 315 | * Rollback Transaction |
| 316 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 317 | * @return bool |
| 318 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 319 | public function trans_rollback() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 320 | { |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 321 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 322 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 323 | { |
| 324 | return TRUE; |
| 325 | } |
| 326 | |
Andrey Andreev | 80144bf | 2012-04-06 22:19:26 +0300 | [diff] [blame] | 327 | return $this->conn_id->rollBack(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // -------------------------------------------------------------------- |
| 331 | |
| 332 | /** |
| 333 | * Escape String |
| 334 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 335 | * @param string |
| 336 | * @param bool whether or not the string will be used in a LIKE condition |
| 337 | * @return string |
| 338 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 339 | public function escape_str($str, $like = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 340 | { |
| 341 | if (is_array($str)) |
| 342 | { |
| 343 | foreach ($str as $key => $val) |
| 344 | { |
| 345 | $str[$key] = $this->escape_str($val, $like); |
| 346 | } |
| 347 | |
| 348 | return $str; |
| 349 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 350 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 351 | // Escape the string |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 352 | $str = $this->conn_id->quote($str); |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 353 | |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 354 | // If there are duplicated quotes, trim them away |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 355 | if (strpos($str, "'") === 0) |
Timothy Warren | ec19332 | 2011-10-07 09:53:35 -0400 | [diff] [blame] | 356 | { |
| 357 | $str = substr($str, 1, -1); |
| 358 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 359 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 360 | // escape LIKE condition wildcards |
| 361 | if ($like === TRUE) |
| 362 | { |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 363 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 364 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 365 | $str); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | return $str; |
| 369 | } |
| 370 | |
| 371 | // -------------------------------------------------------------------- |
| 372 | |
| 373 | /** |
| 374 | * Affected Rows |
| 375 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 376 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 377 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 378 | public function affected_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 379 | { |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 380 | return is_object($this->result_id) ? $this->result_id->rowCount() : 0; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // -------------------------------------------------------------------- |
| 384 | |
| 385 | /** |
| 386 | * Insert ID |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 387 | * |
Andrey Andreev | 2387ed3 | 2012-04-07 00:11:14 +0300 | [diff] [blame] | 388 | * @param string |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 389 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 390 | */ |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 391 | public function insert_id($name = NULL) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 392 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 393 | if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1') |
Timothy Warren | 3351275 | 2011-10-07 09:51:49 -0400 | [diff] [blame] | 394 | { |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 395 | $query = $this->query('SELECT LASTVAL() AS ins_id'); |
| 396 | $query = $query->row(); |
| 397 | return $query->ins_id; |
Timothy Warren | 3351275 | 2011-10-07 09:51:49 -0400 | [diff] [blame] | 398 | } |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 399 | |
| 400 | return $this->conn_id->lastInsertId($name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // -------------------------------------------------------------------- |
| 404 | |
| 405 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 406 | * Show table query |
| 407 | * |
| 408 | * Generates a platform-specific query string so that the table names can be fetched |
| 409 | * |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 410 | * @param bool |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 411 | * @return string |
| 412 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 413 | protected function _list_tables($prefix_limit = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 414 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 415 | if ($this->pdodriver === 'pgsql') |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 416 | { |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 417 | // Analog function to show all tables in postgre |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 418 | $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'"; |
| 419 | } |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 420 | elseif ($this->pdodriver === 'sqlite') |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 421 | { |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 422 | // Analog function to show all tables in sqlite |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 423 | $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"; |
| 424 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 425 | else |
| 426 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 427 | $sql = 'SHOW TABLES FROM '.$this->escape_identifiers($this->database); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 428 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 429 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 430 | if ($prefix_limit !== FALSE AND $this->dbprefix !== '') |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 431 | { |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 432 | return FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | return $sql; |
| 436 | } |
| 437 | |
| 438 | // -------------------------------------------------------------------- |
| 439 | |
| 440 | /** |
| 441 | * Show column query |
| 442 | * |
| 443 | * Generates a platform-specific query string so that the column names can be fetched |
| 444 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 445 | * @param string the table name |
| 446 | * @return string |
| 447 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 448 | protected function _list_columns($table = '') |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 449 | { |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 450 | return 'SHOW COLUMNS FROM '.$this->escape_identifiers($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // -------------------------------------------------------------------- |
| 454 | |
| 455 | /** |
| 456 | * Field data query |
| 457 | * |
| 458 | * Generates a platform-specific query so that the column data can be retrieved |
| 459 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 460 | * @param string the table name |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 461 | * @return string |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 462 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 463 | protected function _field_data($table) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 464 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 465 | if ($this->pdodriver === 'mysql' or $this->pdodriver === 'pgsql') |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 466 | { |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 467 | // Analog function for mysql and postgre |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 468 | return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1'; |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 469 | } |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 470 | elseif ($this->pdodriver === 'oci') |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 471 | { |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 472 | // Analog function for oci |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 473 | return 'SELECT * FROM '.$this->escape_identifiers($table).' WHERE ROWNUM <= 1'; |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 474 | } |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 475 | elseif ($this->pdodriver === 'sqlite') |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 476 | { |
Timothy Warren | 667c9fe | 2012-03-19 19:06:34 -0400 | [diff] [blame] | 477 | // Analog function for sqlite |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 478 | return 'PRAGMA table_info('.$this->escape_identifiers($table).')'; |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 479 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 480 | |
Andrey Andreev | ea09a8a | 2012-04-06 20:50:07 +0300 | [diff] [blame] | 481 | return 'SELECT TOP 1 FROM '.$this->escape_identifiers($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | // -------------------------------------------------------------------- |
| 485 | |
| 486 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 487 | * Error |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 488 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 489 | * Returns an array containing code and message of the last |
| 490 | * database error that has occured. |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 491 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 492 | * @return array |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 493 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 494 | public function error() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 495 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 496 | $error = array('code' => '00000', 'message' => ''); |
| 497 | $pdo_error = $this->conn_id->errorInfo(); |
| 498 | |
| 499 | if (empty($pdo_error[0])) |
| 500 | { |
| 501 | return $error; |
| 502 | } |
| 503 | |
| 504 | $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0]; |
| 505 | if (isset($pdo_error[2])) |
| 506 | { |
| 507 | $error['message'] = $pdo_error[2]; |
| 508 | } |
| 509 | |
| 510 | return $error; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | // -------------------------------------------------------------------- |
| 514 | |
| 515 | /** |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 516 | * Update_Batch statement |
| 517 | * |
| 518 | * Generates a platform-specific batch update string from the supplied data |
| 519 | * |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 520 | * @param string the table name |
| 521 | * @param array the update data |
| 522 | * @param array the where clause |
| 523 | * @return string |
| 524 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 525 | protected function _update_batch($table, $values, $index, $where = NULL) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 526 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 527 | $ids = array(); |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 528 | $where = ($where !== '' && count($where) >=1) ? implode(" ", $where).' AND ' : ''; |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 529 | |
| 530 | foreach ($values as $key => $val) |
| 531 | { |
| 532 | $ids[] = $val[$index]; |
| 533 | |
| 534 | foreach (array_keys($val) as $field) |
| 535 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 536 | if ($field !== $index) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 537 | { |
| 538 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
Andrey Andreev | e9f2095 | 2012-04-06 19:30:41 +0300 | [diff] [blame] | 543 | $sql = 'UPDATE '.$table.' SET '; |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 544 | $cases = ''; |
| 545 | |
| 546 | foreach ($final as $k => $v) |
| 547 | { |
| 548 | $cases .= $k.' = CASE '."\n"; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 549 | |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 550 | foreach ($v as $row) |
| 551 | { |
| 552 | $cases .= $row."\n"; |
| 553 | } |
| 554 | |
| 555 | $cases .= 'ELSE '.$k.' END, '; |
| 556 | } |
| 557 | |
| 558 | $sql .= substr($cases, 0, -2); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 559 | $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; |
| 560 | |
| 561 | return $sql; |
| 562 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 563 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 564 | // -------------------------------------------------------------------- |
| 565 | |
| 566 | /** |
| 567 | * Truncate statement |
| 568 | * |
| 569 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 570 | * |
| 571 | * If the database does not support the truncate() command, |
| 572 | * then this method maps to 'DELETE FROM table' |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 573 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 574 | * @param string the table name |
| 575 | * @return string |
| 576 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 577 | protected function _truncate($table) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 578 | { |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 579 | return 'DELETE FROM '.$table; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // -------------------------------------------------------------------- |
| 583 | |
| 584 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 585 | * Limit string |
| 586 | * |
| 587 | * Generates a platform-specific LIMIT clause |
| 588 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 589 | * @param string the sql query string |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 590 | * @param int the number of rows to limit the query to |
| 591 | * @param int the offset value |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 592 | * @return string |
| 593 | */ |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 594 | protected function _limit($sql, $limit, $offset) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 595 | { |
Andrey Andreev | 2c35b64 | 2012-06-24 03:05:26 +0300 | [diff] [blame] | 596 | if ($this->pdodriver === 'pgsql') |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 597 | { |
Andrey Andreev | 2c35b64 | 2012-06-24 03:05:26 +0300 | [diff] [blame] | 598 | return $sql.' LIMIT '.$limit.($offset ? ' OFFSET '.$offset : ''); |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 599 | } |
Andrey Andreev | bd44d5a | 2012-03-20 22:59:29 +0200 | [diff] [blame] | 600 | |
Andrey Andreev | 2c35b64 | 2012-06-24 03:05:26 +0300 | [diff] [blame] | 601 | return $sql.' LIMIT '.($offset ? $offset.', ' : '').$limit; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 602 | } |
| 603 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 604 | } |
| 605 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 606 | /* End of file pdo_driver.php */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 607 | /* Location: ./system/database/drivers/pdo/pdo_driver.php */ |