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 |
| 32 | * creates dynamically based on whether the active record |
| 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 | /** |
Andrey Andreev | 8e89df8 | 2012-03-03 03:48:12 +0200 | [diff] [blame] | 165 | * Set client character set |
| 166 | * |
| 167 | * @param string |
| 168 | * @return bool |
| 169 | */ |
| 170 | protected function _db_set_charset($charset) |
| 171 | { |
| 172 | return (pg_set_client_encoding($this->conn_id, $charset) === 0); |
| 173 | } |
| 174 | |
| 175 | // -------------------------------------------------------------------- |
| 176 | |
| 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 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 222 | * @return bool |
| 223 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 224 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 227 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | { |
| 229 | return TRUE; |
| 230 | } |
| 231 | |
| 232 | // Reset the transaction failure flag. |
| 233 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 234 | // even if the queries produce a successful result. |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 235 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 237 | return @pg_query($this->conn_id, 'BEGIN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // -------------------------------------------------------------------- |
| 241 | |
| 242 | /** |
| 243 | * Commit Transaction |
| 244 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | * @return bool |
| 246 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 247 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 250 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | { |
| 252 | return TRUE; |
| 253 | } |
| 254 | |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 255 | return @pg_query($this->conn_id, 'COMMIT'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // -------------------------------------------------------------------- |
| 259 | |
| 260 | /** |
| 261 | * Rollback Transaction |
| 262 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 263 | * @return bool |
| 264 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 265 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 268 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | { |
| 270 | return TRUE; |
| 271 | } |
| 272 | |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 273 | return @pg_query($this->conn_id, 'ROLLBACK'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // -------------------------------------------------------------------- |
| 277 | |
| 278 | /** |
| 279 | * Escape String |
| 280 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 282 | * @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] | 283 | * @return string |
| 284 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 285 | public function escape_str($str, $like = FALSE) |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 286 | { |
| 287 | if (is_array($str)) |
| 288 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 289 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 290 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 291 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | return $str; |
| 295 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 296 | |
| 297 | $str = pg_escape_string($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 298 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 299 | // escape LIKE condition wildcards |
| 300 | if ($like === TRUE) |
| 301 | { |
Andrey Andreev | 55201ac | 2012-03-20 14:20:39 +0200 | [diff] [blame] | 302 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 303 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 304 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 305 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 306 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 307 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 309 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | // -------------------------------------------------------------------- |
| 311 | |
| 312 | /** |
| 313 | * Affected Rows |
| 314 | * |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 315 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 317 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | { |
| 319 | return @pg_affected_rows($this->result_id); |
| 320 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 321 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | // -------------------------------------------------------------------- |
| 323 | |
| 324 | /** |
| 325 | * Insert ID |
| 326 | * |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 327 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 329 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 331 | $v = $this->version(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 332 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 333 | $table = func_num_args() > 0 ? func_get_arg(0) : NULL; |
| 334 | $column = func_num_args() > 1 ? func_get_arg(1) : NULL; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 336 | if ($table == NULL && $v >= '8.1') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | { |
| 338 | $sql='SELECT LASTVAL() as ins_id'; |
| 339 | } |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 340 | elseif ($table != NULL && $column != NULL && $v >= '8.0') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | { |
| 342 | $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column); |
| 343 | $query = $this->query($sql); |
| 344 | $row = $query->row(); |
| 345 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq); |
| 346 | } |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 347 | elseif ($table != NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | { |
| 349 | // seq_name passed in table parameter |
| 350 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table); |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | return pg_last_oid($this->result_id); |
| 355 | } |
| 356 | $query = $this->query($sql); |
| 357 | $row = $query->row(); |
| 358 | return $row->ins_id; |
| 359 | } |
| 360 | |
| 361 | // -------------------------------------------------------------------- |
| 362 | |
| 363 | /** |
| 364 | * "Count All" query |
| 365 | * |
| 366 | * Generates a platform-specific query string that counts all records in |
| 367 | * the specified database |
| 368 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | * @param string |
| 370 | * @return string |
| 371 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 372 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | { |
| 374 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 375 | { |
| 376 | return 0; |
| 377 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 379 | $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 380 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 381 | { |
| 382 | return 0; |
| 383 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | |
| 385 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 386 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 387 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // -------------------------------------------------------------------- |
| 391 | |
| 392 | /** |
| 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 | { |
| 402 | $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'"; |
| 403 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 405 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 406 | $sql .= " AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 408 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | return $sql; |
| 410 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | // -------------------------------------------------------------------- |
| 413 | |
| 414 | /** |
| 415 | * Show column query |
| 416 | * |
| 417 | * Generates a platform-specific query string so that the column names can be fetched |
| 418 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | * @param string the table name |
| 420 | * @return string |
| 421 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 422 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | { |
| 424 | return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$table."'"; |
| 425 | } |
| 426 | |
| 427 | // -------------------------------------------------------------------- |
| 428 | |
| 429 | /** |
| 430 | * Field data query |
| 431 | * |
| 432 | * Generates a platform-specific query so that the column data can be retrieved |
| 433 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | * @param string the table name |
| 435 | * @return object |
| 436 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 437 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 438 | { |
| 439 | return "SELECT * FROM ".$table." LIMIT 1"; |
| 440 | } |
| 441 | |
| 442 | // -------------------------------------------------------------------- |
| 443 | |
| 444 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 445 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 447 | * Returns an array containing code and message of the last |
| 448 | * database error that has occured. |
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 |
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 | public function error() |
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 | return array('code' => '', 'message' => pg_last_error($this->conn_id)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // -------------------------------------------------------------------- |
| 458 | |
| 459 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | * From Tables |
| 461 | * |
| 462 | * This function implicitly groups FROM tables so there is no confusion |
| 463 | * about operator precedence in harmony with SQL standards |
| 464 | * |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 465 | * @param array |
| 466 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 468 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | { |
| 470 | if ( ! is_array($tables)) |
| 471 | { |
| 472 | $tables = array($tables); |
| 473 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 474 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | return implode(', ', $tables); |
| 476 | } |
| 477 | |
| 478 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 479 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | * Update statement |
| 482 | * |
| 483 | * Generates a platform-specific update string from the supplied data |
| 484 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | * @param string the table name |
| 486 | * @param array the update data |
| 487 | * @param array the where clause |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 488 | * @param array the orderby clause (ignored) |
| 489 | * @param array the limit clause (ignored) |
| 490 | * @param array the like clause |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | * @return string |
| 492 | */ |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 493 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 495 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | { |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 497 | $valstr[] = $key.' = '.$val; |
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 | |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 500 | $where = empty($where) ? '' : ' WHERE '.implode(' ', $where); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 502 | if ( ! empty($like)) |
| 503 | { |
| 504 | $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like); |
| 505 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | |
Andrey Andreev | 00541ae | 2012-04-09 11:43:10 +0300 | [diff] [blame] | 507 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | // -------------------------------------------------------------------- |
| 511 | |
| 512 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 513 | * Delete statement |
| 514 | * |
| 515 | * Generates a platform-specific delete string from the supplied data |
| 516 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | * @param string the table name |
| 518 | * @param array the where clause |
Andrey Andreev | c01d316 | 2012-04-09 12:55:11 +0300 | [diff] [blame] | 519 | * @param array the like clause |
Andrey Andreev | a254836 | 2012-04-09 13:03:11 +0300 | [diff] [blame^] | 520 | * @param string the limit clause (ignored) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 521 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 522 | */ |
Andrey Andreev | a254836 | 2012-04-09 13:03:11 +0300 | [diff] [blame^] | 523 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | { |
Andrey Andreev | c01d316 | 2012-04-09 12:55:11 +0300 | [diff] [blame] | 525 | $conditions = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 526 | |
Andrey Andreev | c01d316 | 2012-04-09 12:55:11 +0300 | [diff] [blame] | 527 | empty($where) OR $conditions[] = implode(' ', $where); |
| 528 | empty($like) OR $conditions[] = implode(' ', $like); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 529 | |
Andrey Andreev | c01d316 | 2012-04-09 12:55:11 +0300 | [diff] [blame] | 530 | return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : ''); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // -------------------------------------------------------------------- |
| 534 | /** |
| 535 | * Limit string |
| 536 | * |
| 537 | * Generates a platform-specific LIMIT clause |
| 538 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 539 | * @param string the sql query string |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 540 | * @param int the number of rows to limit the query to |
| 541 | * @param int the offset value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | * @return string |
| 543 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 544 | protected function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 545 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 546 | $sql .= "LIMIT ".$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 547 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 548 | if ($offset > 0) |
| 549 | { |
| 550 | $sql .= " OFFSET ".$offset; |
| 551 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 552 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 553 | return $sql; |
| 554 | } |
| 555 | |
| 556 | // -------------------------------------------------------------------- |
| 557 | |
| 558 | /** |
| 559 | * Close DB Connection |
| 560 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 561 | * @param resource |
| 562 | * @return void |
| 563 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 564 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | { |
| 566 | @pg_close($conn_id); |
| 567 | } |
Timothy Warren | 99c02ef | 2012-03-19 19:06:16 -0400 | [diff] [blame] | 568 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | /* End of file postgre_driver.php */ |
Timothy Warren | bbea405 | 2012-03-20 08:24:18 -0400 | [diff] [blame] | 572 | /* Location: ./system/database/drivers/postgre/postgre_driver.php */ |