blob: 8c11c477b21dc4deeaf9b1d19b6efe2f1076e11d [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
Andrey Andreev738497c2012-03-20 14:12:25 +020051 protected $_random_keyword = ' RANDOM()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000052
53 /**
Andrey Andreev6192bc02012-03-26 12:32:32 +030054 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000055 *
Andrey Andreev6192bc02012-03-26 12:32:32 +030056 * Creates a DSN string to be used for db_connect() and db_pconnect()
57 *
58 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020059 */
Andrey Andreev6192bc02012-03-26 12:32:32 +030060 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000061 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030062 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020063
Andrey Andreev6192bc02012-03-26 12:32:32 +030064 if ( ! empty($this->dsn))
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030066 return;
67 }
68
69 $this->dsn === '' OR $this->dsn = '';
70
71 if (strpos($this->hostname, '/') !== FALSE)
72 {
73 // If UNIX sockets are used, we shouldn't set a port
74 $this->port = '';
75 }
76
Andrey Andreevab1d5682012-04-03 20:48:32 +030077 $this->hostname === '' OR $this->dsn = 'host='.$this->hostname.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030078
79 if ( ! empty($this->port) && ctype_digit($this->port))
80 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030081 $this->dsn .= 'port='.$this->port.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030082 }
83
84 if ($this->username !== '')
85 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030086 $this->dsn .= 'user='.$this->username.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030087
88 /* An empty password is valid!
89 *
90 * $db['password'] = NULL must be done in order to ignore it.
91 */
92 $this->password === NULL OR $this->dsn .= "password='".$this->password."' ";
93 }
94
95 $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' ';
96
97 /* We don't have these options as elements in our standard configuration
98 * array, but they might be set by parse_url() if the configuration was
99 * provided via string. Example:
100 *
101 * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
102 */
103 foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key)
104 {
105 if (isset($this->$key) && is_string($this->key) && $this->key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300107 $this->dsn .= $key."='".$this->key."' ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
109 }
Andrey Andreev6192bc02012-03-26 12:32:32 +0300110
111 $this->dsn = rtrim($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Non-persistent database connection
118 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200120 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200121 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200122 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300123 return @pg_connect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 }
125
126 // --------------------------------------------------------------------
127
128 /**
129 * Persistent database connection
130 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200132 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200133 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 {
rwillert82408522012-07-10 14:02:01 +0300135 $conn = @pg_pconnect($this->dsn);
136 if ($conn && pg_connection_status($conn) === PGSQL_CONNECTION_BAD)
137 {
138 if (pg_ping($conn) === FALSE)
139 {
140 return FALSE;
141 }
142 }
143 return $conn;
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
147
148 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000149 * Reconnect
150 *
151 * Keep / reestablish the db connection if no queries have been
152 * sent for a length of time exceeding the server's idle timeout
153 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000154 * @return void
155 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200156 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000157 {
158 if (pg_ping($this->conn_id) === FALSE)
159 {
160 $this->conn_id = FALSE;
161 }
162 }
163
164 // --------------------------------------------------------------------
165
166 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 * Set client character set
168 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * @param string
Andrey Andreev214583f2012-01-26 16:39:09 +0200170 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200172 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200174 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176
177 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200180 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 * @return string
183 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200184 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200186 if (isset($this->data_cache['version']))
187 {
188 return $this->data_cache['version'];
189 }
190
191 if (($pg_version = pg_version($this->conn_id)) === FALSE)
192 {
193 return FALSE;
194 }
195
196 /* If PHP was compiled with PostgreSQL lib versions earlier
197 * than 7.4, pg_version() won't return the server version
198 * and so we'll have to fall back to running a query in
199 * order to get it.
200 */
201 return isset($pg_version['server'])
202 ? $this->data_cache['version'] = $pg_version['server']
203 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
205
206 // --------------------------------------------------------------------
207
208 /**
209 * Execute the query
210 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 * @param string an SQL query
212 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200213 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200214 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 return @pg_query($this->conn_id, $sql);
217 }
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 // --------------------------------------------------------------------
220
221 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 * Begin Transaction
223 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200224 * @param bool
Barry Mienydd671972010-10-04 16:33:58 +0200225 * @return bool
226 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200227 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200230 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
232 return TRUE;
233 }
234
235 // Reset the transaction failure flag.
236 // If the $test_mode flag is set to TRUE transactions will be rolled back
237 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200238 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000239
Andrey Andreev214583f2012-01-26 16:39:09 +0200240 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Commit Transaction
247 *
Barry Mienydd671972010-10-04 16:33:58 +0200248 * @return bool
249 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200250 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200253 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 return TRUE;
256 }
257
Andrey Andreev214583f2012-01-26 16:39:09 +0200258 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260
261 // --------------------------------------------------------------------
262
263 /**
264 * Rollback Transaction
265 *
Barry Mienydd671972010-10-04 16:33:58 +0200266 * @return bool
267 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200268 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200271 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 return TRUE;
274 }
275
Andrey Andreev214583f2012-01-26 16:39:09 +0200276 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Escape String
283 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000285 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @return string
287 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200288 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000289 {
290 if (is_array($str))
291 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500292 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200293 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000294 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200295 }
296
297 return $str;
298 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000299
300 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Jonese4ed5832009-02-20 21:44:59 +0000302 // escape LIKE condition wildcards
303 if ($like === TRUE)
304 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200305 return str_replace(array($this->_like_escape_chr, '%', '_'),
306 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
307 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Jonese4ed5832009-02-20 21:44:59 +0000310 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // --------------------------------------------------------------------
314
315 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700316 * "Smart" Escape String
317 *
318 * Escapes data based on type
319 * Sets boolean and null types
320 *
321 * @param string
322 * @return mixed
323 */
324 public function escape($str)
325 {
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700326 if (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700327 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700328 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700329 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700330
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700331 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700332 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300333
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700334 // --------------------------------------------------------------------
335
336 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 * Affected Rows
338 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200339 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200341 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
343 return @pg_affected_rows($this->result_id);
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
347
348 /**
349 * Insert ID
350 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200351 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200353 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200355 $v = pg_version($this->conn_id);
356 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200357
Andrey Andreev214583f2012-01-26 16:39:09 +0200358 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
359 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200360
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100361 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200363 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100365 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100367 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200368 {
369 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
370 $query = $this->query($sql);
371 $query = $query->row();
372 $seq = $query->seq;
373 }
374 else
375 {
376 // seq_name passed in table parameter
377 $seq = $table;
378 }
379
380 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 }
382 else
383 {
384 return pg_last_oid($this->result_id);
385 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200388 $query = $query->row();
389 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
391
392 // --------------------------------------------------------------------
393
394 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 * Show table query
396 *
397 * Generates a platform-specific query string so that the table names can be fetched
398 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200399 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 * @return string
401 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200402 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200403 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300404 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \'public\'';
Barry Mienydd671972010-10-04 16:33:58 +0200405
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100406 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300408 return $sql.' AND "table_name" LIKE \''
409 .$this->escape_like_str($this->dbprefix)."%' "
410 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 return $sql;
414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 // --------------------------------------------------------------------
417
418 /**
419 * Show column query
420 *
421 * Generates a platform-specific query string so that the column names can be fetched
422 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * @param string the table name
424 * @return string
425 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200426 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300428 return 'SELECT "column_name" FROM "information_schema"."columns" WHERE "table_name" = '.$this->escape($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
430
431 // --------------------------------------------------------------------
432
433 /**
434 * Field data query
435 *
436 * Generates a platform-specific query so that the column data can be retrieved
437 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 * @param string the table name
Andrey Andreev214583f2012-01-26 16:39:09 +0200439 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200441 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200443 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445
446 // --------------------------------------------------------------------
447
448 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200449 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200451 * Returns an array containing code and message of the last
452 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200454 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200456 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200458 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
460
461 // --------------------------------------------------------------------
462
463 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 * From Tables
465 *
466 * This function implicitly groups FROM tables so there is no confusion
467 * about operator precedence in harmony with SQL standards
468 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200469 * @param array
470 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200472 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300474 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476
477 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * Update statement
481 *
482 * Generates a platform-specific update string from the supplied data
483 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @param string the table name
485 * @param array the update data
486 * @param array the where clause
Andrey Andreev00541ae2012-04-09 11:43:10 +0300487 * @param array the orderby clause (ignored)
488 * @param array the limit clause (ignored)
489 * @param array the like clause
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 * @return string
491 */
Andrey Andreev00541ae2012-04-09 11:43:10 +0300492 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE, $like = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500494 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Andrey Andreev00541ae2012-04-09 11:43:10 +0300496 $valstr[] = $key.' = '.$val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Andrey Andreev00541ae2012-04-09 11:43:10 +0300499 $where = empty($where) ? '' : ' WHERE '.implode(' ', $where);
Derek Allard2067d1a2008-11-13 22:59:24 +0000500
Andrey Andreev00541ae2012-04-09 11:43:10 +0300501 if ( ! empty($like))
502 {
503 $where .= ($where === '' ? ' WHERE ' : ' AND ').implode(' ', $like);
504 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000505
Andrey Andreev00541ae2012-04-09 11:43:10 +0300506 return 'UPDATE '.$table.' SET '.implode(', ', $valstr).$where;
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // --------------------------------------------------------------------
510
511 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300512 * Update_Batch statement
513 *
514 * Generates a platform-specific batch update string from the supplied data
515 *
516 * @param string the table name
517 * @param array the update data
518 * @param array the where clause
519 * @return string
520 */
521 protected function _update_batch($table, $values, $index, $where = NULL)
522 {
523 $ids = array();
524 foreach ($values as $key => $val)
525 {
526 $ids[] = $val[$index];
527
528 foreach (array_keys($val) as $field)
529 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100530 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300531 {
532 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
533 }
534 }
535 }
536
537 $cases = '';
538 foreach ($final as $k => $v)
539 {
aroche7f875b72012-07-16 18:22:08 +0300540 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300541 .implode("\n", $v)."\n"
542 .'ELSE '.$k.' END), ';
543 }
544
545 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2)
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100546 .' WHERE '.(($where !== '' && count($where) > 0) ? implode(' ', $where).' AND ' : '')
Andrey Andreevd06acd82012-05-25 00:29:09 +0300547 .$index.' IN('.implode(',', $ids).')';
548 }
549
550 // --------------------------------------------------------------------
551
552 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 * Delete statement
554 *
555 * Generates a platform-specific delete string from the supplied data
556 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 * @param string the table name
558 * @param array the where clause
Andrey Andreevc01d3162012-04-09 12:55:11 +0300559 * @param array the like clause
Andrey Andreeva2548362012-04-09 13:03:11 +0300560 * @param string the limit clause (ignored)
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200562 */
Andrey Andreeva2548362012-04-09 13:03:11 +0300563 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
Andrey Andreevc01d3162012-04-09 12:55:11 +0300565 $conditions = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000566
Andrey Andreevc01d3162012-04-09 12:55:11 +0300567 empty($where) OR $conditions[] = implode(' ', $where);
568 empty($like) OR $conditions[] = implode(' ', $like);
Derek Allard2067d1a2008-11-13 22:59:24 +0000569
Andrey Andreevc01d3162012-04-09 12:55:11 +0300570 return 'DELETE FROM '.$table.(count($conditions) > 0 ? ' WHERE '.implode(' AND ', $conditions) : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 }
572
573 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300574
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 /**
576 * Limit string
577 *
578 * Generates a platform-specific LIMIT clause
579 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 * @param string the sql query string
Andrey Andreev738497c2012-03-20 14:12:25 +0200581 * @param int the number of rows to limit the query to
582 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 * @return string
584 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200585 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200586 {
Andrey Andreev2c35b642012-06-24 03:05:26 +0300587 return $sql.' LIMIT '.$limit.($offset ? ' OFFSET '.$offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 }
589
590 // --------------------------------------------------------------------
591
592 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700593 * Where
594 *
595 * Called by where() or or_where()
596 *
597 * @param mixed
598 * @param mixed
599 * @param string
Andrey Andreev58803fb2012-06-24 00:45:37 +0300600 * @param mixed
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700601 * @return object
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700602 */
603 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
604 {
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700605 if ( ! is_array($key))
606 {
607 $key = array($key => $value);
608 }
609
610 // If the escape value was not set will will base it on the global setting
Andrey Andreev498c1e02012-06-16 03:34:10 +0300611 is_bool($escape) OR $escape = $this->_protect_identifiers;
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700612
613 foreach ($key as $k => $v)
614 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300615 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
616 ? $this->_group_get_type('')
617 : $this->_group_get_type($type);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700618
Andrey Andreev35443c62012-06-25 15:34:30 +0300619 if ($escape === TRUE)
620 {
621 $k = (($op = $this->_get_operator($k)) !== FALSE)
Andrey Andreev40f14042012-06-25 17:54:22 +0300622 ? $this->escape_identifiers(trim(substr($k, 0, strpos($k, $op)))).' '.strstr($k, $op)
623 : $this->escape_identifiers(trim($k));
Andrey Andreev35443c62012-06-25 15:34:30 +0300624 }
Andrey Andreev9c14f652012-06-10 14:35:07 +0300625
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700626 if (is_null($v) && ! $this->_has_operator($k))
627 {
628 // value appears not to have been set, assign the test to IS NULL
629 $k .= ' IS NULL';
630 }
631
632 if ( ! is_null($v))
633 {
634 if ($escape === TRUE)
635 {
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700636 $v = ' '.$this->escape($v);
637 }
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700638 elseif (is_bool($v))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700639 {
Andrey Andreev242925b2012-05-15 13:03:51 +0300640 $v = ($v ? ' TRUE' : ' FALSE');
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700641 }
642
643 if ( ! $this->_has_operator($k))
644 {
645 $k .= ' = ';
646 }
647 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700648
649 $this->qb_where[] = $prefix.$k.$v;
650 if ($this->qb_caching === TRUE)
651 {
652 $this->qb_cache_where[] = $prefix.$k.$v;
653 $this->qb_cache_exists[] = 'where';
654 }
655
656 }
657
658 return $this;
659 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300660
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700661 // --------------------------------------------------------------------
662
663 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 * Close DB Connection
665 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 * @return void
667 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300668 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300670 @pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 }
672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673}
674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300676/* Location: ./system/database/drivers/postgre/postgre_driver.php */