blob: 91d9a238592d860a96b78161fbda8e55ea119f61 [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 Andreev485a3482012-10-27 03:22:43 +030050 * @var string Database schema
51 */
52 public $schema = 'public';
53
54 /**
Andrey Andreev6192bc02012-03-26 12:32:32 +030055 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000056 *
Andrey Andreev6192bc02012-03-26 12:32:32 +030057 * Creates a DSN string to be used for db_connect() and db_pconnect()
58 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030059 * @param array $params
Andrey Andreev6192bc02012-03-26 12:32:32 +030060 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020061 */
Andrey Andreev6192bc02012-03-26 12:32:32 +030062 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000063 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030064 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020065
Andrey Andreev6192bc02012-03-26 12:32:32 +030066 if ( ! empty($this->dsn))
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030068 return;
69 }
70
71 $this->dsn === '' OR $this->dsn = '';
72
73 if (strpos($this->hostname, '/') !== FALSE)
74 {
75 // If UNIX sockets are used, we shouldn't set a port
76 $this->port = '';
77 }
78
Andrey Andreevab1d5682012-04-03 20:48:32 +030079 $this->hostname === '' OR $this->dsn = 'host='.$this->hostname.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030080
81 if ( ! empty($this->port) && ctype_digit($this->port))
82 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030083 $this->dsn .= 'port='.$this->port.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030084 }
85
86 if ($this->username !== '')
87 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030088 $this->dsn .= 'user='.$this->username.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030089
90 /* An empty password is valid!
91 *
92 * $db['password'] = NULL must be done in order to ignore it.
93 */
94 $this->password === NULL OR $this->dsn .= "password='".$this->password."' ";
95 }
96
97 $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' ';
98
99 /* We don't have these options as elements in our standard configuration
100 * array, but they might be set by parse_url() if the configuration was
101 * provided via string. Example:
102 *
103 * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
104 */
105 foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key)
106 {
107 if (isset($this->$key) && is_string($this->key) && $this->key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300109 $this->dsn .= $key."='".$this->key."' ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 }
111 }
Andrey Andreev6192bc02012-03-26 12:32:32 +0300112
113 $this->dsn = rtrim($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Non-persistent database connection
120 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200122 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200123 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200124 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300125 return @pg_connect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 }
127
128 // --------------------------------------------------------------------
129
130 /**
131 * Persistent database connection
132 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200134 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200135 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
rwillert82408522012-07-10 14:02:01 +0300137 $conn = @pg_pconnect($this->dsn);
138 if ($conn && pg_connection_status($conn) === PGSQL_CONNECTION_BAD)
139 {
140 if (pg_ping($conn) === FALSE)
141 {
142 return FALSE;
143 }
144 }
145 return $conn;
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // --------------------------------------------------------------------
149
150 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000151 * Reconnect
152 *
153 * Keep / reestablish the db connection if no queries have been
154 * sent for a length of time exceeding the server's idle timeout
155 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000156 * @return void
157 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200158 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000159 {
160 if (pg_ping($this->conn_id) === FALSE)
161 {
162 $this->conn_id = FALSE;
163 }
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * Set client character set
170 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * @param string
Andrey Andreev214583f2012-01-26 16:39:09 +0200172 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200174 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200176 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
178
179 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200182 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @return string
185 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200186 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200188 if (isset($this->data_cache['version']))
189 {
190 return $this->data_cache['version'];
191 }
192
193 if (($pg_version = pg_version($this->conn_id)) === FALSE)
194 {
195 return FALSE;
196 }
197
198 /* If PHP was compiled with PostgreSQL lib versions earlier
199 * than 7.4, pg_version() won't return the server version
200 * and so we'll have to fall back to running a query in
201 * order to get it.
202 */
203 return isset($pg_version['server'])
204 ? $this->data_cache['version'] = $pg_version['server']
205 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
207
208 // --------------------------------------------------------------------
209
210 /**
211 * Execute the query
212 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * @param string an SQL query
214 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200215 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200216 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 return @pg_query($this->conn_id, $sql);
219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 // --------------------------------------------------------------------
222
223 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 * Begin Transaction
225 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200226 * @param bool
Barry Mienydd671972010-10-04 16:33:58 +0200227 * @return bool
228 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200229 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200232 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
234 return TRUE;
235 }
236
237 // Reset the transaction failure flag.
238 // If the $test_mode flag is set to TRUE transactions will be rolled back
239 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200240 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000241
Andrey Andreev214583f2012-01-26 16:39:09 +0200242 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
244
245 // --------------------------------------------------------------------
246
247 /**
248 * Commit Transaction
249 *
Barry Mienydd671972010-10-04 16:33:58 +0200250 * @return bool
251 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200252 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200255 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
257 return TRUE;
258 }
259
Andrey Andreev214583f2012-01-26 16:39:09 +0200260 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Rollback Transaction
267 *
Barry Mienydd671972010-10-04 16:33:58 +0200268 * @return bool
269 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200270 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200273 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
275 return TRUE;
276 }
277
Andrey Andreev214583f2012-01-26 16:39:09 +0200278 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * Escape String
285 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000287 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * @return string
289 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200290 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000291 {
292 if (is_array($str))
293 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500294 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200295 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000296 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200297 }
298
299 return $str;
300 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000301
302 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Jonese4ed5832009-02-20 21:44:59 +0000304 // escape LIKE condition wildcards
305 if ($like === TRUE)
306 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200307 return str_replace(array($this->_like_escape_chr, '%', '_'),
308 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
309 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Jonese4ed5832009-02-20 21:44:59 +0000312 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 // --------------------------------------------------------------------
316
317 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700318 * "Smart" Escape String
319 *
320 * Escapes data based on type
321 * Sets boolean and null types
322 *
323 * @param string
324 * @return mixed
325 */
326 public function escape($str)
327 {
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700328 if (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700329 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700330 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700331 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700332
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700333 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700334 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300335
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700336 // --------------------------------------------------------------------
337
338 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * Affected Rows
340 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200341 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200343 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
345 return @pg_affected_rows($this->result_id);
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // --------------------------------------------------------------------
349
350 /**
351 * Insert ID
352 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200353 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200355 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200357 $v = pg_version($this->conn_id);
358 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200359
Andrey Andreev214583f2012-01-26 16:39:09 +0200360 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
361 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200362
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100363 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200365 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100367 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100369 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200370 {
371 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
372 $query = $this->query($sql);
373 $query = $query->row();
374 $seq = $query->seq;
375 }
376 else
377 {
378 // seq_name passed in table parameter
379 $seq = $table;
380 }
381
382 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
384 else
385 {
386 return pg_last_oid($this->result_id);
387 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200390 $query = $query->row();
391 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
393
394 // --------------------------------------------------------------------
395
396 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * Show table query
398 *
399 * Generates a platform-specific query string so that the table names can be fetched
400 *
Andrey Andreev485a3482012-10-27 03:22:43 +0300401 * @param bool $prefix_limit = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 * @return string
403 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200404 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200405 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300406 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \''.$this->schema."'";
Barry Mienydd671972010-10-04 16:33:58 +0200407
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100408 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300410 return $sql.' AND "table_name" LIKE \''
411 .$this->escape_like_str($this->dbprefix)."%' "
412 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 return $sql;
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // --------------------------------------------------------------------
419
420 /**
421 * Show column query
422 *
423 * Generates a platform-specific query string so that the column names can be fetched
424 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 * @param string the table name
426 * @return string
427 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200428 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300430 return 'SELECT "column_name" FROM "information_schema"."columns" WHERE "table_name" = '.$this->escape($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Field data query
437 *
438 * Generates a platform-specific query so that the column data can be retrieved
439 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 * @param string the table name
Andrey Andreev214583f2012-01-26 16:39:09 +0200441 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200443 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200445 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447
448 // --------------------------------------------------------------------
449
450 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200451 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200453 * Returns an array containing code and message of the last
454 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200456 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200458 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200460 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462
463 // --------------------------------------------------------------------
464
465 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 * Update statement
467 *
468 * Generates a platform-specific update string from the supplied data
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param string the table name
471 * @param array the update data
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @return string
473 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300474 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300476 $this->qb_limit = FALSE;
477 $this->qb_orderby = array();
478 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 // --------------------------------------------------------------------
482
483 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300484 * Update_Batch statement
485 *
486 * Generates a platform-specific batch update string from the supplied data
487 *
488 * @param string the table name
489 * @param array the update data
Andrey Andreevb0478652012-07-18 15:34:46 +0300490 * @param string the where key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300491 * @return string
492 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300493 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300494 {
495 $ids = array();
496 foreach ($values as $key => $val)
497 {
498 $ids[] = $val[$index];
499
500 foreach (array_keys($val) as $field)
501 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100502 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300503 {
504 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
505 }
506 }
507 }
508
509 $cases = '';
510 foreach ($final as $k => $v)
511 {
aroche7f875b72012-07-16 18:22:08 +0300512 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300513 .implode("\n", $v)."\n"
514 .'ELSE '.$k.' END), ';
515 }
516
Andrey Andreevb0478652012-07-18 15:34:46 +0300517 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
518
Andrey Andreevd40459d2012-07-18 16:46:39 +0300519 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Andrey Andreevd06acd82012-05-25 00:29:09 +0300520 }
521
522 // --------------------------------------------------------------------
523
524 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 * Delete statement
526 *
527 * Generates a platform-specific delete string from the supplied data
528 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 * @param string the table name
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200531 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300532 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300534 $this->qb_limit = FALSE;
535 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 }
537
538 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 /**
541 * Limit string
542 *
543 * Generates a platform-specific LIMIT clause
544 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 * @param string the sql query string
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 * @return string
547 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300548 protected function _limit($sql)
Barry Mienydd671972010-10-04 16:33:58 +0200549 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300550 return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
552
553 // --------------------------------------------------------------------
554
555 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300556 * WHERE, HAVING
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700557 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300558 * Called by where(), or_where(), having(), or_having()
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700559 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300560 * @param string 'qb_where' or 'qb_having'
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700561 * @param mixed
562 * @param mixed
563 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300564 * @param bool
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700565 * @return object
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700566 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300567 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700568 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300569 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
570
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700571 if ( ! is_array($key))
572 {
573 $key = array($key => $value);
574 }
575
576 // If the escape value was not set will will base it on the global setting
Andrey Andreev498c1e02012-06-16 03:34:10 +0300577 is_bool($escape) OR $escape = $this->_protect_identifiers;
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700578
579 foreach ($key as $k => $v)
580 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300581 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300582 ? $this->_group_get_type('')
583 : $this->_group_get_type($type);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700584
585 if (is_null($v) && ! $this->_has_operator($k))
586 {
587 // value appears not to have been set, assign the test to IS NULL
588 $k .= ' IS NULL';
589 }
590
591 if ( ! is_null($v))
592 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300593 if (is_bool($v))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700594 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300595 $v = ' '.($v ? 'TRUE' : 'FALSE');
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700596 }
Andrey Andreevb0478652012-07-18 15:34:46 +0300597 elseif ($escape === TRUE)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700598 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300599 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700600 }
601
602 if ( ! $this->_has_operator($k))
603 {
604 $k .= ' = ';
605 }
606 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700607
Andrey Andreevd40459d2012-07-18 16:46:39 +0300608 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700609 if ($this->qb_caching === TRUE)
610 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300611 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
612 $this->qb_cache_exists[] = substr($qb_key, 3);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700613 }
614
615 }
616
617 return $this;
618 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300619
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700620 // --------------------------------------------------------------------
621
622 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 * Close DB Connection
624 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 * @return void
626 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300627 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300629 @pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 }
631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632}
633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300635/* Location: ./system/database/drivers/postgre/postgre_driver.php */