blob: 30689cc70680089d17fc2917b4ccc21f92f62a3f [file] [log] [blame]
Andrey Andreev738497c2012-03-20 14:12:25 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Postgre Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000032 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000033 * class is being used or not.
34 *
35 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/database/
40 */
41class CI_DB_postgre_driver extends CI_DB {
42
Andrey Andreev738497c2012-03-20 14:12:25 +020043 public $dbdriver = 'postgre';
Barry Mienydd671972010-10-04 16:33:58 +020044
Andrey Andreev738497c2012-03-20 14:12:25 +020045 protected $_escape_char = '"';
Derek Allard2067d1a2008-11-13 22:59:24 +000046
Derek Jonese4ed5832009-02-20 21:44:59 +000047 // clause and character used for LIKE escape sequences
Andrey Andreev738497c2012-03-20 14:12:25 +020048 protected $_like_escape_str = " ESCAPE '%s' ";
49 protected $_like_escape_chr = '!';
Derek Jonese4ed5832009-02-20 21:44:59 +000050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 /**
52 * The syntax to count rows is slightly different across different
53 * database engines, so this string appears in each driver and is
54 * used for the count_all() and count_all_results() functions.
55 */
Andrey Andreev738497c2012-03-20 14:12:25 +020056 protected $_count_string = 'SELECT COUNT(*) AS ';
57 protected $_random_keyword = ' RANDOM()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000058
59 /**
Andrey Andreev6192bc02012-03-26 12:32:32 +030060 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000061 *
Andrey Andreev6192bc02012-03-26 12:32:32 +030062 * Creates a DSN string to be used for db_connect() and db_pconnect()
63 *
64 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020065 */
Andrey Andreev6192bc02012-03-26 12:32:32 +030066 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030068 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020069
Andrey Andreev6192bc02012-03-26 12:32:32 +030070 if ( ! empty($this->dsn))
Derek Allard2067d1a2008-11-13 22:59:24 +000071 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030072 return;
73 }
74
75 $this->dsn === '' OR $this->dsn = '';
76
77 if (strpos($this->hostname, '/') !== FALSE)
78 {
79 // If UNIX sockets are used, we shouldn't set a port
80 $this->port = '';
81 }
82
Andrey Andreevab1d5682012-04-03 20:48:32 +030083 $this->hostname === '' OR $this->dsn = 'host='.$this->hostname.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030084
85 if ( ! empty($this->port) && ctype_digit($this->port))
86 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030087 $this->dsn .= 'port='.$this->port.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030088 }
89
90 if ($this->username !== '')
91 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030092 $this->dsn .= 'user='.$this->username.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030093
94 /* An empty password is valid!
95 *
96 * $db['password'] = NULL must be done in order to ignore it.
97 */
98 $this->password === NULL OR $this->dsn .= "password='".$this->password."' ";
99 }
100
101 $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' ';
102
103 /* We don't have these options as elements in our standard configuration
104 * array, but they might be set by parse_url() if the configuration was
105 * provided via string. Example:
106 *
107 * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
108 */
109 foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key)
110 {
111 if (isset($this->$key) && is_string($this->key) && $this->key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300113 $this->dsn .= $key."='".$this->key."' ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
115 }
Andrey Andreev6192bc02012-03-26 12:32:32 +0300116
117 $this->dsn = rtrim($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Non-persistent database connection
124 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200126 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200127 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200128 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300129 return @pg_connect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Persistent database connection
136 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200138 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200139 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300141 return @pg_pconnect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // --------------------------------------------------------------------
145
146 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000147 * Reconnect
148 *
149 * Keep / reestablish the db connection if no queries have been
150 * sent for a length of time exceeding the server's idle timeout
151 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000152 * @return void
153 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200154 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000155 {
156 if (pg_ping($this->conn_id) === FALSE)
157 {
158 $this->conn_id = FALSE;
159 }
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 * Set client character set
166 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 * @param string
Andrey Andreev214583f2012-01-26 16:39:09 +0200168 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200170 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200172 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 }
174
175 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200178 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 * @return string
181 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200182 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200184 if (isset($this->data_cache['version']))
185 {
186 return $this->data_cache['version'];
187 }
188
189 if (($pg_version = pg_version($this->conn_id)) === FALSE)
190 {
191 return FALSE;
192 }
193
194 /* If PHP was compiled with PostgreSQL lib versions earlier
195 * than 7.4, pg_version() won't return the server version
196 * and so we'll have to fall back to running a query in
197 * order to get it.
198 */
199 return isset($pg_version['server'])
200 ? $this->data_cache['version'] = $pg_version['server']
201 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Execute the query
208 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 * @param string an SQL query
210 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200211 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200212 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 return @pg_query($this->conn_id, $sql);
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
218
219 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * Begin Transaction
221 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200222 * @param bool
Barry Mienydd671972010-10-04 16:33:58 +0200223 * @return bool
224 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200225 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200228 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
230 return TRUE;
231 }
232
233 // Reset the transaction failure flag.
234 // If the $test_mode flag is set to TRUE transactions will be rolled back
235 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200236 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000237
Andrey Andreev214583f2012-01-26 16:39:09 +0200238 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
240
241 // --------------------------------------------------------------------
242
243 /**
244 * Commit Transaction
245 *
Barry Mienydd671972010-10-04 16:33:58 +0200246 * @return bool
247 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200248 public function trans_commit()
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
Andrey Andreev214583f2012-01-26 16:39:09 +0200256 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Rollback Transaction
263 *
Barry Mienydd671972010-10-04 16:33:58 +0200264 * @return bool
265 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200266 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200269 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
271 return TRUE;
272 }
273
Andrey Andreev214583f2012-01-26 16:39:09 +0200274 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * Escape String
281 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000283 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * @return string
285 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200286 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000287 {
288 if (is_array($str))
289 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500290 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200291 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000292 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200293 }
294
295 return $str;
296 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000297
298 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Jonese4ed5832009-02-20 21:44:59 +0000300 // escape LIKE condition wildcards
301 if ($like === TRUE)
302 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200303 return str_replace(array($this->_like_escape_chr, '%', '_'),
304 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
305 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Jonese4ed5832009-02-20 21:44:59 +0000308 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
312
313 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700314 * "Smart" Escape String
315 *
316 * Escapes data based on type
317 * Sets boolean and null types
318 *
319 * @param string
320 * @return mixed
321 */
322 public function escape($str)
323 {
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700324 if (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700325 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700326 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700327 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700328
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700329 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700330 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300331
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700332 // --------------------------------------------------------------------
333
334 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 * Affected Rows
336 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200337 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200339 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
341 return @pg_affected_rows($this->result_id);
342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 // --------------------------------------------------------------------
345
346 /**
347 * Insert ID
348 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200349 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200351 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200353 $v = pg_version($this->conn_id);
354 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200355
Andrey Andreev214583f2012-01-26 16:39:09 +0200356 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
357 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200358
Pascal Kriete8761ef52011-02-14 13:13:52 -0500359 if ($table == NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200361 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500363 elseif ($table != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200365 if ($column != NULL && $v >= '8.0')
366 {
367 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
368 $query = $this->query($sql);
369 $query = $query->row();
370 $seq = $query->seq;
371 }
372 else
373 {
374 // seq_name passed in table parameter
375 $seq = $table;
376 }
377
378 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
380 else
381 {
382 return pg_last_oid($this->result_id);
383 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200386 $query = $query->row();
387 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * "Count All" query
394 *
395 * Generates a platform-specific query string that counts all records in
396 * the specified database
397 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * @param string
399 * @return string
400 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200401 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 {
403 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000404 {
405 return 0;
406 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000407
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200408 $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000410 {
411 return 0;
412 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000413
Andrey Andreev214583f2012-01-26 16:39:09 +0200414 $query = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500415 $this->_reset_select();
Andrey Andreev214583f2012-01-26 16:39:09 +0200416 return (int) $query->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 }
418
419 // --------------------------------------------------------------------
420
421 /**
422 * Show table query
423 *
424 * Generates a platform-specific query string so that the table names can be fetched
425 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200426 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @return string
428 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200429 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200430 {
431 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
432
Andrey Andreev214583f2012-01-26 16:39:09 +0200433 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200435 return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 return $sql;
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 // --------------------------------------------------------------------
442
443 /**
444 * Show column query
445 *
446 * Generates a platform-specific query string so that the column names can be fetched
447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param string the table name
449 * @return string
450 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200451 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200453 return "SELECT column_name FROM information_schema.columns WHERE table_name = '".$table."'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455
456 // --------------------------------------------------------------------
457
458 /**
459 * Field data query
460 *
461 * Generates a platform-specific query so that the column data can be retrieved
462 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * @param string the table name
Andrey Andreev214583f2012-01-26 16:39:09 +0200464 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200466 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200468 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
470
471 // --------------------------------------------------------------------
472
473 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200474 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200476 * Returns an array containing code and message of the last
477 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200479 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200481 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200483 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485
486 // --------------------------------------------------------------------
487
488 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 * From Tables
490 *
491 * This function implicitly groups FROM tables so there is no confusion
492 * about operator precedence in harmony with SQL standards
493 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200494 * @param array
495 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200497 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
499 if ( ! is_array($tables))
500 {
501 $tables = array($tables);
502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 return implode(', ', $tables);
505 }
506
507 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 * Update statement
511 *
512 * Generates a platform-specific update string from the supplied data
513 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 * @param string the table name
515 * @param array the update data
516 * @param array the where clause
Andrey Andreev00541ae2012-04-09 11:43:10 +0300517 * @param array the orderby clause (ignored)
518 * @param array the limit clause (ignored)
519 * @param array the like clause
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * @return string
521 */
Andrey Andreev00541ae2012-04-09 11:43:10 +0300522 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500524 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
Andrey Andreev00541ae2012-04-09 11:43:10 +0300526 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Andrey Andreev00541ae2012-04-09 11:43:10 +0300529 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000530
Andrey Andreev00541ae2012-04-09 11:43:10 +0300531 if ( ! empty($like))
532 {
533 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
534 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000535
Andrey Andreev00541ae2012-04-09 11:43:10 +0300536 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where;
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 // --------------------------------------------------------------------
540
541 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300542 * Update_Batch statement
543 *
544 * Generates a platform-specific batch update string from the supplied data
545 *
546 * @param string the table name
547 * @param array the update data
548 * @param array the where clause
549 * @return string
550 */
551 protected function _update_batch($table, $values, $index, $where = NULL)
552 {
553 $ids = array();
554 foreach ($values as $key => $val)
555 {
556 $ids[] = $val[$index];
557
558 foreach (array_keys($val) as $field)
559 {
560 if ($field != $index)
561 {
562 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
563 }
564 }
565 }
566
567 $cases = '';
568 foreach ($final as $k => $v)
569 {
570 $cases .= $k.' = (CASE '.$k."\n"
571 .implode("\n", $v)."\n"
572 .'ELSE '.$k.' END), ';
573 }
574
575 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
576 .' WHERE '.(($where != '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
577 .$index.' IN('.implode(',', $ids).')';
578 }
579
580 // --------------------------------------------------------------------
581
582 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 * Delete statement
584 *
585 * Generates a platform-specific delete string from the supplied data
586 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 * @param string the table name
588 * @param array the where clause
Andrey Andreevc01d3162012-04-09 12:55:11 +0300589 * @param array the like clause
Andrey Andreeva2548362012-04-09 13:03:11 +0300590 * @param string the limit clause (ignored)
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200592 */
Andrey Andreeva2548362012-04-09 13:03:11 +0300593 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 {
Andrey Andreevc01d3162012-04-09 12:55:11 +0300595 $conditions = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000596
Andrey Andreevc01d3162012-04-09 12:55:11 +0300597 empty($where) OR $conditions[] = implode(' ', $where);
598 empty($like) OR $conditions[] = implode(' ', $like);
Derek Allard2067d1a2008-11-13 22:59:24 +0000599
Andrey Andreevc01d3162012-04-09 12:55:11 +0300600 return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 }
602
603 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 /**
606 * Limit string
607 *
608 * Generates a platform-specific LIMIT clause
609 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 * @param string the sql query string
Andrey Andreev738497c2012-03-20 14:12:25 +0200611 * @param int the number of rows to limit the query to
612 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 * @return string
614 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200615 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200616 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200617 return $sql.' LIMIT '.$limit.($offset == 0 ? '' : ' OFFSET '.$offset);
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 }
619
620 // --------------------------------------------------------------------
621
622 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700623 * Where
624 *
625 * Called by where() or or_where()
626 *
627 * @param mixed
628 * @param mixed
629 * @param string
630 * @return object
631 *
632 */
633 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
634 {
635 $type = $this->_group_get_type($type);
636
637 if ( ! is_array($key))
638 {
639 $key = array($key => $value);
640 }
641
642 // If the escape value was not set will will base it on the global setting
643 if ( ! is_bool($escape))
644 {
645 $escape = $this->_protect_identifiers;
646 }
647
648 foreach ($key as $k => $v)
649 {
650 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0) ? '' : $type;
651
652 if (is_null($v) && ! $this->_has_operator($k))
653 {
654 // value appears not to have been set, assign the test to IS NULL
655 $k .= ' IS NULL';
656 }
657
658 if ( ! is_null($v))
659 {
660 if ($escape === TRUE)
661 {
662 $k = $this->protect_identifiers($k, FALSE, $escape);
663 $v = ' '.$this->escape($v);
664 }
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700665 elseif (is_bool($v))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700666 {
Andrey Andreev242925b2012-05-15 13:03:51 +0300667 $v = ($v ? ' TRUE' : ' FALSE');
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700668 }
669
670 if ( ! $this->_has_operator($k))
671 {
672 $k .= ' = ';
673 }
674 }
675 else
676 {
677 $k = $this->protect_identifiers($k, FALSE, $escape);
678 }
679
680 $this->qb_where[] = $prefix.$k.$v;
681 if ($this->qb_caching === TRUE)
682 {
683 $this->qb_cache_where[] = $prefix.$k.$v;
684 $this->qb_cache_exists[] = 'where';
685 }
686
687 }
688
689 return $this;
690 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300691
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700692 // --------------------------------------------------------------------
693
694 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 * Close DB Connection
696 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000697 * @return void
698 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300699 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300701 @pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 }
703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704}
705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300707/* Location: ./system/database/drivers/postgre/postgre_driver.php */