blob: 4779799a441797157f3c6d2966f573680b15cffd [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 Andreev2b3edbd2012-01-27 21:02:53 +020069 public 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 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200187 public function version()
188 {
189 $version = pg_version($this->conn_id);
190
191 /* If we don't have the server version string - fall back to
192 * the base driver class' method
193 */
194 return isset($version['server']) ? $version['server'] : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
196
197 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 /**
200 * Version number query string
201 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 * @return string
203 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200204 protected function _version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200206 return 'SELECT version() AS ver';
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Execute the query
213 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 * @param string an SQL query
215 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200216 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200217 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200219 return @pg_query($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // --------------------------------------------------------------------
223
224 /**
225 * Prep the query
226 *
227 * If needed, each database adapter can prep the query string
228 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * @param string an SQL query
230 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200231 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200232 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
234 return $sql;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Begin Transaction
241 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200242 * @param bool
Barry Mienydd671972010-10-04 16:33:58 +0200243 * @return bool
244 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200245 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev214583f2012-01-26 16:39:09 +0200248 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
250 return TRUE;
251 }
252
253 // Reset the transaction failure flag.
254 // If the $test_mode flag is set to TRUE transactions will be rolled back
255 // even if the queries produce a successful result.
Andrey Andreev214583f2012-01-26 16:39:09 +0200256 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000257
Andrey Andreev214583f2012-01-26 16:39:09 +0200258 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260
261 // --------------------------------------------------------------------
262
263 /**
264 * Commit Transaction
265 *
Barry Mienydd671972010-10-04 16:33:58 +0200266 * @return bool
267 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200268 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev214583f2012-01-26 16:39:09 +0200271 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 return TRUE;
274 }
275
Andrey Andreev214583f2012-01-26 16:39:09 +0200276 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Rollback Transaction
283 *
Barry Mienydd671972010-10-04 16:33:58 +0200284 * @return bool
285 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200286 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev214583f2012-01-26 16:39:09 +0200289 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 return TRUE;
292 }
293
Andrey Andreev214583f2012-01-26 16:39:09 +0200294 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296
297 // --------------------------------------------------------------------
298
299 /**
300 * Escape String
301 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000303 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 * @return string
305 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200306 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000307 {
308 if (is_array($str))
309 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500310 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200311 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000312 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200313 }
314
315 return $str;
316 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000317
318 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Jonese4ed5832009-02-20 21:44:59 +0000320 // escape LIKE condition wildcards
321 if ($like === TRUE)
322 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200323 return str_replace(array('%', '_', $this->_like_escape_chr),
324 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
325 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Jonese4ed5832009-02-20 21:44:59 +0000328 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 // --------------------------------------------------------------------
332
333 /**
334 * Affected Rows
335 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200336 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200338 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
340 return @pg_affected_rows($this->result_id);
341 }
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // --------------------------------------------------------------------
344
345 /**
346 * Insert ID
347 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200348 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200350 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200352 $v = pg_version($this->conn_id);
353 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200354
Andrey Andreev214583f2012-01-26 16:39:09 +0200355 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
356 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200357
Pascal Kriete8761ef52011-02-14 13:13:52 -0500358 if ($table == NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200360 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500362 elseif ($table != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200364 if ($column != NULL && $v >= '8.0')
365 {
366 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
367 $query = $this->query($sql);
368 $query = $query->row();
369 $seq = $query->seq;
370 }
371 else
372 {
373 // seq_name passed in table parameter
374 $seq = $table;
375 }
376
377 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
379 else
380 {
381 return pg_last_oid($this->result_id);
382 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200385 $query = $query->row();
386 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
388
389 // --------------------------------------------------------------------
390
391 /**
392 * "Count All" query
393 *
394 * Generates a platform-specific query string that counts all records in
395 * the specified database
396 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @param string
398 * @return string
399 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200400 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
402 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000403 {
404 return 0;
405 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000406
Andrey Andreev214583f2012-01-26 16:39:09 +0200407 $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 +0000408 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000409 {
410 return 0;
411 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000412
Andrey Andreev214583f2012-01-26 16:39:09 +0200413 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500414 $this->_reset_select();
Andrey Andreev214583f2012-01-26 16:39:09 +0200415 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Show table query
422 *
423 * Generates a platform-specific query string so that the table names can be fetched
424 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200425 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @return string
427 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200428 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200429 {
430 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
431
Andrey Andreev214583f2012-01-26 16:39:09 +0200432 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200434 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 +0000435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 return $sql;
438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 // --------------------------------------------------------------------
441
442 /**
443 * Show column query
444 *
445 * Generates a platform-specific query string so that the column names can be fetched
446 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @param string the table name
448 * @return string
449 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200450 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200452 return "SELECT column_name FROM information_schema.columns WHERE table_name = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
454
455 // --------------------------------------------------------------------
456
457 /**
458 * Field data query
459 *
460 * Generates a platform-specific query so that the column data can be retrieved
461 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 * @param string the table name
Andrey Andreev214583f2012-01-26 16:39:09 +0200463 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200465 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200467 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469
470 // --------------------------------------------------------------------
471
472 /**
473 * The error message string
474 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 * @return string
476 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200477 protected function _error_message()
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
479 return pg_last_error($this->conn_id);
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * The error message number
486 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200487 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200489 protected function _error_number()
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200491 // Not supported in Postgre
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 return '';
493 }
494
495 // --------------------------------------------------------------------
496
497 /**
498 * Escape the SQL Identifiers
499 *
500 * This function escapes column and table names
501 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * @param string
503 * @return string
504 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200505 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 {
507 if ($this->_escape_char == '')
508 {
509 return $item;
510 }
511
512 foreach ($this->_reserved_identifiers as $id)
513 {
514 if (strpos($item, '.'.$id) !== FALSE)
515 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200516 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 // remove duplicates if the user already included the escape
Andrey Andreev214583f2012-01-26 16:39:09 +0200519 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200520 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 if (strpos($item, '.') !== FALSE)
524 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200525 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 // remove duplicates if the user already included the escape
Andrey Andreev214583f2012-01-26 16:39:09 +0200529 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 // --------------------------------------------------------------------
533
534 /**
535 * From Tables
536 *
537 * This function implicitly groups FROM tables so there is no confusion
538 * about operator precedence in harmony with SQL standards
539 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200540 * @param string table name
541 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200543 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 {
545 if ( ! is_array($tables))
546 {
547 $tables = array($tables);
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 return implode(', ', $tables);
551 }
552
553 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 /**
556 * Insert statement
557 *
558 * Generates a platform-specific insert string from the supplied data
559 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 * @param string the table name
561 * @param array the insert keys
562 * @param array the insert values
563 * @return string
564 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200565 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200566 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200567 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 }
Barry Mienydd671972010-10-04 16:33:58 +0200569
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 // --------------------------------------------------------------------
571
572 /**
Greg Aker60ef4ea2011-04-27 01:45:38 -0500573 * Insert_batch statement
574 *
575 * Generates a platform-specific insert string from the supplied data
576 *
Greg Aker60ef4ea2011-04-27 01:45:38 -0500577 * @param string the table name
578 * @param array the insert keys
579 * @param array the insert values
580 * @return string
581 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200582 protected function _insert_batch($table, $keys, $values)
Greg Aker60ef4ea2011-04-27 01:45:38 -0500583 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200584 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Greg Aker60ef4ea2011-04-27 01:45:38 -0500585 }
586
587 // --------------------------------------------------------------------
588
589 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * Update statement
591 *
592 * Generates a platform-specific update string from the supplied data
593 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 * @param string the table name
595 * @param array the update data
596 * @param array the where clause
597 * @param array the orderby clause
598 * @param array the limit clause
599 * @return string
600 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200601 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500603 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200605 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 }
Barry Mienydd671972010-10-04 16:33:58 +0200607
Andrey Andreev214583f2012-01-26 16:39:09 +0200608 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
609 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
610 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
611 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 }
613
614 // --------------------------------------------------------------------
615
616 /**
617 * Truncate statement
618 *
619 * Generates a platform-specific truncate string from the supplied data
620 * If the database does not support the truncate() command
621 * This function maps to "DELETE FROM table"
622 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 * @param string the table name
624 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200625 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200626 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200628 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 // --------------------------------------------------------------------
632
633 /**
634 * Delete statement
635 *
636 * Generates a platform-specific delete string from the supplied data
637 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 * @param string the table name
639 * @param array the where clause
640 * @param string the limit clause
641 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200642 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200643 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
645 $conditions = '';
646
647 if (count($where) > 0 OR count($like) > 0)
648 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200649 $conditions = "\nWHERE ".implode("\n", $this->ar_where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000650
651 if (count($where) > 0 && count($like) > 0)
652 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200653 $conditions .= ' AND ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 }
655 $conditions .= implode("\n", $like);
656 }
657
Andrey Andreev214583f2012-01-26 16:39:09 +0200658 return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
660
661 // --------------------------------------------------------------------
662 /**
663 * Limit string
664 *
665 * Generates a platform-specific LIMIT clause
666 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 * @param string the sql query string
Andrey Andreev214583f2012-01-26 16:39:09 +0200668 * @param int the number of rows to limit the query to
669 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000670 * @return string
671 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200672 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200673 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200674 return $sql.' LIMIT '.$limit.($offset == 0 ? '' : ' OFFSET '.$offset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 }
676
677 // --------------------------------------------------------------------
678
679 /**
680 * Close DB Connection
681 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 * @param resource
683 * @return void
684 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200685 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000686 {
687 @pg_close($conn_id);
688 }
689
Derek Allard2067d1a2008-11-13 22:59:24 +0000690}
691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692/* End of file postgre_driver.php */
Andrey Andreev214583f2012-01-26 16:39:09 +0200693/* Location: ./system/database/drivers/postgre/postgre_driver.php */