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 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 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 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | * Version number query string |
| 151 | * |
| 152 | * @access public |
| 153 | * @return string |
| 154 | */ |
| 155 | function _version() |
| 156 | { |
| 157 | return "SELECT version() AS ver"; |
| 158 | } |
| 159 | |
| 160 | // -------------------------------------------------------------------- |
| 161 | |
| 162 | /** |
| 163 | * Execute the query |
| 164 | * |
| 165 | * @access private called by the base class |
| 166 | * @param string an SQL query |
| 167 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | function _execute($sql) |
| 170 | { |
| 171 | $sql = $this->_prep_query($sql); |
| 172 | return @pg_query($this->conn_id, $sql); |
| 173 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | // -------------------------------------------------------------------- |
| 176 | |
| 177 | /** |
| 178 | * Prep the query |
| 179 | * |
| 180 | * If needed, each database adapter can prep the query string |
| 181 | * |
| 182 | * @access private called by execute() |
| 183 | * @param string an SQL query |
| 184 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 185 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | function _prep_query($sql) |
| 187 | { |
| 188 | return $sql; |
| 189 | } |
| 190 | |
| 191 | // -------------------------------------------------------------------- |
| 192 | |
| 193 | /** |
| 194 | * Begin Transaction |
| 195 | * |
| 196 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | * @return bool |
| 198 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | function trans_begin($test_mode = FALSE) |
| 200 | { |
| 201 | if ( ! $this->trans_enabled) |
| 202 | { |
| 203 | return TRUE; |
| 204 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 207 | if ($this->_trans_depth > 0) |
| 208 | { |
| 209 | return TRUE; |
| 210 | } |
| 211 | |
| 212 | // Reset the transaction failure flag. |
| 213 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 214 | // even if the queries produce a successful result. |
| 215 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 216 | |
| 217 | return @pg_exec($this->conn_id, "begin"); |
| 218 | } |
| 219 | |
| 220 | // -------------------------------------------------------------------- |
| 221 | |
| 222 | /** |
| 223 | * Commit Transaction |
| 224 | * |
| 225 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | * @return bool |
| 227 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | function trans_commit() |
| 229 | { |
| 230 | if ( ! $this->trans_enabled) |
| 231 | { |
| 232 | return TRUE; |
| 233 | } |
| 234 | |
| 235 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 236 | if ($this->_trans_depth > 0) |
| 237 | { |
| 238 | return TRUE; |
| 239 | } |
| 240 | |
| 241 | return @pg_exec($this->conn_id, "commit"); |
| 242 | } |
| 243 | |
| 244 | // -------------------------------------------------------------------- |
| 245 | |
| 246 | /** |
| 247 | * Rollback Transaction |
| 248 | * |
| 249 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 250 | * @return bool |
| 251 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | function trans_rollback() |
| 253 | { |
| 254 | if ( ! $this->trans_enabled) |
| 255 | { |
| 256 | return TRUE; |
| 257 | } |
| 258 | |
| 259 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 260 | if ($this->_trans_depth > 0) |
| 261 | { |
| 262 | return TRUE; |
| 263 | } |
| 264 | |
| 265 | return @pg_exec($this->conn_id, "rollback"); |
| 266 | } |
| 267 | |
| 268 | // -------------------------------------------------------------------- |
| 269 | |
| 270 | /** |
| 271 | * Escape String |
| 272 | * |
| 273 | * @access public |
| 274 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 275 | * @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] | 276 | * @return string |
| 277 | */ |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 278 | function escape_str($str, $like = FALSE) |
| 279 | { |
| 280 | if (is_array($str)) |
| 281 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 282 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 283 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 284 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | return $str; |
| 288 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 289 | |
| 290 | $str = pg_escape_string($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 291 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 292 | // escape LIKE condition wildcards |
| 293 | if ($like === TRUE) |
| 294 | { |
| 295 | $str = str_replace( array('%', '_', $this->_like_escape_chr), |
| 296 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 297 | $str); |
| 298 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 299 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 300 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 302 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | // -------------------------------------------------------------------- |
| 304 | |
| 305 | /** |
| 306 | * Affected Rows |
| 307 | * |
| 308 | * @access public |
| 309 | * @return integer |
| 310 | */ |
| 311 | function affected_rows() |
| 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 | * |
| 321 | * @access public |
| 322 | * @return integer |
| 323 | */ |
| 324 | function insert_id() |
| 325 | { |
| 326 | $v = $this->_version(); |
| 327 | $v = $v['server']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 328 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 329 | $table = func_num_args() > 0 ? func_get_arg(0) : NULL; |
| 330 | $column = func_num_args() > 1 ? func_get_arg(1) : NULL; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 332 | if ($table == NULL && $v >= '8.1') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | { |
| 334 | $sql='SELECT LASTVAL() as ins_id'; |
| 335 | } |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 336 | elseif ($table != NULL && $column != NULL && $v >= '8.0') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | { |
| 338 | $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column); |
| 339 | $query = $this->query($sql); |
| 340 | $row = $query->row(); |
| 341 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq); |
| 342 | } |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 343 | elseif ($table != NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | { |
| 345 | // seq_name passed in table parameter |
| 346 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table); |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | return pg_last_oid($this->result_id); |
| 351 | } |
| 352 | $query = $this->query($sql); |
| 353 | $row = $query->row(); |
| 354 | return $row->ins_id; |
| 355 | } |
| 356 | |
| 357 | // -------------------------------------------------------------------- |
| 358 | |
| 359 | /** |
| 360 | * "Count All" query |
| 361 | * |
| 362 | * Generates a platform-specific query string that counts all records in |
| 363 | * the specified database |
| 364 | * |
| 365 | * @access public |
| 366 | * @param string |
| 367 | * @return string |
| 368 | */ |
| 369 | function count_all($table = '') |
| 370 | { |
| 371 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 372 | { |
| 373 | return 0; |
| 374 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 376 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
| 377 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 379 | { |
| 380 | return 0; |
| 381 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | |
| 383 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 384 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 385 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // -------------------------------------------------------------------- |
| 389 | |
| 390 | /** |
| 391 | * Show table query |
| 392 | * |
| 393 | * Generates a platform-specific query string so that the table names can be fetched |
| 394 | * |
| 395 | * @access private |
| 396 | * @param boolean |
| 397 | * @return string |
| 398 | */ |
| 399 | function _list_tables($prefix_limit = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | { |
| 401 | $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'"; |
| 402 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 404 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 405 | $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] | 406 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 407 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | return $sql; |
| 409 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 410 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | // -------------------------------------------------------------------- |
| 412 | |
| 413 | /** |
| 414 | * Show column query |
| 415 | * |
| 416 | * Generates a platform-specific query string so that the column names can be fetched |
| 417 | * |
| 418 | * @access public |
| 419 | * @param string the table name |
| 420 | * @return string |
| 421 | */ |
| 422 | function _list_columns($table = '') |
| 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 | * |
| 434 | * @access public |
| 435 | * @param string the table name |
| 436 | * @return object |
| 437 | */ |
| 438 | function _field_data($table) |
| 439 | { |
| 440 | return "SELECT * FROM ".$table." LIMIT 1"; |
| 441 | } |
| 442 | |
| 443 | // -------------------------------------------------------------------- |
| 444 | |
| 445 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 446 | * 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 | * Returns an array containing code and message of the last |
| 449 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 451 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 453 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 454 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame^] | 455 | return array('code' => '', 'message' => pg_last_error($this->conn_id)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // -------------------------------------------------------------------- |
| 459 | |
| 460 | /** |
| 461 | * Escape the SQL Identifiers |
| 462 | * |
| 463 | * This function escapes column and table names |
| 464 | * |
| 465 | * @access private |
| 466 | * @param string |
| 467 | * @return string |
| 468 | */ |
| 469 | function _escape_identifiers($item) |
| 470 | { |
| 471 | if ($this->_escape_char == '') |
| 472 | { |
| 473 | return $item; |
| 474 | } |
| 475 | |
| 476 | foreach ($this->_reserved_identifiers as $id) |
| 477 | { |
| 478 | if (strpos($item, '.'.$id) !== FALSE) |
| 479 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 480 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 481 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | // remove duplicates if the user already included the escape |
| 483 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 484 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 486 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | if (strpos($item, '.') !== FALSE) |
| 488 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 489 | $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] | 490 | } |
| 491 | else |
| 492 | { |
| 493 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 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 | // remove duplicates if the user already included the escape |
| 497 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 498 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | // -------------------------------------------------------------------- |
| 501 | |
| 502 | /** |
| 503 | * From Tables |
| 504 | * |
| 505 | * This function implicitly groups FROM tables so there is no confusion |
| 506 | * about operator precedence in harmony with SQL standards |
| 507 | * |
| 508 | * @access public |
| 509 | * @param type |
| 510 | * @return type |
| 511 | */ |
| 512 | function _from_tables($tables) |
| 513 | { |
| 514 | if ( ! is_array($tables)) |
| 515 | { |
| 516 | $tables = array($tables); |
| 517 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 518 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 519 | return implode(', ', $tables); |
| 520 | } |
| 521 | |
| 522 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 523 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | /** |
| 525 | * Insert statement |
| 526 | * |
| 527 | * Generates a platform-specific insert string from the supplied data |
| 528 | * |
| 529 | * @access public |
| 530 | * @param string the table name |
| 531 | * @param array the insert keys |
| 532 | * @param array the insert values |
| 533 | * @return string |
| 534 | */ |
| 535 | function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 536 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 537 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 538 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 539 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | // -------------------------------------------------------------------- |
| 541 | |
| 542 | /** |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 543 | * Insert_batch statement |
| 544 | * |
| 545 | * Generates a platform-specific insert string from the supplied data |
| 546 | * |
| 547 | * @access public |
| 548 | * @param string the table name |
| 549 | * @param array the insert keys |
| 550 | * @param array the insert values |
| 551 | * @return string |
| 552 | */ |
| 553 | function _insert_batch($table, $keys, $values) |
| 554 | { |
| 555 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values); |
| 556 | } |
| 557 | |
| 558 | // -------------------------------------------------------------------- |
| 559 | |
| 560 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 561 | * Update statement |
| 562 | * |
| 563 | * Generates a platform-specific update string from the supplied data |
| 564 | * |
| 565 | * @access public |
| 566 | * @param string the table name |
| 567 | * @param array the update data |
| 568 | * @param array the where clause |
| 569 | * @param array the orderby clause |
| 570 | * @param array the limit clause |
| 571 | * @return string |
| 572 | */ |
| 573 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 574 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 575 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | { |
| 577 | $valstr[] = $key." = ".$val; |
| 578 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 579 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 581 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 583 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 584 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 585 | |
| 586 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 587 | |
| 588 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 589 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 590 | return $sql; |
| 591 | } |
| 592 | |
| 593 | // -------------------------------------------------------------------- |
| 594 | |
| 595 | /** |
| 596 | * Truncate statement |
| 597 | * |
| 598 | * Generates a platform-specific truncate string from the supplied data |
| 599 | * If the database does not support the truncate() command |
| 600 | * This function maps to "DELETE FROM table" |
| 601 | * |
| 602 | * @access public |
| 603 | * @param string the table name |
| 604 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 605 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 606 | function _truncate($table) |
| 607 | { |
| 608 | return "TRUNCATE ".$table; |
| 609 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 610 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 611 | // -------------------------------------------------------------------- |
| 612 | |
| 613 | /** |
| 614 | * Delete statement |
| 615 | * |
| 616 | * Generates a platform-specific delete string from the supplied data |
| 617 | * |
| 618 | * @access public |
| 619 | * @param string the table name |
| 620 | * @param array the where clause |
| 621 | * @param string the limit clause |
| 622 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 623 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 625 | { |
| 626 | $conditions = ''; |
| 627 | |
| 628 | if (count($where) > 0 OR count($like) > 0) |
| 629 | { |
| 630 | $conditions = "\nWHERE "; |
| 631 | $conditions .= implode("\n", $this->ar_where); |
| 632 | |
| 633 | if (count($where) > 0 && count($like) > 0) |
| 634 | { |
| 635 | $conditions .= " AND "; |
| 636 | } |
| 637 | $conditions .= implode("\n", $like); |
| 638 | } |
| 639 | |
| 640 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 641 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 642 | return "DELETE FROM ".$table.$conditions.$limit; |
| 643 | } |
| 644 | |
| 645 | // -------------------------------------------------------------------- |
| 646 | /** |
| 647 | * Limit string |
| 648 | * |
| 649 | * Generates a platform-specific LIMIT clause |
| 650 | * |
| 651 | * @access public |
| 652 | * @param string the sql query string |
| 653 | * @param integer the number of rows to limit the query to |
| 654 | * @param integer the offset value |
| 655 | * @return string |
| 656 | */ |
| 657 | function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 658 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 659 | $sql .= "LIMIT ".$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 660 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 661 | if ($offset > 0) |
| 662 | { |
| 663 | $sql .= " OFFSET ".$offset; |
| 664 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 665 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 666 | return $sql; |
| 667 | } |
| 668 | |
| 669 | // -------------------------------------------------------------------- |
| 670 | |
| 671 | /** |
| 672 | * Close DB Connection |
| 673 | * |
| 674 | * @access public |
| 675 | * @param resource |
| 676 | * @return void |
| 677 | */ |
| 678 | function _close($conn_id) |
| 679 | { |
| 680 | @pg_close($conn_id); |
| 681 | } |
| 682 | |
| 683 | |
| 684 | } |
| 685 | |
| 686 | |
| 687 | /* End of file postgre_driver.php */ |
Andrey Andreev | 063f596 | 2012-02-27 12:20:52 +0200 | [diff] [blame] | 688 | /* Location: ./system/database/drivers/postgre/postgre_driver.php */ |