admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 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 | } |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame^] | 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 | |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 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 | { |
| 233 | $v = pg_version($this->conn_id); |
| 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 | /** |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame^] | 293 | * Show columnn query |
| 294 | * |
| 295 | * Generates a platform-specific query string so that the column names can be fetched |
| 296 | * |
| 297 | * @access public |
| 298 | * @param string the table name |
| 299 | * @return string |
| 300 | */ |
| 301 | function _list_columns($table = '') |
| 302 | { |
| 303 | return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$this->_escape_table($table)."'"; |
| 304 | } |
| 305 | |
| 306 | // -------------------------------------------------------------------- |
| 307 | |
| 308 | /** |
| 309 | * Field data query |
| 310 | * |
| 311 | * Generates a platform-specific query so that the column data can be retrieved |
| 312 | * |
| 313 | * @access public |
| 314 | * @param string the table name |
| 315 | * @return object |
| 316 | */ |
| 317 | function _field_data($table) |
| 318 | { |
| 319 | return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1"; |
| 320 | } |
| 321 | |
| 322 | // -------------------------------------------------------------------- |
| 323 | |
| 324 | /** |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 325 | * The error message string |
| 326 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 327 | * @access private |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 328 | * @return string |
| 329 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 330 | function _error_message() |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 331 | { |
| 332 | return pg_last_error($this->conn_id); |
| 333 | } |
| 334 | |
| 335 | // -------------------------------------------------------------------- |
| 336 | |
| 337 | /** |
| 338 | * The error message number |
| 339 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 340 | * @access private |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 341 | * @return integer |
| 342 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 343 | function _error_number() |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 344 | { |
| 345 | return ''; |
| 346 | } |
| 347 | |
| 348 | // -------------------------------------------------------------------- |
| 349 | |
| 350 | /** |
| 351 | * Escape Table Name |
| 352 | * |
| 353 | * This function adds backticks if the table name has a period |
| 354 | * in it. Some DBs will get cranky unless periods are escaped. |
| 355 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 356 | * @access private |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 357 | * @param string the table name |
| 358 | * @return string |
| 359 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 360 | function _escape_table($table) |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 361 | { |
| 362 | if (stristr($table, '.')) |
| 363 | { |
| 364 | $table = '"'.preg_replace("/\./", '"."', $table).'"'; |
| 365 | } |
| 366 | |
| 367 | return $table; |
| 368 | } |
| 369 | |
| 370 | // -------------------------------------------------------------------- |
| 371 | |
| 372 | /** |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 373 | * Insert statement |
| 374 | * |
| 375 | * Generates a platform-specific insert string from the supplied data |
| 376 | * |
| 377 | * @access public |
| 378 | * @param string the table name |
| 379 | * @param array the insert keys |
| 380 | * @param array the insert values |
| 381 | * @return string |
| 382 | */ |
| 383 | function _insert($table, $keys, $values) |
| 384 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 385 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // -------------------------------------------------------------------- |
| 389 | |
| 390 | /** |
| 391 | * Update statement |
| 392 | * |
| 393 | * Generates a platform-specific update string from the supplied data |
| 394 | * |
| 395 | * @access public |
| 396 | * @param string the table name |
| 397 | * @param array the update data |
| 398 | * @param array the where clause |
| 399 | * @return string |
| 400 | */ |
| 401 | function _update($table, $values, $where) |
| 402 | { |
| 403 | foreach($values as $key => $val) |
| 404 | { |
| 405 | $valstr[] = $key." = ".$val; |
| 406 | } |
| 407 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 408 | return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where); |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // -------------------------------------------------------------------- |
| 412 | |
| 413 | /** |
| 414 | * Delete statement |
| 415 | * |
| 416 | * Generates a platform-specific delete string from the supplied data |
| 417 | * |
| 418 | * @access public |
| 419 | * @param string the table name |
| 420 | * @param array the where clause |
| 421 | * @return string |
| 422 | */ |
| 423 | function _delete($table, $where) |
| 424 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 425 | return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where); |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 426 | } |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 427 | |
| 428 | // -------------------------------------------------------------------- |
| 429 | |
| 430 | /** |
| 431 | * Limit string |
| 432 | * |
| 433 | * Generates a platform-specific LIMIT clause |
| 434 | * |
| 435 | * @access public |
| 436 | * @param string the sql query string |
| 437 | * @param integer the number of rows to limit the query to |
| 438 | * @param integer the offset value |
| 439 | * @return string |
| 440 | */ |
| 441 | function _limit($sql, $limit, $offset) |
| 442 | { |
| 443 | $sql .= "LIMIT ".$limit; |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 444 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 445 | if ($offset > 0) |
| 446 | { |
| 447 | $sql .= " OFFSET ".$offset; |
| 448 | } |
| 449 | |
| 450 | return $sql; |
| 451 | } |
| 452 | |
| 453 | // -------------------------------------------------------------------- |
| 454 | |
| 455 | /** |
| 456 | * Close DB Connection |
| 457 | * |
| 458 | * @access public |
| 459 | * @param resource |
| 460 | * @return void |
| 461 | */ |
| 462 | function _close($conn_id) |
| 463 | { |
| 464 | pg_close($conn_id); |
| 465 | } |
| 466 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 467 | |
admin | 7eea4f8 | 2006-09-24 18:13:17 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | ?> |