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