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
|
| 8 | * @author Rick Ellis
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Allard | 6838f00 | 2007-10-04 19:29:59 +0000 | [diff] [blame^] | 10 | * @license http://www.codeigniter.com/user_guide/license.html
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com
|
| 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
|
| 28 | * @author Rick Ellis
|
| 29 | * @link http://www.codeigniter.com/user_guide/database/
|
| 30 | */
|
| 31 | class CI_DB_postgre_driver extends CI_DB {
|
| 32 |
|
| 33 | /**
|
| 34 | * Non-persistent database connection
|
| 35 | *
|
| 36 | * @access private called by the base class
|
| 37 | * @return resource
|
| 38 | */
|
| 39 | function db_connect()
|
| 40 | {
|
| 41 | $port = ($this->port == '') ? '' : " port=".$this->port;
|
| 42 |
|
| 43 | return @pg_connect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
|
| 44 | }
|
| 45 |
|
| 46 | // --------------------------------------------------------------------
|
| 47 |
|
| 48 | /**
|
| 49 | * Persistent database connection
|
| 50 | *
|
| 51 | * @access private called by the base class
|
| 52 | * @return resource
|
| 53 | */
|
| 54 | function db_pconnect()
|
| 55 | {
|
| 56 | $port = ($this->port == '') ? '' : " port=".$this->port;
|
| 57 |
|
| 58 | return @pg_pconnect("host=".$this->hostname.$port." dbname=".$this->database." user=".$this->username." password=".$this->password);
|
| 59 | }
|
| 60 |
|
| 61 | // --------------------------------------------------------------------
|
| 62 |
|
| 63 | /**
|
| 64 | * Select the database
|
| 65 | *
|
| 66 | * @access private called by the base class
|
| 67 | * @return resource
|
| 68 | */
|
| 69 | function db_select()
|
| 70 | {
|
| 71 | // Not needed for Postgre so we'll return TRUE
|
| 72 | return TRUE;
|
| 73 | }
|
| 74 |
|
| 75 | // --------------------------------------------------------------------
|
| 76 |
|
| 77 | /**
|
| 78 | * Version number query string
|
| 79 | *
|
| 80 | * @access public
|
| 81 | * @return string
|
| 82 | */
|
| 83 | function _version()
|
| 84 | {
|
| 85 | return "SELECT version() AS ver";
|
| 86 | }
|
| 87 |
|
| 88 | // --------------------------------------------------------------------
|
| 89 |
|
| 90 | /**
|
| 91 | * Execute the query
|
| 92 | *
|
| 93 | * @access private called by the base class
|
| 94 | * @param string an SQL query
|
| 95 | * @return resource
|
| 96 | */
|
| 97 | function _execute($sql)
|
| 98 | {
|
| 99 | $sql = $this->_prep_query($sql);
|
| 100 | return @pg_query($this->conn_id, $sql);
|
| 101 | }
|
| 102 |
|
| 103 | // --------------------------------------------------------------------
|
| 104 |
|
| 105 | /**
|
| 106 | * Prep the query
|
| 107 | *
|
| 108 | * If needed, each database adapter can prep the query string
|
| 109 | *
|
| 110 | * @access private called by execute()
|
| 111 | * @param string an SQL query
|
| 112 | * @return string
|
| 113 | */
|
| 114 | function _prep_query($sql)
|
| 115 | {
|
| 116 | return $sql;
|
| 117 | }
|
| 118 |
|
| 119 | // --------------------------------------------------------------------
|
| 120 |
|
| 121 | /**
|
| 122 | * Begin Transaction
|
| 123 | *
|
| 124 | * @access public
|
| 125 | * @return bool
|
| 126 | */
|
| 127 | function trans_begin($test_mode = FALSE)
|
| 128 | {
|
| 129 | if ( ! $this->trans_enabled)
|
| 130 | {
|
| 131 | return TRUE;
|
| 132 | }
|
| 133 |
|
| 134 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 135 | if ($this->_trans_depth > 0)
|
| 136 | {
|
| 137 | return TRUE;
|
| 138 | }
|
| 139 |
|
| 140 | // Reset the transaction failure flag.
|
| 141 | // If the $test_mode flag is set to TRUE transactions will be rolled back
|
| 142 | // even if the queries produce a successful result.
|
| 143 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
|
| 144 |
|
| 145 | return @pg_exec($this->conn_id, "begin");
|
| 146 | }
|
| 147 |
|
| 148 | // --------------------------------------------------------------------
|
| 149 |
|
| 150 | /**
|
| 151 | * Commit Transaction
|
| 152 | *
|
| 153 | * @access public
|
| 154 | * @return bool
|
| 155 | */
|
| 156 | function trans_commit()
|
| 157 | {
|
| 158 | if ( ! $this->trans_enabled)
|
| 159 | {
|
| 160 | return TRUE;
|
| 161 | }
|
| 162 |
|
| 163 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 164 | if ($this->_trans_depth > 0)
|
| 165 | {
|
| 166 | return TRUE;
|
| 167 | }
|
| 168 |
|
| 169 | return @pg_exec($this->conn_id, "commit");
|
| 170 | }
|
| 171 |
|
| 172 | // --------------------------------------------------------------------
|
| 173 |
|
| 174 | /**
|
| 175 | * Rollback Transaction
|
| 176 | *
|
| 177 | * @access public
|
| 178 | * @return bool
|
| 179 | */
|
| 180 | function trans_rollback()
|
| 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, "rollback");
|
| 194 | }
|
| 195 |
|
| 196 | // --------------------------------------------------------------------
|
| 197 |
|
| 198 | /**
|
| 199 | * Escape String
|
| 200 | *
|
| 201 | * @access public
|
| 202 | * @param string
|
| 203 | * @return string
|
| 204 | */
|
| 205 | function escape_str($str)
|
| 206 | {
|
| 207 | return pg_escape_string($str);
|
| 208 | }
|
| 209 |
|
| 210 | // --------------------------------------------------------------------
|
| 211 |
|
| 212 | /**
|
| 213 | * Affected Rows
|
| 214 | *
|
| 215 | * @access public
|
| 216 | * @return integer
|
| 217 | */
|
| 218 | function affected_rows()
|
| 219 | {
|
| 220 | return @pg_affected_rows($this->result_id);
|
| 221 | }
|
| 222 |
|
| 223 | // --------------------------------------------------------------------
|
| 224 |
|
| 225 | /**
|
| 226 | * Insert ID
|
| 227 | *
|
| 228 | * @access public
|
| 229 | * @return integer
|
| 230 | */
|
| 231 | function insert_id()
|
| 232 | {
|
Derek Jones | f9a4e9e | 2007-07-12 13:56:21 +0000 | [diff] [blame] | 233 | $v = $this->_version();
|
Derek Allard | ba0dd63 | 2007-03-07 12:10:58 +0000 | [diff] [blame] | 234 | $v = $v['server'];
|
| 235 |
|
| 236 | $table = func_num_args() > 0 ? func_get_arg(0) : null;
|
| 237 | $column = func_num_args() > 1 ? func_get_arg(1) : null;
|
| 238 |
|
| 239 | if ($table == null && $v >= '8.1')
|
| 240 | {
|
| 241 | $sql='SELECT LASTVAL() as ins_id';
|
| 242 | }
|
| 243 | elseif ($table != null && $column != null && $v >= '8.0')
|
| 244 | {
|
| 245 | $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
|
| 246 | $query = $this->query($sql);
|
| 247 | $row = $query->row();
|
| 248 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
|
| 249 | }
|
| 250 | elseif ($table != null)
|
| 251 | {
|
| 252 | // seq_name passed in table parameter
|
| 253 | $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
|
| 254 | }
|
| 255 | else
|
| 256 | {
|
| 257 | return pg_last_oid($this->result_id);
|
| 258 | }
|
| 259 | $query = $this->query($sql);
|
| 260 | $row = $query->row();
|
| 261 | return $row->ins_id;
|
| 262 | }
|
| 263 |
|
| 264 | // --------------------------------------------------------------------
|
| 265 |
|
| 266 | /**
|
| 267 | * "Count All" query
|
| 268 | *
|
| 269 | * Generates a platform-specific query string that counts all records in
|
| 270 | * the specified database
|
| 271 | *
|
| 272 | * @access public
|
| 273 | * @param string
|
| 274 | * @return string
|
| 275 | */
|
| 276 | function count_all($table = '')
|
| 277 | {
|
| 278 | if ($table == '')
|
| 279 | return '0';
|
| 280 |
|
| 281 | $query = $this->query('SELECT COUNT(*) AS numrows FROM "'.$this->dbprefix.$table.'"');
|
| 282 |
|
| 283 | if ($query->num_rows() == 0)
|
| 284 | return '0';
|
| 285 |
|
| 286 | $row = $query->row();
|
| 287 | return $row->numrows;
|
| 288 | }
|
| 289 |
|
| 290 | // --------------------------------------------------------------------
|
| 291 |
|
| 292 | /**
|
| 293 | * Show table query
|
| 294 | *
|
| 295 | * Generates a platform-specific query string so that the table names can be fetched
|
| 296 | *
|
| 297 | * @access private
|
| 298 | * @return string
|
| 299 | */
|
| 300 | function _list_tables()
|
| 301 | {
|
| 302 | return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
|
| 303 | }
|
| 304 |
|
| 305 | // --------------------------------------------------------------------
|
| 306 |
|
| 307 | /**
|
| 308 | * Show column query
|
| 309 | *
|
| 310 | * Generates a platform-specific query string so that the column names can be fetched
|
| 311 | *
|
| 312 | * @access public
|
| 313 | * @param string the table name
|
| 314 | * @return string
|
| 315 | */
|
| 316 | function _list_columns($table = '')
|
| 317 | {
|
| 318 | return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->_escape_table($table)."'";
|
| 319 | }
|
| 320 |
|
| 321 | // --------------------------------------------------------------------
|
| 322 |
|
| 323 | /**
|
| 324 | * Field data query
|
| 325 | *
|
| 326 | * Generates a platform-specific query so that the column data can be retrieved
|
| 327 | *
|
| 328 | * @access public
|
| 329 | * @param string the table name
|
| 330 | * @return object
|
| 331 | */
|
| 332 | function _field_data($table)
|
| 333 | {
|
| 334 | return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
|
| 335 | }
|
| 336 |
|
| 337 | // --------------------------------------------------------------------
|
| 338 |
|
| 339 | /**
|
| 340 | * The error message string
|
| 341 | *
|
| 342 | * @access private
|
| 343 | * @return string
|
| 344 | */
|
| 345 | function _error_message()
|
| 346 | {
|
| 347 | return pg_last_error($this->conn_id);
|
| 348 | }
|
| 349 |
|
| 350 | // --------------------------------------------------------------------
|
| 351 |
|
| 352 | /**
|
| 353 | * The error message number
|
| 354 | *
|
| 355 | * @access private
|
| 356 | * @return integer
|
| 357 | */
|
| 358 | function _error_number()
|
| 359 | {
|
| 360 | return '';
|
| 361 | }
|
| 362 |
|
| 363 | // --------------------------------------------------------------------
|
| 364 |
|
| 365 | /**
|
| 366 | * Escape Table Name
|
| 367 | *
|
| 368 | * This function adds backticks if the table name has a period
|
| 369 | * in it. Some DBs will get cranky unless periods are escaped.
|
| 370 | *
|
| 371 | * @access private
|
| 372 | * @param string the table name
|
| 373 | * @return string
|
| 374 | */
|
| 375 | function _escape_table($table)
|
| 376 | {
|
| 377 | if (stristr($table, '.'))
|
| 378 | {
|
| 379 | $table = '"'.preg_replace("/\./", '"."', $table).'"';
|
| 380 | }
|
| 381 |
|
| 382 | return $table;
|
| 383 | }
|
| 384 |
|
| 385 | // --------------------------------------------------------------------
|
| 386 |
|
| 387 | /**
|
| 388 | * Insert statement
|
| 389 | *
|
| 390 | * Generates a platform-specific insert string from the supplied data
|
| 391 | *
|
| 392 | * @access public
|
| 393 | * @param string the table name
|
| 394 | * @param array the insert keys
|
| 395 | * @param array the insert values
|
| 396 | * @return string
|
| 397 | */
|
| 398 | function _insert($table, $keys, $values)
|
| 399 | {
|
| 400 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
|
| 401 | }
|
| 402 |
|
| 403 | // --------------------------------------------------------------------
|
| 404 |
|
| 405 | /**
|
| 406 | * Update statement
|
| 407 | *
|
| 408 | * Generates a platform-specific update string from the supplied data
|
| 409 | *
|
| 410 | * @access public
|
| 411 | * @param string the table name
|
| 412 | * @param array the update data
|
| 413 | * @param array the where clause
|
| 414 | * @return string
|
| 415 | */
|
| 416 | function _update($table, $values, $where)
|
| 417 | {
|
| 418 | foreach($values as $key => $val)
|
| 419 | {
|
| 420 | $valstr[] = $key." = ".$val;
|
| 421 | }
|
| 422 |
|
| 423 | return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
|
| 424 | }
|
| 425 |
|
| 426 | // --------------------------------------------------------------------
|
| 427 |
|
| 428 | /**
|
| 429 | * Delete statement
|
| 430 | *
|
| 431 | * Generates a platform-specific delete string from the supplied data
|
| 432 | *
|
| 433 | * @access public
|
| 434 | * @param string the table name
|
| 435 | * @param array the where clause
|
| 436 | * @return string
|
| 437 | */
|
| 438 | function _delete($table, $where)
|
| 439 | {
|
| 440 | return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
|
| 441 | }
|
| 442 |
|
| 443 | // --------------------------------------------------------------------
|
| 444 |
|
| 445 | /**
|
| 446 | * Limit string
|
| 447 | *
|
| 448 | * Generates a platform-specific LIMIT clause
|
| 449 | *
|
| 450 | * @access public
|
| 451 | * @param string the sql query string
|
| 452 | * @param integer the number of rows to limit the query to
|
| 453 | * @param integer the offset value
|
| 454 | * @return string
|
| 455 | */
|
| 456 | function _limit($sql, $limit, $offset)
|
| 457 | {
|
| 458 | $sql .= "LIMIT ".$limit;
|
| 459 |
|
| 460 | if ($offset > 0)
|
| 461 | {
|
| 462 | $sql .= " OFFSET ".$offset;
|
| 463 | }
|
| 464 |
|
| 465 | return $sql;
|
| 466 | }
|
| 467 |
|
| 468 | // --------------------------------------------------------------------
|
| 469 |
|
| 470 | /**
|
| 471 | * Close DB Connection
|
| 472 | *
|
| 473 | * @access public
|
| 474 | * @param resource
|
| 475 | * @return void
|
| 476 | */
|
| 477 | function _close($conn_id)
|
| 478 | {
|
| 479 | @pg_close($conn_id);
|
| 480 | }
|
| 481 |
|
| 482 |
|
| 483 | }
|
| 484 |
|
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 485 | ?> |