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 |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame^] | 34 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | /** |
| 150 | * Set client character set |
| 151 | * |
| 152 | * @access public |
| 153 | * @param string |
| 154 | * @param string |
| 155 | * @return resource |
| 156 | */ |
| 157 | function db_set_charset($charset, $collation) |
| 158 | { |
| 159 | // @todo - add support if needed |
| 160 | return TRUE; |
| 161 | } |
| 162 | |
| 163 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | /** |
| 166 | * Version number query string |
| 167 | * |
| 168 | * @access public |
| 169 | * @return string |
| 170 | */ |
| 171 | function _version() |
| 172 | { |
| 173 | return "SELECT version() AS ver"; |
| 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 | { |
| 342 | $v = $this->_version(); |
| 343 | $v = $v['server']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 345 | $table = func_num_args() > 0 ? func_get_arg(0) : NULL; |
| 346 | $column = func_num_args() > 1 ? func_get_arg(1) : NULL; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 347 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 348 | if ($table == NULL && $v >= '8.1') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | { |
| 350 | $sql='SELECT LASTVAL() as ins_id'; |
| 351 | } |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 352 | elseif ($table != NULL && $column != NULL && $v >= '8.0') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
| 354 | $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column); |
| 355 | $query = $this->query($sql); |
| 356 | $row = $query->row(); |
| 357 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq); |
| 358 | } |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 359 | elseif ($table != NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | { |
| 361 | // seq_name passed in table parameter |
| 362 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table); |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | return pg_last_oid($this->result_id); |
| 367 | } |
| 368 | $query = $this->query($sql); |
| 369 | $row = $query->row(); |
| 370 | return $row->ins_id; |
| 371 | } |
| 372 | |
| 373 | // -------------------------------------------------------------------- |
| 374 | |
| 375 | /** |
| 376 | * "Count All" query |
| 377 | * |
| 378 | * Generates a platform-specific query string that counts all records in |
| 379 | * the specified database |
| 380 | * |
| 381 | * @access public |
| 382 | * @param string |
| 383 | * @return string |
| 384 | */ |
| 385 | function count_all($table = '') |
| 386 | { |
| 387 | if ($table == '') |
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 | |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 392 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
| 393 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 395 | { |
| 396 | return 0; |
| 397 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | |
| 399 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 400 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 401 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // -------------------------------------------------------------------- |
| 405 | |
| 406 | /** |
| 407 | * Show table query |
| 408 | * |
| 409 | * Generates a platform-specific query string so that the table names can be fetched |
| 410 | * |
| 411 | * @access private |
| 412 | * @param boolean |
| 413 | * @return string |
| 414 | */ |
| 415 | function _list_tables($prefix_limit = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 416 | { |
| 417 | $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'"; |
| 418 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 420 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 421 | $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] | 422 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 423 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | return $sql; |
| 425 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | // -------------------------------------------------------------------- |
| 428 | |
| 429 | /** |
| 430 | * Show column query |
| 431 | * |
| 432 | * Generates a platform-specific query string so that the column names can be fetched |
| 433 | * |
| 434 | * @access public |
| 435 | * @param string the table name |
| 436 | * @return string |
| 437 | */ |
| 438 | function _list_columns($table = '') |
| 439 | { |
| 440 | return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$table."'"; |
| 441 | } |
| 442 | |
| 443 | // -------------------------------------------------------------------- |
| 444 | |
| 445 | /** |
| 446 | * Field data query |
| 447 | * |
| 448 | * Generates a platform-specific query so that the column data can be retrieved |
| 449 | * |
| 450 | * @access public |
| 451 | * @param string the table name |
| 452 | * @return object |
| 453 | */ |
| 454 | function _field_data($table) |
| 455 | { |
| 456 | return "SELECT * FROM ".$table." LIMIT 1"; |
| 457 | } |
| 458 | |
| 459 | // -------------------------------------------------------------------- |
| 460 | |
| 461 | /** |
| 462 | * The error message string |
| 463 | * |
| 464 | * @access private |
| 465 | * @return string |
| 466 | */ |
| 467 | function _error_message() |
| 468 | { |
| 469 | return pg_last_error($this->conn_id); |
| 470 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | // -------------------------------------------------------------------- |
| 473 | |
| 474 | /** |
| 475 | * The error message number |
| 476 | * |
| 477 | * @access private |
| 478 | * @return integer |
| 479 | */ |
| 480 | function _error_number() |
| 481 | { |
| 482 | return ''; |
| 483 | } |
| 484 | |
| 485 | // -------------------------------------------------------------------- |
| 486 | |
| 487 | /** |
| 488 | * Escape the SQL Identifiers |
| 489 | * |
| 490 | * This function escapes column and table names |
| 491 | * |
| 492 | * @access private |
| 493 | * @param string |
| 494 | * @return string |
| 495 | */ |
| 496 | function _escape_identifiers($item) |
| 497 | { |
| 498 | if ($this->_escape_char == '') |
| 499 | { |
| 500 | return $item; |
| 501 | } |
| 502 | |
| 503 | foreach ($this->_reserved_identifiers as $id) |
| 504 | { |
| 505 | if (strpos($item, '.'.$id) !== FALSE) |
| 506 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 507 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 508 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | // remove duplicates if the user already included the escape |
| 510 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 511 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 512 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 513 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 514 | if (strpos($item, '.') !== FALSE) |
| 515 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 516 | $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] | 517 | } |
| 518 | else |
| 519 | { |
| 520 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 521 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 522 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | // remove duplicates if the user already included the escape |
| 524 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 525 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 526 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | // -------------------------------------------------------------------- |
| 528 | |
| 529 | /** |
| 530 | * From Tables |
| 531 | * |
| 532 | * This function implicitly groups FROM tables so there is no confusion |
| 533 | * about operator precedence in harmony with SQL standards |
| 534 | * |
| 535 | * @access public |
| 536 | * @param type |
| 537 | * @return type |
| 538 | */ |
| 539 | function _from_tables($tables) |
| 540 | { |
| 541 | if ( ! is_array($tables)) |
| 542 | { |
| 543 | $tables = array($tables); |
| 544 | } |
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 implode(', ', $tables); |
| 547 | } |
| 548 | |
| 549 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 550 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 551 | /** |
| 552 | * Insert 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($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 563 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 564 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 565 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 566 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 567 | // -------------------------------------------------------------------- |
| 568 | |
| 569 | /** |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 570 | * Insert_batch statement |
| 571 | * |
| 572 | * Generates a platform-specific insert string from the supplied data |
| 573 | * |
| 574 | * @access public |
| 575 | * @param string the table name |
| 576 | * @param array the insert keys |
| 577 | * @param array the insert values |
| 578 | * @return string |
| 579 | */ |
| 580 | function _insert_batch($table, $keys, $values) |
| 581 | { |
| 582 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values); |
| 583 | } |
| 584 | |
| 585 | // -------------------------------------------------------------------- |
| 586 | |
| 587 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | * Update statement |
| 589 | * |
| 590 | * Generates a platform-specific update string from the supplied data |
| 591 | * |
| 592 | * @access public |
| 593 | * @param string the table name |
| 594 | * @param array the update data |
| 595 | * @param array the where clause |
| 596 | * @param array the orderby clause |
| 597 | * @param array the limit clause |
| 598 | * @return string |
| 599 | */ |
| 600 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 601 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 602 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | { |
| 604 | $valstr[] = $key." = ".$val; |
| 605 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 606 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
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 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
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 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 612 | |
| 613 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 614 | |
| 615 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 616 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 617 | return $sql; |
| 618 | } |
| 619 | |
| 620 | // -------------------------------------------------------------------- |
| 621 | |
| 622 | /** |
| 623 | * Truncate statement |
| 624 | * |
| 625 | * Generates a platform-specific truncate string from the supplied data |
| 626 | * If the database does not support the truncate() command |
| 627 | * This function maps to "DELETE FROM table" |
| 628 | * |
| 629 | * @access public |
| 630 | * @param string the table name |
| 631 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 632 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 633 | function _truncate($table) |
| 634 | { |
| 635 | return "TRUNCATE ".$table; |
| 636 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 637 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 638 | // -------------------------------------------------------------------- |
| 639 | |
| 640 | /** |
| 641 | * Delete statement |
| 642 | * |
| 643 | * Generates a platform-specific delete string from the supplied data |
| 644 | * |
| 645 | * @access public |
| 646 | * @param string the table name |
| 647 | * @param array the where clause |
| 648 | * @param string the limit clause |
| 649 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 650 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 651 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 652 | { |
| 653 | $conditions = ''; |
| 654 | |
| 655 | if (count($where) > 0 OR count($like) > 0) |
| 656 | { |
| 657 | $conditions = "\nWHERE "; |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame^] | 658 | $conditions .= implode("\n", $this->qb_where); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 659 | |
| 660 | if (count($where) > 0 && count($like) > 0) |
| 661 | { |
| 662 | $conditions .= " AND "; |
| 663 | } |
| 664 | $conditions .= implode("\n", $like); |
| 665 | } |
| 666 | |
| 667 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 668 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 669 | return "DELETE FROM ".$table.$conditions.$limit; |
| 670 | } |
| 671 | |
| 672 | // -------------------------------------------------------------------- |
| 673 | /** |
| 674 | * Limit string |
| 675 | * |
| 676 | * Generates a platform-specific LIMIT clause |
| 677 | * |
| 678 | * @access public |
| 679 | * @param string the sql query string |
| 680 | * @param integer the number of rows to limit the query to |
| 681 | * @param integer the offset value |
| 682 | * @return string |
| 683 | */ |
| 684 | function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 685 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 686 | $sql .= "LIMIT ".$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 687 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 688 | if ($offset > 0) |
| 689 | { |
| 690 | $sql .= " OFFSET ".$offset; |
| 691 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 692 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 693 | return $sql; |
| 694 | } |
| 695 | |
| 696 | // -------------------------------------------------------------------- |
| 697 | |
| 698 | /** |
| 699 | * Close DB Connection |
| 700 | * |
| 701 | * @access public |
| 702 | * @param resource |
| 703 | * @return void |
| 704 | */ |
| 705 | function _close($conn_id) |
| 706 | { |
| 707 | @pg_close($conn_id); |
| 708 | } |
| 709 | |
| 710 | |
| 711 | } |
| 712 | |
| 713 | |
| 714 | /* End of file postgre_driver.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 715 | /* Location: ./system/database/drivers/postgre/postgre_driver.php */ |