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 | /** |
| 60 | * Connection String |
| 61 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 63 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 64 | protected function _connect_string() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | { |
| 66 | $components = array( |
| 67 | 'hostname' => 'host', |
| 68 | 'port' => 'port', |
| 69 | 'database' => 'dbname', |
| 70 | 'username' => 'user', |
| 71 | 'password' => 'password' |
| 72 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 73 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | $connect_string = ""; |
| 75 | foreach ($components as $key => $val) |
| 76 | { |
| 77 | if (isset($this->$key) && $this->$key != '') |
| 78 | { |
| 79 | $connect_string .= " $val=".$this->$key; |
| 80 | } |
| 81 | } |
| 82 | return trim($connect_string); |
| 83 | } |
| 84 | |
| 85 | // -------------------------------------------------------------------- |
| 86 | |
| 87 | /** |
| 88 | * Non-persistent database connection |
| 89 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 91 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 92 | public function db_connect() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 93 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | return @pg_connect($this->_connect_string()); |
| 95 | } |
| 96 | |
| 97 | // -------------------------------------------------------------------- |
| 98 | |
| 99 | /** |
| 100 | * Persistent database connection |
| 101 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 104 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | { |
| 106 | return @pg_pconnect($this->_connect_string()); |
| 107 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 108 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | // -------------------------------------------------------------------- |
| 110 | |
| 111 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 112 | * Reconnect |
| 113 | * |
| 114 | * Keep / reestablish the db connection if no queries have been |
| 115 | * sent for a length of time exceeding the server's idle timeout |
| 116 | * |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 117 | * @return void |
| 118 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 119 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 120 | { |
| 121 | if (pg_ping($this->conn_id) === FALSE) |
| 122 | { |
| 123 | $this->conn_id = FALSE; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // -------------------------------------------------------------------- |
| 128 | |
| 129 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | * Select the database |
| 131 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 133 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 134 | public function db_select() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | { |
| 136 | // Not needed for Postgre so we'll return TRUE |
| 137 | return TRUE; |
| 138 | } |
| 139 | |
| 140 | // -------------------------------------------------------------------- |
| 141 | |
| 142 | /** |
Andrey Andreev | 8e89df8 | 2012-03-03 03:48:12 +0200 | [diff] [blame] | 143 | * Set client character set |
| 144 | * |
| 145 | * @param string |
| 146 | * @return bool |
| 147 | */ |
| 148 | protected function _db_set_charset($charset) |
| 149 | { |
| 150 | return (pg_set_client_encoding($this->conn_id, $charset) === 0); |
| 151 | } |
| 152 | |
| 153 | // -------------------------------------------------------------------- |
| 154 | |
| 155 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 156 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | * @return string |
| 159 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 160 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 162 | if (isset($this->data_cache['version'])) |
| 163 | { |
| 164 | return $this->data_cache['version']; |
| 165 | } |
| 166 | |
| 167 | if (($pg_version = pg_version($this->conn_id)) === FALSE) |
| 168 | { |
| 169 | return FALSE; |
| 170 | } |
| 171 | |
| 172 | /* If PHP was compiled with PostgreSQL lib versions earlier |
| 173 | * than 7.4, pg_version() won't return the server version |
| 174 | * and so we'll have to fall back to running a query in |
| 175 | * order to get it. |
| 176 | */ |
| 177 | return isset($pg_version['server']) |
| 178 | ? $this->data_cache['version'] = $pg_version['server'] |
| 179 | : parent::version(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // -------------------------------------------------------------------- |
| 183 | |
| 184 | /** |
| 185 | * Execute the query |
| 186 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | * @param string an SQL query |
| 188 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 190 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | { |
| 192 | $sql = $this->_prep_query($sql); |
| 193 | return @pg_query($this->conn_id, $sql); |
| 194 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | // -------------------------------------------------------------------- |
| 197 | |
| 198 | /** |
| 199 | * Prep the query |
| 200 | * |
| 201 | * If needed, each database adapter can prep the query string |
| 202 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | * @param string an SQL query |
| 204 | * @return string |
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 _prep_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | { |
| 208 | return $sql; |
| 209 | } |
| 210 | |
| 211 | // -------------------------------------------------------------------- |
| 212 | |
| 213 | /** |
| 214 | * Begin Transaction |
| 215 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 216 | * @return bool |
| 217 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 218 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 221 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | { |
| 223 | return TRUE; |
| 224 | } |
| 225 | |
| 226 | // Reset the transaction failure flag. |
| 227 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 228 | // even if the queries produce a successful result. |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 229 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 231 | return @pg_query($this->conn_id, 'BEGIN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // -------------------------------------------------------------------- |
| 235 | |
| 236 | /** |
| 237 | * Commit Transaction |
| 238 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 239 | * @return bool |
| 240 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 241 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 244 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | { |
| 246 | return TRUE; |
| 247 | } |
| 248 | |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 249 | return @pg_query($this->conn_id, 'COMMIT'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // -------------------------------------------------------------------- |
| 253 | |
| 254 | /** |
| 255 | * Rollback Transaction |
| 256 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 257 | * @return bool |
| 258 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 259 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 262 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | { |
| 264 | return TRUE; |
| 265 | } |
| 266 | |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 267 | return @pg_query($this->conn_id, 'ROLLBACK'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // -------------------------------------------------------------------- |
| 271 | |
| 272 | /** |
| 273 | * Escape String |
| 274 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 276 | * @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] | 277 | * @return string |
| 278 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 279 | public function escape_str($str, $like = FALSE) |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 280 | { |
| 281 | if (is_array($str)) |
| 282 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 283 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 285 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | return $str; |
| 289 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 290 | |
| 291 | $str = pg_escape_string($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 292 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 293 | // escape LIKE condition wildcards |
| 294 | if ($like === TRUE) |
| 295 | { |
Andrey Andreev | 55201ac | 2012-03-20 14:20:39 +0200 | [diff] [blame] | 296 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
| 297 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
| 298 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 299 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 300 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 301 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | // -------------------------------------------------------------------- |
| 305 | |
| 306 | /** |
| 307 | * Affected Rows |
| 308 | * |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 309 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 311 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | { |
| 313 | return @pg_affected_rows($this->result_id); |
| 314 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 315 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 316 | // -------------------------------------------------------------------- |
| 317 | |
| 318 | /** |
| 319 | * Insert ID |
| 320 | * |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 321 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 323 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 325 | $v = $this->version(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 327 | $table = func_num_args() > 0 ? func_get_arg(0) : NULL; |
| 328 | $column = func_num_args() > 1 ? func_get_arg(1) : NULL; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 329 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 330 | if ($table == NULL && $v >= '8.1') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | { |
| 332 | $sql='SELECT LASTVAL() as ins_id'; |
| 333 | } |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 334 | elseif ($table != NULL && $column != NULL && $v >= '8.0') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | { |
| 336 | $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column); |
| 337 | $query = $this->query($sql); |
| 338 | $row = $query->row(); |
| 339 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq); |
| 340 | } |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 341 | elseif ($table != NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | { |
| 343 | // seq_name passed in table parameter |
| 344 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | return pg_last_oid($this->result_id); |
| 349 | } |
| 350 | $query = $this->query($sql); |
| 351 | $row = $query->row(); |
| 352 | return $row->ins_id; |
| 353 | } |
| 354 | |
| 355 | // -------------------------------------------------------------------- |
| 356 | |
| 357 | /** |
| 358 | * "Count All" query |
| 359 | * |
| 360 | * Generates a platform-specific query string that counts all records in |
| 361 | * the specified database |
| 362 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | * @param string |
| 364 | * @return string |
| 365 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 366 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | { |
| 368 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 369 | { |
| 370 | return 0; |
| 371 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 373 | $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] | 374 | if ($query->num_rows() == 0) |
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 | |
| 379 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 380 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 381 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // -------------------------------------------------------------------- |
| 385 | |
| 386 | /** |
| 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 | { |
| 396 | $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'"; |
| 397 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 399 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 400 | $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] | 401 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 402 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | return $sql; |
| 404 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 405 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | // -------------------------------------------------------------------- |
| 407 | |
| 408 | /** |
| 409 | * Show column query |
| 410 | * |
| 411 | * Generates a platform-specific query string so that the column names can be fetched |
| 412 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | * @param string the table name |
| 414 | * @return string |
| 415 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 416 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | { |
| 418 | return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$table."'"; |
| 419 | } |
| 420 | |
| 421 | // -------------------------------------------------------------------- |
| 422 | |
| 423 | /** |
| 424 | * Field data query |
| 425 | * |
| 426 | * Generates a platform-specific query so that the column data can be retrieved |
| 427 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | * @param string the table name |
| 429 | * @return object |
| 430 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 431 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | { |
| 433 | return "SELECT * FROM ".$table." LIMIT 1"; |
| 434 | } |
| 435 | |
| 436 | // -------------------------------------------------------------------- |
| 437 | |
| 438 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 439 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 440 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 441 | * Returns an array containing code and message of the last |
| 442 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 444 | * @return array |
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 | public function error() |
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 | return array('code' => '', 'message' => pg_last_error($this->conn_id)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // -------------------------------------------------------------------- |
| 452 | |
| 453 | /** |
| 454 | * Escape the SQL Identifiers |
| 455 | * |
| 456 | * This function escapes column and table names |
| 457 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | * @param string |
| 459 | * @return string |
| 460 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 461 | public function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | { |
| 463 | if ($this->_escape_char == '') |
| 464 | { |
| 465 | return $item; |
| 466 | } |
| 467 | |
| 468 | foreach ($this->_reserved_identifiers as $id) |
| 469 | { |
| 470 | if (strpos($item, '.'.$id) !== FALSE) |
| 471 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 472 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 473 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | // remove duplicates if the user already included the escape |
| 475 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 478 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | if (strpos($item, '.') !== FALSE) |
| 480 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 481 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | } |
| 483 | else |
| 484 | { |
| 485 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 486 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 487 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | // remove duplicates if the user already included the escape |
| 489 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 490 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 491 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | // -------------------------------------------------------------------- |
| 493 | |
| 494 | /** |
| 495 | * From Tables |
| 496 | * |
| 497 | * This function implicitly groups FROM tables so there is no confusion |
| 498 | * about operator precedence in harmony with SQL standards |
| 499 | * |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 500 | * @param array |
| 501 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 503 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | { |
| 505 | if ( ! is_array($tables)) |
| 506 | { |
| 507 | $tables = array($tables); |
| 508 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 509 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | return implode(', ', $tables); |
| 511 | } |
| 512 | |
| 513 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 514 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 515 | /** |
| 516 | * Insert statement |
| 517 | * |
| 518 | * Generates a platform-specific insert string from the supplied data |
| 519 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 520 | * @param string the table name |
| 521 | * @param array the insert keys |
| 522 | * @param array the insert values |
| 523 | * @return string |
| 524 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 525 | protected function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 526 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 528 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 529 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 530 | // -------------------------------------------------------------------- |
| 531 | |
| 532 | /** |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 533 | * Insert_batch statement |
| 534 | * |
| 535 | * Generates a platform-specific insert string from the supplied data |
| 536 | * |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 537 | * @param string the table name |
| 538 | * @param array the insert keys |
| 539 | * @param array the insert values |
| 540 | * @return string |
| 541 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 542 | protected function _insert_batch($table, $keys, $values) |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 543 | { |
| 544 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values); |
| 545 | } |
| 546 | |
| 547 | // -------------------------------------------------------------------- |
| 548 | |
| 549 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 550 | * Update statement |
| 551 | * |
| 552 | * Generates a platform-specific update string from the supplied data |
| 553 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 554 | * @param string the table name |
| 555 | * @param array the update data |
| 556 | * @param array the where clause |
| 557 | * @param array the orderby clause |
| 558 | * @param array the limit clause |
| 559 | * @return string |
| 560 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 561 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 563 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 564 | { |
| 565 | $valstr[] = $key." = ".$val; |
| 566 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 567 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 568 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 569 | |
| 570 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 571 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | return $sql; |
| 573 | } |
| 574 | |
| 575 | // -------------------------------------------------------------------- |
| 576 | |
| 577 | /** |
| 578 | * Truncate statement |
| 579 | * |
| 580 | * Generates a platform-specific truncate string from the supplied data |
| 581 | * If the database does not support the truncate() command |
| 582 | * This function maps to "DELETE FROM table" |
| 583 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | * @param string the table name |
| 585 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 586 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 587 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | { |
| 589 | return "TRUNCATE ".$table; |
| 590 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 591 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 592 | // -------------------------------------------------------------------- |
| 593 | |
| 594 | /** |
| 595 | * Delete statement |
| 596 | * |
| 597 | * Generates a platform-specific delete string from the supplied data |
| 598 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 599 | * @param string the table name |
| 600 | * @param array the where clause |
| 601 | * @param string the limit clause |
| 602 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 603 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 604 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 605 | { |
| 606 | $conditions = ''; |
| 607 | |
| 608 | if (count($where) > 0 OR count($like) > 0) |
| 609 | { |
| 610 | $conditions = "\nWHERE "; |
| 611 | $conditions .= implode("\n", $this->ar_where); |
| 612 | |
| 613 | if (count($where) > 0 && count($like) > 0) |
| 614 | { |
| 615 | $conditions .= " AND "; |
| 616 | } |
| 617 | $conditions .= implode("\n", $like); |
| 618 | } |
| 619 | |
SammyK | 1bf8eeb | 2012-03-05 16:56:06 -0600 | [diff] [blame] | 620 | return "DELETE FROM ".$table.$conditions; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // -------------------------------------------------------------------- |
| 624 | /** |
| 625 | * Limit string |
| 626 | * |
| 627 | * Generates a platform-specific LIMIT clause |
| 628 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 629 | * @param string the sql query string |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 630 | * @param int the number of rows to limit the query to |
| 631 | * @param int the offset value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | * @return string |
| 633 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 634 | protected function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 635 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 636 | $sql .= "LIMIT ".$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 637 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 638 | if ($offset > 0) |
| 639 | { |
| 640 | $sql .= " OFFSET ".$offset; |
| 641 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 642 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 643 | return $sql; |
| 644 | } |
| 645 | |
| 646 | // -------------------------------------------------------------------- |
| 647 | |
| 648 | /** |
| 649 | * Close DB Connection |
| 650 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 651 | * @param resource |
| 652 | * @return void |
| 653 | */ |
Andrey Andreev | 738497c | 2012-03-20 14:12:25 +0200 | [diff] [blame] | 654 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | { |
| 656 | @pg_close($conn_id); |
| 657 | } |
| 658 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 661 | /* End of file postgre_driver.php */ |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 662 | /* Location: ./system/database/drivers/postgre/postgre_driver.php */ |