Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [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/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Postgre Database Adapter Class |
| 31 | * |
| 32 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 33 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 34 | * class is being used or not. |
| 35 | * |
| 36 | * @package CodeIgniter |
| 37 | * @subpackage Drivers |
| 38 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 39 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | * @link http://codeigniter.com/user_guide/database/ |
| 41 | */ |
| 42 | class CI_DB_postgre_driver extends CI_DB { |
| 43 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 44 | /** |
| 45 | * Database driver |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 49 | public $dbdriver = 'postgre'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 50 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 52 | * Database schema |
| 53 | * |
| 54 | * @var string |
Andrey Andreev | 485a348 | 2012-10-27 03:22:43 +0300 | [diff] [blame] | 55 | */ |
| 56 | public $schema = 'public'; |
| 57 | |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 58 | // -------------------------------------------------------------------- |
| 59 | |
Andrey Andreev | 485a348 | 2012-10-27 03:22:43 +0300 | [diff] [blame] | 60 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 61 | * ORDER BY random keyword |
| 62 | * |
| 63 | * @var string |
| 64 | */ |
| 65 | protected $_random_keyword = ' RANDOM()'; // database specific random keyword |
| 66 | |
| 67 | // -------------------------------------------------------------------- |
| 68 | |
| 69 | /** |
| 70 | * Class constructor |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | * |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 72 | * Creates a DSN string to be used for db_connect() and db_pconnect() |
| 73 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 74 | * @param array $params |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 75 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 76 | */ |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 77 | public function __construct($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | { |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 79 | parent::__construct($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 80 | |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 81 | if ( ! empty($this->dsn)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | { |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
| 86 | $this->dsn === '' OR $this->dsn = ''; |
| 87 | |
| 88 | if (strpos($this->hostname, '/') !== FALSE) |
| 89 | { |
| 90 | // If UNIX sockets are used, we shouldn't set a port |
| 91 | $this->port = ''; |
| 92 | } |
| 93 | |
Andrey Andreev | ab1d568 | 2012-04-03 20:48:32 +0300 | [diff] [blame] | 94 | $this->hostname === '' OR $this->dsn = 'host='.$this->hostname.' '; |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 95 | |
| 96 | if ( ! empty($this->port) && ctype_digit($this->port)) |
| 97 | { |
Andrey Andreev | ab1d568 | 2012-04-03 20:48:32 +0300 | [diff] [blame] | 98 | $this->dsn .= 'port='.$this->port.' '; |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if ($this->username !== '') |
| 102 | { |
Andrey Andreev | ab1d568 | 2012-04-03 20:48:32 +0300 | [diff] [blame] | 103 | $this->dsn .= 'user='.$this->username.' '; |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 104 | |
| 105 | /* An empty password is valid! |
| 106 | * |
| 107 | * $db['password'] = NULL must be done in order to ignore it. |
| 108 | */ |
| 109 | $this->password === NULL OR $this->dsn .= "password='".$this->password."' "; |
| 110 | } |
| 111 | |
| 112 | $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' '; |
| 113 | |
| 114 | /* We don't have these options as elements in our standard configuration |
| 115 | * array, but they might be set by parse_url() if the configuration was |
| 116 | * provided via string. Example: |
| 117 | * |
| 118 | * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1 |
| 119 | */ |
| 120 | foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key) |
| 121 | { |
| 122 | if (isset($this->$key) && is_string($this->key) && $this->key !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | { |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 124 | $this->dsn .= $key."='".$this->key."' "; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 127 | |
| 128 | $this->dsn = rtrim($this->dsn); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // -------------------------------------------------------------------- |
| 132 | |
| 133 | /** |
| 134 | * Non-persistent database connection |
| 135 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 138 | public function db_connect() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | { |
Andrey Andreev | 6192bc0 | 2012-03-26 12:32:32 +0300 | [diff] [blame] | 140 | return @pg_connect($this->dsn); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // -------------------------------------------------------------------- |
| 144 | |
| 145 | /** |
| 146 | * Persistent database connection |
| 147 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 150 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | { |
rwillert | 8240852 | 2012-07-10 14:02:01 +0300 | [diff] [blame] | 152 | $conn = @pg_pconnect($this->dsn); |
| 153 | if ($conn && pg_connection_status($conn) === PGSQL_CONNECTION_BAD) |
| 154 | { |
| 155 | if (pg_ping($conn) === FALSE) |
| 156 | { |
| 157 | return FALSE; |
| 158 | } |
| 159 | } |
| 160 | return $conn; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | // -------------------------------------------------------------------- |
| 164 | |
| 165 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 166 | * Reconnect |
| 167 | * |
| 168 | * Keep / reestablish the db connection if no queries have been |
| 169 | * sent for a length of time exceeding the server's idle timeout |
| 170 | * |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 171 | * @return void |
| 172 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 173 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 174 | { |
| 175 | if (pg_ping($this->conn_id) === FALSE) |
| 176 | { |
| 177 | $this->conn_id = FALSE; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // -------------------------------------------------------------------- |
| 182 | |
| 183 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | * Set client character set |
| 185 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 186 | * @param string $charset |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 187 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | */ |
Andrey Andreev | 8e89df8 | 2012-03-03 03:48:12 +0200 | [diff] [blame] | 189 | protected function _db_set_charset($charset) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | { |
Andrey Andreev | 8e89df8 | 2012-03-03 03:48:12 +0200 | [diff] [blame] | 191 | return (pg_set_client_encoding($this->conn_id, $charset) === 0); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 197 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | * @return string |
| 200 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 201 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 203 | if (isset($this->data_cache['version'])) |
| 204 | { |
| 205 | return $this->data_cache['version']; |
| 206 | } |
Andrey Andreev | 2b73037 | 2012-11-05 17:01:11 +0200 | [diff] [blame^] | 207 | elseif ( ! $this->conn_id) |
| 208 | { |
| 209 | $this->initialize(); |
| 210 | } |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 211 | |
Andrey Andreev | 2b73037 | 2012-11-05 17:01:11 +0200 | [diff] [blame^] | 212 | if ( ! $this->conn_id OR ($pg_version = pg_version($this->conn_id)) === FALSE) |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 213 | { |
| 214 | return FALSE; |
| 215 | } |
| 216 | |
| 217 | /* If PHP was compiled with PostgreSQL lib versions earlier |
| 218 | * than 7.4, pg_version() won't return the server version |
| 219 | * and so we'll have to fall back to running a query in |
| 220 | * order to get it. |
| 221 | */ |
| 222 | return isset($pg_version['server']) |
| 223 | ? $this->data_cache['version'] = $pg_version['server'] |
| 224 | : parent::version(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // -------------------------------------------------------------------- |
| 228 | |
| 229 | /** |
| 230 | * Execute the query |
| 231 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 232 | * @param string $sql an SQL query |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 235 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | return @pg_query($this->conn_id, $sql); |
| 238 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 239 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | // -------------------------------------------------------------------- |
| 241 | |
| 242 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | * Begin Transaction |
| 244 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 245 | * @param bool $test_mode |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 246 | * @return bool |
| 247 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 248 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 251 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | { |
| 253 | return TRUE; |
| 254 | } |
| 255 | |
| 256 | // Reset the transaction failure flag. |
| 257 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 258 | // even if the queries produce a successful result. |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 259 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 261 | return (bool) @pg_query($this->conn_id, 'BEGIN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // -------------------------------------------------------------------- |
| 265 | |
| 266 | /** |
| 267 | * Commit Transaction |
| 268 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 269 | * @return bool |
| 270 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 271 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 274 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | { |
| 276 | return TRUE; |
| 277 | } |
| 278 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 279 | return (bool) @pg_query($this->conn_id, 'COMMIT'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // -------------------------------------------------------------------- |
| 283 | |
| 284 | /** |
| 285 | * Rollback Transaction |
| 286 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 287 | * @return bool |
| 288 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 289 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 292 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | { |
| 294 | return TRUE; |
| 295 | } |
| 296 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 297 | return (bool) @pg_query($this->conn_id, 'ROLLBACK'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // -------------------------------------------------------------------- |
| 301 | |
| 302 | /** |
| 303 | * Escape String |
| 304 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 305 | * @param string $str |
| 306 | * @param bool $like Whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | * @return string |
| 308 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 309 | public function escape_str($str, $like = FALSE) |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 310 | { |
| 311 | if (is_array($str)) |
| 312 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 313 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 314 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 315 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return $str; |
| 319 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 320 | |
| 321 | $str = pg_escape_string($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 323 | // escape LIKE condition wildcards |
| 324 | if ($like === TRUE) |
| 325 | { |
Andrey Andreev | 55201ac | 2012-03-20 14:20:39 +0200 | [diff] [blame] | 326 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 327 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 328 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 329 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 330 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 331 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | // -------------------------------------------------------------------- |
| 335 | |
| 336 | /** |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 337 | * "Smart" Escape String |
| 338 | * |
| 339 | * Escapes data based on type |
| 340 | * Sets boolean and null types |
| 341 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 342 | * @param string $str |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 343 | * @return mixed |
| 344 | */ |
| 345 | public function escape($str) |
| 346 | { |
Soesapto Joeni Hantoro | dabeaa1 | 2012-05-15 16:37:46 +0700 | [diff] [blame] | 347 | if (is_bool($str)) |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 348 | { |
Soesapto Joeni Hantoro | d2574db | 2012-05-15 16:28:16 +0700 | [diff] [blame] | 349 | return ($str) ? 'TRUE' : 'FALSE'; |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 350 | } |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 351 | |
Soesapto Joeni Hantoro | dabeaa1 | 2012-05-15 16:37:46 +0700 | [diff] [blame] | 352 | return parent::escape($str); |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 353 | } |
Andrey Andreev | 242925b | 2012-05-15 13:03:51 +0300 | [diff] [blame] | 354 | |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 355 | // -------------------------------------------------------------------- |
| 356 | |
| 357 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | * Affected Rows |
| 359 | * |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 360 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 362 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | { |
| 364 | return @pg_affected_rows($this->result_id); |
| 365 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 366 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | // -------------------------------------------------------------------- |
| 368 | |
| 369 | /** |
| 370 | * Insert ID |
| 371 | * |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 372 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 374 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 376 | $v = pg_version($this->conn_id); |
| 377 | $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 378 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 379 | $table = (func_num_args() > 0) ? func_get_arg(0) : NULL; |
| 380 | $column = (func_num_args() > 1) ? func_get_arg(1) : NULL; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 382 | if ($table === NULL && $v >= '8.1') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 383 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 384 | $sql = 'SELECT LASTVAL() AS ins_id'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | } |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 386 | elseif ($table !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 388 | if ($column !== NULL && $v >= '8.0') |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 389 | { |
| 390 | $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq"; |
| 391 | $query = $this->query($sql); |
| 392 | $query = $query->row(); |
| 393 | $seq = $query->seq; |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | // seq_name passed in table parameter |
| 398 | $seq = $table; |
| 399 | } |
| 400 | |
| 401 | $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | } |
| 403 | else |
| 404 | { |
| 405 | return pg_last_oid($this->result_id); |
| 406 | } |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 407 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | $query = $this->query($sql); |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 409 | $query = $query->row(); |
| 410 | return (int) $query->ins_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // -------------------------------------------------------------------- |
| 414 | |
| 415 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | * Show table query |
| 417 | * |
| 418 | * Generates a platform-specific query string so that the table names can be fetched |
| 419 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 420 | * @param bool $prefix_limit |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | * @return string |
| 422 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 423 | protected function _list_tables($prefix_limit = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | { |
Andrey Andreev | 485a348 | 2012-10-27 03:22:43 +0300 | [diff] [blame] | 425 | $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \''.$this->schema."'"; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 427 | if ($prefix_limit !== FALSE && $this->dbprefix !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | { |
Andrey Andreev | 58803fb | 2012-06-24 00:45:37 +0300 | [diff] [blame] | 429 | return $sql.' AND "table_name" LIKE \'' |
| 430 | .$this->escape_like_str($this->dbprefix)."%' " |
| 431 | .sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 433 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | return $sql; |
| 435 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 436 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | // -------------------------------------------------------------------- |
| 438 | |
| 439 | /** |
| 440 | * Show column query |
| 441 | * |
| 442 | * Generates a platform-specific query string so that the column names can be fetched |
| 443 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 444 | * @param string $table |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | * @return string |
| 446 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 447 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | { |
Andrey Andreev | 58803fb | 2012-06-24 00:45:37 +0300 | [diff] [blame] | 449 | return 'SELECT "column_name" FROM "information_schema"."columns" WHERE "table_name" = '.$this->escape($table); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // -------------------------------------------------------------------- |
| 453 | |
| 454 | /** |
| 455 | * Field data query |
| 456 | * |
| 457 | * Generates a platform-specific query so that the column data can be retrieved |
| 458 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 459 | * @param string $table |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 460 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 462 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame] | 464 | return 'SELECT * FROM '.$table.' LIMIT 1'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // -------------------------------------------------------------------- |
| 468 | |
| 469 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 470 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 472 | * Returns an array containing code and message of the last |
| 473 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 475 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 477 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 479 | return array('code' => '', 'message' => pg_last_error($this->conn_id)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | // -------------------------------------------------------------------- |
| 483 | |
| 484 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | * Update statement |
| 486 | * |
| 487 | * Generates a platform-specific update string from the supplied data |
| 488 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 489 | * @param string $table |
| 490 | * @param array $values |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | * @return string |
| 492 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 493 | protected function _update($table, $values) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 495 | $this->qb_limit = FALSE; |
| 496 | $this->qb_orderby = array(); |
| 497 | return parent::_update($table, $values); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | // -------------------------------------------------------------------- |
| 501 | |
| 502 | /** |
Andrey Andreev | d06acd8 | 2012-05-25 00:29:09 +0300 | [diff] [blame] | 503 | * Update_Batch statement |
| 504 | * |
| 505 | * Generates a platform-specific batch update string from the supplied data |
| 506 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 507 | * @param string $table Table name |
| 508 | * @param array $values Update data |
| 509 | * @param string $index WHERE key |
Andrey Andreev | d06acd8 | 2012-05-25 00:29:09 +0300 | [diff] [blame] | 510 | * @return string |
| 511 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 512 | protected function _update_batch($table, $values, $index) |
Andrey Andreev | d06acd8 | 2012-05-25 00:29:09 +0300 | [diff] [blame] | 513 | { |
| 514 | $ids = array(); |
| 515 | foreach ($values as $key => $val) |
| 516 | { |
| 517 | $ids[] = $val[$index]; |
| 518 | |
| 519 | foreach (array_keys($val) as $field) |
| 520 | { |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 521 | if ($field !== $index) |
Andrey Andreev | d06acd8 | 2012-05-25 00:29:09 +0300 | [diff] [blame] | 522 | { |
| 523 | $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field]; |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | $cases = ''; |
| 529 | foreach ($final as $k => $v) |
| 530 | { |
aroche | 7f875b7 | 2012-07-16 18:22:08 +0300 | [diff] [blame] | 531 | $cases .= $k.' = (CASE '.$index."\n" |
Andrey Andreev | d06acd8 | 2012-05-25 00:29:09 +0300 | [diff] [blame] | 532 | .implode("\n", $v)."\n" |
| 533 | .'ELSE '.$k.' END), '; |
| 534 | } |
| 535 | |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 536 | $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE); |
| 537 | |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 538 | return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where'); |
Andrey Andreev | d06acd8 | 2012-05-25 00:29:09 +0300 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | // -------------------------------------------------------------------- |
| 542 | |
| 543 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 544 | * Delete statement |
| 545 | * |
| 546 | * Generates a platform-specific delete string from the supplied data |
| 547 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 548 | * @param string $table |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 549 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 550 | */ |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 551 | protected function _delete($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 552 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 553 | $this->qb_limit = FALSE; |
| 554 | return parent::_delete($table); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | // -------------------------------------------------------------------- |
Andrey Andreev | 82e88b9 | 2012-04-06 21:18:59 +0300 | [diff] [blame] | 558 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | /** |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 560 | * LIMIT |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 561 | * |
| 562 | * Generates a platform-specific LIMIT clause |
| 563 | * |
Andrey Andreev | a24e52e | 2012-11-02 03:54:12 +0200 | [diff] [blame] | 564 | * @param string $sql SQL Query |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | * @return string |
| 566 | */ |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 567 | protected function _limit($sql) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 568 | { |
Andrey Andreev | c9b924c | 2012-07-19 13:06:02 +0300 | [diff] [blame] | 569 | return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : ''); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // -------------------------------------------------------------------- |
| 573 | |
| 574 | /** |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 575 | * WHERE, HAVING |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 576 | * |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 577 | * Called by where(), or_where(), having(), or_having() |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 578 | * |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 579 | * @param string 'qb_where' or 'qb_having' |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 580 | * @param mixed |
| 581 | * @param mixed |
| 582 | * @param string |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 583 | * @param bool |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 584 | * @return object |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 585 | */ |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 586 | protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL) |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 587 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 588 | $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where'; |
| 589 | |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 590 | if ( ! is_array($key)) |
| 591 | { |
| 592 | $key = array($key => $value); |
| 593 | } |
| 594 | |
| 595 | // If the escape value was not set will will base it on the global setting |
Andrey Andreev | 498c1e0 | 2012-06-16 03:34:10 +0300 | [diff] [blame] | 596 | is_bool($escape) OR $escape = $this->_protect_identifiers; |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 597 | |
| 598 | foreach ($key as $k => $v) |
| 599 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 600 | $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0) |
Andrey Andreev | 58803fb | 2012-06-24 00:45:37 +0300 | [diff] [blame] | 601 | ? $this->_group_get_type('') |
| 602 | : $this->_group_get_type($type); |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 603 | |
| 604 | if (is_null($v) && ! $this->_has_operator($k)) |
| 605 | { |
| 606 | // value appears not to have been set, assign the test to IS NULL |
| 607 | $k .= ' IS NULL'; |
| 608 | } |
| 609 | |
| 610 | if ( ! is_null($v)) |
| 611 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 612 | if (is_bool($v)) |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 613 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 614 | $v = ' '.($v ? 'TRUE' : 'FALSE'); |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 615 | } |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 616 | elseif ($escape === TRUE) |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 617 | { |
Andrey Andreev | b047865 | 2012-07-18 15:34:46 +0300 | [diff] [blame] | 618 | $v = ' '.(is_int($v) ? $v : $this->escape($v)); |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | if ( ! $this->_has_operator($k)) |
| 622 | { |
| 623 | $k .= ' = '; |
| 624 | } |
| 625 | } |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 626 | |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 627 | $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 628 | if ($this->qb_caching === TRUE) |
| 629 | { |
Andrey Andreev | d40459d | 2012-07-18 16:46:39 +0300 | [diff] [blame] | 630 | $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape); |
| 631 | $this->qb_cache_exists[] = substr($qb_key, 3); |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | } |
| 635 | |
| 636 | return $this; |
| 637 | } |
Andrey Andreev | 242925b | 2012-05-15 13:03:51 +0300 | [diff] [blame] | 638 | |
Soesapto Joeni Hantoro | f25b929 | 2012-05-15 08:10:31 +0700 | [diff] [blame] | 639 | // -------------------------------------------------------------------- |
| 640 | |
| 641 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 642 | * Close DB Connection |
| 643 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 644 | * @return void |
| 645 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 646 | protected function _close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 647 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 648 | @pg_close($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 653 | /* End of file postgre_driver.php */ |
Andrey Andreev | 242925b | 2012-05-15 13:03:51 +0300 | [diff] [blame] | 654 | /* Location: ./system/database/drivers/postgre/postgre_driver.php */ |