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