blob: bdb8a712749792fa857e87e5f6c6eb58682be63b [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 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev738497c2012-03-20 14:12:25 +02008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreev738497c2012-03-20 14:12:25 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.3.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Postgre Database Adapter Class
42 *
43 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000044 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000045 * class is being used or not.
46 *
47 * @package CodeIgniter
48 * @subpackage Drivers
49 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050050 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000051 * @link http://codeigniter.com/user_guide/database/
52 */
53class CI_DB_postgre_driver extends CI_DB {
54
Andrey Andreeva24e52e2012-11-02 03:54:12 +020055 /**
56 * Database driver
57 *
58 * @var string
59 */
Andrey Andreev738497c2012-03-20 14:12:25 +020060 public $dbdriver = 'postgre';
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030063 * Database schema
64 *
65 * @var string
Andrey Andreev485a3482012-10-27 03:22:43 +030066 */
67 public $schema = 'public';
68
Andrey Andreeva24e52e2012-11-02 03:54:12 +020069 // --------------------------------------------------------------------
70
Andrey Andreev485a3482012-10-27 03:22:43 +030071 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +020072 * ORDER BY random keyword
73 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +020074 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +020075 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +020076 protected $_random_keyword = array('RANDOM()', 'RANDOM()');
Andrey Andreeva24e52e2012-11-02 03:54:12 +020077
78 // --------------------------------------------------------------------
79
80 /**
81 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000082 *
Andrey Andreev6192bc02012-03-26 12:32:32 +030083 * Creates a DSN string to be used for db_connect() and db_pconnect()
84 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030085 * @param array $params
Andrey Andreev6192bc02012-03-26 12:32:32 +030086 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020087 */
Andrey Andreev6192bc02012-03-26 12:32:32 +030088 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000089 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030090 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020091
Andrey Andreev6192bc02012-03-26 12:32:32 +030092 if ( ! empty($this->dsn))
Derek Allard2067d1a2008-11-13 22:59:24 +000093 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030094 return;
95 }
96
97 $this->dsn === '' OR $this->dsn = '';
98
99 if (strpos($this->hostname, '/') !== FALSE)
100 {
101 // If UNIX sockets are used, we shouldn't set a port
102 $this->port = '';
103 }
104
Andrey Andreevab1d5682012-04-03 20:48:32 +0300105 $this->hostname === '' OR $this->dsn = 'host='.$this->hostname.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +0300106
107 if ( ! empty($this->port) && ctype_digit($this->port))
108 {
Andrey Andreevab1d5682012-04-03 20:48:32 +0300109 $this->dsn .= 'port='.$this->port.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +0300110 }
111
112 if ($this->username !== '')
113 {
Andrey Andreevab1d5682012-04-03 20:48:32 +0300114 $this->dsn .= 'user='.$this->username.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +0300115
116 /* An empty password is valid!
117 *
118 * $db['password'] = NULL must be done in order to ignore it.
119 */
120 $this->password === NULL OR $this->dsn .= "password='".$this->password."' ";
121 }
122
123 $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' ';
124
125 /* We don't have these options as elements in our standard configuration
126 * array, but they might be set by parse_url() if the configuration was
127 * provided via string. Example:
128 *
129 * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
130 */
131 foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key)
132 {
133 if (isset($this->$key) && is_string($this->key) && $this->key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300135 $this->dsn .= $key."='".$this->key."' ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
137 }
Andrey Andreev6192bc02012-03-26 12:32:32 +0300138
139 $this->dsn = rtrim($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
141
142 // --------------------------------------------------------------------
143
144 /**
Andrey Andreev0259d122012-12-03 14:55:56 +0200145 * Database connection
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 *
Andrey Andreev0259d122012-12-03 14:55:56 +0200147 * @param bool $persistent
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200149 */
Andrey Andreev0259d122012-12-03 14:55:56 +0200150 public function db_connect($persistent = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200151 {
Andrey Andreev0259d122012-12-03 14:55:56 +0200152 if ($persistent === TRUE
Andrey Andreevf2818bd2014-02-25 15:26:20 +0200153 && ($this->conn_id = pg_pconnect($this->dsn))
Andrey Andreevc07c4852012-12-03 16:42:10 +0200154 && pg_connection_status($this->conn_id) === PGSQL_CONNECTION_BAD
155 && pg_ping($this->conn_id) === FALSE
Andrey Andreev0259d122012-12-03 14:55:56 +0200156 )
157 {
158 return FALSE;
159 }
160 else
161 {
Andrey Andreevf2818bd2014-02-25 15:26:20 +0200162 $this->conn_id = pg_connect($this->dsn);
Andrey Andreev0259d122012-12-03 14:55:56 +0200163 }
164
Andrey Andreevc07c4852012-12-03 16:42:10 +0200165 if ($this->conn_id && ! empty($this->schema))
Andrey Andreev0259d122012-12-03 14:55:56 +0200166 {
167 $this->simple_query('SET search_path TO '.$this->schema.',public');
168 }
169
Andrey Andreevc07c4852012-12-03 16:42:10 +0200170 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
172
173 // --------------------------------------------------------------------
174
175 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000176 * Reconnect
177 *
178 * Keep / reestablish the db connection if no queries have been
179 * sent for a length of time exceeding the server's idle timeout
180 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000181 * @return void
182 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200183 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000184 {
185 if (pg_ping($this->conn_id) === FALSE)
186 {
187 $this->conn_id = FALSE;
188 }
189 }
190
191 // --------------------------------------------------------------------
192
193 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 * Set client character set
195 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200196 * @param string $charset
Andrey Andreev214583f2012-01-26 16:39:09 +0200197 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200199 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200201 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200207 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 * @return string
210 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200211 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200213 if (isset($this->data_cache['version']))
214 {
215 return $this->data_cache['version'];
216 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200217 elseif ( ! $this->conn_id)
218 {
219 $this->initialize();
220 }
Andrey Andreev08856b82012-03-03 03:19:28 +0200221
Andrey Andreev2b730372012-11-05 17:01:11 +0200222 if ( ! $this->conn_id OR ($pg_version = pg_version($this->conn_id)) === FALSE)
Andrey Andreev08856b82012-03-03 03:19:28 +0200223 {
224 return FALSE;
225 }
226
227 /* If PHP was compiled with PostgreSQL lib versions earlier
228 * than 7.4, pg_version() won't return the server version
229 * and so we'll have to fall back to running a query in
230 * order to get it.
231 */
232 return isset($pg_version['server'])
233 ? $this->data_cache['version'] = $pg_version['server']
234 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Execute the query
241 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200242 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200244 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200245 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300247 return pg_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // --------------------------------------------------------------------
251
252 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * Begin Transaction
254 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200255 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200256 * @return bool
257 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200258 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200261 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 return TRUE;
264 }
265
266 // Reset the transaction failure flag.
267 // If the $test_mode flag is set to TRUE transactions will be rolled back
268 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200269 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000270
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300271 return (bool) pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Commit Transaction
278 *
Barry Mienydd671972010-10-04 16:33:58 +0200279 * @return bool
280 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200281 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200284 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 {
286 return TRUE;
287 }
288
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300289 return (bool) pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Rollback Transaction
296 *
Barry Mienydd671972010-10-04 16:33:58 +0200297 * @return bool
298 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200299 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200302 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 return TRUE;
305 }
306
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300307 return (bool) pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 }
309
310 // --------------------------------------------------------------------
311
312 /**
Andrey Andreevd5ab75e2013-07-17 20:28:48 +0300313 * Determines if a query is a "write" type.
314 *
315 * @param string An SQL query string
316 * @return bool
317 */
318 public function is_write_type($sql)
319 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +0200320 return (bool) preg_match('/^\s*"?(SET|INSERT(?![^\)]+\)\s+RETURNING)|UPDATE(?!.*\sRETURNING)|DELETE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s/i', str_replace(array("\r\n", "\r", "\n"), ' ', $sql));
Andrey Andreevd5ab75e2013-07-17 20:28:48 +0300321 }
322
323 // --------------------------------------------------------------------
324
325 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200326 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200328 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * @return string
330 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200331 protected function _escape_str($str)
Derek Jonese4ed5832009-02-20 21:44:59 +0000332 {
Andrey Andreeva9346aa2013-09-13 16:03:07 +0300333 return pg_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
337
338 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700339 * "Smart" Escape String
340 *
341 * Escapes data based on type
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700342 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200343 * @param string $str
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700344 * @return mixed
345 */
346 public function escape($str)
347 {
Andrey Andreeva9346aa2013-09-13 16:03:07 +0300348 if (is_php('5.4.4') && (is_string($str) OR (is_object($str) && method_exists($str, '__toString'))))
349 {
350 return pg_escape_literal($this->conn_id, $str);
351 }
352 elseif (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700353 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700354 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700355 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700356
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700357 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700358 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300359
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700360 // --------------------------------------------------------------------
361
362 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * Affected Rows
364 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200365 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200367 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300369 return pg_affected_rows($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // --------------------------------------------------------------------
373
374 /**
375 * Insert ID
376 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200377 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200379 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200381 $v = pg_version($this->conn_id);
382 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200383
Andrey Andreev214583f2012-01-26 16:39:09 +0200384 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
385 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200386
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100387 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200389 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100391 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100393 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200394 {
395 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
396 $query = $this->query($sql);
397 $query = $query->row();
398 $seq = $query->seq;
399 }
400 else
401 {
402 // seq_name passed in table parameter
403 $seq = $table;
404 }
405
406 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408 else
409 {
410 return pg_last_oid($this->result_id);
411 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200414 $query = $query->row();
415 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
417
418 // --------------------------------------------------------------------
419
420 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 * Show table query
422 *
423 * Generates a platform-specific query string so that the table names can be fetched
424 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200425 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @return string
427 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200428 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200429 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300430 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \''.$this->schema."'";
Barry Mienydd671972010-10-04 16:33:58 +0200431
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100432 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300434 return $sql.' AND "table_name" LIKE \''
435 .$this->escape_like_str($this->dbprefix)."%' "
436 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 return $sql;
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // --------------------------------------------------------------------
443
444 /**
Andrey Andreev46583072012-11-16 16:44:31 +0200445 * List column query
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 *
447 * Generates a platform-specific query string so that the column names can be fetched
448 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200449 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 * @return string
451 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200452 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
Andrey Andreev46583072012-11-16 16:44:31 +0200454 return 'SELECT "column_name"
455 FROM "information_schema"."columns"
456 WHERE LOWER("table_name") = '.$this->escape(strtolower($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 }
458
459 // --------------------------------------------------------------------
460
461 /**
Andrey Andreev46583072012-11-16 16:44:31 +0200462 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200464 * @param string $table
Andrey Andreev46583072012-11-16 16:44:31 +0200465 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 */
Andrey Andreev46583072012-11-16 16:44:31 +0200467 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
Andrey Andreev46583072012-11-16 16:44:31 +0200469 if ($table === '')
470 {
471 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
472 }
473
474 $sql = 'SELECT "column_name", "data_type", "character_maximum_length", "numeric_precision", "column_default"
475 FROM "information_schema"."columns"
476 WHERE LOWER("table_name") = '.$this->escape(strtolower($table));
477
478 if (($query = $this->query($sql)) === FALSE)
479 {
480 return FALSE;
481 }
482 $query = $query->result_object();
483
484 $retval = array();
485 for ($i = 0, $c = count($query); $i < $c; $i++)
486 {
487 $retval[$i] = new stdClass();
488 $retval[$i]->name = $query[$i]->column_name;
489 $retval[$i]->type = $query[$i]->data_type;
490 $retval[$i]->max_length = ($query[$i]->character_maximum_length > 0) ? $query[$i]->character_maximum_length : $query[$i]->numeric_precision;
491 $retval[$i]->default = $query[$i]->column_default;
492 }
493
494 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
496
497 // --------------------------------------------------------------------
498
499 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200500 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200502 * Returns an array containing code and message of the last
503 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200505 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200507 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200509 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511
512 // --------------------------------------------------------------------
513
514 /**
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200515 * ORDER BY
516 *
517 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +0200518 * @param string $direction ASC, DESC or RANDOM
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200519 * @param bool $escape
520 * @return object
521 */
522 public function order_by($orderby, $direction = '', $escape = NULL)
523 {
524 $direction = strtoupper(trim($direction));
525 if ($direction === 'RANDOM')
526 {
527 if ( ! is_float($orderby) && ctype_digit((string) $orderby))
528 {
529 $orderby = ($orderby > 1)
530 ? (float) '0.'.$orderby
531 : (float) $orderby;
532 }
533
534 if (is_float($orderby))
535 {
536 $this->simple_query('SET SEED '.$orderby);
537 }
538
539 $orderby = $this->_random_keyword[0];
540 $direction = '';
541 $escape = FALSE;
542 }
543
544 return parent::order_by($orderby, $direction, $escape);
545 }
546
547 // --------------------------------------------------------------------
548
549 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * Update statement
551 *
552 * Generates a platform-specific update string from the supplied data
553 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200554 * @param string $table
555 * @param array $values
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 * @return string
557 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300558 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300560 $this->qb_limit = FALSE;
561 $this->qb_orderby = array();
562 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000563 }
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 // --------------------------------------------------------------------
566
567 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300568 * Update_Batch statement
569 *
570 * Generates a platform-specific batch update string from the supplied data
571 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200572 * @param string $table Table name
573 * @param array $values Update data
574 * @param string $index WHERE key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300575 * @return string
576 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300577 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300578 {
579 $ids = array();
580 foreach ($values as $key => $val)
581 {
582 $ids[] = $val[$index];
583
584 foreach (array_keys($val) as $field)
585 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100586 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300587 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200588 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
Andrey Andreevd06acd82012-05-25 00:29:09 +0300589 }
590 }
591 }
592
593 $cases = '';
594 foreach ($final as $k => $v)
595 {
aroche7f875b72012-07-16 18:22:08 +0300596 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300597 .implode("\n", $v)."\n"
598 .'ELSE '.$k.' END), ';
599 }
600
Andrey Andreevb0478652012-07-18 15:34:46 +0300601 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
602
Andrey Andreevd40459d2012-07-18 16:46:39 +0300603 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Andrey Andreevd06acd82012-05-25 00:29:09 +0300604 }
605
606 // --------------------------------------------------------------------
607
608 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 * Delete statement
610 *
611 * Generates a platform-specific delete string from the supplied data
612 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200613 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200615 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300616 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300618 $this->qb_limit = FALSE;
619 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 }
621
622 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200625 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 *
627 * Generates a platform-specific LIMIT clause
628 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200629 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 * @return string
631 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300632 protected function _limit($sql)
Barry Mienydd671972010-10-04 16:33:58 +0200633 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300634 return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 }
636
637 // --------------------------------------------------------------------
638
639 /**
640 * Close DB Connection
641 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 * @return void
643 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300644 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000645 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300646 pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 }
648
Derek Allard2067d1a2008-11-13 22:59:24 +0000649}
650
Derek Allard2067d1a2008-11-13 22:59:24 +0000651/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300652/* Location: ./system/database/drivers/postgre/postgre_driver.php */