Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * Postgre Database Adapter Class
|
| 20 | *
|
| 21 | * Note: _DB is an extender class that the app controller
|
| 22 | * creates dynamically based on whether the active record
|
| 23 | * class is being used or not.
|
| 24 | *
|
| 25 | * @package CodeIgniter
|
| 26 | * @subpackage Drivers
|
| 27 | * @category Database
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 28 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 29 | * @link http://codeigniter.com/user_guide/database/
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 30 | */
|
| 31 | class CI_DB_postgre_driver extends CI_DB {
|
| 32 |
|
| 33 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 34 | * The syntax to count rows is slightly different across different
|
| 35 | * database engines, so this string appears in each driver and is
|
| 36 | * used for the count_all() and count_all_results() functions.
|
| 37 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 38 | var $_count_string = "SELECT COUNT(*) AS ";
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 39 | var $_random_keyword = ' RANDOM()'; // database specific random keyword
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 40 |
|
| 41 | /**
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 42 | * Non-persistent database connection
|
| 43 | *
|
| 44 | * @access private called by the base class
|
| 45 | * @return resource
|
| 46 | */
|
| 47 | function db_connect()
|
| 48 | {
|
| 49 | $port = ($this->port == '') ? '' : " port=".$this->port;
|
| 50 |
|
| 51 | return @pg_connect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
|
| 52 | }
|
| 53 |
|
| 54 | // --------------------------------------------------------------------
|
| 55 |
|
| 56 | /**
|
| 57 | * Persistent database connection
|
| 58 | *
|
| 59 | * @access private called by the base class
|
| 60 | * @return resource
|
| 61 | */
|
| 62 | function db_pconnect()
|
| 63 | {
|
| 64 | $port = ($this->port == '') ? '' : " port=".$this->port;
|
| 65 |
|
| 66 | return @pg_pconnect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
|
| 67 | }
|
| 68 |
|
| 69 | // --------------------------------------------------------------------
|
| 70 |
|
| 71 | /**
|
| 72 | * Select the database
|
| 73 | *
|
| 74 | * @access private called by the base class
|
| 75 | * @return resource
|
| 76 | */
|
| 77 | function db_select()
|
| 78 | {
|
| 79 | // Not needed for Postgre so we'll return TRUE
|
| 80 | return TRUE;
|
| 81 | }
|
| 82 |
|
| 83 | // --------------------------------------------------------------------
|
| 84 |
|
| 85 | /**
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 86 | * Set client character set
|
| 87 | *
|
| 88 | * @access public
|
| 89 | * @param string
|
| 90 | * @param string
|
| 91 | * @return resource
|
| 92 | */
|
| 93 | function db_set_charset($charset, $collation)
|
| 94 | {
|
| 95 | // TODO - add support if needed
|
| 96 | return TRUE;
|
| 97 | }
|
| 98 |
|
| 99 | // --------------------------------------------------------------------
|
| 100 |
|
| 101 | /**
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 102 | * Version number query string
|
| 103 | *
|
| 104 | * @access public
|
| 105 | * @return string
|
| 106 | */
|
| 107 | function _version()
|
| 108 | {
|
| 109 | return "SELECT version() AS ver";
|
| 110 | }
|
| 111 |
|
| 112 | // --------------------------------------------------------------------
|
| 113 |
|
| 114 | /**
|
| 115 | * Execute the query
|
| 116 | *
|
| 117 | * @access private called by the base class
|
| 118 | * @param string an SQL query
|
| 119 | * @return resource
|
| 120 | */
|
| 121 | function _execute($sql)
|
| 122 | {
|
| 123 | $sql = $this->_prep_query($sql);
|
| 124 | return @pg_query($this->conn_id, $sql);
|
| 125 | }
|
| 126 |
|
| 127 | // --------------------------------------------------------------------
|
| 128 |
|
| 129 | /**
|
| 130 | * Prep the query
|
| 131 | *
|
| 132 | * If needed, each database adapter can prep the query string
|
| 133 | *
|
| 134 | * @access private called by execute()
|
| 135 | * @param string an SQL query
|
| 136 | * @return string
|
| 137 | */
|
| 138 | function _prep_query($sql)
|
| 139 | {
|
| 140 | return $sql;
|
| 141 | }
|
| 142 |
|
| 143 | // --------------------------------------------------------------------
|
| 144 |
|
| 145 | /**
|
| 146 | * Begin Transaction
|
| 147 | *
|
| 148 | * @access public
|
| 149 | * @return bool
|
| 150 | */
|
| 151 | function trans_begin($test_mode = FALSE)
|
| 152 | {
|
| 153 | if ( ! $this->trans_enabled)
|
| 154 | {
|
| 155 | return TRUE;
|
| 156 | }
|
| 157 |
|
| 158 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 159 | if ($this->_trans_depth > 0)
|
| 160 | {
|
| 161 | return TRUE;
|
| 162 | }
|
| 163 |
|
| 164 | // Reset the transaction failure flag.
|
| 165 | // If the $test_mode flag is set to TRUE transactions will be rolled back
|
| 166 | // even if the queries produce a successful result.
|
| 167 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
|
| 168 |
|
| 169 | return @pg_exec($this->conn_id, "begin");
|
| 170 | }
|
| 171 |
|
| 172 | // --------------------------------------------------------------------
|
| 173 |
|
| 174 | /**
|
| 175 | * Commit Transaction
|
| 176 | *
|
| 177 | * @access public
|
| 178 | * @return bool
|
| 179 | */
|
| 180 | function trans_commit()
|
| 181 | {
|
| 182 | if ( ! $this->trans_enabled)
|
| 183 | {
|
| 184 | return TRUE;
|
| 185 | }
|
| 186 |
|
| 187 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 188 | if ($this->_trans_depth > 0)
|
| 189 | {
|
| 190 | return TRUE;
|
| 191 | }
|
| 192 |
|
| 193 | return @pg_exec($this->conn_id, "commit");
|
| 194 | }
|
| 195 |
|
| 196 | // --------------------------------------------------------------------
|
| 197 |
|
| 198 | /**
|
| 199 | * Rollback Transaction
|
| 200 | *
|
| 201 | * @access public
|
| 202 | * @return bool
|
| 203 | */
|
| 204 | function trans_rollback()
|
| 205 | {
|
| 206 | if ( ! $this->trans_enabled)
|
| 207 | {
|
| 208 | return TRUE;
|
| 209 | }
|
| 210 |
|
| 211 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 212 | if ($this->_trans_depth > 0)
|
| 213 | {
|
| 214 | return TRUE;
|
| 215 | }
|
| 216 |
|
| 217 | return @pg_exec($this->conn_id, "rollback");
|
| 218 | }
|
| 219 |
|
| 220 | // --------------------------------------------------------------------
|
| 221 |
|
| 222 | /**
|
| 223 | * Escape String
|
| 224 | *
|
| 225 | * @access public
|
| 226 | * @param string
|
| 227 | * @return string
|
| 228 | */
|
| 229 | function escape_str($str)
|
| 230 | {
|
| 231 | return pg_escape_string($str);
|
| 232 | }
|
| 233 |
|
| 234 | // --------------------------------------------------------------------
|
| 235 |
|
| 236 | /**
|
| 237 | * Affected Rows
|
| 238 | *
|
| 239 | * @access public
|
| 240 | * @return integer
|
| 241 | */
|
| 242 | function affected_rows()
|
| 243 | {
|
| 244 | return @pg_affected_rows($this->result_id);
|
| 245 | }
|
| 246 |
|
| 247 | // --------------------------------------------------------------------
|
| 248 |
|
| 249 | /**
|
| 250 | * Insert ID
|
| 251 | *
|
| 252 | * @access public
|
| 253 | * @return integer
|
| 254 | */
|
| 255 | function insert_id()
|
| 256 | {
|
Derek Jones | f9a4e9e | 2007-07-12 13:56:21 +0000 | [diff] [blame] | 257 | $v = $this->_version();
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 258 | $v = $v['server'];
|
| 259 |
|
| 260 | $table = func_num_args() > 0 ? func_get_arg(0) : null;
|
| 261 | $column = func_num_args() > 1 ? func_get_arg(1) : null;
|
| 262 |
|
| 263 | if ($table == null && $v >= '8.1')
|
| 264 | {
|
| 265 | $sql='SELECT LASTVAL() as ins_id';
|
| 266 | }
|
| 267 | elseif ($table != null && $column != null && $v >= '8.0')
|
| 268 | {
|
| 269 | $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
|
| 270 | $query = $this->query($sql);
|
| 271 | $row = $query->row();
|
| 272 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
|
| 273 | }
|
| 274 | elseif ($table != null)
|
| 275 | {
|
| 276 | // seq_name passed in table parameter
|
| 277 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
|
| 278 | }
|
| 279 | else
|
| 280 | {
|
| 281 | return pg_last_oid($this->result_id);
|
| 282 | }
|
| 283 | $query = $this->query($sql);
|
| 284 | $row = $query->row();
|
| 285 | return $row->ins_id;
|
| 286 | }
|
| 287 |
|
| 288 | // --------------------------------------------------------------------
|
| 289 |
|
| 290 | /**
|
| 291 | * "Count All" query
|
| 292 | *
|
| 293 | * Generates a platform-specific query string that counts all records in
|
| 294 | * the specified database
|
| 295 | *
|
| 296 | * @access public
|
| 297 | * @param string
|
| 298 | * @return string
|
| 299 | */
|
| 300 | function count_all($table = '')
|
| 301 | {
|
| 302 | if ($table == '')
|
| 303 | return '0';
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 304 |
|
Derek Allard | f6cd45c | 2008-01-18 14:31:51 +0000 | [diff] [blame] | 305 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 306 |
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 307 | if ($query->num_rows() == 0)
|
| 308 | return '0';
|
| 309 |
|
| 310 | $row = $query->row();
|
| 311 | return $row->numrows;
|
| 312 | }
|
| 313 |
|
| 314 | // --------------------------------------------------------------------
|
| 315 |
|
| 316 | /**
|
| 317 | * Show table query
|
| 318 | *
|
| 319 | * Generates a platform-specific query string so that the table names can be fetched
|
| 320 | *
|
| 321 | * @access private
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 322 | * @param boolean
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 323 | * @return string
|
| 324 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 325 | function _list_tables($prefix_limit = FALSE)
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 326 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 327 | $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
|
| 328 |
|
| 329 | if ($prefix_limit !== FALSE AND $this->dbprefix != '')
|
| 330 | {
|
| 331 | $sql .= " AND table_name LIKE '".$this->dbprefix."%'";
|
| 332 | }
|
| 333 |
|
| 334 | return $sql;
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 335 | }
|
| 336 |
|
| 337 | // --------------------------------------------------------------------
|
| 338 |
|
| 339 | /**
|
| 340 | * Show column query
|
| 341 | *
|
| 342 | * Generates a platform-specific query string so that the column names can be fetched
|
| 343 | *
|
| 344 | * @access public
|
| 345 | * @param string the table name
|
| 346 | * @return string
|
| 347 | */
|
| 348 | function _list_columns($table = '')
|
| 349 | {
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 350 | return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->_escape_table($table)."'";
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 351 | }
|
| 352 |
|
| 353 | // --------------------------------------------------------------------
|
| 354 |
|
| 355 | /**
|
| 356 | * Field data query
|
| 357 | *
|
| 358 | * Generates a platform-specific query so that the column data can be retrieved
|
| 359 | *
|
| 360 | * @access public
|
| 361 | * @param string the table name
|
| 362 | * @return object
|
| 363 | */
|
| 364 | function _field_data($table)
|
| 365 | {
|
| 366 | return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
|
| 367 | }
|
| 368 |
|
| 369 | // --------------------------------------------------------------------
|
| 370 |
|
| 371 | /**
|
| 372 | * The error message string
|
| 373 | *
|
| 374 | * @access private
|
| 375 | * @return string
|
| 376 | */
|
| 377 | function _error_message()
|
| 378 | {
|
| 379 | return pg_last_error($this->conn_id);
|
| 380 | }
|
| 381 |
|
| 382 | // --------------------------------------------------------------------
|
| 383 |
|
| 384 | /**
|
| 385 | * The error message number
|
| 386 | *
|
| 387 | * @access private
|
| 388 | * @return integer
|
| 389 | */
|
| 390 | function _error_number()
|
| 391 | {
|
| 392 | return '';
|
| 393 | }
|
| 394 |
|
| 395 | // --------------------------------------------------------------------
|
| 396 |
|
| 397 | /**
|
| 398 | * Escape Table Name
|
| 399 | *
|
| 400 | * This function adds backticks if the table name has a period
|
| 401 | * in it. Some DBs will get cranky unless periods are escaped.
|
| 402 | *
|
| 403 | * @access private
|
| 404 | * @param string the table name
|
| 405 | * @return string
|
| 406 | */
|
| 407 | function _escape_table($table)
|
| 408 | {
|
| 409 | if (stristr($table, '.'))
|
| 410 | {
|
| 411 | $table = '"'.preg_replace("/\./", '"."', $table).'"';
|
| 412 | }
|
| 413 |
|
| 414 | return $table;
|
| 415 | }
|
| 416 |
|
| 417 | // --------------------------------------------------------------------
|
| 418 |
|
| 419 | /**
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 420 | * Protect Identifiers
|
| 421 | *
|
| 422 | * This function adds backticks if appropriate based on db type
|
| 423 | *
|
| 424 | * @access private
|
| 425 | * @param mixed the item to escape
|
| 426 | * @param boolean only affect the first word
|
| 427 | * @return mixed the item with backticks
|
| 428 | */
|
| 429 | function _protect_identifiers($item, $first_word_only = FALSE)
|
| 430 | {
|
| 431 | if (is_array($item))
|
| 432 | {
|
| 433 | $escaped_array = array();
|
| 434 |
|
| 435 | foreach($item as $k=>$v)
|
| 436 | {
|
| 437 | $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
|
| 438 | }
|
| 439 |
|
| 440 | return $escaped_array;
|
| 441 | }
|
| 442 |
|
| 443 | // This function may get "item1 item2" as a string, and so
|
| 444 | // we may need ""item1" "item2"" and not ""item1 item2""
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 445 | if (ctype_alnum($item) === FALSE)
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 446 | {
|
| 447 | // This function may get "field >= 1", and need it to return ""field" >= 1"
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 448 | $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 449 |
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 450 | $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1"$2"$3', $item);
|
| 451 | }
|
| 452 | else
|
| 453 | {
|
Derek Allard | 4acd792 | 2008-01-26 19:33:59 +0000 | [diff] [blame] | 454 | return "\"{$item}\"";
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 455 | }
|
| 456 |
|
| 457 | $exceptions = array('AS', '/', '-', '%', '+', '*');
|
| 458 |
|
| 459 | foreach ($exceptions as $exception)
|
| 460 | {
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 461 |
|
Derek Allard | 4acd792 | 2008-01-26 19:33:59 +0000 | [diff] [blame] | 462 | if (stristr($item, " \"{$exception}\" ") !== FALSE)
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 463 | {
|
| 464 | $item = preg_replace('/ "('.preg_quote($exception).')" /i', ' $1 ', $item);
|
| 465 | }
|
| 466 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 467 | return $item;
|
| 468 | }
|
| 469 |
|
| 470 | // --------------------------------------------------------------------
|
| 471 |
|
| 472 | /**
|
Derek Jones | c6ad023 | 2008-01-29 18:44:54 +0000 | [diff] [blame^] | 473 | * From Tables
|
| 474 | *
|
| 475 | * This function implicitly groups FROM tables so there is no confusion
|
| 476 | * about operator precedence in harmony with SQL standards
|
| 477 | *
|
| 478 | * @access public
|
| 479 | * @param type
|
| 480 | * @return type
|
| 481 | */
|
| 482 | function _from_tables($tables)
|
| 483 | {
|
| 484 | if (! is_array($tables))
|
| 485 | {
|
| 486 | $tables = array($tables);
|
| 487 | }
|
| 488 |
|
| 489 | return '('.implode(', ', $tables).')';
|
| 490 | }
|
| 491 |
|
| 492 | // --------------------------------------------------------------------
|
| 493 |
|
| 494 | /**
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 495 | * Insert statement
|
| 496 | *
|
| 497 | * Generates a platform-specific insert string from the supplied data
|
| 498 | *
|
| 499 | * @access public
|
| 500 | * @param string the table name
|
| 501 | * @param array the insert keys
|
| 502 | * @param array the insert values
|
| 503 | * @return string
|
| 504 | */
|
| 505 | function _insert($table, $keys, $values)
|
| 506 | {
|
| 507 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
|
| 508 | }
|
| 509 |
|
| 510 | // --------------------------------------------------------------------
|
| 511 |
|
| 512 | /**
|
| 513 | * Update statement
|
| 514 | *
|
| 515 | * Generates a platform-specific update string from the supplied data
|
| 516 | *
|
| 517 | * @access public
|
| 518 | * @param string the table name
|
| 519 | * @param array the update data
|
| 520 | * @param array the where clause
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 521 | * @param array the orderby clause
|
| 522 | * @param array the limit clause
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 523 | * @return string
|
| 524 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 525 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 526 | {
|
| 527 | foreach($values as $key => $val)
|
| 528 | {
|
| 529 | $valstr[] = $key." = ".$val;
|
| 530 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 531 |
|
| 532 | $limit = (!$limit) ? '' : ' LIMIT '.$limit;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 533 |
|
| 534 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 535 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 536 | return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
|
| 537 | }
|
| 538 |
|
| 539 |
|
| 540 | // --------------------------------------------------------------------
|
| 541 |
|
| 542 | /**
|
| 543 | * Truncate statement
|
| 544 | *
|
| 545 | * Generates a platform-specific truncate string from the supplied data
|
| 546 | * If the database does not support the truncate() command
|
| 547 | * This function maps to "DELETE FROM table"
|
| 548 | *
|
| 549 | * @access public
|
| 550 | * @param string the table name
|
| 551 | * @return string
|
| 552 | */
|
| 553 | function _truncate($table)
|
| 554 | {
|
| 555 | return "TRUNCATE ".$this->_escape_table($table);
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 556 | }
|
| 557 |
|
| 558 | // --------------------------------------------------------------------
|
| 559 |
|
| 560 | /**
|
| 561 | * Delete statement
|
| 562 | *
|
| 563 | * Generates a platform-specific delete string from the supplied data
|
| 564 | *
|
| 565 | * @access public
|
| 566 | * @param string the table name
|
| 567 | * @param array the where clause
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 568 | * @param string the limit clause
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 569 | * @return string
|
| 570 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 571 | function _delete($table, $where = array(), $like = array(), $limit = FALSE)
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 572 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 573 | $conditions = '';
|
| 574 |
|
| 575 | if (count($where) > 0 || count($like) > 0)
|
| 576 | {
|
| 577 | $conditions = "\nWHERE ";
|
| 578 | $conditions .= implode("\n", $this->ar_where);
|
| 579 |
|
| 580 | if (count($where) > 0 && count($like) > 0)
|
| 581 | {
|
| 582 | $conditions .= " AND ";
|
| 583 | }
|
| 584 | $conditions .= implode("\n", $like);
|
| 585 | }
|
| 586 |
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 587 | $limit = (!$limit) ? '' : ' LIMIT '.$limit;
|
| 588 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 589 | return "DELETE FROM ".$table.$conditions.$limit;
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 590 | }
|
| 591 |
|
| 592 | // --------------------------------------------------------------------
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 593 | /**
|
| 594 | * Limit string
|
| 595 | *
|
| 596 | * Generates a platform-specific LIMIT clause
|
| 597 | *
|
| 598 | * @access public
|
| 599 | * @param string the sql query string
|
| 600 | * @param integer the number of rows to limit the query to
|
| 601 | * @param integer the offset value
|
| 602 | * @return string
|
| 603 | */
|
| 604 | function _limit($sql, $limit, $offset)
|
| 605 | {
|
| 606 | $sql .= "LIMIT ".$limit;
|
| 607 |
|
| 608 | if ($offset > 0)
|
| 609 | {
|
| 610 | $sql .= " OFFSET ".$offset;
|
| 611 | }
|
| 612 |
|
| 613 | return $sql;
|
| 614 | }
|
| 615 |
|
| 616 | // --------------------------------------------------------------------
|
| 617 |
|
| 618 | /**
|
| 619 | * Close DB Connection
|
| 620 | *
|
| 621 | * @access public
|
| 622 | * @param resource
|
| 623 | * @return void
|
| 624 | */
|
| 625 | function _close($conn_id)
|
| 626 | {
|
| 627 | @pg_close($conn_id);
|
| 628 | }
|
| 629 |
|
| 630 |
|
| 631 | }
|
| 632 |
|
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 633 | ?> |