blob: c380a7c4927578ce2d376b506879bab0b57d660e [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 Andreevf2818bd2014-02-25 15:26:20 +0200142 && ($this->conn_id = pg_pconnect($this->dsn))
Andrey Andreevc07c4852012-12-03 16:42:10 +0200143 && 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 Andreevf2818bd2014-02-25 15:26:20 +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 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000165 * Reconnect
166 *
167 * Keep / reestablish the db connection if no queries have been
168 * sent for a length of time exceeding the server's idle timeout
169 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000170 * @return void
171 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200172 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000173 {
174 if (pg_ping($this->conn_id) === FALSE)
175 {
176 $this->conn_id = FALSE;
177 }
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 * Set client character set
184 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200185 * @param string $charset
Andrey Andreev214583f2012-01-26 16:39:09 +0200186 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200188 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200190 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
192
193 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200196 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 * @return string
199 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200200 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200202 if (isset($this->data_cache['version']))
203 {
204 return $this->data_cache['version'];
205 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200206 elseif ( ! $this->conn_id)
207 {
208 $this->initialize();
209 }
Andrey Andreev08856b82012-03-03 03:19:28 +0200210
Andrey Andreev2b730372012-11-05 17:01:11 +0200211 if ( ! $this->conn_id OR ($pg_version = pg_version($this->conn_id)) === FALSE)
Andrey Andreev08856b82012-03-03 03:19:28 +0200212 {
213 return FALSE;
214 }
215
216 /* If PHP was compiled with PostgreSQL lib versions earlier
217 * than 7.4, pg_version() won't return the server version
218 * and so we'll have to fall back to running a query in
219 * order to get it.
220 */
221 return isset($pg_version['server'])
222 ? $this->data_cache['version'] = $pg_version['server']
223 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Execute the query
230 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200231 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200233 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200234 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300236 return pg_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 // --------------------------------------------------------------------
240
241 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * Begin Transaction
243 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200244 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200245 * @return bool
246 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200247 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200250 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 return TRUE;
253 }
254
255 // Reset the transaction failure flag.
256 // If the $test_mode flag is set to TRUE transactions will be rolled back
257 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200258 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000259
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300260 return (bool) pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Commit Transaction
267 *
Barry Mienydd671972010-10-04 16:33:58 +0200268 * @return bool
269 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200270 public function trans_commit()
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 Andreev2bbbd1a2014-05-09 10:24:14 +0300278 return (bool) pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * Rollback Transaction
285 *
Barry Mienydd671972010-10-04 16:33:58 +0200286 * @return bool
287 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200288 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200291 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
293 return TRUE;
294 }
295
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300296 return (bool) pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 }
298
299 // --------------------------------------------------------------------
300
301 /**
Andrey Andreevd5ab75e2013-07-17 20:28:48 +0300302 * Determines if a query is a "write" type.
303 *
304 * @param string An SQL query string
305 * @return bool
306 */
307 public function is_write_type($sql)
308 {
Andrey Andreevd98c4a32014-01-07 17:33:32 +0200309 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 +0300310 }
311
312 // --------------------------------------------------------------------
313
314 /**
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200315 * Platform-dependant string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200317 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 * @return string
319 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200320 protected function _escape_str($str)
Derek Jonese4ed5832009-02-20 21:44:59 +0000321 {
Andrey Andreeva9346aa2013-09-13 16:03:07 +0300322 return pg_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
326
327 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700328 * "Smart" Escape String
329 *
330 * Escapes data based on type
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700331 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200332 * @param string $str
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700333 * @return mixed
334 */
335 public function escape($str)
336 {
Andrey Andreeva9346aa2013-09-13 16:03:07 +0300337 if (is_php('5.4.4') && (is_string($str) OR (is_object($str) && method_exists($str, '__toString'))))
338 {
339 return pg_escape_literal($this->conn_id, $str);
340 }
341 elseif (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700342 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700343 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700344 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700345
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700346 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700347 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300348
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700349 // --------------------------------------------------------------------
350
351 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 * Affected Rows
353 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200354 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200356 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300358 return pg_affected_rows($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // --------------------------------------------------------------------
362
363 /**
364 * Insert ID
365 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200366 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200368 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200370 $v = pg_version($this->conn_id);
371 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200372
Andrey Andreev214583f2012-01-26 16:39:09 +0200373 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
374 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200375
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100376 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200378 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100380 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100382 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200383 {
384 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
385 $query = $this->query($sql);
386 $query = $query->row();
387 $seq = $query->seq;
388 }
389 else
390 {
391 // seq_name passed in table parameter
392 $seq = $table;
393 }
394
395 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397 else
398 {
399 return pg_last_oid($this->result_id);
400 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200403 $query = $query->row();
404 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
406
407 // --------------------------------------------------------------------
408
409 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * Show table query
411 *
412 * Generates a platform-specific query string so that the table names can be fetched
413 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200414 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * @return string
416 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200417 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200418 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300419 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \''.$this->schema."'";
Barry Mienydd671972010-10-04 16:33:58 +0200420
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100421 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300423 return $sql.' AND "table_name" LIKE \''
424 .$this->escape_like_str($this->dbprefix)."%' "
425 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 return $sql;
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // --------------------------------------------------------------------
432
433 /**
Andrey Andreev46583072012-11-16 16:44:31 +0200434 * List column query
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 *
436 * Generates a platform-specific query string so that the column names can be fetched
437 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200438 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @return string
440 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200441 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Andrey Andreev46583072012-11-16 16:44:31 +0200443 return 'SELECT "column_name"
444 FROM "information_schema"."columns"
445 WHERE LOWER("table_name") = '.$this->escape(strtolower($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447
448 // --------------------------------------------------------------------
449
450 /**
Andrey Andreev46583072012-11-16 16:44:31 +0200451 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200453 * @param string $table
Andrey Andreev46583072012-11-16 16:44:31 +0200454 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 */
Andrey Andreev46583072012-11-16 16:44:31 +0200456 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreev46583072012-11-16 16:44:31 +0200458 if ($table === '')
459 {
460 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
461 }
462
463 $sql = 'SELECT "column_name", "data_type", "character_maximum_length", "numeric_precision", "column_default"
464 FROM "information_schema"."columns"
465 WHERE LOWER("table_name") = '.$this->escape(strtolower($table));
466
467 if (($query = $this->query($sql)) === FALSE)
468 {
469 return FALSE;
470 }
471 $query = $query->result_object();
472
473 $retval = array();
474 for ($i = 0, $c = count($query); $i < $c; $i++)
475 {
476 $retval[$i] = new stdClass();
477 $retval[$i]->name = $query[$i]->column_name;
478 $retval[$i]->type = $query[$i]->data_type;
479 $retval[$i]->max_length = ($query[$i]->character_maximum_length > 0) ? $query[$i]->character_maximum_length : $query[$i]->numeric_precision;
480 $retval[$i]->default = $query[$i]->column_default;
481 }
482
483 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485
486 // --------------------------------------------------------------------
487
488 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200489 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200491 * Returns an array containing code and message of the last
492 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200494 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200496 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200498 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500
501 // --------------------------------------------------------------------
502
503 /**
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200504 * ORDER BY
505 *
506 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +0200507 * @param string $direction ASC, DESC or RANDOM
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200508 * @param bool $escape
509 * @return object
510 */
511 public function order_by($orderby, $direction = '', $escape = NULL)
512 {
513 $direction = strtoupper(trim($direction));
514 if ($direction === 'RANDOM')
515 {
516 if ( ! is_float($orderby) && ctype_digit((string) $orderby))
517 {
518 $orderby = ($orderby > 1)
519 ? (float) '0.'.$orderby
520 : (float) $orderby;
521 }
522
523 if (is_float($orderby))
524 {
525 $this->simple_query('SET SEED '.$orderby);
526 }
527
528 $orderby = $this->_random_keyword[0];
529 $direction = '';
530 $escape = FALSE;
531 }
532
533 return parent::order_by($orderby, $direction, $escape);
534 }
535
536 // --------------------------------------------------------------------
537
538 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 * Update statement
540 *
541 * Generates a platform-specific update string from the supplied data
542 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200543 * @param string $table
544 * @param array $values
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 * @return string
546 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300547 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300549 $this->qb_limit = FALSE;
550 $this->qb_orderby = array();
551 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 // --------------------------------------------------------------------
555
556 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300557 * Update_Batch statement
558 *
559 * Generates a platform-specific batch update string from the supplied data
560 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200561 * @param string $table Table name
562 * @param array $values Update data
563 * @param string $index WHERE key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300564 * @return string
565 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300566 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300567 {
568 $ids = array();
569 foreach ($values as $key => $val)
570 {
571 $ids[] = $val[$index];
572
573 foreach (array_keys($val) as $field)
574 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100575 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300576 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200577 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
Andrey Andreevd06acd82012-05-25 00:29:09 +0300578 }
579 }
580 }
581
582 $cases = '';
583 foreach ($final as $k => $v)
584 {
aroche7f875b72012-07-16 18:22:08 +0300585 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300586 .implode("\n", $v)."\n"
587 .'ELSE '.$k.' END), ';
588 }
589
Andrey Andreevb0478652012-07-18 15:34:46 +0300590 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
591
Andrey Andreevd40459d2012-07-18 16:46:39 +0300592 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Andrey Andreevd06acd82012-05-25 00:29:09 +0300593 }
594
595 // --------------------------------------------------------------------
596
597 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 * Delete statement
599 *
600 * Generates a platform-specific delete string from the supplied data
601 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200602 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200604 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300605 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300607 $this->qb_limit = FALSE;
608 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 }
610
611 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200614 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 *
616 * Generates a platform-specific LIMIT clause
617 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200618 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 * @return string
620 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300621 protected function _limit($sql)
Barry Mienydd671972010-10-04 16:33:58 +0200622 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300623 return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
625
626 // --------------------------------------------------------------------
627
628 /**
629 * Close DB Connection
630 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * @return void
632 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300633 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300635 pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638}
639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300641/* Location: ./system/database/drivers/postgre/postgre_driver.php */