blob: 1b947492097d2fdad0e2159027fd025d56c33cb0 [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
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * 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
Andrey Andreev738497c2012-03-20 14:12:25 +020047 protected $_random_keyword = ' RANDOM()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000048
49 /**
Andrey Andreev6192bc02012-03-26 12:32:32 +030050 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000051 *
Andrey Andreev6192bc02012-03-26 12:32:32 +030052 * Creates a DSN string to be used for db_connect() and db_pconnect()
53 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030054 * @param array $params
Andrey Andreev6192bc02012-03-26 12:32:32 +030055 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020056 */
Andrey Andreev6192bc02012-03-26 12:32:32 +030057 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000058 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030059 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020060
Andrey Andreev6192bc02012-03-26 12:32:32 +030061 if ( ! empty($this->dsn))
Derek Allard2067d1a2008-11-13 22:59:24 +000062 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030063 return;
64 }
65
66 $this->dsn === '' OR $this->dsn = '';
67
68 if (strpos($this->hostname, '/') !== FALSE)
69 {
70 // If UNIX sockets are used, we shouldn't set a port
71 $this->port = '';
72 }
73
Andrey Andreevab1d5682012-04-03 20:48:32 +030074 $this->hostname === '' OR $this->dsn = 'host='.$this->hostname.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030075
76 if ( ! empty($this->port) && ctype_digit($this->port))
77 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030078 $this->dsn .= 'port='.$this->port.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030079 }
80
81 if ($this->username !== '')
82 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030083 $this->dsn .= 'user='.$this->username.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030084
85 /* An empty password is valid!
86 *
87 * $db['password'] = NULL must be done in order to ignore it.
88 */
89 $this->password === NULL OR $this->dsn .= "password='".$this->password."' ";
90 }
91
92 $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' ';
93
94 /* We don't have these options as elements in our standard configuration
95 * array, but they might be set by parse_url() if the configuration was
96 * provided via string. Example:
97 *
98 * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
99 */
100 foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key)
101 {
102 if (isset($this->$key) && is_string($this->key) && $this->key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300104 $this->dsn .= $key."='".$this->key."' ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 }
106 }
Andrey Andreev6192bc02012-03-26 12:32:32 +0300107
108 $this->dsn = rtrim($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
110
111 // --------------------------------------------------------------------
112
113 /**
114 * Non-persistent database connection
115 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200117 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200118 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200119 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300120 return @pg_connect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
122
123 // --------------------------------------------------------------------
124
125 /**
126 * Persistent database connection
127 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200129 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200130 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
rwillert82408522012-07-10 14:02:01 +0300132 $conn = @pg_pconnect($this->dsn);
133 if ($conn && pg_connection_status($conn) === PGSQL_CONNECTION_BAD)
134 {
135 if (pg_ping($conn) === FALSE)
136 {
137 return FALSE;
138 }
139 }
140 return $conn;
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 // --------------------------------------------------------------------
144
145 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000146 * Reconnect
147 *
148 * Keep / reestablish the db connection if no queries have been
149 * sent for a length of time exceeding the server's idle timeout
150 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000151 * @return void
152 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200153 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000154 {
155 if (pg_ping($this->conn_id) === FALSE)
156 {
157 $this->conn_id = FALSE;
158 }
159 }
160
161 // --------------------------------------------------------------------
162
163 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 * Set client character set
165 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 * @param string
Andrey Andreev214583f2012-01-26 16:39:09 +0200167 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200169 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200171 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 }
173
174 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200177 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 * @return string
180 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200181 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200183 if (isset($this->data_cache['version']))
184 {
185 return $this->data_cache['version'];
186 }
187
188 if (($pg_version = pg_version($this->conn_id)) === FALSE)
189 {
190 return FALSE;
191 }
192
193 /* If PHP was compiled with PostgreSQL lib versions earlier
194 * than 7.4, pg_version() won't return the server version
195 * and so we'll have to fall back to running a query in
196 * order to get it.
197 */
198 return isset($pg_version['server'])
199 ? $this->data_cache['version'] = $pg_version['server']
200 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202
203 // --------------------------------------------------------------------
204
205 /**
206 * Execute the query
207 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 * @param string an SQL query
209 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200210 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200211 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 return @pg_query($this->conn_id, $sql);
214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 // --------------------------------------------------------------------
217
218 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 * Begin Transaction
220 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200221 * @param bool
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 Andreev214583f2012-01-26 16:39:09 +0200237 return (bool) @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 Andreev214583f2012-01-26 16:39:09 +0200255 return (bool) @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 Andreev214583f2012-01-26 16:39:09 +0200273 return (bool) @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 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700313 * "Smart" Escape String
314 *
315 * Escapes data based on type
316 * Sets boolean and null types
317 *
318 * @param string
319 * @return mixed
320 */
321 public function escape($str)
322 {
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700323 if (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700324 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700325 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700326 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700327
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700328 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700329 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300330
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700331 // --------------------------------------------------------------------
332
333 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 * Affected Rows
335 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200336 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 */
Andrey Andreev738497c2012-03-20 14:12:25 +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 Andreev738497c2012-03-20 14:12:25 +0200348 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 */
Andrey Andreev738497c2012-03-20 14:12:25 +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
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100358 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 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100362 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100364 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200365 {
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 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * Show table query
393 *
394 * Generates a platform-specific query string so that the table names can be fetched
395 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200396 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @return string
398 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200399 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200400 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300401 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \'public\'';
Barry Mienydd671972010-10-04 16:33:58 +0200402
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100403 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300405 return $sql.' AND "table_name" LIKE \''
406 .$this->escape_like_str($this->dbprefix)."%' "
407 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 return $sql;
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // --------------------------------------------------------------------
414
415 /**
416 * Show column query
417 *
418 * Generates a platform-specific query string so that the column names can be fetched
419 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 * @param string the table name
421 * @return string
422 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200423 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300425 return 'SELECT "column_name" FROM "information_schema"."columns" WHERE "table_name" = '.$this->escape($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
427
428 // --------------------------------------------------------------------
429
430 /**
431 * Field data query
432 *
433 * Generates a platform-specific query so that the column data can be retrieved
434 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 * @param string the table name
Andrey Andreev214583f2012-01-26 16:39:09 +0200436 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200438 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200440 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
442
443 // --------------------------------------------------------------------
444
445 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200446 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200448 * Returns an array containing code and message of the last
449 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200451 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200453 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200455 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
457
458 // --------------------------------------------------------------------
459
460 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 * Update statement
462 *
463 * Generates a platform-specific update string from the supplied data
464 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 * @param string the table name
466 * @param array the update data
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 * @return string
468 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300469 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300471 $this->qb_limit = FALSE;
472 $this->qb_orderby = array();
473 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
477
478 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300479 * Update_Batch statement
480 *
481 * Generates a platform-specific batch update string from the supplied data
482 *
483 * @param string the table name
484 * @param array the update data
Andrey Andreevb0478652012-07-18 15:34:46 +0300485 * @param string the where key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300486 * @return string
487 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300488 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300489 {
490 $ids = array();
491 foreach ($values as $key => $val)
492 {
493 $ids[] = $val[$index];
494
495 foreach (array_keys($val) as $field)
496 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100497 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300498 {
499 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
500 }
501 }
502 }
503
504 $cases = '';
505 foreach ($final as $k => $v)
506 {
aroche7f875b72012-07-16 18:22:08 +0300507 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300508 .implode("\n", $v)."\n"
509 .'ELSE '.$k.' END), ';
510 }
511
Andrey Andreevb0478652012-07-18 15:34:46 +0300512 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
513
Andrey Andreevd40459d2012-07-18 16:46:39 +0300514 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Andrey Andreevd06acd82012-05-25 00:29:09 +0300515 }
516
517 // --------------------------------------------------------------------
518
519 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * Delete statement
521 *
522 * Generates a platform-specific delete string from the supplied data
523 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 * @param string the table name
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200526 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300527 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300529 $this->qb_limit = FALSE;
530 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532
533 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 /**
536 * Limit string
537 *
538 * Generates a platform-specific LIMIT clause
539 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * @param string the sql query string
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 * @return string
542 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300543 protected function _limit($sql)
Barry Mienydd671972010-10-04 16:33:58 +0200544 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300545 return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 }
547
548 // --------------------------------------------------------------------
549
550 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300551 * WHERE, HAVING
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700552 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300553 * Called by where(), or_where(), having(), or_having()
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700554 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300555 * @param string 'qb_where' or 'qb_having'
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700556 * @param mixed
557 * @param mixed
558 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300559 * @param bool
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700560 * @return object
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700561 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300562 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700563 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300564 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
565
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700566 if ( ! is_array($key))
567 {
568 $key = array($key => $value);
569 }
570
571 // If the escape value was not set will will base it on the global setting
Andrey Andreev498c1e02012-06-16 03:34:10 +0300572 is_bool($escape) OR $escape = $this->_protect_identifiers;
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700573
574 foreach ($key as $k => $v)
575 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300576 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300577 ? $this->_group_get_type('')
578 : $this->_group_get_type($type);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700579
580 if (is_null($v) && ! $this->_has_operator($k))
581 {
582 // value appears not to have been set, assign the test to IS NULL
583 $k .= ' IS NULL';
584 }
585
586 if ( ! is_null($v))
587 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300588 if (is_bool($v))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700589 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300590 $v = ' '.($v ? 'TRUE' : 'FALSE');
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700591 }
Andrey Andreevb0478652012-07-18 15:34:46 +0300592 elseif ($escape === TRUE)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700593 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300594 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700595 }
596
597 if ( ! $this->_has_operator($k))
598 {
599 $k .= ' = ';
600 }
601 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700602
Andrey Andreevd40459d2012-07-18 16:46:39 +0300603 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700604 if ($this->qb_caching === TRUE)
605 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300606 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
607 $this->qb_cache_exists[] = substr($qb_key, 3);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700608 }
609
610 }
611
612 return $this;
613 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300614
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700615 // --------------------------------------------------------------------
616
617 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 * Close DB Connection
619 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 * @return void
621 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300622 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300624 @pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 }
626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627}
628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300630/* Location: ./system/database/drivers/postgre/postgre_driver.php */