blob: 028dc199683d209ace3bdce65efb201ace5223e9 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Postgre Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
42class CI_DB_postgre_driver extends CI_DB {
43
Andrey Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreev738497c2012-03-20 14:12:25 +020049 public $dbdriver = 'postgre';
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030052 * Database schema
53 *
54 * @var string
Andrey Andreev485a3482012-10-27 03:22:43 +030055 */
56 public $schema = 'public';
57
Andrey Andreeva24e52e2012-11-02 03:54:12 +020058 // --------------------------------------------------------------------
59
Andrey Andreev485a3482012-10-27 03:22:43 +030060 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020061 * ORDER BY random keyword
62 *
63 * @var string
64 */
65 protected $_random_keyword = ' RANDOM()'; // database specific random keyword
66
67 // --------------------------------------------------------------------
68
69 /**
70 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000071 *
Andrey Andreev6192bc02012-03-26 12:32:32 +030072 * Creates a DSN string to be used for db_connect() and db_pconnect()
73 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030074 * @param array $params
Andrey Andreev6192bc02012-03-26 12:32:32 +030075 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020076 */
Andrey Andreev6192bc02012-03-26 12:32:32 +030077 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000078 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030079 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020080
Andrey Andreev6192bc02012-03-26 12:32:32 +030081 if ( ! empty($this->dsn))
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030083 return;
84 }
85
86 $this->dsn === '' OR $this->dsn = '';
87
88 if (strpos($this->hostname, '/') !== FALSE)
89 {
90 // If UNIX sockets are used, we shouldn't set a port
91 $this->port = '';
92 }
93
Andrey Andreevab1d5682012-04-03 20:48:32 +030094 $this->hostname === '' OR $this->dsn = 'host='.$this->hostname.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030095
96 if ( ! empty($this->port) && ctype_digit($this->port))
97 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030098 $this->dsn .= 'port='.$this->port.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030099 }
100
101 if ($this->username !== '')
102 {
Andrey Andreevab1d5682012-04-03 20:48:32 +0300103 $this->dsn .= 'user='.$this->username.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +0300104
105 /* An empty password is valid!
106 *
107 * $db['password'] = NULL must be done in order to ignore it.
108 */
109 $this->password === NULL OR $this->dsn .= "password='".$this->password."' ";
110 }
111
112 $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' ';
113
114 /* We don't have these options as elements in our standard configuration
115 * array, but they might be set by parse_url() if the configuration was
116 * provided via string. Example:
117 *
118 * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
119 */
120 foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key)
121 {
122 if (isset($this->$key) && is_string($this->key) && $this->key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300124 $this->dsn .= $key."='".$this->key."' ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 }
126 }
Andrey Andreev6192bc02012-03-26 12:32:32 +0300127
128 $this->dsn = rtrim($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Non-persistent database connection
135 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200137 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200138 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200139 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300140 return @pg_connect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Persistent database connection
147 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200149 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200150 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
rwillert82408522012-07-10 14:02:01 +0300152 $conn = @pg_pconnect($this->dsn);
153 if ($conn && pg_connection_status($conn) === PGSQL_CONNECTION_BAD)
154 {
155 if (pg_ping($conn) === FALSE)
156 {
157 return FALSE;
158 }
159 }
160 return $conn;
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // --------------------------------------------------------------------
164
165 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000166 * Reconnect
167 *
168 * Keep / reestablish the db connection if no queries have been
169 * sent for a length of time exceeding the server's idle timeout
170 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000171 * @return void
172 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200173 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000174 {
175 if (pg_ping($this->conn_id) === FALSE)
176 {
177 $this->conn_id = FALSE;
178 }
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * Set client character set
185 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200186 * @param string $charset
Andrey Andreev214583f2012-01-26 16:39:09 +0200187 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200189 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200191 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
193
194 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200197 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 * @return string
200 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200201 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200203 if (isset($this->data_cache['version']))
204 {
205 return $this->data_cache['version'];
206 }
207
208 if (($pg_version = pg_version($this->conn_id)) === FALSE)
209 {
210 return FALSE;
211 }
212
213 /* If PHP was compiled with PostgreSQL lib versions earlier
214 * than 7.4, pg_version() won't return the server version
215 * and so we'll have to fall back to running a query in
216 * order to get it.
217 */
218 return isset($pg_version['server'])
219 ? $this->data_cache['version'] = $pg_version['server']
220 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Execute the query
227 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200228 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200230 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200231 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 return @pg_query($this->conn_id, $sql);
234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 // --------------------------------------------------------------------
237
238 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 * Begin Transaction
240 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200241 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200242 * @return bool
243 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200244 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200247 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
249 return TRUE;
250 }
251
252 // Reset the transaction failure flag.
253 // If the $test_mode flag is set to TRUE transactions will be rolled back
254 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200255 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000256
Andrey Andreev214583f2012-01-26 16:39:09 +0200257 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Commit Transaction
264 *
Barry Mienydd671972010-10-04 16:33:58 +0200265 * @return bool
266 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200267 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200270 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
272 return TRUE;
273 }
274
Andrey Andreev214583f2012-01-26 16:39:09 +0200275 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 }
277
278 // --------------------------------------------------------------------
279
280 /**
281 * Rollback Transaction
282 *
Barry Mienydd671972010-10-04 16:33:58 +0200283 * @return bool
284 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200285 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200288 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
290 return TRUE;
291 }
292
Andrey Andreev214583f2012-01-26 16:39:09 +0200293 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
295
296 // --------------------------------------------------------------------
297
298 /**
299 * Escape String
300 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200301 * @param string $str
302 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 * @return string
304 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200305 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000306 {
307 if (is_array($str))
308 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500309 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200310 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000311 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200312 }
313
314 return $str;
315 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000316
317 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Jonese4ed5832009-02-20 21:44:59 +0000319 // escape LIKE condition wildcards
320 if ($like === TRUE)
321 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200322 return str_replace(array($this->_like_escape_chr, '%', '_'),
323 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
324 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Jonese4ed5832009-02-20 21:44:59 +0000327 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 // --------------------------------------------------------------------
331
332 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700333 * "Smart" Escape String
334 *
335 * Escapes data based on type
336 * Sets boolean and null types
337 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200338 * @param string $str
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700339 * @return mixed
340 */
341 public function escape($str)
342 {
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700343 if (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700344 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700345 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700346 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700347
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700348 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700349 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300350
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700351 // --------------------------------------------------------------------
352
353 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * Affected Rows
355 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200356 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200358 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 return @pg_affected_rows($this->result_id);
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // --------------------------------------------------------------------
364
365 /**
366 * Insert ID
367 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200368 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200370 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200372 $v = pg_version($this->conn_id);
373 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200374
Andrey Andreev214583f2012-01-26 16:39:09 +0200375 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
376 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200377
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100378 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200380 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100382 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100384 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200385 {
386 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
387 $query = $this->query($sql);
388 $query = $query->row();
389 $seq = $query->seq;
390 }
391 else
392 {
393 // seq_name passed in table parameter
394 $seq = $table;
395 }
396
397 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
399 else
400 {
401 return pg_last_oid($this->result_id);
402 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200405 $query = $query->row();
406 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408
409 // --------------------------------------------------------------------
410
411 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * Show table query
413 *
414 * Generates a platform-specific query string so that the table names can be fetched
415 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200416 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @return string
418 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200419 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200420 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300421 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \''.$this->schema."'";
Barry Mienydd671972010-10-04 16:33:58 +0200422
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100423 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300425 return $sql.' AND "table_name" LIKE \''
426 .$this->escape_like_str($this->dbprefix)."%' "
427 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 return $sql;
431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 // --------------------------------------------------------------------
434
435 /**
436 * Show column query
437 *
438 * Generates a platform-specific query string so that the column names can be fetched
439 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200440 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 * @return string
442 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200443 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300445 return 'SELECT "column_name" FROM "information_schema"."columns" WHERE "table_name" = '.$this->escape($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447
448 // --------------------------------------------------------------------
449
450 /**
451 * Field data query
452 *
453 * Generates a platform-specific query so that the column data can be retrieved
454 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200455 * @param string $table
Andrey Andreev214583f2012-01-26 16:39:09 +0200456 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200458 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200460 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462
463 // --------------------------------------------------------------------
464
465 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200466 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200468 * Returns an array containing code and message of the last
469 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200471 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200473 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200475 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477
478 // --------------------------------------------------------------------
479
480 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * Update statement
482 *
483 * Generates a platform-specific update string from the supplied data
484 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200485 * @param string $table
486 * @param array $values
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * @return string
488 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300489 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300491 $this->qb_limit = FALSE;
492 $this->qb_orderby = array();
493 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 // --------------------------------------------------------------------
497
498 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300499 * Update_Batch statement
500 *
501 * Generates a platform-specific batch update string from the supplied data
502 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200503 * @param string $table Table name
504 * @param array $values Update data
505 * @param string $index WHERE key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300506 * @return string
507 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300508 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300509 {
510 $ids = array();
511 foreach ($values as $key => $val)
512 {
513 $ids[] = $val[$index];
514
515 foreach (array_keys($val) as $field)
516 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100517 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300518 {
519 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
520 }
521 }
522 }
523
524 $cases = '';
525 foreach ($final as $k => $v)
526 {
aroche7f875b72012-07-16 18:22:08 +0300527 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300528 .implode("\n", $v)."\n"
529 .'ELSE '.$k.' END), ';
530 }
531
Andrey Andreevb0478652012-07-18 15:34:46 +0300532 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
533
Andrey Andreevd40459d2012-07-18 16:46:39 +0300534 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Andrey Andreevd06acd82012-05-25 00:29:09 +0300535 }
536
537 // --------------------------------------------------------------------
538
539 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * Delete statement
541 *
542 * Generates a platform-specific delete string from the supplied data
543 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200544 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200546 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300547 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300549 $this->qb_limit = FALSE;
550 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
552
553 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200556 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 *
558 * Generates a platform-specific LIMIT clause
559 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200560 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @return string
562 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300563 protected function _limit($sql)
Barry Mienydd671972010-10-04 16:33:58 +0200564 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300565 return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 }
567
568 // --------------------------------------------------------------------
569
570 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300571 * WHERE, HAVING
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700572 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300573 * Called by where(), or_where(), having(), or_having()
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700574 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300575 * @param string 'qb_where' or 'qb_having'
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700576 * @param mixed
577 * @param mixed
578 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300579 * @param bool
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700580 * @return object
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700581 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300582 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700583 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300584 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
585
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700586 if ( ! is_array($key))
587 {
588 $key = array($key => $value);
589 }
590
591 // If the escape value was not set will will base it on the global setting
Andrey Andreev498c1e02012-06-16 03:34:10 +0300592 is_bool($escape) OR $escape = $this->_protect_identifiers;
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700593
594 foreach ($key as $k => $v)
595 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300596 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300597 ? $this->_group_get_type('')
598 : $this->_group_get_type($type);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700599
600 if (is_null($v) && ! $this->_has_operator($k))
601 {
602 // value appears not to have been set, assign the test to IS NULL
603 $k .= ' IS NULL';
604 }
605
606 if ( ! is_null($v))
607 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300608 if (is_bool($v))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700609 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300610 $v = ' '.($v ? 'TRUE' : 'FALSE');
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700611 }
Andrey Andreevb0478652012-07-18 15:34:46 +0300612 elseif ($escape === TRUE)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700613 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300614 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700615 }
616
617 if ( ! $this->_has_operator($k))
618 {
619 $k .= ' = ';
620 }
621 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700622
Andrey Andreevd40459d2012-07-18 16:46:39 +0300623 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700624 if ($this->qb_caching === TRUE)
625 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300626 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
627 $this->qb_cache_exists[] = substr($qb_key, 3);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700628 }
629
630 }
631
632 return $this;
633 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300634
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700635 // --------------------------------------------------------------------
636
637 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 * Close DB Connection
639 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 * @return void
641 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300642 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300644 @pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 }
646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647}
648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300650/* Location: ./system/database/drivers/postgre/postgre_driver.php */