blob: 8b8f333710a9e6b8361585ce0912977b741f8bd1 [file] [log] [blame]
Andrey Andreev214583f2012-01-26 16:39:09 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev214583f2012-01-26 16:39:09 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev214583f2012-01-26 16:39:09 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Postgre Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
32 * creates dynamically based on whether the active record
33 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_postgre_driver extends CI_DB {
42
Andrey Andreev214583f2012-01-26 16:39:09 +020043 public $dbdriver = 'postgre';
Barry Mienydd671972010-10-04 16:33:58 +020044
Andrey Andreev214583f2012-01-26 16:39:09 +020045 protected $_escape_char = '"';
Derek Allard2067d1a2008-11-13 22:59:24 +000046
Derek Jonese4ed5832009-02-20 21:44:59 +000047 // clause and character used for LIKE escape sequences
Andrey Andreev214583f2012-01-26 16:39:09 +020048 protected $_like_escape_str = ' ESCAPE \'%s\' ';
49 protected $_like_escape_chr = '!';
Derek Jonese4ed5832009-02-20 21:44:59 +000050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 /**
52 * The syntax to count rows is slightly different across different
53 * database engines, so this string appears in each driver and is
54 * used for the count_all() and count_all_results() functions.
55 */
Andrey Andreev214583f2012-01-26 16:39:09 +020056 protected $_count_string = 'SELECT COUNT(*) AS ';
57 protected $_random_keyword = ' RANDOM()'; // database specific random keyword
58
59 // Postgre-specific properties
60 protected $_pg_dsn;
Derek Allard2067d1a2008-11-13 22:59:24 +000061
62 /**
Andrey Andreev214583f2012-01-26 16:39:09 +020063 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000064 *
Andrey Andreev214583f2012-01-26 16:39:09 +020065 * Creates a DSN string to be used for db_connect() and db_pconnect()
66 *
67 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020068 */
Andrey Andreev214583f2012-01-26 16:39:09 +020069 private function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
Andrey Andreev214583f2012-01-26 16:39:09 +020071 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020072
Andrey Andreev214583f2012-01-26 16:39:09 +020073 $components = array(
74 'hostname' => 'host',
75 'port' => 'port',
76 'database' => 'dbname',
77 'username' => 'user',
78 'password' => 'password'
79 );
80
Derek Allard2067d1a2008-11-13 22:59:24 +000081 foreach ($components as $key => $val)
82 {
83 if (isset($this->$key) && $this->$key != '')
84 {
Andrey Andreev214583f2012-01-26 16:39:09 +020085 $this->_pg_dsn .= $val.'='.$this->$key.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
87 }
Andrey Andreev214583f2012-01-26 16:39:09 +020088
89 if (strlen($this->_pg_dsn) > 0)
90 {
91 $this->_pg_dsn = rtrim($this->_pg_dsn);
92 }
Derek Allard2067d1a2008-11-13 22:59:24 +000093 }
94
95 // --------------------------------------------------------------------
96
97 /**
98 * Non-persistent database connection
99 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200101 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200102 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200103 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200104 $ret = @pg_connect($this->_pg_dsn);
105 if (is_resource($ret) && $this->char_set != '')
106 {
107 pg_set_client_encoding($ret, $this->char_set);
108 }
109
110 return $ret;
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
112
113 // --------------------------------------------------------------------
114
115 /**
116 * Persistent database connection
117 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200119 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200120 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200122 $ret = @pg_pconnect($this->_pg_dsn);
123 if (is_resource($ret) && $this->char_set != '')
124 {
125 pg_set_client_encoding($ret, $this->char_set);
126 }
127
128 return $ret;
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
Barry Mienydd671972010-10-04 16:33:58 +0200130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 // --------------------------------------------------------------------
132
133 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000134 * Reconnect
135 *
136 * Keep / reestablish the db connection if no queries have been
137 * sent for a length of time exceeding the server's idle timeout
138 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000139 * @return void
140 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200141 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000142 {
143 if (pg_ping($this->conn_id) === FALSE)
144 {
145 $this->conn_id = FALSE;
146 }
147 }
148
149 // --------------------------------------------------------------------
150
151 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 * Select the database
153 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200155 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200156 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
158 // Not needed for Postgre so we'll return TRUE
159 return TRUE;
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Set client character set
166 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 * @param string
168 * @param string
Andrey Andreev214583f2012-01-26 16:39:09 +0200169 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200171 public function db_set_charset($charset, $collation)
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200173 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * Database version
180 *
181 * This method overrides the one from the base class, as if PHP was
182 * compiled with PostgreSQL version >= 7.4 libraries, we have a native
183 * function to get the server's version.
184 *
185 * @return string
186 */
187
188 public function version()
189 {
190 $version = pg_version($this->conn_id);
191
192 /* If we don't have the server version string - fall back to
193 * the base driver class' method
194 */
195 return isset($version['server']) ? $version['server'] : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
197
198 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 /**
201 * Version number query string
202 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * @return string
204 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200205 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200207 return 'SELECT version() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Execute the query
214 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 * @param string an SQL query
216 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200217 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200218 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200220 return @pg_query($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 // --------------------------------------------------------------------
224
225 /**
226 * Prep the query
227 *
228 * If needed, each database adapter can prep the query string
229 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 * @param string an SQL query
231 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200232 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200233 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
235 return $sql;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Begin Transaction
242 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200243 * @param bool
Barry Mienydd671972010-10-04 16:33:58 +0200244 * @return bool
245 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200246 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev214583f2012-01-26 16:39:09 +0200249 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
251 return TRUE;
252 }
253
254 // Reset the transaction failure flag.
255 // If the $test_mode flag is set to TRUE transactions will be rolled back
256 // even if the queries produce a successful result.
Andrey Andreev214583f2012-01-26 16:39:09 +0200257 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000258
Andrey Andreev214583f2012-01-26 16:39:09 +0200259 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261
262 // --------------------------------------------------------------------
263
264 /**
265 * Commit Transaction
266 *
Barry Mienydd671972010-10-04 16:33:58 +0200267 * @return bool
268 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200269 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev214583f2012-01-26 16:39:09 +0200272 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 return TRUE;
275 }
276
Andrey Andreev214583f2012-01-26 16:39:09 +0200277 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Rollback Transaction
284 *
Barry Mienydd671972010-10-04 16:33:58 +0200285 * @return bool
286 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200287 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev214583f2012-01-26 16:39:09 +0200290 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 return TRUE;
293 }
294
Andrey Andreev214583f2012-01-26 16:39:09 +0200295 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * Escape String
302 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000304 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @return string
306 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200307 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000308 {
309 if (is_array($str))
310 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500311 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200312 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000313 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200314 }
315
316 return $str;
317 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000318
319 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Jonese4ed5832009-02-20 21:44:59 +0000321 // escape LIKE condition wildcards
322 if ($like === TRUE)
323 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200324 return str_replace(array('%', '_', $this->_like_escape_chr),
325 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
326 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Jonese4ed5832009-02-20 21:44:59 +0000329 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 // --------------------------------------------------------------------
333
334 /**
335 * Affected Rows
336 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200337 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200339 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
341 return @pg_affected_rows($this->result_id);
342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // --------------------------------------------------------------------
345
346 /**
347 * Insert ID
348 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200349 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200351 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200353 $v = pg_version($this->conn_id);
354 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200355
Andrey Andreev214583f2012-01-26 16:39:09 +0200356 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
357 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200358
Pascal Kriete8761ef52011-02-14 13:13:52 -0500359 if ($table == NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200361 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500363 elseif ($table != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200365 if ($column != NULL && $v >= '8.0')
366 {
367 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
368 $query = $this->query($sql);
369 $query = $query->row();
370 $seq = $query->seq;
371 }
372 else
373 {
374 // seq_name passed in table parameter
375 $seq = $table;
376 }
377
378 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
380 else
381 {
382 return pg_last_oid($this->result_id);
383 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200386 $query = $query->row();
387 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * "Count All" query
394 *
395 * Generates a platform-specific query string that counts all records in
396 * the specified database
397 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * @param string
399 * @return string
400 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200401 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
403 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000404 {
405 return 0;
406 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000407
Andrey Andreev214583f2012-01-26 16:39:09 +0200408 $query = $this->query($this->_count_string.$this->_protect_identifiers('numrows').' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000410 {
411 return 0;
412 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000413
Andrey Andreev214583f2012-01-26 16:39:09 +0200414 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500415 $this->_reset_select();
Andrey Andreev214583f2012-01-26 16:39:09 +0200416 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418
419 // --------------------------------------------------------------------
420
421 /**
422 * Show table query
423 *
424 * Generates a platform-specific query string so that the table names can be fetched
425 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200426 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @return string
428 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200429 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200430 {
431 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
432
Andrey Andreev214583f2012-01-26 16:39:09 +0200433 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200435 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 return $sql;
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 // --------------------------------------------------------------------
442
443 /**
444 * Show column query
445 *
446 * Generates a platform-specific query string so that the column names can be fetched
447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param string the table name
449 * @return string
450 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200451 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200453 return "SELECT column_name FROM information_schema.columns WHERE table_name = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455
456 // --------------------------------------------------------------------
457
458 /**
459 * Field data query
460 *
461 * Generates a platform-specific query so that the column data can be retrieved
462 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * @param string the table name
Andrey Andreev214583f2012-01-26 16:39:09 +0200464 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200466 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200468 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
470
471 // --------------------------------------------------------------------
472
473 /**
474 * The error message string
475 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @return string
477 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200478 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
480 return pg_last_error($this->conn_id);
481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 // --------------------------------------------------------------------
484
485 /**
486 * The error message number
487 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200488 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200490 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200492 // Not supported in Postgre
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 return '';
494 }
495
496 // --------------------------------------------------------------------
497
498 /**
499 * Escape the SQL Identifiers
500 *
501 * This function escapes column and table names
502 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 * @param string
504 * @return string
505 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200506 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 if ($this->_escape_char == '')
509 {
510 return $item;
511 }
512
513 foreach ($this->_reserved_identifiers as $id)
514 {
515 if (strpos($item, '.'.$id) !== FALSE)
516 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200517 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 // remove duplicates if the user already included the escape
Andrey Andreev214583f2012-01-26 16:39:09 +0200520 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200521 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 if (strpos($item, '.') !== FALSE)
525 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200526 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 // remove duplicates if the user already included the escape
Andrey Andreev214583f2012-01-26 16:39:09 +0200530 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 // --------------------------------------------------------------------
534
535 /**
536 * From Tables
537 *
538 * This function implicitly groups FROM tables so there is no confusion
539 * about operator precedence in harmony with SQL standards
540 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200541 * @param string table name
542 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200544 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 {
546 if ( ! is_array($tables))
547 {
548 $tables = array($tables);
549 }
Barry Mienydd671972010-10-04 16:33:58 +0200550
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 return implode(', ', $tables);
552 }
553
554 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 /**
557 * Insert statement
558 *
559 * Generates a platform-specific insert string from the supplied data
560 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @param string the table name
562 * @param array the insert keys
563 * @param array the insert values
564 * @return string
565 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200566 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200567 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200568 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 // --------------------------------------------------------------------
572
573 /**
Greg Aker60ef4ea2011-04-27 01:45:38 -0500574 * Insert_batch statement
575 *
576 * Generates a platform-specific insert string from the supplied data
577 *
Greg Aker60ef4ea2011-04-27 01:45:38 -0500578 * @param string the table name
579 * @param array the insert keys
580 * @param array the insert values
581 * @return string
582 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200583 protected function _insert_batch($table, $keys, $values)
Greg Aker60ef4ea2011-04-27 01:45:38 -0500584 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200585 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Greg Aker60ef4ea2011-04-27 01:45:38 -0500586 }
587
588 // --------------------------------------------------------------------
589
590 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 * Update statement
592 *
593 * Generates a platform-specific update string from the supplied data
594 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 * @param string the table name
596 * @param array the update data
597 * @param array the where clause
598 * @param array the orderby clause
599 * @param array the limit clause
600 * @return string
601 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200602 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500604 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200606 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
Barry Mienydd671972010-10-04 16:33:58 +0200608
Andrey Andreev214583f2012-01-26 16:39:09 +0200609 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
610 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
611 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
612 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 }
614
615 // --------------------------------------------------------------------
616
617 /**
618 * Truncate statement
619 *
620 * Generates a platform-specific truncate string from the supplied data
621 * If the database does not support the truncate() command
622 * This function maps to "DELETE FROM table"
623 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 * @param string the table name
625 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200626 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200627 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200629 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 // --------------------------------------------------------------------
633
634 /**
635 * Delete statement
636 *
637 * Generates a platform-specific delete string from the supplied data
638 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 * @param string the table name
640 * @param array the where clause
641 * @param string the limit clause
642 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200643 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200644 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
646 $conditions = '';
647
648 if (count($where) > 0 OR count($like) > 0)
649 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200650 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000651
652 if (count($where) > 0 && count($like) > 0)
653 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200654 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 }
656 $conditions .= implode("\n", $like);
657 }
658
Andrey Andreev214583f2012-01-26 16:39:09 +0200659 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 }
661
662 // --------------------------------------------------------------------
663 /**
664 * Limit string
665 *
666 * Generates a platform-specific LIMIT clause
667 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 * @param string the sql query string
Andrey Andreev214583f2012-01-26 16:39:09 +0200669 * @param int the number of rows to limit the query to
670 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 * @return string
672 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200673 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200674 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200675 return $sql.' LIMIT '.$limit.($offset == 0 ? '' : ' OFFSET '.$offset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 }
677
678 // --------------------------------------------------------------------
679
680 /**
681 * Close DB Connection
682 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 * @param resource
684 * @return void
685 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200686 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 {
688 @pg_close($conn_id);
689 }
690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691}
692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693/* End of file postgre_driver.php */
Andrey Andreev214583f2012-01-26 16:39:09 +0200694/* Location: ./system/database/drivers/postgre/postgre_driver.php */