blob: 15059f3d4ae2b95521958db4cf8c5d500ec68cb2 [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 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300135 return @pg_pconnect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 // --------------------------------------------------------------------
139
140 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000141 * Reconnect
142 *
143 * Keep / reestablish the db connection if no queries have been
144 * sent for a length of time exceeding the server's idle timeout
145 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000146 * @return void
147 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200148 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000149 {
150 if (pg_ping($this->conn_id) === FALSE)
151 {
152 $this->conn_id = FALSE;
153 }
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * Set client character set
160 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 * @param string
Andrey Andreev214583f2012-01-26 16:39:09 +0200162 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200164 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200166 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 }
168
169 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200172 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * @return string
175 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200176 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200178 if (isset($this->data_cache['version']))
179 {
180 return $this->data_cache['version'];
181 }
182
183 if (($pg_version = pg_version($this->conn_id)) === FALSE)
184 {
185 return FALSE;
186 }
187
188 /* If PHP was compiled with PostgreSQL lib versions earlier
189 * than 7.4, pg_version() won't return the server version
190 * and so we'll have to fall back to running a query in
191 * order to get it.
192 */
193 return isset($pg_version['server'])
194 ? $this->data_cache['version'] = $pg_version['server']
195 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Execute the query
202 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * @param string an SQL query
204 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200205 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200206 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 return @pg_query($this->conn_id, $sql);
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 // --------------------------------------------------------------------
212
213 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 * Begin Transaction
215 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200216 * @param bool
Barry Mienydd671972010-10-04 16:33:58 +0200217 * @return bool
218 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200219 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200222 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
224 return TRUE;
225 }
226
227 // Reset the transaction failure flag.
228 // If the $test_mode flag is set to TRUE transactions will be rolled back
229 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200230 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000231
Andrey Andreev214583f2012-01-26 16:39:09 +0200232 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
234
235 // --------------------------------------------------------------------
236
237 /**
238 * Commit Transaction
239 *
Barry Mienydd671972010-10-04 16:33:58 +0200240 * @return bool
241 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200242 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200245 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
247 return TRUE;
248 }
249
Andrey Andreev214583f2012-01-26 16:39:09 +0200250 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 }
252
253 // --------------------------------------------------------------------
254
255 /**
256 * Rollback Transaction
257 *
Barry Mienydd671972010-10-04 16:33:58 +0200258 * @return bool
259 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200260 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200263 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
265 return TRUE;
266 }
267
Andrey Andreev214583f2012-01-26 16:39:09 +0200268 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
270
271 // --------------------------------------------------------------------
272
273 /**
274 * Escape String
275 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000277 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * @return string
279 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200280 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000281 {
282 if (is_array($str))
283 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500284 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200285 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000286 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200287 }
288
289 return $str;
290 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000291
292 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Jonese4ed5832009-02-20 21:44:59 +0000294 // escape LIKE condition wildcards
295 if ($like === TRUE)
296 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200297 return str_replace(array($this->_like_escape_chr, '%', '_'),
298 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
299 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Jonese4ed5832009-02-20 21:44:59 +0000302 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 // --------------------------------------------------------------------
306
307 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700308 * "Smart" Escape String
309 *
310 * Escapes data based on type
311 * Sets boolean and null types
312 *
313 * @param string
314 * @return mixed
315 */
316 public function escape($str)
317 {
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700318 if (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700319 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700320 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700321 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700322
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700323 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700324 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300325
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700326 // --------------------------------------------------------------------
327
328 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * Affected Rows
330 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200331 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200333 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 return @pg_affected_rows($this->result_id);
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // --------------------------------------------------------------------
339
340 /**
341 * Insert ID
342 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200343 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200345 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200347 $v = pg_version($this->conn_id);
348 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200349
Andrey Andreev214583f2012-01-26 16:39:09 +0200350 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
351 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200352
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100353 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200355 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100357 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100359 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200360 {
361 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
362 $query = $this->query($sql);
363 $query = $query->row();
364 $seq = $query->seq;
365 }
366 else
367 {
368 // seq_name passed in table parameter
369 $seq = $table;
370 }
371
372 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
374 else
375 {
376 return pg_last_oid($this->result_id);
377 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200380 $query = $query->row();
381 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
383
384 // --------------------------------------------------------------------
385
386 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 * Show table query
388 *
389 * Generates a platform-specific query string so that the table names can be fetched
390 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200391 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * @return string
393 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200394 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200395 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300396 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \'public\'';
Barry Mienydd671972010-10-04 16:33:58 +0200397
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100398 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300400 return $sql.' AND "table_name" LIKE \''
401 .$this->escape_like_str($this->dbprefix)."%' "
402 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 return $sql;
406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // --------------------------------------------------------------------
409
410 /**
411 * Show column query
412 *
413 * Generates a platform-specific query string so that the column names can be fetched
414 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * @param string the table name
416 * @return string
417 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200418 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300420 return 'SELECT "column_name" FROM "information_schema"."columns" WHERE "table_name" = '.$this->escape($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Field data query
427 *
428 * Generates a platform-specific query so that the column data can be retrieved
429 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @param string the table name
Andrey Andreev214583f2012-01-26 16:39:09 +0200431 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200433 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200435 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437
438 // --------------------------------------------------------------------
439
440 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200441 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200443 * Returns an array containing code and message of the last
444 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200446 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200448 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200450 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
452
453 // --------------------------------------------------------------------
454
455 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 * From Tables
457 *
458 * This function implicitly groups FROM tables so there is no confusion
459 * about operator precedence in harmony with SQL standards
460 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200461 * @param array
462 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200464 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300466 return is_array($tables) ? implode(', ', $tables) : $tables;
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468
469 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * Update statement
473 *
474 * Generates a platform-specific update string from the supplied data
475 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @param string the table name
477 * @param array the update data
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 * @return string
479 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300480 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300482 $this->qb_limit = FALSE;
483 $this->qb_orderby = array();
484 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 // --------------------------------------------------------------------
488
489 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300490 * Update_Batch statement
491 *
492 * Generates a platform-specific batch update string from the supplied data
493 *
494 * @param string the table name
495 * @param array the update data
Andrey Andreevb0478652012-07-18 15:34:46 +0300496 * @param string the where key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300497 * @return string
498 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300499 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300500 {
501 $ids = array();
502 foreach ($values as $key => $val)
503 {
504 $ids[] = $val[$index];
505
506 foreach (array_keys($val) as $field)
507 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100508 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300509 {
510 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
511 }
512 }
513 }
514
515 $cases = '';
516 foreach ($final as $k => $v)
517 {
aroche7f875b72012-07-16 18:22:08 +0300518 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300519 .implode("\n", $v)."\n"
520 .'ELSE '.$k.' END), ';
521 }
522
Andrey Andreevb0478652012-07-18 15:34:46 +0300523 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
524
525 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_where();
Andrey Andreevd06acd82012-05-25 00:29:09 +0300526 }
527
528 // --------------------------------------------------------------------
529
530 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 * Delete statement
532 *
533 * Generates a platform-specific delete string from the supplied data
534 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 * @param string the table name
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200537 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300538 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300540 $this->qb_limit = FALSE;
541 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 }
543
544 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 /**
547 * Limit string
548 *
549 * Generates a platform-specific LIMIT clause
550 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 * @param string the sql query string
Andrey Andreev738497c2012-03-20 14:12:25 +0200552 * @param int the number of rows to limit the query to
553 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * @return string
555 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200556 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200557 {
Andrey Andreev2c35b642012-06-24 03:05:26 +0300558 return $sql.' LIMIT '.$limit.($offset ? ' OFFSET '.$offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 }
560
561 // --------------------------------------------------------------------
562
563 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700564 * Where
565 *
Andrey Andreevb0478652012-07-18 15:34:46 +0300566 * Called by where(), or_where()
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700567 *
568 * @param mixed
569 * @param mixed
570 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300571 * @param bool
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700572 * @return object
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700573 */
574 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
575 {
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700576 if ( ! is_array($key))
577 {
578 $key = array($key => $value);
579 }
580
581 // If the escape value was not set will will base it on the global setting
Andrey Andreev498c1e02012-06-16 03:34:10 +0300582 is_bool($escape) OR $escape = $this->_protect_identifiers;
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700583
584 foreach ($key as $k => $v)
585 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300586 $prefix = (count($this->qb_where) === 0 && count($this->qb_cache_where) === 0)
587 ? $this->_group_get_type('')
588 : $this->_group_get_type($type);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700589
590 if (is_null($v) && ! $this->_has_operator($k))
591 {
592 // value appears not to have been set, assign the test to IS NULL
593 $k .= ' IS NULL';
594 }
595
596 if ( ! is_null($v))
597 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300598 if (is_bool($v))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700599 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300600 $v = ' '.($v ? 'TRUE' : 'FALSE');
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700601 }
Andrey Andreevb0478652012-07-18 15:34:46 +0300602 elseif ($escape === TRUE)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700603 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300604 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700605 }
606
607 if ( ! $this->_has_operator($k))
608 {
609 $k .= ' = ';
610 }
611 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700612
Andrey Andreevb0478652012-07-18 15:34:46 +0300613 $this->qb_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700614 if ($this->qb_caching === TRUE)
615 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300616 // check this shit
617 $this->qb_cache_where[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700618 $this->qb_cache_exists[] = 'where';
619 }
620
621 }
622
623 return $this;
624 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300625
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700626 // --------------------------------------------------------------------
627
628 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * Close DB Connection
630 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * @return void
632 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300633 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300635 @pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 }
637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638}
639
Derek Allard2067d1a2008-11-13 22:59:24 +0000640/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300641/* Location: ./system/database/drivers/postgre/postgre_driver.php */