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