blob: 96ea5f4f99a0b4c55ce1de9e4c9f3a312219f2a2 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev738497c2012-03-20 14:12:25 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev738497c2012-03-20 14:12:25 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Postgre Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelow7efad202012-02-19 12:37:00 +000033 * creates dynamically based on whether the query builder
Derek Allard2067d1a2008-11-13 22:59:24 +000034 * class is being used or not.
35 *
36 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050039 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000040 * @link http://codeigniter.com/user_guide/database/
41 */
42class CI_DB_postgre_driver extends CI_DB {
43
Andrey Andreev738497c2012-03-20 14:12:25 +020044 public $dbdriver = 'postgre';
Barry Mienydd671972010-10-04 16:33:58 +020045
Andrey Andreev738497c2012-03-20 14:12:25 +020046 protected $_escape_char = '"';
Derek Allard2067d1a2008-11-13 22:59:24 +000047
Andrey Andreev738497c2012-03-20 14:12:25 +020048 protected $_random_keyword = ' RANDOM()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000049
50 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030051 * Database schema
52 *
53 * @var string
Andrey Andreev485a3482012-10-27 03:22:43 +030054 */
55 public $schema = 'public';
56
57 /**
Andrey Andreev6192bc02012-03-26 12:32:32 +030058 * Constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000059 *
Andrey Andreev6192bc02012-03-26 12:32:32 +030060 * Creates a DSN string to be used for db_connect() and db_pconnect()
61 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030062 * @param array $params
Andrey Andreev6192bc02012-03-26 12:32:32 +030063 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020064 */
Andrey Andreev6192bc02012-03-26 12:32:32 +030065 public function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030067 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020068
Andrey Andreev6192bc02012-03-26 12:32:32 +030069 if ( ! empty($this->dsn))
Derek Allard2067d1a2008-11-13 22:59:24 +000070 {
Andrey Andreev6192bc02012-03-26 12:32:32 +030071 return;
72 }
73
74 $this->dsn === '' OR $this->dsn = '';
75
76 if (strpos($this->hostname, '/') !== FALSE)
77 {
78 // If UNIX sockets are used, we shouldn't set a port
79 $this->port = '';
80 }
81
Andrey Andreevab1d5682012-04-03 20:48:32 +030082 $this->hostname === '' OR $this->dsn = 'host='.$this->hostname.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030083
84 if ( ! empty($this->port) && ctype_digit($this->port))
85 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030086 $this->dsn .= 'port='.$this->port.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030087 }
88
89 if ($this->username !== '')
90 {
Andrey Andreevab1d5682012-04-03 20:48:32 +030091 $this->dsn .= 'user='.$this->username.' ';
Andrey Andreev6192bc02012-03-26 12:32:32 +030092
93 /* An empty password is valid!
94 *
95 * $db['password'] = NULL must be done in order to ignore it.
96 */
97 $this->password === NULL OR $this->dsn .= "password='".$this->password."' ";
98 }
99
100 $this->database === '' OR $this->dsn .= 'dbname='.$this->database.' ';
101
102 /* We don't have these options as elements in our standard configuration
103 * array, but they might be set by parse_url() if the configuration was
104 * provided via string. Example:
105 *
106 * postgre://username:password@localhost:5432/database?connect_timeout=5&sslmode=1
107 */
108 foreach (array('connect_timeout', 'options', 'sslmode', 'service') as $key)
109 {
110 if (isset($this->$key) && is_string($this->key) && $this->key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300112 $this->dsn .= $key."='".$this->key."' ";
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114 }
Andrey Andreev6192bc02012-03-26 12:32:32 +0300115
116 $this->dsn = rtrim($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Non-persistent database connection
123 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200125 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200126 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +0200127 {
Andrey Andreev6192bc02012-03-26 12:32:32 +0300128 return @pg_connect($this->dsn);
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Persistent database connection
135 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200137 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200138 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
rwillert82408522012-07-10 14:02:01 +0300140 $conn = @pg_pconnect($this->dsn);
141 if ($conn && pg_connection_status($conn) === PGSQL_CONNECTION_BAD)
142 {
143 if (pg_ping($conn) === FALSE)
144 {
145 return FALSE;
146 }
147 }
148 return $conn;
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 // --------------------------------------------------------------------
152
153 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000154 * Reconnect
155 *
156 * Keep / reestablish the db connection if no queries have been
157 * sent for a length of time exceeding the server's idle timeout
158 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000159 * @return void
160 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200161 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000162 {
163 if (pg_ping($this->conn_id) === FALSE)
164 {
165 $this->conn_id = FALSE;
166 }
167 }
168
169 // --------------------------------------------------------------------
170
171 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * Set client character set
173 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * @param string
Andrey Andreev214583f2012-01-26 16:39:09 +0200175 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 */
Andrey Andreev8e89df82012-03-03 03:48:12 +0200177 protected function _db_set_charset($charset)
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
Andrey Andreev8e89df82012-03-03 03:48:12 +0200179 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
181
182 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200185 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @return string
188 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200189 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200191 if (isset($this->data_cache['version']))
192 {
193 return $this->data_cache['version'];
194 }
195
196 if (($pg_version = pg_version($this->conn_id)) === FALSE)
197 {
198 return FALSE;
199 }
200
201 /* If PHP was compiled with PostgreSQL lib versions earlier
202 * than 7.4, pg_version() won't return the server version
203 * and so we'll have to fall back to running a query in
204 * order to get it.
205 */
206 return isset($pg_version['server'])
207 ? $this->data_cache['version'] = $pg_version['server']
208 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Execute the query
215 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 * @param string an SQL query
217 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200218 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200219 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 return @pg_query($this->conn_id, $sql);
222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
225
226 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 * Begin Transaction
228 *
Andrey Andreev214583f2012-01-26 16:39:09 +0200229 * @param bool
Barry Mienydd671972010-10-04 16:33:58 +0200230 * @return bool
231 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200232 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200235 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
237 return TRUE;
238 }
239
240 // Reset the transaction failure flag.
241 // If the $test_mode flag is set to TRUE transactions will be rolled back
242 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200243 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000244
Andrey Andreev214583f2012-01-26 16:39:09 +0200245 return (bool) @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247
248 // --------------------------------------------------------------------
249
250 /**
251 * Commit Transaction
252 *
Barry Mienydd671972010-10-04 16:33:58 +0200253 * @return bool
254 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200255 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200258 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 return TRUE;
261 }
262
Andrey Andreev214583f2012-01-26 16:39:09 +0200263 return (bool) @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265
266 // --------------------------------------------------------------------
267
268 /**
269 * Rollback Transaction
270 *
Barry Mienydd671972010-10-04 16:33:58 +0200271 * @return bool
272 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200273 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200276 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 return TRUE;
279 }
280
Andrey Andreev214583f2012-01-26 16:39:09 +0200281 return (bool) @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283
284 // --------------------------------------------------------------------
285
286 /**
287 * Escape String
288 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000290 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 * @return string
292 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200293 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000294 {
295 if (is_array($str))
296 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500297 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200298 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000299 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200300 }
301
302 return $str;
303 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000304
305 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Jonese4ed5832009-02-20 21:44:59 +0000307 // escape LIKE condition wildcards
308 if ($like === TRUE)
309 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200310 return str_replace(array($this->_like_escape_chr, '%', '_'),
311 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
312 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Jonese4ed5832009-02-20 21:44:59 +0000315 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // --------------------------------------------------------------------
319
320 /**
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700321 * "Smart" Escape String
322 *
323 * Escapes data based on type
324 * Sets boolean and null types
325 *
326 * @param string
327 * @return mixed
328 */
329 public function escape($str)
330 {
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700331 if (is_bool($str))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700332 {
Soesapto Joeni Hantorod2574db2012-05-15 16:28:16 +0700333 return ($str) ? 'TRUE' : 'FALSE';
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700334 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700335
Soesapto Joeni Hantorodabeaa12012-05-15 16:37:46 +0700336 return parent::escape($str);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700337 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300338
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700339 // --------------------------------------------------------------------
340
341 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 * Affected Rows
343 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200344 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200346 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 return @pg_affected_rows($this->result_id);
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // --------------------------------------------------------------------
352
353 /**
354 * Insert ID
355 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200356 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200358 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200360 $v = pg_version($this->conn_id);
361 $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4
Barry Mienydd671972010-10-04 16:33:58 +0200362
Andrey Andreev214583f2012-01-26 16:39:09 +0200363 $table = (func_num_args() > 0) ? func_get_arg(0) : NULL;
364 $column = (func_num_args() > 1) ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200365
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100366 if ($table === NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200368 $sql = 'SELECT LASTVAL() AS ins_id';
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100370 elseif ($table !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100372 if ($column !== NULL && $v >= '8.0')
Andrey Andreev214583f2012-01-26 16:39:09 +0200373 {
374 $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq";
375 $query = $this->query($sql);
376 $query = $query->row();
377 $seq = $query->seq;
378 }
379 else
380 {
381 // seq_name passed in table parameter
382 $seq = $table;
383 }
384
385 $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id";
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
387 else
388 {
389 return pg_last_oid($this->result_id);
390 }
Andrey Andreev214583f2012-01-26 16:39:09 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $query = $this->query($sql);
Andrey Andreev214583f2012-01-26 16:39:09 +0200393 $query = $query->row();
394 return (int) $query->ins_id;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
396
397 // --------------------------------------------------------------------
398
399 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 * Show table query
401 *
402 * Generates a platform-specific query string so that the table names can be fetched
403 *
Andrey Andreev485a3482012-10-27 03:22:43 +0300404 * @param bool $prefix_limit = FALSE
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @return string
406 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200407 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200408 {
Andrey Andreev485a3482012-10-27 03:22:43 +0300409 $sql = 'SELECT "table_name" FROM "information_schema"."tables" WHERE "table_schema" = \''.$this->schema."'";
Barry Mienydd671972010-10-04 16:33:58 +0200410
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100411 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300413 return $sql.' AND "table_name" LIKE \''
414 .$this->escape_like_str($this->dbprefix)."%' "
415 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 return $sql;
419 }
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // --------------------------------------------------------------------
422
423 /**
424 * Show column query
425 *
426 * Generates a platform-specific query string so that the column names can be fetched
427 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @param string the table name
429 * @return string
430 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200431 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
Andrey Andreev58803fb2012-06-24 00:45:37 +0300433 return 'SELECT "column_name" FROM "information_schema"."columns" WHERE "table_name" = '.$this->escape($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
435
436 // --------------------------------------------------------------------
437
438 /**
439 * Field data query
440 *
441 * Generates a platform-specific query so that the column data can be retrieved
442 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 * @param string the table name
Andrey Andreev214583f2012-01-26 16:39:09 +0200444 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200446 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Andrey Andreev214583f2012-01-26 16:39:09 +0200448 return 'SELECT * FROM '.$table.' LIMIT 1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450
451 // --------------------------------------------------------------------
452
453 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200454 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200456 * Returns an array containing code and message of the last
457 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200459 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200461 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200463 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
465
466 // --------------------------------------------------------------------
467
468 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * Update statement
470 *
471 * Generates a platform-specific update string from the supplied data
472 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * @param string the table name
474 * @param array the update data
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 * @return string
476 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300477 protected function _update($table, $values)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300479 $this->qb_limit = FALSE;
480 $this->qb_orderby = array();
481 return parent::_update($table, $values);
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 // --------------------------------------------------------------------
485
486 /**
Andrey Andreevd06acd82012-05-25 00:29:09 +0300487 * Update_Batch statement
488 *
489 * Generates a platform-specific batch update string from the supplied data
490 *
491 * @param string the table name
492 * @param array the update data
Andrey Andreevb0478652012-07-18 15:34:46 +0300493 * @param string the where key
Andrey Andreevd06acd82012-05-25 00:29:09 +0300494 * @return string
495 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300496 protected function _update_batch($table, $values, $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300497 {
498 $ids = array();
499 foreach ($values as $key => $val)
500 {
501 $ids[] = $val[$index];
502
503 foreach (array_keys($val) as $field)
504 {
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100505 if ($field !== $index)
Andrey Andreevd06acd82012-05-25 00:29:09 +0300506 {
507 $final[$field][] = 'WHEN '.$val[$index].' THEN '.$val[$field];
508 }
509 }
510 }
511
512 $cases = '';
513 foreach ($final as $k => $v)
514 {
aroche7f875b72012-07-16 18:22:08 +0300515 $cases .= $k.' = (CASE '.$index."\n"
Andrey Andreevd06acd82012-05-25 00:29:09 +0300516 .implode("\n", $v)."\n"
517 .'ELSE '.$k.' END), ';
518 }
519
Andrey Andreevb0478652012-07-18 15:34:46 +0300520 $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
521
Andrey Andreevd40459d2012-07-18 16:46:39 +0300522 return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
Andrey Andreevd06acd82012-05-25 00:29:09 +0300523 }
524
525 // --------------------------------------------------------------------
526
527 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 * Delete statement
529 *
530 * Generates a platform-specific delete string from the supplied data
531 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 * @param string the table name
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200534 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300535 protected function _delete($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300537 $this->qb_limit = FALSE;
538 return parent::_delete($table);
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 }
540
541 // --------------------------------------------------------------------
Andrey Andreev82e88b92012-04-06 21:18:59 +0300542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 /**
544 * Limit string
545 *
546 * Generates a platform-specific LIMIT clause
547 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 * @param string the sql query string
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 * @return string
550 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300551 protected function _limit($sql)
Barry Mienydd671972010-10-04 16:33:58 +0200552 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300553 return $sql.' LIMIT '.$this->qb_limit.($this->qb_offset ? ' OFFSET '.$this->qb_offset : '');
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
555
556 // --------------------------------------------------------------------
557
558 /**
Andrey Andreevd40459d2012-07-18 16:46:39 +0300559 * WHERE, HAVING
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700560 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300561 * Called by where(), or_where(), having(), or_having()
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700562 *
Andrey Andreevd40459d2012-07-18 16:46:39 +0300563 * @param string 'qb_where' or 'qb_having'
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700564 * @param mixed
565 * @param mixed
566 * @param string
Andrey Andreevb0478652012-07-18 15:34:46 +0300567 * @param bool
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700568 * @return object
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700569 */
Andrey Andreevd40459d2012-07-18 16:46:39 +0300570 protected function _wh($qb_key, $key, $value = NULL, $type = 'AND ', $escape = NULL)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700571 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300572 $qb_cache_key = ($qb_key === 'qb_having') ? 'qb_cache_having' : 'qb_cache_where';
573
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700574 if ( ! is_array($key))
575 {
576 $key = array($key => $value);
577 }
578
579 // If the escape value was not set will will base it on the global setting
Andrey Andreev498c1e02012-06-16 03:34:10 +0300580 is_bool($escape) OR $escape = $this->_protect_identifiers;
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700581
582 foreach ($key as $k => $v)
583 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300584 $prefix = (count($this->$qb_key) === 0 && count($this->$qb_cache_key) === 0)
Andrey Andreev58803fb2012-06-24 00:45:37 +0300585 ? $this->_group_get_type('')
586 : $this->_group_get_type($type);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700587
588 if (is_null($v) && ! $this->_has_operator($k))
589 {
590 // value appears not to have been set, assign the test to IS NULL
591 $k .= ' IS NULL';
592 }
593
594 if ( ! is_null($v))
595 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300596 if (is_bool($v))
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700597 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300598 $v = ' '.($v ? 'TRUE' : 'FALSE');
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700599 }
Andrey Andreevb0478652012-07-18 15:34:46 +0300600 elseif ($escape === TRUE)
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700601 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300602 $v = ' '.(is_int($v) ? $v : $this->escape($v));
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700603 }
604
605 if ( ! $this->_has_operator($k))
606 {
607 $k .= ' = ';
608 }
609 }
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700610
Andrey Andreevd40459d2012-07-18 16:46:39 +0300611 $this->{$qb_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700612 if ($this->qb_caching === TRUE)
613 {
Andrey Andreevd40459d2012-07-18 16:46:39 +0300614 $this->{$qb_cache_key}[] = array('condition' => $prefix.$k.$v, 'escape' => $escape);
615 $this->qb_cache_exists[] = substr($qb_key, 3);
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700616 }
617
618 }
619
620 return $this;
621 }
Andrey Andreev242925b2012-05-15 13:03:51 +0300622
Soesapto Joeni Hantorof25b9292012-05-15 08:10:31 +0700623 // --------------------------------------------------------------------
624
625 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 * Close DB Connection
627 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 * @return void
629 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300630 protected function _close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300632 @pg_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 }
634
Derek Allard2067d1a2008-11-13 22:59:24 +0000635}
636
Derek Allard2067d1a2008-11-13 22:59:24 +0000637/* End of file postgre_driver.php */
Andrey Andreev242925b2012-05-15 13:03:51 +0300638/* Location: ./system/database/drivers/postgre/postgre_driver.php */