blob: 7fb258abb32fca11def50c13bde04eaca46c2143 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
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 Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020051 * @link https://codeigniter.com/user_guide/database/
Derek Allard2067d1a2008-11-13 22:59:24 +000052 */
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 {
Andrey Andreev62b655b2017-03-20 09:14:14 +0200133 if (isset($this->$key) && is_string($this->$key) && $this->$key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
Andrey Andreev62b655b2017-03-20 09:14:14 +0200135 $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 Andreev71f00992014-11-13 15:16:09 +0200152 $this->conn_id = ($persistent === TRUE)
153 ? pg_pconnect($this->dsn)
154 : pg_connect($this->dsn);
Andrey Andreev0259d122012-12-03 14:55:56 +0200155
Andrey Andreev71f00992014-11-13 15:16:09 +0200156 if ($this->conn_id !== FALSE)
Andrey Andreev0259d122012-12-03 14:55:56 +0200157 {
Andrey Andreev71f00992014-11-13 15:16:09 +0200158 if ($persistent === TRUE
159 && pg_connection_status($this->conn_id) === PGSQL_CONNECTION_BAD
160 && pg_ping($this->conn_id) === FALSE
161 )
162 {
163 return FALSE;
164 }
165
166 empty($this->schema) OR $this->simple_query('SET search_path TO '.$this->schema.',public');
Andrey Andreev0259d122012-12-03 14:55:56 +0200167 }
168
Andrey Andreevc07c4852012-12-03 16:42:10 +0200169 return $this->conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 }
171
172 // --------------------------------------------------------------------
173
174 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000175 * Reconnect
176 *
177 * Keep / reestablish the db connection if no queries have been
178 * sent for a length of time exceeding the server's idle timeout
179 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000180 * @return void
181 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200182 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000183 {
184 if (pg_ping($this->conn_id) === FALSE)
185 {
186 $this->conn_id = FALSE;
187 }
188 }
189
190 // --------------------------------------------------------------------
191
192 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * Set client character set
194 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200195 * @param string $charset
Andrey Andreev214583f2012-01-26 16:39:09 +0200196 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200198 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200200 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
202
203 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200206 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 * @return string
209 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200210 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200212 if (isset($this->data_cache['version']))
213 {
214 return $this->data_cache['version'];
215 }
216
Andrey Andreev2b730372012-11-05 17:01:11 +0200217 if ( ! $this->conn_id OR ($pg_version = pg_version($this->conn_id)) === FALSE)
Andrey Andreev08856b82012-03-03 03:19:28 +0200218 {
219 return FALSE;
220 }
221
222 /* If PHP was compiled with PostgreSQL lib versions earlier
223 * than 7.4, pg_version() won't return the server version
224 * and so we'll have to fall back to running a query in
225 * order to get it.
226 */
227 return isset($pg_version['server'])
228 ? $this->data_cache['version'] = $pg_version['server']
229 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
231
232 // --------------------------------------------------------------------
233
234 /**
235 * Execute the query
236 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200237 * @param string $sql an SQL query
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200239 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200240 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300242 return pg_query($this->conn_id, $sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 // --------------------------------------------------------------------
246
247 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * Begin Transaction
249 *
Barry Mienydd671972010-10-04 16:33:58 +0200250 * @return bool
251 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300252 protected function _trans_begin()
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300254 return (bool) pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Commit Transaction
261 *
Barry Mienydd671972010-10-04 16:33:58 +0200262 * @return bool
263 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300264 protected function _trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300266 return (bool) pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
268
269 // --------------------------------------------------------------------
270
271 /**
272 * Rollback Transaction
273 *
Barry Mienydd671972010-10-04 16:33:58 +0200274 * @return bool
275 */
Andrey Andreeva7d4aba2015-10-19 14:39:44 +0300276 protected function _trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300278 return (bool) pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280
281 // --------------------------------------------------------------------
282
283 /**
Andrey Andreevd5ab75e2013-07-17 20:28:48 +0300284 * Determines if a query is a "write" type.
285 *
286 * @param string An SQL query string
287 * @return bool
288 */
289 public function is_write_type($sql)
290 {
Andrey Andreev442ea682016-09-16 11:51:25 +0300291 if (preg_match('#^(INSERT|UPDATE).*RETURNING\s.+(\,\s?.+)*$#is', $sql))
Andrey Andreev0ca9ae62016-01-06 14:51:27 +0200292 {
293 return FALSE;
294 }
295
296 return parent::is_write_type($sql);
Andrey Andreevd5ab75e2013-07-17 20:28:48 +0300297 }
298
299 // --------------------------------------------------------------------
300
301 /**
Andrey Andreev71d8f722017-01-17 12:01:00 +0200302 * Platform-dependent string escape
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 *
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200304 * @param string
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 * @return string
306 */
Andrey Andreev0b6a4922013-01-10 16:53:44 +0200307 protected function _escape_str($str)
Derek Jonese4ed5832009-02-20 21:44:59 +0000308 {
Andrey Andreeva9346aa2013-09-13 16:03:07 +0300309 return pg_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // --------------------------------------------------------------------
313
314 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700315 * "Smart" Escape String
316 *
317 * Escapes data based on type
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700318 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200319 * @param string $str
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700320 * @return mixed
321 */
322 public function escape($str)
323 {
Andrey Andreeva9346aa2013-09-13 16:03:07 +0300324 if (is_php('5.4.4') && (is_string($str) OR (is_object($str) && method_exists($str, '__toString'))))
325 {
326 return pg_escape_literal($this->conn_id, $str);
327 }
328 elseif (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700329 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700330 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700331 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700332
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700333 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700334 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300335
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700336 // --------------------------------------------------------------------
337
338 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 * Affected Rows
340 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200341 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200343 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300345 return pg_affected_rows($this->result_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // --------------------------------------------------------------------
349
350 /**
351 * Insert ID
352 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200353 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200355 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200357 $v = pg_version($this->conn_id);
358 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200359
Andrey Andreev214583f2012-01-26 16:39:09 +0200360 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
361 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200362
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100363 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200365 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100367 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100369 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200370 {
371 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
372 $query = $this->query($sql);
373 $query = $query->row();
374 $seq = $query->seq;
375 }
376 else
377 {
378 // seq_name passed in table parameter
379 $seq = $table;
380 }
381
382 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
384 else
385 {
386 return pg_last_oid($this->result_id);
387 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200390 $query = $query->row();
391 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
393
394 // --------------------------------------------------------------------
395
396 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * Show table query
398 *
399 * Generates a platform-specific query string so that the table names can be fetched
400 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200401 * @param bool $prefix_limit
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 * @return string
403 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200404 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200405 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300406 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \''.$this->schema."'";
Barry Mienydd671972010-10-04 16:33:58 +0200407
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100408 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300410 return $sql.' AND "table_name" LIKE \''
411 .$this->escape_like_str($this->dbprefix)."%' "
412 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 return $sql;
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 // --------------------------------------------------------------------
419
420 /**
Andrey Andreev46583072012-11-16 16:44:31 +0200421 * List column query
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 *
423 * Generates a platform-specific query string so that the column names can be fetched
424 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200425 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 * @return string
427 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200428 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
Andrey Andreev46583072012-11-16 16:44:31 +0200430 return 'SELECT "column_name"
431 FROM "information_schema"."columns"
432 WHERE LOWER("table_name") = '.$this->escape(strtolower($table));
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434
435 // --------------------------------------------------------------------
436
437 /**
Andrey Andreev46583072012-11-16 16:44:31 +0200438 * Returns an object with field data
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200440 * @param string $table
Andrey Andreev46583072012-11-16 16:44:31 +0200441 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 */
Andrey Andreev5350f052015-01-12 12:33:37 +0200443 public function field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Andrey Andreev46583072012-11-16 16:44:31 +0200445 $sql = 'SELECT "column_name", "data_type", "character_maximum_length", "numeric_precision", "column_default"
446 FROM "information_schema"."columns"
447 WHERE LOWER("table_name") = '.$this->escape(strtolower($table));
448
449 if (($query = $this->query($sql)) === FALSE)
450 {
451 return FALSE;
452 }
453 $query = $query->result_object();
454
455 $retval = array();
456 for ($i = 0, $c = count($query); $i < $c; $i++)
457 {
458 $retval[$i] = new stdClass();
459 $retval[$i]->name = $query[$i]->column_name;
460 $retval[$i]->type = $query[$i]->data_type;
461 $retval[$i]->max_length = ($query[$i]->character_maximum_length > 0) ? $query[$i]->character_maximum_length : $query[$i]->numeric_precision;
462 $retval[$i]->default = $query[$i]->column_default;
463 }
464
465 return $retval;
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
467
468 // --------------------------------------------------------------------
469
470 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200471 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200473 * Returns an array containing code and message of the last
Andrey Andreev71d8f722017-01-17 12:01:00 +0200474 * database error that has occurred.
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200476 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200478 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200480 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482
483 // --------------------------------------------------------------------
484
485 /**
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200486 * ORDER BY
487 *
488 * @param string $orderby
vlakoff9f8e2992013-09-08 14:05:04 +0200489 * @param string $direction ASC, DESC or RANDOM
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200490 * @param bool $escape
491 * @return object
492 */
493 public function order_by($orderby, $direction = '', $escape = NULL)
494 {
495 $direction = strtoupper(trim($direction));
496 if ($direction === 'RANDOM')
497 {
498 if ( ! is_float($orderby) && ctype_digit((string) $orderby))
499 {
500 $orderby = ($orderby > 1)
501 ? (float) '0.'.$orderby
502 : (float) $orderby;
503 }
504
505 if (is_float($orderby))
506 {
507 $this->simple_query('SET SEED '.$orderby);
508 }
509
510 $orderby = $this->_random_keyword[0];
511 $direction = '';
512 $escape = FALSE;
513 }
514
515 return parent::order_by($orderby, $direction, $escape);
516 }
517
518 // --------------------------------------------------------------------
519
520 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 * Update statement
522 *
523 * Generates a platform-specific update string from the supplied data
524 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200525 * @param string $table
526 * @param array $values
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @return string
528 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300529 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300531 $this->qb_limit = FALSE;
532 $this->qb_orderby = array();
533 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 // --------------------------------------------------------------------
537
538 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300539 * Update_Batch statement
540 *
541 * Generates a platform-specific batch update string from the supplied data
542 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200543 * @param string $table Table name
544 * @param array $values Update data
545 * @param string $index WHERE key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300546 * @return string
547 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300548 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300549 {
550 $ids = array();
551 foreach ($values as $key => $val)
552 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +0200553 $ids[] = $val[$index]['value'];
Andrey Andreevd06acd82012-05-25 00:29:09 +0300554
555 foreach (array_keys($val) as $field)
556 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100557 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300558 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +0200559 $final[$val[$field]['field']][] = 'WHEN '.$val[$index]['value'].' THEN '.$val[$field]['value'];
Andrey Andreevd06acd82012-05-25 00:29:09 +0300560 }
561 }
562 }
563
564 $cases = '';
565 foreach ($final as $k => $v)
566 {
Andrey Andreev8338bbb2016-12-12 14:17:52 +0200567 $cases .= $k.' = (CASE '.$val[$index]['field']."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300568 .implode("\n", $v)."\n"
569 .'ELSE '.$k.' END), ';
570 }
571
Andrey Andreev8338bbb2016-12-12 14:17:52 +0200572 $this->where($val[$index]['field'].' IN('.implode(',', $ids).')', NULL, FALSE);
Andrey Andreevb0478652012-07-18 15:34:46 +0300573
Andrey Andreevd40459d2012-07-18 16:46:39 +0300574 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Andrey Andreevd06acd82012-05-25 00:29:09 +0300575 }
576
577 // --------------------------------------------------------------------
578
579 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * Delete statement
581 *
582 * Generates a platform-specific delete string from the supplied data
583 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200584 * @param string $table
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200586 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300587 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300589 $this->qb_limit = FALSE;
590 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 }
592
593 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200596 * LIMIT
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 *
598 * Generates a platform-specific LIMIT clause
599 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200600 * @param string $sql SQL Query
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 * @return string
602 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300603 protected function _limit($sql)
Barry Mienydd671972010-10-04 16:33:58 +0200604 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300605 return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 }
607
608 // --------------------------------------------------------------------
609
610 /**
611 * Close DB Connection
612 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 * @return void
614 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300615 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300617 pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 }
619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620}