blob: ca231f6f76568b7ed320a44a9d5e479180eba450 [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 Andreev3e9d2b82012-10-27 14:28:51 +030050 * Database schema
51 *
52 * @var string
Andrey Andreev485a3482012-10-27 03:22:43 +030053 */
54 public $schema = 'public';
55
56 /**
Andrey Andreev6192bc02012-03-26 12:32:32 +030057 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000058 *
Andrey Andreev6192bc02012-03-26 12:32:32 +030059 * Creates a DSN string to be used for db_connect() and db_pconnect()
60 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030061 * @param array $params
Andrey Andreev6192bc02012-03-26 12:32:32 +030062 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020063 */
Andrey Andreev6192bc02012-03-26 12:32:32 +030064 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030066 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020067
Andrey Andreev6192bc02012-03-26 12:32:32 +030068 if ( ! empty($this->dsn))
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030070 return;
71 }
72
73 $this->dsn === '' OR $this->dsn = '';
74
75 if (strpos($this->hostname, '/') !== FALSE)
76 {
77 // If UNIX sockets are used, we shouldn't set a port
78 $this->port = '';
79 }
80
Andrey Andreevab1d5682012-04-03 20:48:32 +030081 $this->hostname === '' OR $this->dsn = 'host='.$this->hostname.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030082
83 if ( ! empty($this->port) && ctype_digit($this->port))
84 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030085 $this->dsn .= 'port='.$this->port.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030086 }
87
88 if ($this->username !== '')
89 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030090 $this->dsn .= 'user='.$this->username.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030091
92 /* An empty password is valid!
93 *
94 * $db['password'] = NULL must be done in order to ignore it.
95 */
96 $this->password === NULL OR $this->dsn .= "password='".$this->password."' ";
97 }
98
99 $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' ';
100
101 /* We don't have these options as elements in our standard configuration
102 * array, but they might be set by parse_url() if the configuration was
103 * provided via string. Example:
104 *
105 * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
106 */
107 foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key)
108 {
109 if (isset($this->$key) && is_string($this->key) && $this->key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300111 $this->dsn .= $key."='".$this->key."' ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113 }
Andrey Andreev6192bc02012-03-26 12:32:32 +0300114
115 $this->dsn = rtrim($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
117
118 // --------------------------------------------------------------------
119
120 /**
121 * Non-persistent database connection
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200124 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200125 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200126 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300127 return @pg_connect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Persistent database connection
134 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200136 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200137 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
rwillert82408522012-07-10 14:02:01 +0300139 $conn = @pg_pconnect($this->dsn);
140 if ($conn && pg_connection_status($conn) === PGSQL_CONNECTION_BAD)
141 {
142 if (pg_ping($conn) === FALSE)
143 {
144 return FALSE;
145 }
146 }
147 return $conn;
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // --------------------------------------------------------------------
151
152 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000153 * Reconnect
154 *
155 * Keep / reestablish the db connection if no queries have been
156 * sent for a length of time exceeding the server's idle timeout
157 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000158 * @return void
159 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200160 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000161 {
162 if (pg_ping($this->conn_id) === FALSE)
163 {
164 $this->conn_id = FALSE;
165 }
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * Set client character set
172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string
Andrey Andreev214583f2012-01-26 16:39:09 +0200174 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200176 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200178 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 }
180
181 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200184 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * @return string
187 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200188 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200190 if (isset($this->data_cache['version']))
191 {
192 return $this->data_cache['version'];
193 }
194
195 if (($pg_version = pg_version($this->conn_id)) === FALSE)
196 {
197 return FALSE;
198 }
199
200 /* If PHP was compiled with PostgreSQL lib versions earlier
201 * than 7.4, pg_version() won't return the server version
202 * and so we'll have to fall back to running a query in
203 * order to get it.
204 */
205 return isset($pg_version['server'])
206 ? $this->data_cache['version'] = $pg_version['server']
207 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Execute the query
214 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 * @param string an SQL query
216 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200217 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200218 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 return @pg_query($this->conn_id, $sql);
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 // --------------------------------------------------------------------
224
225 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * Begin Transaction
227 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200228 * @param bool
Barry Mienydd671972010-10-04 16:33:58 +0200229 * @return bool
230 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200231 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200234 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
236 return TRUE;
237 }
238
239 // Reset the transaction failure flag.
240 // If the $test_mode flag is set to TRUE transactions will be rolled back
241 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200242 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243
Andrey Andreev214583f2012-01-26 16:39:09 +0200244 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246
247 // --------------------------------------------------------------------
248
249 /**
250 * Commit Transaction
251 *
Barry Mienydd671972010-10-04 16:33:58 +0200252 * @return bool
253 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200254 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200257 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
259 return TRUE;
260 }
261
Andrey Andreev214583f2012-01-26 16:39:09 +0200262 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264
265 // --------------------------------------------------------------------
266
267 /**
268 * Rollback Transaction
269 *
Barry Mienydd671972010-10-04 16:33:58 +0200270 * @return bool
271 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200272 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200275 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
277 return TRUE;
278 }
279
Andrey Andreev214583f2012-01-26 16:39:09 +0200280 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
282
283 // --------------------------------------------------------------------
284
285 /**
286 * Escape String
287 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000289 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * @return string
291 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200292 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000293 {
294 if (is_array($str))
295 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500296 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200297 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000298 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200299 }
300
301 return $str;
302 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000303
304 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Jonese4ed5832009-02-20 21:44:59 +0000306 // escape LIKE condition wildcards
307 if ($like === TRUE)
308 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200309 return str_replace(array($this->_like_escape_chr, '%', '_'),
310 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
311 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Jonese4ed5832009-02-20 21:44:59 +0000314 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // --------------------------------------------------------------------
318
319 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700320 * "Smart" Escape String
321 *
322 * Escapes data based on type
323 * Sets boolean and null types
324 *
325 * @param string
326 * @return mixed
327 */
328 public function escape($str)
329 {
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700330 if (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700331 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700332 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700333 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700334
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700335 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700336 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300337
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700338 // --------------------------------------------------------------------
339
340 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * Affected Rows
342 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200343 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200345 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
347 return @pg_affected_rows($this->result_id);
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // --------------------------------------------------------------------
351
352 /**
353 * Insert ID
354 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200355 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200357 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200359 $v = pg_version($this->conn_id);
360 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200361
Andrey Andreev214583f2012-01-26 16:39:09 +0200362 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
363 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200364
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100365 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200367 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100369 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100371 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200372 {
373 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
374 $query = $this->query($sql);
375 $query = $query->row();
376 $seq = $query->seq;
377 }
378 else
379 {
380 // seq_name passed in table parameter
381 $seq = $table;
382 }
383
384 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386 else
387 {
388 return pg_last_oid($this->result_id);
389 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200392 $query = $query->row();
393 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
395
396 // --------------------------------------------------------------------
397
398 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * Show table query
400 *
401 * Generates a platform-specific query string so that the table names can be fetched
402 *
Andrey Andreev485a3482012-10-27 03:22:43 +0300403 * @param bool $prefix_limit = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @return string
405 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200406 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200407 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300408 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \''.$this->schema."'";
Barry Mienydd671972010-10-04 16:33:58 +0200409
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100410 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300412 return $sql.' AND "table_name" LIKE \''
413 .$this->escape_like_str($this->dbprefix)."%' "
414 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 return $sql;
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // --------------------------------------------------------------------
421
422 /**
423 * Show column query
424 *
425 * Generates a platform-specific query string so that the column names can be fetched
426 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @param string the table name
428 * @return string
429 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200430 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300432 return 'SELECT "column_name" FROM "information_schema"."columns" WHERE "table_name" = '.$this->escape($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434
435 // --------------------------------------------------------------------
436
437 /**
438 * Field data query
439 *
440 * Generates a platform-specific query so that the column data can be retrieved
441 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * @param string the table name
Andrey Andreev214583f2012-01-26 16:39:09 +0200443 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200445 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200447 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449
450 // --------------------------------------------------------------------
451
452 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200453 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200455 * Returns an array containing code and message of the last
456 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200458 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200460 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200462 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
464
465 // --------------------------------------------------------------------
466
467 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 * Update statement
469 *
470 * Generates a platform-specific update string from the supplied data
471 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @param string the table name
473 * @param array the update data
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 * @return string
475 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300476 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300478 $this->qb_limit = FALSE;
479 $this->qb_orderby = array();
480 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 // --------------------------------------------------------------------
484
485 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300486 * Update_Batch statement
487 *
488 * Generates a platform-specific batch update string from the supplied data
489 *
490 * @param string the table name
491 * @param array the update data
Andrey Andreevb0478652012-07-18 15:34:46 +0300492 * @param string the where key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300493 * @return string
494 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300495 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300496 {
497 $ids = array();
498 foreach ($values as $key => $val)
499 {
500 $ids[] = $val[$index];
501
502 foreach (array_keys($val) as $field)
503 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100504 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300505 {
506 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
507 }
508 }
509 }
510
511 $cases = '';
512 foreach ($final as $k => $v)
513 {
aroche7f875b72012-07-16 18:22:08 +0300514 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300515 .implode("\n", $v)."\n"
516 .'ELSE '.$k.' END), ';
517 }
518
Andrey Andreevb0478652012-07-18 15:34:46 +0300519 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
520
Andrey Andreevd40459d2012-07-18 16:46:39 +0300521 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Andrey Andreevd06acd82012-05-25 00:29:09 +0300522 }
523
524 // --------------------------------------------------------------------
525
526 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * Delete statement
528 *
529 * Generates a platform-specific delete string from the supplied data
530 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 * @param string the table name
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200533 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300534 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300536 $this->qb_limit = FALSE;
537 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
539
540 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 /**
543 * Limit string
544 *
545 * Generates a platform-specific LIMIT clause
546 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * @param string the sql query string
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * @return string
549 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300550 protected function _limit($sql)
Barry Mienydd671972010-10-04 16:33:58 +0200551 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300552 return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
554
555 // --------------------------------------------------------------------
556
557 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300558 * WHERE, HAVING
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700559 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300560 * Called by where(), or_where(), having(), or_having()
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700561 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300562 * @param string 'qb_where' or 'qb_having'
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700563 * @param mixed
564 * @param mixed
565 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300566 * @param bool
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700567 * @return object
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700568 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300569 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700570 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300571 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
572
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700573 if ( ! is_array($key))
574 {
575 $key = array($key => $value);
576 }
577
578 // If the escape value was not set will will base it on the global setting
Andrey Andreev498c1e02012-06-16 03:34:10 +0300579 is_bool($escape) OR $escape = $this->_protect_identifiers;
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700580
581 foreach ($key as $k => $v)
582 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300583 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300584 ? $this->_group_get_type('')
585 : $this->_group_get_type($type);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700586
587 if (is_null($v) && ! $this->_has_operator($k))
588 {
589 // value appears not to have been set, assign the test to IS NULL
590 $k .= ' IS NULL';
591 }
592
593 if ( ! is_null($v))
594 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300595 if (is_bool($v))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700596 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300597 $v = ' '.($v ? 'TRUE' : 'FALSE');
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700598 }
Andrey Andreevb0478652012-07-18 15:34:46 +0300599 elseif ($escape === TRUE)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700600 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300601 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700602 }
603
604 if ( ! $this->_has_operator($k))
605 {
606 $k .= ' = ';
607 }
608 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700609
Andrey Andreevd40459d2012-07-18 16:46:39 +0300610 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700611 if ($this->qb_caching === TRUE)
612 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300613 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
614 $this->qb_cache_exists[] = substr($qb_key, 3);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700615 }
616
617 }
618
619 return $this;
620 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300621
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700622 // --------------------------------------------------------------------
623
624 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 * Close DB Connection
626 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @return void
628 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300629 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300631 @pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 }
633
Derek Allard2067d1a2008-11-13 22:59:24 +0000634}
635
Derek Allard2067d1a2008-11-13 22:59:24 +0000636/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300637/* Location: ./system/database/drivers/postgre/postgre_driver.php */