blob: 02db8084e08a5660bab7e6848b63cc612d1099e0 [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
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 */
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 /**
134 * Non-persistent database connection
135 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200137 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200138 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200139 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300140 return @pg_connect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Persistent database connection
147 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200149 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200150 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
rwillert82408522012-07-10 14:02:01 +0300152 $conn = @pg_pconnect($this->dsn);
153 if ($conn && pg_connection_status($conn) === PGSQL_CONNECTION_BAD)
154 {
155 if (pg_ping($conn) === FALSE)
156 {
157 return FALSE;
158 }
159 }
160 return $conn;
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // --------------------------------------------------------------------
164
165 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000166 * Reconnect
167 *
168 * Keep / reestablish the db connection if no queries have been
169 * sent for a length of time exceeding the server's idle timeout
170 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000171 * @return void
172 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200173 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000174 {
175 if (pg_ping($this->conn_id) === FALSE)
176 {
177 $this->conn_id = FALSE;
178 }
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * Set client character set
185 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200186 * @param string $charset
Andrey Andreev214583f2012-01-26 16:39:09 +0200187 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200189 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200191 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
193
194 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200197 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 * @return string
200 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200201 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200203 if (isset($this->data_cache['version']))
204 {
205 return $this->data_cache['version'];
206 }
Andrey Andreev2b730372012-11-05 17:01:11 +0200207 elseif ( ! $this->conn_id)
208 {
209 $this->initialize();
210 }
Andrey Andreev08856b82012-03-03 03:19:28 +0200211
Andrey Andreev2b730372012-11-05 17:01:11 +0200212 if ( ! $this->conn_id OR ($pg_version = pg_version($this->conn_id)) === FALSE)
Andrey Andreev08856b82012-03-03 03:19:28 +0200213 {
214 return FALSE;
215 }
216
217 /* If PHP was compiled with PostgreSQL lib versions earlier
218 * than 7.4, pg_version() won't return the server version
219 * and so we'll have to fall back to running a query in
220 * order to get it.
221 */
222 return isset($pg_version['server'])
223 ? $this->data_cache['version'] = $pg_version['server']
224 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 }
226
227 // --------------------------------------------------------------------
228
229 /**
230 * Execute the query
231 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200232 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200234 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200235 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 return @pg_query($this->conn_id, $sql);
238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // --------------------------------------------------------------------
241
242 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 * Begin Transaction
244 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200245 * @param bool $test_mode
Barry Mienydd671972010-10-04 16:33:58 +0200246 * @return bool
247 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200248 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200251 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
253 return TRUE;
254 }
255
256 // Reset the transaction failure flag.
257 // If the $test_mode flag is set to TRUE transactions will be rolled back
258 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200259 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000260
Andrey Andreev214583f2012-01-26 16:39:09 +0200261 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Commit Transaction
268 *
Barry Mienydd671972010-10-04 16:33:58 +0200269 * @return bool
270 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200271 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200274 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 return TRUE;
277 }
278
Andrey Andreev214583f2012-01-26 16:39:09 +0200279 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281
282 // --------------------------------------------------------------------
283
284 /**
285 * Rollback Transaction
286 *
Barry Mienydd671972010-10-04 16:33:58 +0200287 * @return bool
288 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200289 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200292 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 return TRUE;
295 }
296
Andrey Andreev214583f2012-01-26 16:39:09 +0200297 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Escape String
304 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200305 * @param string $str
306 * @param bool $like Whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @return string
308 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200309 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000310 {
311 if (is_array($str))
312 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500313 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200314 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000315 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200316 }
317
318 return $str;
319 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000320
321 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Jonese4ed5832009-02-20 21:44:59 +0000323 // escape LIKE condition wildcards
324 if ($like === TRUE)
325 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200326 return str_replace(array($this->_like_escape_chr, '%', '_'),
327 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
328 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Jonese4ed5832009-02-20 21:44:59 +0000331 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
335
336 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700337 * "Smart" Escape String
338 *
339 * Escapes data based on type
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700340 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200341 * @param string $str
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700342 * @return mixed
343 */
344 public function escape($str)
345 {
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700346 if (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700347 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700348 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700349 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700350
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700351 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700352 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300353
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700354 // --------------------------------------------------------------------
355
356 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 * Affected Rows
358 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200359 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200361 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
363 return @pg_affected_rows($this->result_id);
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // --------------------------------------------------------------------
367
368 /**
369 * Insert ID
370 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200371 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200373 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200375 $v = pg_version($this->conn_id);
376 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200377
Andrey Andreev214583f2012-01-26 16:39:09 +0200378 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
379 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200380
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100381 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200383 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100385 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100387 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200388 {
389 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
390 $query = $this->query($sql);
391 $query = $query->row();
392 $seq = $query->seq;
393 }
394 else
395 {
396 // seq_name passed in table parameter
397 $seq = $table;
398 }
399
400 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402 else
403 {
404 return pg_last_oid($this->result_id);
405 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200408 $query = $query->row();
409 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
411
412 // --------------------------------------------------------------------
413
414 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * Show table query
416 *
417 * Generates a platform-specific query string so that the table names can be fetched
418 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200419 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 * @return string
421 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200422 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200423 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300424 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \''.$this->schema."'";
Barry Mienydd671972010-10-04 16:33:58 +0200425
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100426 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300428 return $sql.' AND "table_name" LIKE \''
429 .$this->escape_like_str($this->dbprefix)."%' "
430 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 return $sql;
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // --------------------------------------------------------------------
437
438 /**
Andrey Andreev46583072012-11-16 16:44:31 +0200439 * List column query
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 *
441 * Generates a platform-specific query string so that the column names can be fetched
442 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200443 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @return string
445 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200446 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Andrey Andreev46583072012-11-16 16:44:31 +0200448 return 'SELECT "column_name"
449 FROM "information_schema"."columns"
450 WHERE LOWER("table_name") = '.$this->escape(strtolower($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
452
453 // --------------------------------------------------------------------
454
455 /**
Andrey Andreev46583072012-11-16 16:44:31 +0200456 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200458 * @param string $table
Andrey Andreev46583072012-11-16 16:44:31 +0200459 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 */
Andrey Andreev46583072012-11-16 16:44:31 +0200461 public function field_data($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Andrey Andreev46583072012-11-16 16:44:31 +0200463 if ($table === '')
464 {
465 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
466 }
467
468 $sql = 'SELECT "column_name", "data_type", "character_maximum_length", "numeric_precision", "column_default"
469 FROM "information_schema"."columns"
470 WHERE LOWER("table_name") = '.$this->escape(strtolower($table));
471
472 if (($query = $this->query($sql)) === FALSE)
473 {
474 return FALSE;
475 }
476 $query = $query->result_object();
477
478 $retval = array();
479 for ($i = 0, $c = count($query); $i < $c; $i++)
480 {
481 $retval[$i] = new stdClass();
482 $retval[$i]->name = $query[$i]->column_name;
483 $retval[$i]->type = $query[$i]->data_type;
484 $retval[$i]->max_length = ($query[$i]->character_maximum_length > 0) ? $query[$i]->character_maximum_length : $query[$i]->numeric_precision;
485 $retval[$i]->default = $query[$i]->column_default;
486 }
487
488 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490
491 // --------------------------------------------------------------------
492
493 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200494 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200496 * Returns an array containing code and message of the last
497 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200499 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200501 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200503 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
505
506 // --------------------------------------------------------------------
507
508 /**
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200509 * ORDER BY
510 *
511 * @param string $orderby
512 * @param string $direction ASC or DESC
513 * @param bool $escape
514 * @return object
515 */
516 public function order_by($orderby, $direction = '', $escape = NULL)
517 {
518 $direction = strtoupper(trim($direction));
519 if ($direction === 'RANDOM')
520 {
521 if ( ! is_float($orderby) && ctype_digit((string) $orderby))
522 {
523 $orderby = ($orderby > 1)
524 ? (float) '0.'.$orderby
525 : (float) $orderby;
526 }
527
528 if (is_float($orderby))
529 {
530 $this->simple_query('SET SEED '.$orderby);
531 }
532
533 $orderby = $this->_random_keyword[0];
534 $direction = '';
535 $escape = FALSE;
536 }
537
538 return parent::order_by($orderby, $direction, $escape);
539 }
540
541 // --------------------------------------------------------------------
542
543 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 * Update statement
545 *
546 * Generates a platform-specific update string from the supplied data
547 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200548 * @param string $table
549 * @param array $values
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * @return string
551 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300552 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300554 $this->qb_limit = FALSE;
555 $this->qb_orderby = array();
556 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 }
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 // --------------------------------------------------------------------
560
561 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300562 * Update_Batch statement
563 *
564 * Generates a platform-specific batch update string from the supplied data
565 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200566 * @param string $table Table name
567 * @param array $values Update data
568 * @param string $index WHERE key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300569 * @return string
570 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300571 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300572 {
573 $ids = array();
574 foreach ($values as $key => $val)
575 {
576 $ids[] = $val[$index];
577
578 foreach (array_keys($val) as $field)
579 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100580 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300581 {
582 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
583 }
584 }
585 }
586
587 $cases = '';
588 foreach ($final as $k => $v)
589 {
aroche7f875b72012-07-16 18:22:08 +0300590 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300591 .implode("\n", $v)."\n"
592 .'ELSE '.$k.' END), ';
593 }
594
Andrey Andreevb0478652012-07-18 15:34:46 +0300595 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
596
Andrey Andreevd40459d2012-07-18 16:46:39 +0300597 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Andrey Andreevd06acd82012-05-25 00:29:09 +0300598 }
599
600 // --------------------------------------------------------------------
601
602 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 * Delete statement
604 *
605 * Generates a platform-specific delete string from the supplied data
606 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200607 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200609 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300610 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300612 $this->qb_limit = FALSE;
613 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 }
615
616 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200619 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 *
621 * Generates a platform-specific LIMIT clause
622 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200623 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 * @return string
625 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300626 protected function _limit($sql)
Barry Mienydd671972010-10-04 16:33:58 +0200627 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300628 return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 }
630
631 // --------------------------------------------------------------------
632
633 /**
634 * Close DB Connection
635 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * @return void
637 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300638 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300640 @pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 }
642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643}
644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300646/* Location: ./system/database/drivers/postgre/postgre_driver.php */