blob: f24db46b4ee98890554faa5653e9663d17dd6c2e [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +020063 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +020064 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +020065 protected $_random_keyword = array('RANDOM()', 'RANDOM()');
Andrey Andreeva24e52e2012-11-02 03:54:12 +020066
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 /**
Andrey Andreev0259d122012-12-03 14:55:56 +0200134 * Database connection
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 *
Andrey Andreev0259d122012-12-03 14:55:56 +0200136 * @param bool $persistent
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200138 */
Andrey Andreev0259d122012-12-03 14:55:56 +0200139 public function db_connect($persistent = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200140 {
Andrey Andreev0259d122012-12-03 14:55:56 +0200141 if ($persistent === TRUE
Andrey Andreevc07c4852012-12-03 16:42:10 +0200142 && ($this->conn_id = @pg_pconnect($this->dsn))
143 && pg_connection_status($this->conn_id) === PGSQL_CONNECTION_BAD
144 && pg_ping($this->conn_id) === FALSE
Andrey Andreev0259d122012-12-03 14:55:56 +0200145 )
146 {
147 return FALSE;
148 }
149 else
150 {
Andrey Andreevc07c4852012-12-03 16:42:10 +0200151 $this->conn_id = @pg_connect($this->dsn);
Andrey Andreev0259d122012-12-03 14:55:56 +0200152 }
153
Andrey Andreevc07c4852012-12-03 16:42:10 +0200154 if ($this->conn_id && ! empty($this->schema))
Andrey Andreev0259d122012-12-03 14:55:56 +0200155 {
156 $this->simple_query('SET search_path TO '.$this->schema.',public');
157 }
158
Andrey Andreevc07c4852012-12-03 16:42:10 +0200159 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Persistent database connection
166 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200168 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200169 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Andrey Andreev0259d122012-12-03 14:55:56 +0200171 return $this->db_connect(TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // --------------------------------------------------------------------
175
176 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000177 * Reconnect
178 *
179 * Keep / reestablish the db connection if no queries have been
180 * sent for a length of time exceeding the server's idle timeout
181 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000182 * @return void
183 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200184 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000185 {
186 if (pg_ping($this->conn_id) === FALSE)
187 {
188 $this->conn_id = FALSE;
189 }
190 }
191
192 // --------------------------------------------------------------------
193
194 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 * Set client character set
196 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200197 * @param string $charset
Andrey Andreev214583f2012-01-26 16:39:09 +0200198 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200200 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200202 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
205 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200208 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 * @return string
211 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200212 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200214 if (isset($this->data_cache['version']))
215 {
216 return $this->data_cache['version'];
217 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200218 elseif ( ! $this->conn_id)
219 {
220 $this->initialize();
221 }
Andrey Andreev08856b82012-03-03 03:19:28 +0200222
Andrey Andreev2b730372012-11-05 17:01:11 +0200223 if ( ! $this->conn_id OR ($pg_version = pg_version($this->conn_id)) === FALSE)
Andrey Andreev08856b82012-03-03 03:19:28 +0200224 {
225 return FALSE;
226 }
227
228 /* If PHP was compiled with PostgreSQL lib versions earlier
229 * than 7.4, pg_version() won't return the server version
230 * and so we'll have to fall back to running a query in
231 * order to get it.
232 */
233 return isset($pg_version['server'])
234 ? $this->data_cache['version'] = $pg_version['server']
235 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Execute the query
242 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200243 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200245 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200246 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 return @pg_query($this->conn_id, $sql);
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 // --------------------------------------------------------------------
252
253 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * Begin Transaction
255 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200256 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200257 * @return bool
258 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200259 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200262 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 return TRUE;
265 }
266
267 // Reset the transaction failure flag.
268 // If the $test_mode flag is set to TRUE transactions will be rolled back
269 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200270 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000271
Andrey Andreev214583f2012-01-26 16:39:09 +0200272 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
274
275 // --------------------------------------------------------------------
276
277 /**
278 * Commit Transaction
279 *
Barry Mienydd671972010-10-04 16:33:58 +0200280 * @return bool
281 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200282 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200285 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
287 return TRUE;
288 }
289
Andrey Andreev214583f2012-01-26 16:39:09 +0200290 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Rollback Transaction
297 *
Barry Mienydd671972010-10-04 16:33:58 +0200298 * @return bool
299 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200300 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200303 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
305 return TRUE;
306 }
307
Andrey Andreev214583f2012-01-26 16:39:09 +0200308 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
310
311 // --------------------------------------------------------------------
312
313 /**
Andrey Andreevd5ab75e2013-07-17 20:28:48 +0300314 * Determines if a query is a "write" type.
315 *
316 * @param string An SQL query string
317 * @return bool
318 */
319 public function is_write_type($sql)
320 {
321 return (bool) preg_match('/^\s*"?(SET|INSERT(?![^\)]+\)\s+RETURNING)|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql);
322 }
323
324 // --------------------------------------------------------------------
325
326 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200327 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200329 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @return string
331 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200332 protected function _escape_str($str)
Derek Jonese4ed5832009-02-20 21:44:59 +0000333 {
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200334 return pg_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // --------------------------------------------------------------------
338
339 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700340 * "Smart" Escape String
341 *
342 * Escapes data based on type
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700343 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200344 * @param string $str
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700345 * @return mixed
346 */
347 public function escape($str)
348 {
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700349 if (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700350 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700351 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700352 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700353
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700354 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700355 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300356
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700357 // --------------------------------------------------------------------
358
359 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 * Affected Rows
361 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200362 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200364 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
366 return @pg_affected_rows($this->result_id);
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // --------------------------------------------------------------------
370
371 /**
372 * Insert ID
373 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200374 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200376 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200378 $v = pg_version($this->conn_id);
379 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200380
Andrey Andreev214583f2012-01-26 16:39:09 +0200381 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
382 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200383
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100384 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200386 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100388 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100390 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200391 {
392 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
393 $query = $this->query($sql);
394 $query = $query->row();
395 $seq = $query->seq;
396 }
397 else
398 {
399 // seq_name passed in table parameter
400 $seq = $table;
401 }
402
403 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 }
405 else
406 {
407 return pg_last_oid($this->result_id);
408 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200411 $query = $query->row();
412 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
414
415 // --------------------------------------------------------------------
416
417 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 * Show table query
419 *
420 * Generates a platform-specific query string so that the table names can be fetched
421 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200422 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * @return string
424 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200425 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200426 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300427 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \''.$this->schema."'";
Barry Mienydd671972010-10-04 16:33:58 +0200428
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100429 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300431 return $sql.' AND "table_name" LIKE \''
432 .$this->escape_like_str($this->dbprefix)."%' "
433 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 return $sql;
437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // --------------------------------------------------------------------
440
441 /**
Andrey Andreev46583072012-11-16 16:44:31 +0200442 * List column query
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 *
444 * Generates a platform-specific query string so that the column names can be fetched
445 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200446 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @return string
448 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200449 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Andrey Andreev46583072012-11-16 16:44:31 +0200451 return 'SELECT "column_name"
452 FROM "information_schema"."columns"
453 WHERE LOWER("table_name") = '.$this->escape(strtolower($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455
456 // --------------------------------------------------------------------
457
458 /**
Andrey Andreev46583072012-11-16 16:44:31 +0200459 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200461 * @param string $table
Andrey Andreev46583072012-11-16 16:44:31 +0200462 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 */
Andrey Andreev46583072012-11-16 16:44:31 +0200464 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Andrey Andreev46583072012-11-16 16:44:31 +0200466 if ($table === '')
467 {
468 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
469 }
470
471 $sql = 'SELECT "column_name", "data_type", "character_maximum_length", "numeric_precision", "column_default"
472 FROM "information_schema"."columns"
473 WHERE LOWER("table_name") = '.$this->escape(strtolower($table));
474
475 if (($query = $this->query($sql)) === FALSE)
476 {
477 return FALSE;
478 }
479 $query = $query->result_object();
480
481 $retval = array();
482 for ($i = 0, $c = count($query); $i < $c; $i++)
483 {
484 $retval[$i] = new stdClass();
485 $retval[$i]->name = $query[$i]->column_name;
486 $retval[$i]->type = $query[$i]->data_type;
487 $retval[$i]->max_length = ($query[$i]->character_maximum_length > 0) ? $query[$i]->character_maximum_length : $query[$i]->numeric_precision;
488 $retval[$i]->default = $query[$i]->column_default;
489 }
490
491 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493
494 // --------------------------------------------------------------------
495
496 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200497 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200499 * Returns an array containing code and message of the last
500 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200502 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200504 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200506 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508
509 // --------------------------------------------------------------------
510
511 /**
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200512 * ORDER BY
513 *
514 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +0200515 * @param string $direction ASC, DESC or RANDOM
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200516 * @param bool $escape
517 * @return object
518 */
519 public function order_by($orderby, $direction = '', $escape = NULL)
520 {
521 $direction = strtoupper(trim($direction));
522 if ($direction === 'RANDOM')
523 {
524 if ( ! is_float($orderby) && ctype_digit((string) $orderby))
525 {
526 $orderby = ($orderby > 1)
527 ? (float) '0.'.$orderby
528 : (float) $orderby;
529 }
530
531 if (is_float($orderby))
532 {
533 $this->simple_query('SET SEED '.$orderby);
534 }
535
536 $orderby = $this->_random_keyword[0];
537 $direction = '';
538 $escape = FALSE;
539 }
540
541 return parent::order_by($orderby, $direction, $escape);
542 }
543
544 // --------------------------------------------------------------------
545
546 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * Update statement
548 *
549 * Generates a platform-specific update string from the supplied data
550 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200551 * @param string $table
552 * @param array $values
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 * @return string
554 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300555 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300557 $this->qb_limit = FALSE;
558 $this->qb_orderby = array();
559 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 // --------------------------------------------------------------------
563
564 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300565 * Update_Batch statement
566 *
567 * Generates a platform-specific batch update string from the supplied data
568 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200569 * @param string $table Table name
570 * @param array $values Update data
571 * @param string $index WHERE key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300572 * @return string
573 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300574 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300575 {
576 $ids = array();
577 foreach ($values as $key => $val)
578 {
579 $ids[] = $val[$index];
580
581 foreach (array_keys($val) as $field)
582 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100583 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300584 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200585 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
Andrey Andreevd06acd82012-05-25 00:29:09 +0300586 }
587 }
588 }
589
590 $cases = '';
591 foreach ($final as $k => $v)
592 {
aroche7f875b72012-07-16 18:22:08 +0300593 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300594 .implode("\n", $v)."\n"
595 .'ELSE '.$k.' END), ';
596 }
597
Andrey Andreevb0478652012-07-18 15:34:46 +0300598 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
599
Andrey Andreevd40459d2012-07-18 16:46:39 +0300600 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Andrey Andreevd06acd82012-05-25 00:29:09 +0300601 }
602
603 // --------------------------------------------------------------------
604
605 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 * Delete statement
607 *
608 * Generates a platform-specific delete string from the supplied data
609 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200610 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200612 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300613 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300615 $this->qb_limit = FALSE;
616 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 }
618
619 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200622 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 *
624 * Generates a platform-specific LIMIT clause
625 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200626 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 * @return string
628 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300629 protected function _limit($sql)
Barry Mienydd671972010-10-04 16:33:58 +0200630 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300631 return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 }
633
634 // --------------------------------------------------------------------
635
636 /**
637 * Close DB Connection
638 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 * @return void
640 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300641 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300643 @pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 }
645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646}
647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300649/* Location: ./system/database/drivers/postgre/postgre_driver.php */