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