blob: 9680b3d966a07259c61e36637ba9a08679e237c0 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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 Andreev738497c2012-03-20 14:12:25 +020048 protected $_like_escape_str = " ESCAPE '%s' ";
Andrey Andreev214583f2012-01-26 16:39:09 +020049 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
Derek Allard2067d1a2008-11-13 22:59:24 +000059 /**
Andrey Andreev214583f2012-01-26 16:39:09 +020060 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000061 *
Andrey Andreev214583f2012-01-26 16:39:09 +020062 * Creates a DSN string to be used for db_connect() and db_pconnect()
63 *
64 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020065 */
Andrey Andreev2b3edbd2012-01-27 21:02:53 +020066 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Andrey Andreev214583f2012-01-26 16:39:09 +020068 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020069
Andrey Andreeva64c61a2012-02-12 22:26:46 +020070 if ( ! empty($this->dsn))
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Andrey Andreeva64c61a2012-02-12 22:26:46 +020072 return;
73 }
74
75 $this->dsn === '' OR $this->dsn = '';
76
77 if (strpos($this->hostname, '/') !== FALSE)
78 {
79 // If UNIX sockets are used, we shouldn't set a port
80 $this->port = '';
81 }
82
83 $this->hostname === '' OR $this->dsn = 'host='.$this->hostname;
84
85 if ( ! empty($this->port) && ctype_digit($this->port))
86 {
87 $this->dsn .= 'host='.$this->port.' ';
88 }
89
90 if ($this->username !== '')
91 {
92 $this->dsn .= 'username='.$this->username.' ';
93
94 /* An empty password is valid!
95 *
96 * $db['password'] = NULL must be done in order to ignore it.
97 */
98 $this->password === NULL OR $this->dsn .= "password='".$this->password."' ";
99 }
100
101 $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' ';
102
103 /* We don't have these options as elements in our standard configuration
104 * array, but they might be set by parse_url() if the configuration was
105 * provided via string. Example:
106 *
107 * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
108 */
109 foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key)
110 {
111 if (isset($this->$key) && is_string($this->key) && $this->key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
Andrey Andreeva64c61a2012-02-12 22:26:46 +0200113 $this->dsn .= $key."='".$this->key."' ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
115 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200116
Andrey Andreeva64c61a2012-02-12 22:26:46 +0200117 $this->dsn = rtrim($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Non-persistent database connection
124 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200126 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200127 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200128 {
Andrey Andreeva6a14962012-03-03 03:57:26 +0200129 return @pg_connect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Persistent database connection
136 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200138 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200139 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreeva6a14962012-03-03 03:57:26 +0200141 return @pg_pconnect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // --------------------------------------------------------------------
145
146 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000147 * Reconnect
148 *
149 * Keep / reestablish the db connection if no queries have been
150 * sent for a length of time exceeding the server's idle timeout
151 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000152 * @return void
153 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200154 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000155 {
156 if (pg_ping($this->conn_id) === FALSE)
157 {
158 $this->conn_id = FALSE;
159 }
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 * Select the database
166 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200168 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200169 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 // Not needed for Postgre so we'll return TRUE
172 return TRUE;
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
178 * Set client character set
179 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 * @param string
Andrey Andreev214583f2012-01-26 16:39:09 +0200181 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 */
Andrey Andreev718d9ef2012-03-01 19:37:11 +0200183 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200185 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200191 * Database version number
Andrey Andreev214583f2012-01-26 16:39:09 +0200192 *
193 * @return string
194 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200195 public function version()
196 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200197 if (isset($this->data_cache['version']))
198 {
199 return $this->data_cache['version'];
200 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200201
Andrey Andreev08856b82012-03-03 03:19:28 +0200202 if (($pg_version = pg_version($this->conn_id)) === FALSE)
203 {
204 return FALSE;
205 }
206
207 /* If PHP was compiled with PostgreSQL lib versions earlier
208 * than 7.4, pg_version() won't return the server version
209 * and so we'll have to fall back to running a query in
210 * order to get it.
Andrey Andreev214583f2012-01-26 16:39:09 +0200211 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200212 return isset($pg_version['server'])
213 ? $this->data_cache['version'] = $pg_version['server']
214 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
216
217 // --------------------------------------------------------------------
218
219 /**
220 * Execute the query
221 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 * @param string an SQL query
223 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200224 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200225 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200227 return @pg_query($this->conn_id, $this->_prep_query($sql));
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // --------------------------------------------------------------------
231
232 /**
233 * Prep the query
234 *
235 * If needed, each database adapter can prep the query string
236 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 * @param string an SQL query
238 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200239 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200240 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 return $sql;
243 }
244
245 // --------------------------------------------------------------------
246
247 /**
248 * Begin Transaction
249 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200250 * @param bool
Barry Mienydd671972010-10-04 16:33:58 +0200251 * @return bool
252 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200253 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev214583f2012-01-26 16:39:09 +0200256 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
258 return TRUE;
259 }
260
261 // Reset the transaction failure flag.
262 // If the $test_mode flag is set to TRUE transactions will be rolled back
263 // even if the queries produce a successful result.
Andrey Andreev214583f2012-01-26 16:39:09 +0200264 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000265
Andrey Andreev214583f2012-01-26 16:39:09 +0200266 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Commit Transaction
273 *
Barry Mienydd671972010-10-04 16:33:58 +0200274 * @return bool
275 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200276 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev214583f2012-01-26 16:39:09 +0200279 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
281 return TRUE;
282 }
283
Andrey Andreev214583f2012-01-26 16:39:09 +0200284 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * Rollback Transaction
291 *
Barry Mienydd671972010-10-04 16:33:58 +0200292 * @return bool
293 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200294 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev214583f2012-01-26 16:39:09 +0200297 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
299 return TRUE;
300 }
301
Andrey Andreev214583f2012-01-26 16:39:09 +0200302 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * Escape String
309 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000311 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 * @return string
313 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200314 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000315 {
316 if (is_array($str))
317 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500318 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200319 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000320 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200321 }
322
323 return $str;
324 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000325
326 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Jonese4ed5832009-02-20 21:44:59 +0000328 // escape LIKE condition wildcards
329 if ($like === TRUE)
330 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200331 return str_replace(array($this->_like_escape_chr, '%', '_'),
332 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
Andrey Andreev214583f2012-01-26 16:39:09 +0200333 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Jonese4ed5832009-02-20 21:44:59 +0000336 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // --------------------------------------------------------------------
340
341 /**
342 * Affected Rows
343 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200344 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200346 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 return @pg_affected_rows($this->result_id);
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // --------------------------------------------------------------------
352
353 /**
354 * Insert ID
355 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200356 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200358 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200360 $v = pg_version($this->conn_id);
361 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200362
Andrey Andreev214583f2012-01-26 16:39:09 +0200363 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
364 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200365
Pascal Kriete8761ef52011-02-14 13:13:52 -0500366 if ($table == NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200368 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500370 elseif ($table != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200372 if ($column != NULL && $v >= '8.0')
373 {
374 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
375 $query = $this->query($sql);
376 $query = $query->row();
377 $seq = $query->seq;
378 }
379 else
380 {
381 // seq_name passed in table parameter
382 $seq = $table;
383 }
384
385 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387 else
388 {
389 return pg_last_oid($this->result_id);
390 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200393 $query = $query->row();
394 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
396
397 // --------------------------------------------------------------------
398
399 /**
400 * "Count All" query
401 *
402 * Generates a platform-specific query string that counts all records in
403 * the specified database
404 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @param string
406 * @return string
407 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200408 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000411 {
412 return 0;
413 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000414
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200415 $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 +0000416 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000417 {
418 return 0;
419 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000420
Andrey Andreev214583f2012-01-26 16:39:09 +0200421 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500422 $this->_reset_select();
Andrey Andreev214583f2012-01-26 16:39:09 +0200423 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
425
426 // --------------------------------------------------------------------
427
428 /**
429 * Show table query
430 *
431 * Generates a platform-specific query string so that the table names can be fetched
432 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200433 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @return string
435 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200436 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200437 {
438 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
439
Andrey Andreev214583f2012-01-26 16:39:09 +0200440 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200442 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 +0000443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 return $sql;
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
449
450 /**
451 * Show column query
452 *
453 * Generates a platform-specific query string so that the column names can be fetched
454 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 * @param string the table name
456 * @return string
457 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200458 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200460 return "SELECT column_name FROM information_schema.columns WHERE table_name = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Field data query
467 *
468 * Generates a platform-specific query so that the column data can be retrieved
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param string the table name
Andrey Andreev214583f2012-01-26 16:39:09 +0200471 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200473 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200475 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477
478 // --------------------------------------------------------------------
479
480 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200481 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200483 * Returns an array containing code and message of the last
484 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200486 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200488 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200490 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
492
493 // --------------------------------------------------------------------
494
495 /**
496 * Escape the SQL Identifiers
497 *
498 * This function escapes column and table names
499 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @param string
501 * @return string
502 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200503 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
505 if ($this->_escape_char == '')
506 {
507 return $item;
508 }
509
510 foreach ($this->_reserved_identifiers as $id)
511 {
512 if (strpos($item, '.'.$id) !== FALSE)
513 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200514 $item = str_replace('.', $this->_escape_char.'.', $item);
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // remove duplicates if the user already included the escape
Andrey Andreev214583f2012-01-26 16:39:09 +0200517 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Barry Mienydd671972010-10-04 16:33:58 +0200518 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 }
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 if (strpos($item, '.') !== FALSE)
522 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200523 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 // remove duplicates if the user already included the escape
Andrey Andreev214583f2012-01-26 16:39:09 +0200527 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 }
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 // --------------------------------------------------------------------
531
532 /**
533 * From Tables
534 *
535 * This function implicitly groups FROM tables so there is no confusion
536 * about operator precedence in harmony with SQL standards
537 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200538 * @param array
Andrey Andreev214583f2012-01-26 16:39:09 +0200539 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200541 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 {
543 if ( ! is_array($tables))
544 {
545 $tables = array($tables);
546 }
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 return implode(', ', $tables);
549 }
550
551 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 /**
554 * Insert statement
555 *
556 * Generates a platform-specific insert string from the supplied data
557 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 * @param string the table name
559 * @param array the insert keys
560 * @param array the insert values
561 * @return string
562 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200563 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200564 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200565 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 // --------------------------------------------------------------------
569
570 /**
Greg Aker60ef4ea2011-04-27 01:45:38 -0500571 * Insert_batch statement
572 *
573 * Generates a platform-specific insert string from the supplied data
574 *
Greg Aker60ef4ea2011-04-27 01:45:38 -0500575 * @param string the table name
576 * @param array the insert keys
577 * @param array the insert values
578 * @return string
579 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200580 protected function _insert_batch($table, $keys, $values)
Greg Aker60ef4ea2011-04-27 01:45:38 -0500581 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200582 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
Greg Aker60ef4ea2011-04-27 01:45:38 -0500583 }
584
585 // --------------------------------------------------------------------
586
587 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 * Update statement
589 *
590 * Generates a platform-specific update string from the supplied data
591 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 * @param string the table name
593 * @param array the update data
594 * @param array the where clause
595 * @param array the orderby clause
596 * @param array the limit clause
597 * @return string
598 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200599 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500601 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200603 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 }
Barry Mienydd671972010-10-04 16:33:58 +0200605
Andrey Andreev214583f2012-01-26 16:39:09 +0200606 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
607 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
608 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
609 .( ! $limit ? '' : ' LIMIT '.$limit);
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 }
611
612 // --------------------------------------------------------------------
613
614 /**
615 * Truncate statement
616 *
617 * Generates a platform-specific truncate string from the supplied data
618 * If the database does not support the truncate() command
619 * This function maps to "DELETE FROM table"
620 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 * @param string the table name
622 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200623 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200624 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200626 return 'TRUNCATE '.$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 // --------------------------------------------------------------------
630
631 /**
632 * Delete statement
633 *
634 * Generates a platform-specific delete string from the supplied data
635 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @param string the table name
637 * @param array the where clause
638 * @param string the limit clause
639 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200640 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200641 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 if (count($where) > 0 OR count($like) > 0)
644 {
Andrey Andreev5f2214f2012-03-09 14:17:18 +0200645 $conditions = "\nWHERE ".implode("\n", $this->ar_where)
646 .((count($where) > 0 && count($like) > 0) ? ' AND ' : '')
647 .implode("\n", $like);
648 }
649 else
650 {
651 $conditions = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 }
653
Andrey Andreev5f2214f2012-03-09 14:17:18 +0200654 return 'DELETE FROM '.$table.$conditions;
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 }
656
657 // --------------------------------------------------------------------
658 /**
659 * Limit string
660 *
661 * Generates a platform-specific LIMIT clause
662 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 * @param string the sql query string
Andrey Andreev214583f2012-01-26 16:39:09 +0200664 * @param int the number of rows to limit the query to
665 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 * @return string
667 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200668 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200669 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200670 return $sql.' LIMIT '.$limit.($offset == 0 ? '' : ' OFFSET '.$offset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
672
673 // --------------------------------------------------------------------
674
675 /**
676 * Close DB Connection
677 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 * @param resource
679 * @return void
680 */
Andrey Andreev214583f2012-01-26 16:39:09 +0200681 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 {
683 @pg_close($conn_id);
684 }
685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686}
687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688/* End of file postgre_driver.php */
Andrey Andreev135efb22012-03-20 15:30:56 +0200689/* Location: ./system/database/drivers/postgre/postgre_driver.php */