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