Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 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 |
| 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 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 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
Timothy Warren | 0261596 | 2011-08-24 08:21:36 -0400 | [diff] [blame] | 31 | * PDO Database Adapter Class |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 32 | * |
| 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 Warren | d1a5ba2 | 2011-10-21 04:45:28 -0400 | [diff] [blame] | 40 | * @author EllisLab Dev Team |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 41 | * @link http://codeigniter.com/user_guide/database/ |
| 42 | */ |
| 43 | class CI_DB_pdo_driver extends CI_DB { |
| 44 | |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 45 | public $dbdriver = 'pdo'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 46 | |
| 47 | // the character used to excape - not necessary for PDO |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 48 | protected $_escape_char = ''; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 49 | |
| 50 | // clause and character used for LIKE escape sequences |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 51 | protected $_like_escape_str; |
| 52 | protected $_like_escape_chr; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * The syntax to count rows is slightly different across different |
| 56 | * database engines, so this string appears in each driver and is |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 57 | * used for the count_all() and count_all_results() public functions. |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 58 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 59 | protected $_count_string = "SELECT COUNT(*) AS "; |
| 60 | protected $_random_keyword; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 61 | |
Andrey Andreev | 738f534 | 2012-03-06 20:43:40 +0200 | [diff] [blame] | 62 | // need to track the pdo driver and options |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 63 | protected $pdodriver; |
| 64 | protected $options = array(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 65 | |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 66 | /** |
| 67 | * Pre-connection setup |
| 68 | */ |
| 69 | public function __construct($params) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 70 | { |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 71 | parent::__construct($params); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 72 | |
| 73 | if (preg_match('/([^;]+):/', $this->dsn, $match) && count($match) == 2) |
| 74 | { |
| 75 | // 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] | 76 | // 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] | 77 | $this->pdodriver = end($match); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | // Try to build a complete DSN string from params |
| 82 | $this->_connect_string($params); |
| 83 | } |
| 84 | |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 85 | // clause and character used for LIKE escape sequences |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 86 | // this one depends on the driver being used |
| 87 | if ($this->pdodriver == 'mysql') |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 88 | { |
| 89 | $this->_like_escape_str = ''; |
| 90 | $this->_like_escape_chr = ''; |
| 91 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 92 | elseif ($this->pdodriver == 'odbc') |
Timothy Warren | c7ba664 | 2011-09-14 12:25:14 -0400 | [diff] [blame] | 93 | { |
| 94 | $this->_like_escape_str = " {escape '%s'} "; |
| 95 | $this->_like_escape_chr = '!'; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | $this->_like_escape_str = " ESCAPE '%s' "; |
| 100 | $this->_like_escape_chr = '!'; |
| 101 | } |
| 102 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 103 | $this->trans_enabled = FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 104 | $this->_random_keyword = ' RND('.time().')'; // database specific random keyword |
| 105 | } |
| 106 | |
| 107 | /** |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 108 | * Connection String |
| 109 | * |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 110 | * @param array |
| 111 | * @return void |
| 112 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 113 | protected function _connect_string($params) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 114 | { |
| 115 | if (strpos($this->hostname, ':')) |
| 116 | { |
| 117 | // hostname generally would have this prototype |
| 118 | // $db['hostname'] = 'pdodriver:host(/Server(/DSN))=hostname(/DSN);'; |
| 119 | // We need to get the prefix (pdodriver used by PDO). |
Timothy Warren | 928d83c | 2012-02-13 14:07:57 -0500 | [diff] [blame] | 120 | $dsnarray = explode(':', $this->hostname); |
| 121 | $this->pdodriver = $dsnarray[0]; |
Taufan Aditya | 5dcdbc3 | 2012-02-13 21:15:56 +0700 | [diff] [blame] | 122 | |
| 123 | // End dsn with a semicolon for extra backward compability |
| 124 | // if database property was not empty. |
Timothy Warren | f452469 | 2012-02-13 14:13:28 -0500 | [diff] [blame] | 125 | if ( ! empty($this->database)) |
Timothy Warren | 25dc755 | 2012-02-13 14:12:35 -0500 | [diff] [blame] | 126 | { |
| 127 | $this->dsn .= rtrim($this->hostname, ';').';'; |
| 128 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | { |
| 132 | // Invalid DSN, display an error |
| 133 | if ( ! array_key_exists('pdodriver', $params)) |
| 134 | { |
| 135 | show_error('Invalid DB Connection String for PDO'); |
| 136 | } |
| 137 | |
| 138 | // Assuming that the following DSN string format is used: |
| 139 | // $dsn = 'pdo://username:password@hostname:port/database?pdodriver=pgsql'; |
| 140 | $this->dsn = $this->pdodriver.':'; |
| 141 | |
| 142 | // Add hostname to the DSN for databases that need it |
Timothy Warren | 928d83c | 2012-02-13 14:07:57 -0500 | [diff] [blame] | 143 | if ( ! empty($this->hostname) |
| 144 | && strpos($this->hostname, ':') === FALSE |
| 145 | && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'sybase', 'mssql', 'dblib', 'cubrid'))) |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 146 | { |
| 147 | $this->dsn .= 'host='.$this->hostname.';'; |
| 148 | } |
| 149 | |
| 150 | // Add a port to the DSN for databases that can use it |
| 151 | if ( ! empty($this->port) && in_array($this->pdodriver, array('informix', 'mysql', 'pgsql', 'ibm', 'cubrid'))) |
| 152 | { |
| 153 | $this->dsn .= 'port='.$this->port.';'; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Add the database name to the DSN, if needed |
| 158 | if (stripos($this->dsn, 'dbname') === FALSE |
| 159 | && in_array($this->pdodriver, array('4D', 'pgsql', 'mysql', 'firebird', 'sybase', 'mssql', 'dblib', 'cubrid'))) |
| 160 | { |
| 161 | $this->dsn .= 'dbname='.$this->database.';'; |
| 162 | } |
| 163 | elseif (stripos($this->dsn, 'database') === FALSE && in_array($this->pdodriver, array('ibm', 'sqlsrv'))) |
| 164 | { |
| 165 | if (stripos($this->dsn, 'dsn') === FALSE) |
| 166 | { |
| 167 | $this->dsn .= 'database='.$this->database.';'; |
| 168 | } |
| 169 | } |
| 170 | elseif ($this->pdodriver === 'sqlite' && $this->dsn === 'sqlite:') |
| 171 | { |
| 172 | if ($this->database !== ':memory') |
| 173 | { |
| 174 | if ( ! file_exists($this->database)) |
| 175 | { |
| 176 | show_error('Invalid DB Connection string for PDO SQLite'); |
| 177 | } |
| 178 | |
| 179 | $this->dsn .= (strpos($this->database, DIRECTORY_SEPARATOR) !== 0) ? DIRECTORY_SEPARATOR : ''; |
| 180 | } |
| 181 | |
| 182 | $this->dsn .= $this->database; |
| 183 | } |
| 184 | |
| 185 | // Add charset to the DSN, if needed |
| 186 | if ( ! empty($this->char_set) && in_array($this->pdodriver, array('4D', 'mysql', 'sybase', 'mssql', 'dblib', 'oci'))) |
| 187 | { |
| 188 | $this->dsn .= 'charset='.$this->char_set.';'; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 193 | * Non-persistent database connection |
| 194 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 195 | * @return resource |
| 196 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 197 | protected function db_connect() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 198 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 199 | $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT; |
| 200 | |
| 201 | return $this->pdo_connect(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // -------------------------------------------------------------------- |
| 205 | |
| 206 | /** |
| 207 | * Persistent database connection |
| 208 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 209 | * @return resource |
| 210 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 211 | protected function db_pconnect() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 212 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 213 | $this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT; |
| 214 | $this->options[PDO::ATTR_PERSISTENT] = TRUE; |
Timothy Warren | d93393f | 2011-10-26 11:31:49 -0400 | [diff] [blame] | 215 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 216 | return $this->pdo_connect(); |
| 217 | } |
| 218 | |
| 219 | // -------------------------------------------------------------------- |
| 220 | |
| 221 | /** |
| 222 | * PDO connection |
| 223 | * |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 224 | * @return resource |
| 225 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 226 | protected function pdo_connect() |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 227 | { |
Taufan Aditya | 62b8cae | 2012-02-09 16:40:39 +0700 | [diff] [blame] | 228 | // Refer : http://php.net/manual/en/ref.pdo-mysql.connection.php |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 229 | if ($this->pdodriver == 'mysql' && is_php('5.3.6')) |
| 230 | { |
Taufan Aditya | 55bb97e | 2012-02-09 16:43:26 +0700 | [diff] [blame] | 231 | $this->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES $this->char_set COLLATE '$this->dbcollat'"; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // Connecting... |
| 235 | try |
| 236 | { |
| 237 | $db = new PDO($this->dsn, $this->username, $this->password, $this->options); |
| 238 | } |
| 239 | catch (PDOException $e) |
| 240 | { |
| 241 | if ($this->db_debug && empty($this->failover)) |
| 242 | { |
| 243 | $this->display_error($e->getMessage(), '', TRUE); |
| 244 | } |
| 245 | |
| 246 | return FALSE; |
| 247 | } |
| 248 | |
| 249 | return $db; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // -------------------------------------------------------------------- |
| 253 | |
| 254 | /** |
| 255 | * Reconnect |
| 256 | * |
| 257 | * Keep / reestablish the db connection if no queries have been |
| 258 | * sent for a length of time exceeding the server's idle timeout |
| 259 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 260 | * @return void |
| 261 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 262 | public function reconnect() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 263 | { |
Timothy Warren | 0261596 | 2011-08-24 08:21:36 -0400 | [diff] [blame] | 264 | if ($this->db->db_debug) |
| 265 | { |
| 266 | return $this->db->display_error('db_unsuported_feature'); |
| 267 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 268 | |
Timothy Warren | 0261596 | 2011-08-24 08:21:36 -0400 | [diff] [blame] | 269 | return FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // -------------------------------------------------------------------- |
| 273 | |
| 274 | /** |
| 275 | * Select the database |
| 276 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 277 | * @return resource |
| 278 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 279 | protected function db_select() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 280 | { |
| 281 | // Not needed for PDO |
| 282 | return TRUE; |
| 283 | } |
| 284 | |
| 285 | // -------------------------------------------------------------------- |
| 286 | |
| 287 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 288 | * Database version number |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 289 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 290 | * @return string |
| 291 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 292 | public function version() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 293 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 294 | return isset($this->data_cache['version']) |
| 295 | ? $this->data_cache['version'] |
| 296 | : $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // -------------------------------------------------------------------- |
| 300 | |
| 301 | /** |
| 302 | * Execute the query |
| 303 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 304 | * @param string an SQL query |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 305 | * @return object |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 306 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 307 | protected function _execute($sql) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 308 | { |
| 309 | $sql = $this->_prep_query($sql); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 310 | |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 311 | $result_id = $this->conn_id->query($sql); |
| 312 | |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 313 | if (is_object($result_id)) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 314 | { |
| 315 | $this->affect_rows = $result_id->rowCount(); |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | $this->affect_rows = 0; |
| 320 | } |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 321 | |
| 322 | return $result_id; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // -------------------------------------------------------------------- |
| 326 | |
| 327 | /** |
| 328 | * Prep the query |
| 329 | * |
| 330 | * If needed, each database adapter can prep the query string |
| 331 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 332 | * @param string an SQL query |
| 333 | * @return string |
| 334 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 335 | protected function _prep_query($sql) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 336 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 337 | if ($this->pdodriver === 'pgsql') |
| 338 | { |
| 339 | // Change the backtick(s) for Postgre |
| 340 | $sql = str_replace('`', '"', $sql); |
| 341 | } |
| 342 | elseif ($this->pdodriver === 'sqlite') |
| 343 | { |
| 344 | // Change the backtick(s) for SQLite |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 345 | $sql = str_replace('`', '"', $sql); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 346 | } |
| 347 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 348 | return $sql; |
| 349 | } |
| 350 | |
| 351 | // -------------------------------------------------------------------- |
| 352 | |
| 353 | /** |
| 354 | * Begin Transaction |
| 355 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 356 | * @return bool |
| 357 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 358 | public function trans_begin($test_mode = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 359 | { |
| 360 | if ( ! $this->trans_enabled) |
| 361 | { |
| 362 | return TRUE; |
| 363 | } |
| 364 | |
| 365 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 366 | if ($this->_trans_depth > 0) |
| 367 | { |
| 368 | return TRUE; |
| 369 | } |
| 370 | |
| 371 | // Reset the transaction failure flag. |
| 372 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 373 | // even if the queries produce a successful result. |
Timothy Warren | d019fd6 | 2011-10-26 11:26:17 -0400 | [diff] [blame] | 374 | $this->_trans_failure = (bool) ($test_mode === TRUE); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 375 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 376 | return $this->conn_id->beginTransaction(); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // -------------------------------------------------------------------- |
| 380 | |
| 381 | /** |
| 382 | * Commit Transaction |
| 383 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 384 | * @return bool |
| 385 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 386 | public function trans_commit() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 387 | { |
| 388 | if ( ! $this->trans_enabled) |
| 389 | { |
| 390 | return TRUE; |
| 391 | } |
| 392 | |
| 393 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 394 | if ($this->_trans_depth > 0) |
| 395 | { |
| 396 | return TRUE; |
| 397 | } |
| 398 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 399 | $ret = $this->conn->commit(); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 400 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 401 | return $ret; |
| 402 | } |
| 403 | |
| 404 | // -------------------------------------------------------------------- |
| 405 | |
| 406 | /** |
| 407 | * Rollback Transaction |
| 408 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 409 | * @return bool |
| 410 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 411 | public function trans_rollback() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 412 | { |
| 413 | if ( ! $this->trans_enabled) |
| 414 | { |
| 415 | return TRUE; |
| 416 | } |
| 417 | |
| 418 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 419 | if ($this->_trans_depth > 0) |
| 420 | { |
| 421 | return TRUE; |
| 422 | } |
| 423 | |
Timothy Warren | ab34758 | 2011-08-23 12:29:29 -0400 | [diff] [blame] | 424 | $ret = $this->conn_id->rollBack(); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 425 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 426 | return $ret; |
| 427 | } |
| 428 | |
| 429 | // -------------------------------------------------------------------- |
| 430 | |
| 431 | /** |
| 432 | * Escape String |
| 433 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 434 | * @param string |
| 435 | * @param bool whether or not the string will be used in a LIKE condition |
| 436 | * @return string |
| 437 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 438 | public function escape_str($str, $like = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 439 | { |
| 440 | if (is_array($str)) |
| 441 | { |
| 442 | foreach ($str as $key => $val) |
| 443 | { |
| 444 | $str[$key] = $this->escape_str($val, $like); |
| 445 | } |
| 446 | |
| 447 | return $str; |
| 448 | } |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 449 | |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 450 | //Escape the string |
| 451 | $str = $this->conn_id->quote($str); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 452 | |
Timothy Warren | 4766397 | 2011-10-05 16:44:50 -0400 | [diff] [blame] | 453 | //If there are duplicated quotes, trim them away |
Timothy Warren | d669153 | 2011-10-07 10:03:01 -0400 | [diff] [blame] | 454 | if (strpos($str, "'") === 0) |
Timothy Warren | ec19332 | 2011-10-07 09:53:35 -0400 | [diff] [blame] | 455 | { |
| 456 | $str = substr($str, 1, -1); |
| 457 | } |
| 458 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 459 | // escape LIKE condition wildcards |
| 460 | if ($like === TRUE) |
| 461 | { |
| 462 | $str = str_replace( array('%', '_', $this->_like_escape_chr), |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 463 | array($this->_like_escape_chr.'%', |
| 464 | $this->_like_escape_chr.'_', |
| 465 | $this->_like_escape_chr.$this->_like_escape_chr), |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 466 | $str); |
| 467 | } |
| 468 | |
| 469 | return $str; |
| 470 | } |
| 471 | |
| 472 | // -------------------------------------------------------------------- |
| 473 | |
| 474 | /** |
| 475 | * Affected Rows |
| 476 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 477 | * @return integer |
| 478 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 479 | public function affected_rows() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 480 | { |
Timothy Warren | 51a4888 | 2011-09-14 13:47:06 -0400 | [diff] [blame] | 481 | return $this->affect_rows; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | // -------------------------------------------------------------------- |
| 485 | |
| 486 | /** |
| 487 | * Insert ID |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 488 | * |
| 489 | * @return int |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 490 | */ |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 491 | public function insert_id($name = NULL) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 492 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 493 | if ($this->pdodriver === 'pgsql' && $name === NULL && $this->version() >= '8.1') |
Timothy Warren | 3351275 | 2011-10-07 09:51:49 -0400 | [diff] [blame] | 494 | { |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 495 | $query = $this->query('SELECT LASTVAL() AS ins_id'); |
| 496 | $query = $query->row(); |
| 497 | return $query->ins_id; |
Timothy Warren | 3351275 | 2011-10-07 09:51:49 -0400 | [diff] [blame] | 498 | } |
Andrey Andreev | a39d699 | 2012-03-01 19:11:39 +0200 | [diff] [blame] | 499 | |
| 500 | return $this->conn_id->lastInsertId($name); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | // -------------------------------------------------------------------- |
| 504 | |
| 505 | /** |
| 506 | * "Count All" query |
| 507 | * |
| 508 | * Generates a platform-specific query string that counts all records in |
| 509 | * the specified database |
| 510 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 511 | * @param string |
| 512 | * @return string |
| 513 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 514 | public function count_all($table = '') |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 515 | { |
| 516 | if ($table == '') |
| 517 | { |
| 518 | return 0; |
| 519 | } |
| 520 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 521 | $sql = $this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 522 | $query = $this->query($sql); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 523 | |
| 524 | if ($query->num_rows() == 0) |
| 525 | { |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | $row = $query->row(); |
| 530 | $this->_reset_select(); |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 531 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 532 | return (int) $row->numrows; |
| 533 | } |
| 534 | |
| 535 | // -------------------------------------------------------------------- |
| 536 | |
| 537 | /** |
| 538 | * Show table query |
| 539 | * |
| 540 | * Generates a platform-specific query string so that the table names can be fetched |
| 541 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 542 | * @param boolean |
| 543 | * @return string |
| 544 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 545 | protected function _list_tables($prefix_limit = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 546 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 547 | if ($this->pdodriver == 'pgsql') |
| 548 | { |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 549 | // Analog public function to show all tables in postgre |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 550 | $sql = "SELECT * FROM information_schema.tables WHERE table_schema = 'public'"; |
| 551 | } |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 552 | elseif ($this->pdodriver == 'sqlite') |
| 553 | { |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 554 | // Analog public function to show all tables in sqlite |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 555 | $sql = "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"; |
| 556 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 557 | else |
| 558 | { |
| 559 | $sql = "SHOW TABLES FROM `".$this->database."`"; |
| 560 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 561 | |
| 562 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 563 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 564 | return FALSE; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | return $sql; |
| 568 | } |
| 569 | |
| 570 | // -------------------------------------------------------------------- |
| 571 | |
| 572 | /** |
| 573 | * Show column query |
| 574 | * |
| 575 | * Generates a platform-specific query string so that the column names can be fetched |
| 576 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 577 | * @param string the table name |
| 578 | * @return string |
| 579 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 580 | public function _list_columns($table = '') |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 581 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 582 | return 'SHOW COLUMNS FROM '.$this->_from_tables($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | // -------------------------------------------------------------------- |
| 586 | |
| 587 | /** |
| 588 | * Field data query |
| 589 | * |
| 590 | * Generates a platform-specific query so that the column data can be retrieved |
| 591 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 592 | * @param string the table name |
| 593 | * @return object |
| 594 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 595 | public function _field_data($table) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 596 | { |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 597 | if ($this->pdodriver == 'mysql' or $this->pdodriver == 'pgsql') |
| 598 | { |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 599 | // Analog public function for mysql and postgre |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 600 | return 'SELECT * FROM '.$this->_from_tables($table).' LIMIT 1'; |
| 601 | } |
| 602 | elseif ($this->pdodriver == 'oci') |
| 603 | { |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 604 | // Analog public function for oci |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 605 | return 'SELECT * FROM '.$this->_from_tables($table).' WHERE ROWNUM <= 1'; |
| 606 | } |
| 607 | elseif ($this->pdodriver == 'sqlite') |
| 608 | { |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 609 | // Analog public function for sqlite |
Taufan Aditya | 4e44b34 | 2012-02-18 22:47:36 +0700 | [diff] [blame] | 610 | return 'PRAGMA table_info('.$this->_from_tables($table).')'; |
| 611 | } |
| 612 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 613 | return 'SELECT TOP 1 FROM '.$this->_from_tables($table); |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | // -------------------------------------------------------------------- |
| 617 | |
| 618 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 619 | * Error |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 620 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 621 | * Returns an array containing code and message of the last |
| 622 | * database error that has occured. |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 623 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 624 | * @return array |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 625 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 626 | public function error() |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 627 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 628 | $error = array('code' => '00000', 'message' => ''); |
| 629 | $pdo_error = $this->conn_id->errorInfo(); |
| 630 | |
| 631 | if (empty($pdo_error[0])) |
| 632 | { |
| 633 | return $error; |
| 634 | } |
| 635 | |
| 636 | $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0]; |
| 637 | if (isset($pdo_error[2])) |
| 638 | { |
| 639 | $error['message'] = $pdo_error[2]; |
| 640 | } |
| 641 | |
| 642 | return $error; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | // -------------------------------------------------------------------- |
| 646 | |
| 647 | /** |
| 648 | * Escape the SQL Identifiers |
| 649 | * |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 650 | * This public function escapes column and table names |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 651 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 652 | * @param string |
| 653 | * @return string |
| 654 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 655 | protected function _escape_identifiers($item) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 656 | { |
| 657 | if ($this->_escape_char == '') |
| 658 | { |
| 659 | return $item; |
| 660 | } |
| 661 | |
| 662 | foreach ($this->_reserved_identifiers as $id) |
| 663 | { |
| 664 | if (strpos($item, '.'.$id) !== FALSE) |
| 665 | { |
| 666 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 667 | |
| 668 | // remove duplicates if the user already included the escape |
| 669 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | if (strpos($item, '.') !== FALSE) |
| 674 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 675 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); |
| 676 | $str .= $this->_escape_char; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 677 | } |
| 678 | else |
| 679 | { |
| 680 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 681 | } |
| 682 | |
| 683 | // remove duplicates if the user already included the escape |
| 684 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 685 | } |
| 686 | |
| 687 | // -------------------------------------------------------------------- |
| 688 | |
| 689 | /** |
| 690 | * From Tables |
| 691 | * |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 692 | * This public function implicitly groups FROM tables so there is no confusion |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 693 | * about operator precedence in harmony with SQL standards |
| 694 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 695 | * @param type |
| 696 | * @return type |
| 697 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 698 | public function _from_tables($tables) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 699 | { |
| 700 | if ( ! is_array($tables)) |
| 701 | { |
| 702 | $tables = array($tables); |
| 703 | } |
| 704 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 705 | return (count($tables) == 1) ? '`'.$tables[0].'`' : '('.implode(', ', $tables).')'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // -------------------------------------------------------------------- |
| 709 | |
| 710 | /** |
| 711 | * Insert statement |
| 712 | * |
| 713 | * Generates a platform-specific insert string from the supplied data |
| 714 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 715 | * @param string the table name |
| 716 | * @param array the insert keys |
| 717 | * @param array the insert values |
| 718 | * @return string |
| 719 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 720 | public function _insert($table, $keys, $values) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 721 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 722 | return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 723 | } |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 724 | |
| 725 | // -------------------------------------------------------------------- |
| 726 | |
| 727 | /** |
| 728 | * Insert_batch statement |
| 729 | * |
| 730 | * Generates a platform-specific insert string from the supplied data |
| 731 | * |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 732 | * @param string the table name |
| 733 | * @param array the insert keys |
| 734 | * @param array the insert values |
| 735 | * @return string |
| 736 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 737 | public function _insert_batch($table, $keys, $values) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 738 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 739 | return 'INSERT INTO '.$this->_from_tables($table).' ('.implode(', ', $keys).') VALUES '.implode(', ', $values); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 740 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 741 | |
| 742 | // -------------------------------------------------------------------- |
| 743 | |
| 744 | /** |
| 745 | * Update statement |
| 746 | * |
| 747 | * Generates a platform-specific update string from the supplied data |
| 748 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 749 | * @param string the table name |
| 750 | * @param array the update data |
| 751 | * @param array the where clause |
| 752 | * @param array the orderby clause |
| 753 | * @param array the limit clause |
| 754 | * @return string |
| 755 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 756 | public function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 757 | { |
| 758 | foreach ($values as $key => $val) |
| 759 | { |
| 760 | $valstr[] = $key." = ".$val; |
| 761 | } |
| 762 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 763 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
| 764 | $orderby = (count($orderby) >= 1) ? ' ORDER BY '.implode(', ', $orderby) : ''; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 765 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 766 | $sql = 'UPDATE '.$this->_from_tables($table).' SET '.implode(', ', $valstr); |
| 767 | $sql .= ($where != '' && count($where) >= 1) ? ' WHERE '.implode(' ', $where) : ''; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 768 | $sql .= $orderby.$limit; |
| 769 | |
| 770 | return $sql; |
| 771 | } |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 772 | |
| 773 | // -------------------------------------------------------------------- |
| 774 | |
| 775 | /** |
| 776 | * Update_Batch statement |
| 777 | * |
| 778 | * Generates a platform-specific batch update string from the supplied data |
| 779 | * |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 780 | * @param string the table name |
| 781 | * @param array the update data |
| 782 | * @param array the where clause |
| 783 | * @return string |
| 784 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 785 | public function _update_batch($table, $values, $index, $where = NULL) |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 786 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 787 | $ids = array(); |
| 788 | $where = ($where != '' && count($where) >=1) ? implode(" ", $where).' AND ' : ''; |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 789 | |
| 790 | foreach ($values as $key => $val) |
| 791 | { |
| 792 | $ids[] = $val[$index]; |
| 793 | |
| 794 | foreach (array_keys($val) as $field) |
| 795 | { |
| 796 | if ($field != $index) |
| 797 | { |
| 798 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 803 | $sql = 'UPDATE '.$this->_from_tables($table).' SET '; |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 804 | $cases = ''; |
| 805 | |
| 806 | foreach ($final as $k => $v) |
| 807 | { |
| 808 | $cases .= $k.' = CASE '."\n"; |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 809 | |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 810 | foreach ($v as $row) |
| 811 | { |
| 812 | $cases .= $row."\n"; |
| 813 | } |
| 814 | |
| 815 | $cases .= 'ELSE '.$k.' END, '; |
| 816 | } |
| 817 | |
| 818 | $sql .= substr($cases, 0, -2); |
Timothy Warren | b5a43b0 | 2011-10-04 17:26:04 -0400 | [diff] [blame] | 819 | $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; |
| 820 | |
| 821 | return $sql; |
| 822 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 823 | |
| 824 | |
| 825 | // -------------------------------------------------------------------- |
| 826 | |
| 827 | /** |
| 828 | * Truncate statement |
| 829 | * |
| 830 | * Generates a platform-specific truncate string from the supplied data |
| 831 | * If the database does not support the truncate() command |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 832 | * This public function maps to "DELETE FROM table" |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 833 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 834 | * @param string the table name |
| 835 | * @return string |
| 836 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 837 | public function _truncate($table) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 838 | { |
| 839 | return $this->_delete($table); |
| 840 | } |
| 841 | |
| 842 | // -------------------------------------------------------------------- |
| 843 | |
| 844 | /** |
| 845 | * Delete statement |
| 846 | * |
| 847 | * Generates a platform-specific delete string from the supplied data |
| 848 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 849 | * @param string the table name |
| 850 | * @param array the where clause |
| 851 | * @param string the limit clause |
| 852 | * @return string |
| 853 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 854 | public function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 855 | { |
| 856 | $conditions = ''; |
| 857 | |
| 858 | if (count($where) > 0 OR count($like) > 0) |
| 859 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 860 | $conditions = "\nWHERE "; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 861 | $conditions .= implode("\n", $this->ar_where); |
| 862 | |
| 863 | if (count($where) > 0 && count($like) > 0) |
| 864 | { |
| 865 | $conditions .= " AND "; |
| 866 | } |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 867 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 868 | $conditions .= implode("\n", $like); |
| 869 | } |
| 870 | |
| 871 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
| 872 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 873 | return 'DELETE FROM '.$this->_from_tables($table).$conditions.$limit; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | // -------------------------------------------------------------------- |
| 877 | |
| 878 | /** |
| 879 | * Limit string |
| 880 | * |
| 881 | * Generates a platform-specific LIMIT clause |
| 882 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 883 | * @param string the sql query string |
| 884 | * @param integer the number of rows to limit the query to |
| 885 | * @param integer the offset value |
| 886 | * @return string |
| 887 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 888 | public function _limit($sql, $limit, $offset) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 889 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 890 | if ($this->pdodriver == 'cubrid' OR $this->pdodriver == 'sqlite') |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 891 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 892 | $offset = ($offset == 0) ? '' : $offset.', '; |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 893 | |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 894 | return $sql.'LIMIT '.$offset.$limit; |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 895 | } |
| 896 | else |
| 897 | { |
Taufan Aditya | 1820933 | 2012-02-09 16:07:27 +0700 | [diff] [blame] | 898 | $sql .= 'LIMIT '.$limit; |
| 899 | $sql .= ($offset > 0) ? ' OFFSET '.$offset : ''; |
Timothy Warren | 0a43ad8 | 2011-09-15 20:15:19 -0400 | [diff] [blame] | 900 | |
| 901 | return $sql; |
| 902 | } |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | // -------------------------------------------------------------------- |
| 906 | |
| 907 | /** |
| 908 | * Close DB Connection |
| 909 | * |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 910 | * @param resource |
| 911 | * @return void |
| 912 | */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 913 | public function _close($conn_id) |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 914 | { |
Timothy Warren | 6a450cf | 2011-08-23 12:46:11 -0400 | [diff] [blame] | 915 | $this->conn_id = null; |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 916 | } |
| 917 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 918 | } |
| 919 | |
Timothy Warren | 80ab816 | 2011-08-22 18:26:12 -0400 | [diff] [blame] | 920 | /* End of file pdo_driver.php */ |
Timothy Warren | 8c332e7 | 2012-03-19 18:09:13 -0400 | [diff] [blame] | 921 | /* Location: ./system/database/drivers/pdo/pdo_driver.php */ |