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