1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * Postgre Database Adapter Class
\r
21 * Note: _DB is an extender class that the app controller
\r
22 * creates dynamically based on whether the active record
\r
23 * class is being used or not.
\r
25 * @package CodeIgniter
\r
26 * @subpackage Drivers
\r
27 * @category Database
\r
28 * @author ExpressionEngine Dev Team
\r
29 * @link http://codeigniter.com/user_guide/database/
\r
31 class CI_DB_postgre_driver extends CI_DB {
\r
33 var $dbdriver = 'postgre';
\r
35 var $_escape_char = '"';
\r
38 * The syntax to count rows is slightly different across different
\r
39 * database engines, so this string appears in each driver and is
\r
40 * used for the count_all() and count_all_results() functions.
\r
42 var $_count_string = "SELECT COUNT(*) AS ";
\r
43 var $_random_keyword = ' RANDOM()'; // database specific random keyword
\r
51 function _connect_string()
\r
53 $components = array(
\r
54 'hostname' => 'host',
\r
56 'database' => 'dbname',
\r
57 'username' => 'user',
\r
58 'password' => 'password'
\r
61 $connect_string = "";
\r
62 foreach ($components as $key => $val)
\r
64 if (isset($this->$key) && $this->$key != '')
\r
66 $connect_string .= " $val=".$this->$key;
\r
69 return trim($connect_string);
\r
72 // --------------------------------------------------------------------
\r
75 * Non-persistent database connection
\r
77 * @access private called by the base class
\r
80 function db_connect()
\r
82 return @pg_connect($this->_connect_string());
\r
85 // --------------------------------------------------------------------
\r
88 * Persistent database connection
\r
90 * @access private called by the base class
\r
93 function db_pconnect()
\r
95 return @pg_pconnect($this->_connect_string());
\r
98 // --------------------------------------------------------------------
\r
101 * Select the database
\r
103 * @access private called by the base class
\r
106 function db_select()
\r
108 // Not needed for Postgre so we'll return TRUE
\r
112 // --------------------------------------------------------------------
\r
115 * Set client character set
\r
122 function db_set_charset($charset, $collation)
\r
124 // @todo - add support if needed
\r
128 // --------------------------------------------------------------------
\r
131 * Version number query string
\r
136 function _version()
\r
138 return "SELECT version() AS ver";
\r
141 // --------------------------------------------------------------------
\r
144 * Execute the query
\r
146 * @access private called by the base class
\r
147 * @param string an SQL query
\r
150 function _execute($sql)
\r
152 $sql = $this->_prep_query($sql);
\r
153 return @pg_query($this->conn_id, $sql);
\r
156 // --------------------------------------------------------------------
\r
161 * If needed, each database adapter can prep the query string
\r
163 * @access private called by execute()
\r
164 * @param string an SQL query
\r
167 function _prep_query($sql)
\r
172 // --------------------------------------------------------------------
\r
175 * Begin Transaction
\r
180 function trans_begin($test_mode = FALSE)
\r
182 if ( ! $this->trans_enabled)
\r
187 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
188 if ($this->_trans_depth > 0)
\r
193 // Reset the transaction failure flag.
\r
194 // If the $test_mode flag is set to TRUE transactions will be rolled back
\r
195 // even if the queries produce a successful result.
\r
196 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
\r
198 return @pg_exec($this->conn_id, "begin");
\r
201 // --------------------------------------------------------------------
\r
204 * Commit Transaction
\r
209 function trans_commit()
\r
211 if ( ! $this->trans_enabled)
\r
216 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
217 if ($this->_trans_depth > 0)
\r
222 return @pg_exec($this->conn_id, "commit");
\r
225 // --------------------------------------------------------------------
\r
228 * Rollback Transaction
\r
233 function trans_rollback()
\r
235 if ( ! $this->trans_enabled)
\r
240 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
241 if ($this->_trans_depth > 0)
\r
246 return @pg_exec($this->conn_id, "rollback");
\r
249 // --------------------------------------------------------------------
\r
258 function escape_str($str)
\r
260 return pg_escape_string($str);
\r
263 // --------------------------------------------------------------------
\r
271 function affected_rows()
\r
273 return @pg_affected_rows($this->result_id);
\r
276 // --------------------------------------------------------------------
\r
284 function insert_id()
\r
286 $v = $this->_version();
\r
289 $table = func_num_args() > 0 ? func_get_arg(0) : null;
\r
290 $column = func_num_args() > 1 ? func_get_arg(1) : null;
\r
292 if ($table == null && $v >= '8.1')
\r
294 $sql='SELECT LASTVAL() as ins_id';
\r
296 elseif ($table != null && $column != null && $v >= '8.0')
\r
298 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
\r
299 $query = $this->query($sql);
\r
300 $row = $query->row();
\r
301 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
\r
303 elseif ($table != null)
\r
305 // seq_name passed in table parameter
\r
306 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
\r
310 return pg_last_oid($this->result_id);
\r
312 $query = $this->query($sql);
\r
313 $row = $query->row();
\r
314 return $row->ins_id;
\r
317 // --------------------------------------------------------------------
\r
320 * "Count All" query
\r
322 * Generates a platform-specific query string that counts all records in
\r
323 * the specified database
\r
329 function count_all($table = '')
\r
334 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
\r
336 if ($query->num_rows() == 0)
\r
339 $row = $query->row();
\r
340 return $row->numrows;
\r
343 // --------------------------------------------------------------------
\r
348 * Generates a platform-specific query string so that the table names can be fetched
\r
354 function _list_tables($prefix_limit = FALSE)
\r
356 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
\r
358 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
\r
360 $sql .= " AND table_name LIKE '".$this->dbprefix."%'";
\r
366 // --------------------------------------------------------------------
\r
369 * Show column query
\r
371 * Generates a platform-specific query string so that the column names can be fetched
\r
374 * @param string the table name
\r
377 function _list_columns($table = '')
\r
379 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$table."'";
\r
382 // --------------------------------------------------------------------
\r
387 * Generates a platform-specific query so that the column data can be retrieved
\r
390 * @param string the table name
\r
393 function _field_data($table)
\r
395 return "SELECT * FROM ".$table." LIMIT 1";
\r
398 // --------------------------------------------------------------------
\r
401 * The error message string
\r
406 function _error_message()
\r
408 return pg_last_error($this->conn_id);
\r
411 // --------------------------------------------------------------------
\r
414 * The error message number
\r
419 function _error_number()
\r
424 // --------------------------------------------------------------------
\r
427 * Escape the SQL Identifiers
\r
429 * This function escapes column and table names
\r
435 function _escape_identifiers($item)
\r
437 if ($this->_escape_char == '')
\r
442 if (strpos($item, '.') !== FALSE)
\r
444 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
\r
448 $str = $this->_escape_char.$item.$this->_escape_char;
\r
451 // remove duplicates if the user already included the escape
\r
452 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
\r
455 // --------------------------------------------------------------------
\r
460 * This function implicitly groups FROM tables so there is no confusion
\r
461 * about operator precedence in harmony with SQL standards
\r
467 function _from_tables($tables)
\r
469 if ( ! is_array($tables))
\r
471 $tables = array($tables);
\r
474 return implode(', ', $tables);
\r
477 // --------------------------------------------------------------------
\r
482 * Generates a platform-specific insert string from the supplied data
\r
485 * @param string the table name
\r
486 * @param array the insert keys
\r
487 * @param array the insert values
\r
490 function _insert($table, $keys, $values)
\r
492 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
\r
495 // --------------------------------------------------------------------
\r
500 * Generates a platform-specific update string from the supplied data
\r
503 * @param string the table name
\r
504 * @param array the update data
\r
505 * @param array the where clause
\r
506 * @param array the orderby clause
\r
507 * @param array the limit clause
\r
510 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
\r
512 foreach($values as $key => $val)
\r
514 $valstr[] = $key." = ".$val;
\r
517 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
\r
519 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
\r
521 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
\r
523 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
\r
525 $sql .= $orderby.$limit;
\r
530 // --------------------------------------------------------------------
\r
533 * Truncate statement
\r
535 * Generates a platform-specific truncate string from the supplied data
\r
536 * If the database does not support the truncate() command
\r
537 * This function maps to "DELETE FROM table"
\r
540 * @param string the table name
\r
543 function _truncate($table)
\r
545 return "TRUNCATE ".$table;
\r
548 // --------------------------------------------------------------------
\r
553 * Generates a platform-specific delete string from the supplied data
\r
556 * @param string the table name
\r
557 * @param array the where clause
\r
558 * @param string the limit clause
\r
561 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
\r
565 if (count($where) > 0 OR count($like) > 0)
\r
567 $conditions = "\nWHERE ";
\r
568 $conditions .= implode("\n", $this->ar_where);
\r
570 if (count($where) > 0 && count($like) > 0)
\r
572 $conditions .= " AND ";
\r
574 $conditions .= implode("\n", $like);
\r
577 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
\r
579 return "DELETE FROM ".$table.$conditions.$limit;
\r
582 // --------------------------------------------------------------------
\r
586 * Generates a platform-specific LIMIT clause
\r
589 * @param string the sql query string
\r
590 * @param integer the number of rows to limit the query to
\r
591 * @param integer the offset value
\r
594 function _limit($sql, $limit, $offset)
\r
596 $sql .= "LIMIT ".$limit;
\r
600 $sql .= " OFFSET ".$offset;
\r
606 // --------------------------------------------------------------------
\r
609 * Close DB Connection
\r
615 function _close($conn_id)
\r
617 @pg_close($conn_id);
\r
624 /* End of file postgre_driver.php */
\r
625 /* Location: ./system/database/drivers/postgre/postgre_driver.php */