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