blob: 8112cf9defbc0fab490fc95b4a945c87dfa3990e [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 {
192 $sql = $this->_prep_query($sql);
193 return @pg_query($this->conn_id, $sql);
194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // --------------------------------------------------------------------
197
198 /**
199 * Prep the query
200 *
201 * If needed, each database adapter can prep the query string
202 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 * @param string an SQL query
204 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200205 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200206 protected function _prep_query($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
208 return $sql;
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Begin Transaction
215 *
Barry Mienydd671972010-10-04 16:33:58 +0200216 * @return bool
217 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200218 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200221 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 return TRUE;
224 }
225
226 // Reset the transaction failure flag.
227 // If the $test_mode flag is set to TRUE transactions will be rolled back
228 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200229 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000230
Andrey Andreeva19beb02012-03-03 04:20:50 +0200231 return @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
233
234 // --------------------------------------------------------------------
235
236 /**
237 * Commit Transaction
238 *
Barry Mienydd671972010-10-04 16:33:58 +0200239 * @return bool
240 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200241 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200244 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
246 return TRUE;
247 }
248
Andrey Andreeva19beb02012-03-03 04:20:50 +0200249 return @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Rollback Transaction
256 *
Barry Mienydd671972010-10-04 16:33:58 +0200257 * @return bool
258 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200259 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200262 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
264 return TRUE;
265 }
266
Andrey Andreeva19beb02012-03-03 04:20:50 +0200267 return @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * Escape String
274 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000276 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 * @return string
278 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200279 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000280 {
281 if (is_array($str))
282 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500283 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200284 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000285 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200286 }
287
288 return $str;
289 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000290
291 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Jonese4ed5832009-02-20 21:44:59 +0000293 // escape LIKE condition wildcards
294 if ($like === TRUE)
295 {
Andrey Andreev55201ac2012-03-20 14:20:39 +0200296 return str_replace(array($this->_like_escape_chr, '%', '_'),
297 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
298 $str);
Derek Jonese4ed5832009-02-20 21:44:59 +0000299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Jonese4ed5832009-02-20 21:44:59 +0000301 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // --------------------------------------------------------------------
305
306 /**
307 * Affected Rows
308 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200309 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200311 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 return @pg_affected_rows($this->result_id);
314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 // --------------------------------------------------------------------
317
318 /**
319 * Insert ID
320 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200321 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200323 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200325 $v = $this->version();
Barry Mienydd671972010-10-04 16:33:58 +0200326
Pascal Kriete8761ef52011-02-14 13:13:52 -0500327 $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
328 $column = func_num_args() > 1 ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200329
Pascal Kriete8761ef52011-02-14 13:13:52 -0500330 if ($table == NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $sql='SELECT LASTVAL() as ins_id';
333 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500334 elseif ($table != NULL && $column != NULL && $v >= '8.0')
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
337 $query = $this->query($sql);
338 $row = $query->row();
339 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
340 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500341 elseif ($table != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 {
343 // seq_name passed in table parameter
344 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
345 }
346 else
347 {
348 return pg_last_oid($this->result_id);
349 }
350 $query = $this->query($sql);
351 $row = $query->row();
352 return $row->ins_id;
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * "Count All" query
359 *
360 * Generates a platform-specific query string that counts all records in
361 * the specified database
362 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @param string
364 * @return string
365 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200366 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
368 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000369 {
370 return 0;
371 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000372
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200373 $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 +0000374 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000375 {
376 return 0;
377 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000378
379 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500380 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000381 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * 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 {
396 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
399 {
Greg Aker0d424892010-01-26 02:14:44 +0000400 $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 +0000401 }
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 return $sql;
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // --------------------------------------------------------------------
407
408 /**
409 * Show column query
410 *
411 * Generates a platform-specific query string so that the column names can be fetched
412 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 * @param string the table name
414 * @return string
415 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200416 protected function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
418 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$table."'";
419 }
420
421 // --------------------------------------------------------------------
422
423 /**
424 * Field data query
425 *
426 * Generates a platform-specific query so that the column data can be retrieved
427 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @param string the table name
429 * @return object
430 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200431 protected function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 return "SELECT * FROM ".$table." LIMIT 1";
434 }
435
436 // --------------------------------------------------------------------
437
438 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200439 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200441 * Returns an array containing code and message of the last
442 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200444 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200446 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200448 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450
451 // --------------------------------------------------------------------
452
453 /**
454 * Escape the SQL Identifiers
455 *
456 * This function escapes column and table names
457 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @param string
459 * @return string
460 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200461 public function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 if ($this->_escape_char == '')
464 {
465 return $item;
466 }
467
468 foreach ($this->_reserved_identifiers as $id)
469 {
470 if (strpos($item, '.'.$id) !== FALSE)
471 {
Barry Mienydd671972010-10-04 16:33:58 +0200472 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // remove duplicates if the user already included the escape
475 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200476 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 if (strpos($item, '.') !== FALSE)
480 {
Barry Mienydd671972010-10-04 16:33:58 +0200481 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
483 else
484 {
485 $str = $this->_escape_char.$item.$this->_escape_char;
486 }
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 // remove duplicates if the user already included the escape
489 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 // --------------------------------------------------------------------
493
494 /**
495 * From Tables
496 *
497 * This function implicitly groups FROM tables so there is no confusion
498 * about operator precedence in harmony with SQL standards
499 *
Andrey Andreev738497c2012-03-20 14:12:25 +0200500 * @param array
501 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200503 protected function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
505 if ( ! is_array($tables))
506 {
507 $tables = array($tables);
508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 return implode(', ', $tables);
511 }
512
513 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 /**
516 * Insert statement
517 *
518 * Generates a platform-specific insert string from the supplied data
519 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * @param string the table name
521 * @param array the insert keys
522 * @param array the insert values
523 * @return string
524 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200525 protected function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200526 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
528 }
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 // --------------------------------------------------------------------
531
532 /**
Greg Aker60ef4ea2011-04-27 01:45:38 -0500533 * Insert_batch statement
534 *
535 * Generates a platform-specific insert string from the supplied data
536 *
Greg Aker60ef4ea2011-04-27 01:45:38 -0500537 * @param string the table name
538 * @param array the insert keys
539 * @param array the insert values
540 * @return string
541 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200542 protected function _insert_batch($table, $keys, $values)
Greg Aker60ef4ea2011-04-27 01:45:38 -0500543 {
544 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
545 }
546
547 // --------------------------------------------------------------------
548
549 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 * Update statement
551 *
552 * Generates a platform-specific update string from the supplied data
553 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * @param string the table name
555 * @param array the update data
556 * @param array the where clause
557 * @param array the orderby clause
558 * @param array the limit clause
559 * @return string
560 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200561 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500563 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
565 $valstr[] = $key." = ".$val;
566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
569
570 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
571
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 return $sql;
573 }
574
575 // --------------------------------------------------------------------
576
577 /**
578 * Truncate statement
579 *
580 * Generates a platform-specific truncate string from the supplied data
581 * If the database does not support the truncate() command
582 * This function maps to "DELETE FROM table"
583 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 * @param string the table name
585 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200586 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200587 protected function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 {
589 return "TRUNCATE ".$table;
590 }
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 // --------------------------------------------------------------------
593
594 /**
595 * Delete statement
596 *
597 * Generates a platform-specific delete string from the supplied data
598 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 * @param string the table name
600 * @param array the where clause
601 * @param string the limit clause
602 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200603 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200604 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
606 $conditions = '';
607
608 if (count($where) > 0 OR count($like) > 0)
609 {
610 $conditions = "\nWHERE ";
611 $conditions .= implode("\n", $this->ar_where);
612
613 if (count($where) > 0 && count($like) > 0)
614 {
615 $conditions .= " AND ";
616 }
617 $conditions .= implode("\n", $like);
618 }
619
SammyK1bf8eeb2012-03-05 16:56:06 -0600620 return "DELETE FROM ".$table.$conditions;
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 }
622
623 // --------------------------------------------------------------------
624 /**
625 * Limit string
626 *
627 * Generates a platform-specific LIMIT clause
628 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 * @param string the sql query string
Andrey Andreev738497c2012-03-20 14:12:25 +0200630 * @param int the number of rows to limit the query to
631 * @param int the offset value
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 * @return string
633 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200634 protected function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200635 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 $sql .= "LIMIT ".$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200637
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 if ($offset > 0)
639 {
640 $sql .= " OFFSET ".$offset;
641 }
Barry Mienydd671972010-10-04 16:33:58 +0200642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 return $sql;
644 }
645
646 // --------------------------------------------------------------------
647
648 /**
649 * Close DB Connection
650 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 * @param resource
652 * @return void
653 */
Andrey Andreev738497c2012-03-20 14:12:25 +0200654 protected function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
656 @pg_close($conn_id);
657 }
658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659}
660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661/* End of file postgre_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200662/* Location: ./system/database/drivers/postgre/postgre_driver.php */