blob: a033d74f3ee32cf979cf16a74b1a254810e43bdd [file] [log] [blame]
Andrey Andreev738497c2012-03-20 14:12:25 +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 Andreev738497c2012-03-20 14:12:25 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev738497c2012-03-20 14:12:25 +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 Andreev738497c2012-03-20 14:12:25 +020043 public $dbdriver = 'postgre';
Barry Mienydd671972010-10-04 16:33:58 +020044
Andrey Andreev738497c2012-03-20 14:12:25 +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' ";
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 Andreev738497c2012-03-20 14:12:25 +020056 protected $_count_string = 'SELECT COUNT(*) AS ';
57 protected $_random_keyword = ' RANDOM()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000058
59 /**
Andrey Andreev6192bc02012-03-26 12:32:32 +030060 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000061 *
Andrey Andreev6192bc02012-03-26 12:32:32 +030062 * 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 Andreev6192bc02012-03-26 12:32:32 +030066 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030068 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020069
Andrey Andreev6192bc02012-03-26 12:32:32 +030070 if ( ! empty($this->dsn))
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030072 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 Andreev6192bc02012-03-26 12:32:32 +0300113 $this->dsn .= $key."='".$this->key."' ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
115 }
Andrey Andreev6192bc02012-03-26 12:32:32 +0300116
117 $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 Andreev738497c2012-03-20 14:12:25 +0200127 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200128 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300129 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 Andreev738497c2012-03-20 14:12:25 +0200139 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300141 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 Andreev738497c2012-03-20 14:12:25 +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 /**
Andrey Andreev8e89df82012-03-03 03:48:12 +0200165 * Set client character set
166 *
167 * @param string
168 * @return bool
169 */
170 protected function _db_set_charset($charset)
171 {
172 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200178 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 * @return string
181 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200182 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200184 if (isset($this->data_cache['version']))
185 {
186 return $this->data_cache['version'];
187 }
188
189 if (($pg_version = pg_version($this->conn_id)) === FALSE)
190 {
191 return FALSE;
192 }
193
194 /* If PHP was compiled with PostgreSQL lib versions earlier
195 * than 7.4, pg_version() won't return the server version
196 * and so we'll have to fall back to running a query in
197 * order to get it.
198 */
199 return isset($pg_version['server'])
200 ? $this->data_cache['version'] = $pg_version['server']
201 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Execute the query
208 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 * @param string an SQL query
210 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200211 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200212 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 return @pg_query($this->conn_id, $sql);
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
218
219 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * Begin Transaction
221 *
Barry Mienydd671972010-10-04 16:33:58 +0200222 * @return bool
223 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200224 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200227 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
229 return TRUE;
230 }
231
232 // Reset the transaction failure flag.
233 // If the $test_mode flag is set to TRUE transactions will be rolled back
234 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200235 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000236
Andrey Andreeva19beb02012-03-03 04:20:50 +0200237 return @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Commit Transaction
244 *
Barry Mienydd671972010-10-04 16:33:58 +0200245 * @return bool
246 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200247 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200250 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 return TRUE;
253 }
254
Andrey Andreeva19beb02012-03-03 04:20:50 +0200255 return @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Rollback Transaction
262 *
Barry Mienydd671972010-10-04 16:33:58 +0200263 * @return bool
264 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200265 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200268 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
270 return TRUE;
271 }
272
Andrey Andreeva19beb02012-03-03 04:20:50 +0200273 return @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275
276 // --------------------------------------------------------------------
277
278 /**
279 * Escape String
280 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000282 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 * @return string
284 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200285 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000286 {
287 if (is_array($str))
288 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500289 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200290 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000291 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200292 }
293
294 return $str;
295 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000296
297 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Jonese4ed5832009-02-20 21:44:59 +0000299 // escape LIKE condition wildcards
300 if ($like === TRUE)
301 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200302 return str_replace(array($this->_like_escape_chr, '%', '_'),
303 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
304 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Jonese4ed5832009-02-20 21:44:59 +0000307 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // --------------------------------------------------------------------
311
312 /**
313 * Affected Rows
314 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200315 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200317 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 return @pg_affected_rows($this->result_id);
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // --------------------------------------------------------------------
323
324 /**
325 * Insert ID
326 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200327 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200329 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200331 $v = $this->version();
Barry Mienydd671972010-10-04 16:33:58 +0200332
Pascal Kriete8761ef52011-02-14 13:13:52 -0500333 $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
334 $column = func_num_args() > 1 ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200335
Pascal Kriete8761ef52011-02-14 13:13:52 -0500336 if ($table == NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 {
338 $sql='SELECT LASTVAL() as ins_id';
339 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500340 elseif ($table != NULL && $column != NULL && $v >= '8.0')
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
343 $query = $this->query($sql);
344 $row = $query->row();
345 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
346 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500347 elseif ($table != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
349 // seq_name passed in table parameter
350 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
351 }
352 else
353 {
354 return pg_last_oid($this->result_id);
355 }
356 $query = $this->query($sql);
357 $row = $query->row();
358 return $row->ins_id;
359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * "Count All" query
365 *
366 * Generates a platform-specific query string that counts all records in
367 * the specified database
368 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 * @param string
370 * @return string
371 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200372 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
374 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000375 {
376 return 0;
377 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000378
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200379 $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 +0000380 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000381 {
382 return 0;
383 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000384
385 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500386 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000387 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * Show table query
394 *
395 * Generates a platform-specific query string so that the table names can be fetched
396 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200397 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * @return string
399 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200400 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200401 {
402 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
405 {
Greg Aker0d424892010-01-26 02:14:44 +0000406 $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 +0000407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 return $sql;
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 // --------------------------------------------------------------------
413
414 /**
415 * Show column query
416 *
417 * Generates a platform-specific query string so that the column names can be fetched
418 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 * @param string the table name
420 * @return string
421 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200422 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
424 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$table."'";
425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Field data query
431 *
432 * Generates a platform-specific query so that the column data can be retrieved
433 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @param string the table name
435 * @return object
436 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200437 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
439 return "SELECT * FROM ".$table." LIMIT 1";
440 }
441
442 // --------------------------------------------------------------------
443
444 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200445 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200447 * Returns an array containing code and message of the last
448 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200450 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200452 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200454 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456
457 // --------------------------------------------------------------------
458
459 /**
460 * Escape the SQL Identifiers
461 *
462 * This function escapes column and table names
463 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * @param string
465 * @return string
466 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200467 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 if ($this->_escape_char == '')
470 {
471 return $item;
472 }
473
474 foreach ($this->_reserved_identifiers as $id)
475 {
476 if (strpos($item, '.'.$id) !== FALSE)
477 {
Barry Mienydd671972010-10-04 16:33:58 +0200478 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 // remove duplicates if the user already included the escape
481 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200482 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 if (strpos($item, '.') !== FALSE)
486 {
Barry Mienydd671972010-10-04 16:33:58 +0200487 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489 else
490 {
491 $str = $this->_escape_char.$item.$this->_escape_char;
492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 // remove duplicates if the user already included the escape
495 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 // --------------------------------------------------------------------
499
500 /**
501 * From Tables
502 *
503 * This function implicitly groups FROM tables so there is no confusion
504 * about operator precedence in harmony with SQL standards
505 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200506 * @param array
507 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200509 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
511 if ( ! is_array($tables))
512 {
513 $tables = array($tables);
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 return implode(', ', $tables);
517 }
518
519 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 /**
522 * Insert statement
523 *
524 * Generates a platform-specific insert string from the supplied data
525 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 * @param string the table name
527 * @param array the insert keys
528 * @param array the insert values
529 * @return string
530 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200531 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200532 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 // --------------------------------------------------------------------
537
538 /**
Greg Aker60ef4ea2011-04-27 01:45:38 -0500539 * Insert_batch statement
540 *
541 * Generates a platform-specific insert string from the supplied data
542 *
Greg Aker60ef4ea2011-04-27 01:45:38 -0500543 * @param string the table name
544 * @param array the insert keys
545 * @param array the insert values
546 * @return string
547 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200548 protected function _insert_batch($table, $keys, $values)
Greg Aker60ef4ea2011-04-27 01:45:38 -0500549 {
550 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
551 }
552
553 // --------------------------------------------------------------------
554
555 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 * Update statement
557 *
558 * Generates a platform-specific update string from the supplied data
559 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 * @param string the table name
561 * @param array the update data
562 * @param array the where clause
563 * @param array the orderby clause
564 * @param array the limit clause
565 * @return string
566 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200567 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500569 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
571 $valstr[] = $key." = ".$val;
572 }
Barry Mienydd671972010-10-04 16:33:58 +0200573
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
575
576 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 return $sql;
579 }
580
581 // --------------------------------------------------------------------
582
583 /**
584 * Truncate statement
585 *
586 * Generates a platform-specific truncate string from the supplied data
587 * If the database does not support the truncate() command
588 * This function maps to "DELETE FROM table"
589 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * @param string the table name
591 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200592 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200593 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
595 return "TRUNCATE ".$table;
596 }
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 // --------------------------------------------------------------------
599
600 /**
601 * Delete statement
602 *
603 * Generates a platform-specific delete string from the supplied data
604 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 * @param string the table name
606 * @param array the where clause
607 * @param string the limit clause
608 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200609 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200610 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
612 $conditions = '';
613
614 if (count($where) > 0 OR count($like) > 0)
615 {
616 $conditions = "\nWHERE ";
617 $conditions .= implode("\n", $this->ar_where);
618
619 if (count($where) > 0 && count($like) > 0)
620 {
621 $conditions .= " AND ";
622 }
623 $conditions .= implode("\n", $like);
624 }
625
SammyK1bf8eeb2012-03-05 16:56:06 -0600626 return "DELETE FROM ".$table.$conditions;
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 }
628
629 // --------------------------------------------------------------------
630 /**
631 * Limit string
632 *
633 * Generates a platform-specific LIMIT clause
634 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 * @param string the sql query string
Andrey Andreev738497c2012-03-20 14:12:25 +0200636 * @param int the number of rows to limit the query to
637 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 * @return string
639 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200640 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200641 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 $sql .= "LIMIT ".$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 if ($offset > 0)
645 {
646 $sql .= " OFFSET ".$offset;
647 }
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649 return $sql;
650 }
651
652 // --------------------------------------------------------------------
653
654 /**
655 * Close DB Connection
656 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 * @param resource
658 * @return void
659 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200660 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 {
662 @pg_close($conn_id);
663 }
Timothy Warren99c02ef2012-03-19 19:06:16 -0400664
Derek Allard2067d1a2008-11-13 22:59:24 +0000665}
666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667/* End of file postgre_driver.php */
Timothy Warrenbbea4052012-03-20 08:24:18 -0400668/* Location: ./system/database/drivers/postgre/postgre_driver.php */