blob: 6ef83726a48d75c2a0e72e733600a21840c87340 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?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
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
28// ------------------------------------------------------------------------
29
30/**
31 * Postgre Database Adapter Class
32 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_postgre_driver extends CI_DB {
44
Timothy Warren7a3f8972012-03-19 18:18:27 -040045 public $dbdriver = 'postgre';
Barry Mienydd671972010-10-04 16:33:58 +020046
Timothy Warren7a3f8972012-03-19 18:18:27 -040047 protected $_escape_char = '"';
Derek Allard2067d1a2008-11-13 22:59:24 +000048
Derek Jonese4ed5832009-02-20 21:44:59 +000049 // clause and character used for LIKE escape sequences
Timothy Warren7a3f8972012-03-19 18:18:27 -040050 protected $_like_escape_str = " ESCAPE '%s' ";
51 protected $_like_escape_chr = '!';
Derek Jonese4ed5832009-02-20 21:44:59 +000052
Derek Allard2067d1a2008-11-13 22:59:24 +000053 /**
54 * The syntax to count rows is slightly different across different
55 * database engines, so this string appears in each driver and is
56 * used for the count_all() and count_all_results() functions.
57 */
Timothy Warren7a3f8972012-03-19 18:18:27 -040058 protected $_count_string = "SELECT COUNT(*) AS ";
59 protected $_random_keyword = ' RANDOM()'; // database specific random keyword
Derek Allard2067d1a2008-11-13 22:59:24 +000060
61 /**
62 * Connection String
63 *
Derek Allard2067d1a2008-11-13 22:59:24 +000064 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020065 */
Timothy Warren7a3f8972012-03-19 18:18:27 -040066 protected function _connect_string()
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
68 $components = array(
69 'hostname' => 'host',
70 'port' => 'port',
71 'database' => 'dbname',
72 'username' => 'user',
73 'password' => 'password'
74 );
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 $connect_string = "";
77 foreach ($components as $key => $val)
78 {
79 if (isset($this->$key) && $this->$key != '')
80 {
81 $connect_string .= " $val=".$this->$key;
82 }
83 }
84 return trim($connect_string);
85 }
86
87 // --------------------------------------------------------------------
88
89 /**
90 * Non-persistent database connection
91 *
Derek Allard2067d1a2008-11-13 22:59:24 +000092 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020093 */
Timothy Warren7a3f8972012-03-19 18:18:27 -040094 protected function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +020095 {
Derek Allard2067d1a2008-11-13 22:59:24 +000096 return @pg_connect($this->_connect_string());
97 }
98
99 // --------------------------------------------------------------------
100
101 /**
102 * Persistent database connection
103 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200105 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400106 protected function db_pconnect()
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 {
108 return @pg_pconnect($this->_connect_string());
109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // --------------------------------------------------------------------
112
113 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000114 * Reconnect
115 *
116 * Keep / reestablish the db connection if no queries have been
117 * sent for a length of time exceeding the server's idle timeout
118 *
Derek Jones87cbafc2009-02-27 16:29:59 +0000119 * @return void
120 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400121 public function reconnect()
Derek Jones87cbafc2009-02-27 16:29:59 +0000122 {
123 if (pg_ping($this->conn_id) === FALSE)
124 {
125 $this->conn_id = FALSE;
126 }
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * Select the database
133 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200135 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400136 protected function db_select()
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
138 // Not needed for Postgre so we'll return TRUE
139 return TRUE;
140 }
141
142 // --------------------------------------------------------------------
143
144 /**
Andrey Andreev8e89df82012-03-03 03:48:12 +0200145 * Set client character set
146 *
147 * @param string
148 * @return bool
149 */
150 protected function _db_set_charset($charset)
151 {
152 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
153 }
154
155 // --------------------------------------------------------------------
156
157 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200158 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 * @return string
161 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200162 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200164 if (isset($this->data_cache['version']))
165 {
166 return $this->data_cache['version'];
167 }
168
169 if (($pg_version = pg_version($this->conn_id)) === FALSE)
170 {
171 return FALSE;
172 }
173
174 /* If PHP was compiled with PostgreSQL lib versions earlier
175 * than 7.4, pg_version() won't return the server version
176 * and so we'll have to fall back to running a query in
177 * order to get it.
178 */
179 return isset($pg_version['server'])
180 ? $this->data_cache['version'] = $pg_version['server']
181 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Execute the query
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @param string an SQL query
190 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200191 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400192 protected function _execute($sql)
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 return @pg_query($this->conn_id, $sql);
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 // --------------------------------------------------------------------
198
199 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * Begin Transaction
201 *
Barry Mienydd671972010-10-04 16:33:58 +0200202 * @return bool
203 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200204 public function trans_begin($test_mode = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200207 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 {
209 return TRUE;
210 }
211
212 // Reset the transaction failure flag.
213 // If the $test_mode flag is set to TRUE transactions will be rolled back
214 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200215 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
Andrey Andreeva19beb02012-03-03 04:20:50 +0200217 return @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Commit Transaction
224 *
Barry Mienydd671972010-10-04 16:33:58 +0200225 * @return bool
226 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200227 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200230 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
232 return TRUE;
233 }
234
Andrey Andreeva19beb02012-03-03 04:20:50 +0200235 return @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Rollback Transaction
242 *
Barry Mienydd671972010-10-04 16:33:58 +0200243 * @return bool
244 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200245 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200248 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 {
250 return TRUE;
251 }
252
Andrey Andreeva19beb02012-03-03 04:20:50 +0200253 return @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
255
256 // --------------------------------------------------------------------
257
258 /**
259 * Escape String
260 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000262 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 * @return string
264 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400265 public function escape_str($str, $like = FALSE)
Derek Jonese4ed5832009-02-20 21:44:59 +0000266 {
267 if (is_array($str))
268 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500269 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200270 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000271 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200272 }
273
274 return $str;
275 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000276
277 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Jonese4ed5832009-02-20 21:44:59 +0000279 // escape LIKE condition wildcards
280 if ($like === TRUE)
281 {
282 $str = str_replace( array('%', '_', $this->_like_escape_chr),
283 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
284 $str);
285 }
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Jonese4ed5832009-02-20 21:44:59 +0000287 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // --------------------------------------------------------------------
291
292 /**
293 * Affected Rows
294 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 * @return integer
296 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400297 public function affected_rows()
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
299 return @pg_affected_rows($this->result_id);
300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // --------------------------------------------------------------------
303
304 /**
305 * Insert ID
306 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @return integer
308 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400309 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200311 $v = $this->version();
Barry Mienydd671972010-10-04 16:33:58 +0200312
Pascal Kriete8761ef52011-02-14 13:13:52 -0500313 $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
314 $column = func_num_args() > 1 ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200315
Pascal Kriete8761ef52011-02-14 13:13:52 -0500316 if ($table == NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
318 $sql='SELECT LASTVAL() as ins_id';
319 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500320 elseif ($table != NULL && $column != NULL && $v >= '8.0')
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
322 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
323 $query = $this->query($sql);
324 $row = $query->row();
325 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
326 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500327 elseif ($table != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 {
329 // seq_name passed in table parameter
330 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
331 }
332 else
333 {
334 return pg_last_oid($this->result_id);
335 }
336 $query = $this->query($sql);
337 $row = $query->row();
338 return $row->ins_id;
339 }
340
341 // --------------------------------------------------------------------
342
343 /**
344 * "Count All" query
345 *
346 * Generates a platform-specific query string that counts all records in
347 * the specified database
348 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * @param string
350 * @return string
351 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400352 public function count_all($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
354 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000355 {
356 return 0;
357 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000358
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200359 $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 +0000360 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000361 {
362 return 0;
363 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000364
365 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500366 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000367 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * Show table query
374 *
375 * Generates a platform-specific query string so that the table names can be fetched
376 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 * @param boolean
378 * @return string
379 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400380 protected function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200381 {
382 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
385 {
Greg Aker0d424892010-01-26 02:14:44 +0000386 $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 +0000387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 return $sql;
390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 // --------------------------------------------------------------------
393
394 /**
395 * Show column query
396 *
397 * Generates a platform-specific query string so that the column names can be fetched
398 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 * @param string the table name
400 * @return string
401 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400402 public function _list_columns($table = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$table."'";
405 }
406
407 // --------------------------------------------------------------------
408
409 /**
410 * Field data query
411 *
412 * Generates a platform-specific query so that the column data can be retrieved
413 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @param string the table name
415 * @return object
416 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400417 public function _field_data($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 return "SELECT * FROM ".$table." LIMIT 1";
420 }
421
422 // --------------------------------------------------------------------
423
424 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200425 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200427 * Returns an array containing code and message of the last
428 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200430 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200432 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200434 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * Escape the SQL Identifiers
441 *
442 * This function escapes column and table names
443 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @param string
445 * @return string
446 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400447 protected function _escape_identifiers($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 if ($this->_escape_char == '')
450 {
451 return $item;
452 }
453
454 foreach ($this->_reserved_identifiers as $id)
455 {
456 if (strpos($item, '.'.$id) !== FALSE)
457 {
Barry Mienydd671972010-10-04 16:33:58 +0200458 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 // remove duplicates if the user already included the escape
461 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200462 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 if (strpos($item, '.') !== FALSE)
466 {
Barry Mienydd671972010-10-04 16:33:58 +0200467 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469 else
470 {
471 $str = $this->_escape_char.$item.$this->_escape_char;
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
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);
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
479
480 /**
481 * From Tables
482 *
483 * This function implicitly groups FROM tables so there is no confusion
484 * about operator precedence in harmony with SQL standards
485 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * @param type
487 * @return type
488 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400489 public function _from_tables($tables)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 if ( ! is_array($tables))
492 {
493 $tables = array($tables);
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 return implode(', ', $tables);
497 }
498
499 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 /**
502 * Insert statement
503 *
504 * Generates a platform-specific insert string from the supplied data
505 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @param string the table name
507 * @param array the insert keys
508 * @param array the insert values
509 * @return string
510 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400511 public function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200512 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // --------------------------------------------------------------------
517
518 /**
Greg Aker60ef4ea2011-04-27 01:45:38 -0500519 * Insert_batch statement
520 *
521 * Generates a platform-specific insert string from the supplied data
522 *
Greg Aker60ef4ea2011-04-27 01:45:38 -0500523 * @param string the table name
524 * @param array the insert keys
525 * @param array the insert values
526 * @return string
527 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400528 public function _insert_batch($table, $keys, $values)
Greg Aker60ef4ea2011-04-27 01:45:38 -0500529 {
530 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 * Update statement
537 *
538 * Generates a platform-specific update string from the supplied data
539 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 * @param string the table name
541 * @param array the update data
542 * @param array the where clause
543 * @param array the orderby clause
544 * @param array the limit clause
545 * @return string
546 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400547 public function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500549 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 {
551 $valstr[] = $key." = ".$val;
552 }
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
555
556 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 return $sql;
559 }
560
561 // --------------------------------------------------------------------
562
563 /**
564 * Truncate statement
565 *
566 * Generates a platform-specific truncate string from the supplied data
567 * If the database does not support the truncate() command
568 * This function maps to "DELETE FROM table"
569 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 * @param string the table name
571 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200572 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400573 public function _truncate($table)
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 {
575 return "TRUNCATE ".$table;
576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 // --------------------------------------------------------------------
579
580 /**
581 * Delete statement
582 *
583 * Generates a platform-specific delete string from the supplied data
584 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 * @param string the table name
586 * @param array the where clause
587 * @param string the limit clause
588 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200589 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400590 public function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
592 $conditions = '';
593
594 if (count($where) > 0 OR count($like) > 0)
595 {
596 $conditions = "\nWHERE ";
597 $conditions .= implode("\n", $this->ar_where);
598
599 if (count($where) > 0 && count($like) > 0)
600 {
601 $conditions .= " AND ";
602 }
603 $conditions .= implode("\n", $like);
604 }
605
SammyK1bf8eeb2012-03-05 16:56:06 -0600606 return "DELETE FROM ".$table.$conditions;
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
608
609 // --------------------------------------------------------------------
610 /**
611 * Limit string
612 *
613 * Generates a platform-specific LIMIT clause
614 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 * @param string the sql query string
616 * @param integer the number of rows to limit the query to
617 * @param integer the offset value
618 * @return string
619 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400620 public function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200621 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 $sql .= "LIMIT ".$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200623
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 if ($offset > 0)
625 {
626 $sql .= " OFFSET ".$offset;
627 }
Barry Mienydd671972010-10-04 16:33:58 +0200628
Derek Allard2067d1a2008-11-13 22:59:24 +0000629 return $sql;
630 }
631
632 // --------------------------------------------------------------------
633
634 /**
635 * Close DB Connection
636 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 * @param resource
638 * @return void
639 */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400640 public function _close($conn_id)
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
642 @pg_close($conn_id);
643 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000644}
645
Derek Allard2067d1a2008-11-13 22:59:24 +0000646/* End of file postgre_driver.php */
Timothy Warren7a3f8972012-03-19 18:18:27 -0400647/* Location: ./system/database/drivers/postgre/postgre_driver.php */