blob: 3bfccad0590d9335b75bc06272655785f22b32e3 [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
32 * creates dynamically based on whether the active record
33 * 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 /**
60 * Connection String
61 *
Derek Allard2067d1a2008-11-13 22:59:24 +000062 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020063 */
Andrey Andreev738497c2012-03-20 14:12:25 +020064 protected function _connect_string()
Derek Allard2067d1a2008-11-13 22:59:24 +000065 {
66 $components = array(
67 'hostname' => 'host',
68 'port' => 'port',
69 'database' => 'dbname',
70 'username' => 'user',
71 'password' => 'password'
72 );
Barry Mienydd671972010-10-04 16:33:58 +020073
Derek Allard2067d1a2008-11-13 22:59:24 +000074 $connect_string = "";
75 foreach ($components as $key => $val)
76 {
77 if (isset($this->$key) && $this->$key != '')
78 {
79 $connect_string .= " $val=".$this->$key;
80 }
81 }
82 return trim($connect_string);
83 }
84
85 // --------------------------------------------------------------------
86
87 /**
88 * Non-persistent database connection
89 *
Derek Allard2067d1a2008-11-13 22:59:24 +000090 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020091 */
Andrey Andreev738497c2012-03-20 14:12:25 +020092 public function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +020093 {
Derek Allard2067d1a2008-11-13 22:59:24 +000094 return @pg_connect($this->_connect_string());
95 }
96
97 // --------------------------------------------------------------------
98
99 /**
100 * Persistent database connection
101 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200103 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200104 public function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
106 return @pg_pconnect($this->_connect_string());
107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // --------------------------------------------------------------------
110
111 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000112 * Reconnect
113 *
114 * Keep / reestablish the db connection if no queries have been
115 * sent for a length of time exceeding the server's idle timeout
116 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000117 * @return void
118 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200119 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000120 {
121 if (pg_ping($this->conn_id) === FALSE)
122 {
123 $this->conn_id = FALSE;
124 }
125 }
126
127 // --------------------------------------------------------------------
128
129 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 * Select the database
131 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200133 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200134 public function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
136 // Not needed for Postgre so we'll return TRUE
137 return TRUE;
138 }
139
140 // --------------------------------------------------------------------
141
142 /**
Andrey Andreev8e89df82012-03-03 03:48:12 +0200143 * Set client character set
144 *
145 * @param string
146 * @return bool
147 */
148 protected function _db_set_charset($charset)
149 {
150 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200156 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @return string
159 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200160 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200162 if (isset($this->data_cache['version']))
163 {
164 return $this->data_cache['version'];
165 }
166
167 if (($pg_version = pg_version($this->conn_id)) === FALSE)
168 {
169 return FALSE;
170 }
171
172 /* If PHP was compiled with PostgreSQL lib versions earlier
173 * than 7.4, pg_version() won't return the server version
174 * and so we'll have to fall back to running a query in
175 * order to get it.
176 */
177 return isset($pg_version['server'])
178 ? $this->data_cache['version'] = $pg_version['server']
179 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Execute the query
186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @param string an SQL query
188 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200189 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200190 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 return @pg_query($this->conn_id, $sql);
193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // --------------------------------------------------------------------
196
197 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 * Begin Transaction
199 *
Barry Mienydd671972010-10-04 16:33:58 +0200200 * @return bool
201 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200202 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200205 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
207 return TRUE;
208 }
209
210 // Reset the transaction failure flag.
211 // If the $test_mode flag is set to TRUE transactions will be rolled back
212 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200213 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000214
Andrey Andreeva19beb02012-03-03 04:20:50 +0200215 return @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Commit Transaction
222 *
Barry Mienydd671972010-10-04 16:33:58 +0200223 * @return bool
224 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200225 public function trans_commit()
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
Andrey Andreeva19beb02012-03-03 04:20:50 +0200233 return @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Rollback Transaction
240 *
Barry Mienydd671972010-10-04 16:33:58 +0200241 * @return bool
242 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200243 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200246 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
248 return TRUE;
249 }
250
Andrey Andreeva19beb02012-03-03 04:20:50 +0200251 return @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
253
254 // --------------------------------------------------------------------
255
256 /**
257 * Escape String
258 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000260 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 * @return string
262 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200263 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000264 {
265 if (is_array($str))
266 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500267 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200268 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000269 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200270 }
271
272 return $str;
273 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000274
275 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Jonese4ed5832009-02-20 21:44:59 +0000277 // escape LIKE condition wildcards
278 if ($like === TRUE)
279 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200280 return str_replace(array($this->_like_escape_chr, '%', '_'),
281 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
282 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Jonese4ed5832009-02-20 21:44:59 +0000285 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // --------------------------------------------------------------------
289
290 /**
291 * Affected Rows
292 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200293 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200295 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
297 return @pg_affected_rows($this->result_id);
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
301
302 /**
303 * Insert ID
304 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200305 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200307 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200309 $v = $this->version();
Barry Mienydd671972010-10-04 16:33:58 +0200310
Pascal Kriete8761ef52011-02-14 13:13:52 -0500311 $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
312 $column = func_num_args() > 1 ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200313
Pascal Kriete8761ef52011-02-14 13:13:52 -0500314 if ($table == NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
316 $sql='SELECT LASTVAL() as ins_id';
317 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500318 elseif ($table != NULL && $column != NULL && $v >= '8.0')
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
320 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
321 $query = $this->query($sql);
322 $row = $query->row();
323 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
324 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500325 elseif ($table != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 // seq_name passed in table parameter
328 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
329 }
330 else
331 {
332 return pg_last_oid($this->result_id);
333 }
334 $query = $this->query($sql);
335 $row = $query->row();
336 return $row->ins_id;
337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * "Count All" query
343 *
344 * Generates a platform-specific query string that counts all records in
345 * the specified database
346 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 * @param string
348 * @return string
349 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200350 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 {
352 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000353 {
354 return 0;
355 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000356
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200357 $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 +0000358 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000359 {
360 return 0;
361 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000362
363 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500364 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000365 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
367
368 // --------------------------------------------------------------------
369
370 /**
371 * Show table query
372 *
373 * Generates a platform-specific query string so that the table names can be fetched
374 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200375 * @param bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 * @return string
377 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200378 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200379 {
380 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
383 {
Greg Aker0d424892010-01-26 02:14:44 +0000384 $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 +0000385 }
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 return $sql;
388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // --------------------------------------------------------------------
391
392 /**
393 * Show column query
394 *
395 * Generates a platform-specific query string so that the column names can be fetched
396 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @param string the table name
398 * @return string
399 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200400 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
402 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$table."'";
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Field data query
409 *
410 * Generates a platform-specific query so that the column data can be retrieved
411 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 * @param string the table name
413 * @return object
414 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200415 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 return "SELECT * FROM ".$table." LIMIT 1";
418 }
419
420 // --------------------------------------------------------------------
421
422 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200423 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200425 * Returns an array containing code and message of the last
426 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200428 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200430 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200432 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434
435 // --------------------------------------------------------------------
436
437 /**
438 * Escape the SQL Identifiers
439 *
440 * This function escapes column and table names
441 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 * @param string
443 * @return string
444 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200445 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 if ($this->_escape_char == '')
448 {
449 return $item;
450 }
451
452 foreach ($this->_reserved_identifiers as $id)
453 {
454 if (strpos($item, '.'.$id) !== FALSE)
455 {
Barry Mienydd671972010-10-04 16:33:58 +0200456 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // remove duplicates if the user already included the escape
459 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200460 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 if (strpos($item, '.') !== FALSE)
464 {
Barry Mienydd671972010-10-04 16:33:58 +0200465 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
467 else
468 {
469 $str = $this->_escape_char.$item.$this->_escape_char;
470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 // remove duplicates if the user already included the escape
473 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
474 }
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 // --------------------------------------------------------------------
477
478 /**
479 * From Tables
480 *
481 * This function implicitly groups FROM tables so there is no confusion
482 * about operator precedence in harmony with SQL standards
483 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200484 * @param array
485 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200487 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
489 if ( ! is_array($tables))
490 {
491 $tables = array($tables);
492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 return implode(', ', $tables);
495 }
496
497 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 /**
500 * Insert statement
501 *
502 * Generates a platform-specific insert string from the supplied data
503 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 * @param string the table name
505 * @param array the insert keys
506 * @param array the insert values
507 * @return string
508 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200509 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200510 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 // --------------------------------------------------------------------
515
516 /**
Greg Aker60ef4ea2011-04-27 01:45:38 -0500517 * Insert_batch statement
518 *
519 * Generates a platform-specific insert string from the supplied data
520 *
Greg Aker60ef4ea2011-04-27 01:45:38 -0500521 * @param string the table name
522 * @param array the insert keys
523 * @param array the insert values
524 * @return string
525 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200526 protected function _insert_batch($table, $keys, $values)
Greg Aker60ef4ea2011-04-27 01:45:38 -0500527 {
528 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
529 }
530
531 // --------------------------------------------------------------------
532
533 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 * Update statement
535 *
536 * Generates a platform-specific update string from the supplied data
537 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 * @param string the table name
539 * @param array the update data
540 * @param array the where clause
541 * @param array the orderby clause
542 * @param array the limit clause
543 * @return string
544 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200545 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500547 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
549 $valstr[] = $key." = ".$val;
550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
553
554 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 return $sql;
557 }
558
559 // --------------------------------------------------------------------
560
561 /**
562 * Truncate statement
563 *
564 * Generates a platform-specific truncate string from the supplied data
565 * If the database does not support the truncate() command
566 * This function maps to "DELETE FROM table"
567 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 * @param string the table name
569 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200570 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200571 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 {
573 return "TRUNCATE ".$table;
574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 // --------------------------------------------------------------------
577
578 /**
579 * Delete statement
580 *
581 * Generates a platform-specific delete string from the supplied data
582 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 * @param string the table name
584 * @param array the where clause
585 * @param string the limit clause
586 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200587 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200588 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
590 $conditions = '';
591
592 if (count($where) > 0 OR count($like) > 0)
593 {
594 $conditions = "\nWHERE ";
595 $conditions .= implode("\n", $this->ar_where);
596
597 if (count($where) > 0 && count($like) > 0)
598 {
599 $conditions .= " AND ";
600 }
601 $conditions .= implode("\n", $like);
602 }
603
SammyK1bf8eeb2012-03-05 16:56:06 -0600604 return "DELETE FROM ".$table.$conditions;
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 }
606
607 // --------------------------------------------------------------------
608 /**
609 * Limit string
610 *
611 * Generates a platform-specific LIMIT clause
612 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 * @param string the sql query string
Andrey Andreev738497c2012-03-20 14:12:25 +0200614 * @param int the number of rows to limit the query to
615 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 * @return string
617 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200618 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200619 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 $sql .= "LIMIT ".$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200621
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 if ($offset > 0)
623 {
624 $sql .= " OFFSET ".$offset;
625 }
Barry Mienydd671972010-10-04 16:33:58 +0200626
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 return $sql;
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * Close DB Connection
634 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 * @param resource
636 * @return void
637 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200638 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 {
640 @pg_close($conn_id);
641 }
Timothy Warren99c02ef2012-03-19 19:06:16 -0400642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643}
644
Derek Allard2067d1a2008-11-13 22:59:24 +0000645/* End of file postgre_driver.php */
Timothy Warrenbbea4052012-03-20 08:24:18 -0400646/* Location: ./system/database/drivers/postgre/postgre_driver.php */