blob: df0f50da55c5146bac19baa05ccbbee60bb05eb0 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
45 var $dbdriver = 'postgre';
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 var $_escape_char = '"';
48
Derek Jonese4ed5832009-02-20 21:44:59 +000049 // clause and character used for LIKE escape sequences
50 var $_like_escape_str = " ESCAPE '%s' ";
51 var $_like_escape_chr = '!';
52
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 */
58 var $_count_string = "SELECT COUNT(*) AS ";
59 var $_random_keyword = ' RANDOM()'; // database specific random keyword
60
61 /**
62 * Connection String
63 *
64 * @access private
65 * @return string
Barry Mienydd671972010-10-04 16:33:58 +020066 */
Derek Allard2067d1a2008-11-13 22:59:24 +000067 function _connect_string()
68 {
69 $components = array(
70 'hostname' => 'host',
71 'port' => 'port',
72 'database' => 'dbname',
73 'username' => 'user',
74 'password' => 'password'
75 );
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 $connect_string = "";
78 foreach ($components as $key => $val)
79 {
80 if (isset($this->$key) && $this->$key != '')
81 {
82 $connect_string .= " $val=".$this->$key;
83 }
84 }
85 return trim($connect_string);
86 }
87
88 // --------------------------------------------------------------------
89
90 /**
91 * Non-persistent database connection
92 *
93 * @access private called by the base class
94 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020095 */
Derek Allard2067d1a2008-11-13 22:59:24 +000096 function db_connect()
Barry Mienydd671972010-10-04 16:33:58 +020097 {
Derek Allard2067d1a2008-11-13 22:59:24 +000098 return @pg_connect($this->_connect_string());
99 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Persistent database connection
105 *
106 * @access private called by the base class
107 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200108 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 function db_pconnect()
110 {
111 return @pg_pconnect($this->_connect_string());
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // --------------------------------------------------------------------
115
116 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000117 * Reconnect
118 *
119 * Keep / reestablish the db connection if no queries have been
120 * sent for a length of time exceeding the server's idle timeout
121 *
122 * @access public
123 * @return void
124 */
125 function reconnect()
126 {
127 if (pg_ping($this->conn_id) === FALSE)
128 {
129 $this->conn_id = FALSE;
130 }
131 }
132
133 // --------------------------------------------------------------------
134
135 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 * Select the database
137 *
138 * @access private called by the base class
139 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200140 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 function db_select()
142 {
143 // Not needed for Postgre so we'll return TRUE
144 return TRUE;
145 }
146
147 // --------------------------------------------------------------------
148
149 /**
Andrey Andreev8e89df82012-03-03 03:48:12 +0200150 * Set client character set
151 *
152 * @param string
153 * @return bool
154 */
155 protected function _db_set_charset($charset)
156 {
157 return (pg_set_client_encoding($this->conn_id, $charset) === 0);
158 }
159
160 // --------------------------------------------------------------------
161
162 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200163 * Database version number
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 * @return string
166 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200167 public function version()
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200169 if (isset($this->data_cache['version']))
170 {
171 return $this->data_cache['version'];
172 }
173
174 if (($pg_version = pg_version($this->conn_id)) === FALSE)
175 {
176 return FALSE;
177 }
178
179 /* If PHP was compiled with PostgreSQL lib versions earlier
180 * than 7.4, pg_version() won't return the server version
181 * and so we'll have to fall back to running a query in
182 * order to get it.
183 */
184 return isset($pg_version['server'])
185 ? $this->data_cache['version'] = $pg_version['server']
186 : parent::version();
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
188
189 // --------------------------------------------------------------------
190
191 /**
192 * Execute the query
193 *
194 * @access private called by the base class
195 * @param string an SQL query
196 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200197 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 function _execute($sql)
199 {
200 $sql = $this->_prep_query($sql);
201 return @pg_query($this->conn_id, $sql);
202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // --------------------------------------------------------------------
205
206 /**
207 * Prep the query
208 *
209 * If needed, each database adapter can prep the query string
210 *
211 * @access private called by execute()
212 * @param string an SQL query
213 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200214 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 function _prep_query($sql)
216 {
217 return $sql;
218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Begin Transaction
224 *
Barry Mienydd671972010-10-04 16:33:58 +0200225 * @return bool
226 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200227 public function trans_begin($test_mode = FALSE)
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
235 // Reset the transaction failure flag.
236 // If the $test_mode flag is set to TRUE transactions will be rolled back
237 // even if the queries produce a successful result.
Andrey Andreeva19beb02012-03-03 04:20:50 +0200238 $this->_trans_failure = ($test_mode === TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000239
Andrey Andreeva19beb02012-03-03 04:20:50 +0200240 return @pg_query($this->conn_id, 'BEGIN');
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
242
243 // --------------------------------------------------------------------
244
245 /**
246 * Commit Transaction
247 *
Barry Mienydd671972010-10-04 16:33:58 +0200248 * @return bool
249 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200250 public function trans_commit()
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200253 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 return TRUE;
256 }
257
Andrey Andreeva19beb02012-03-03 04:20:50 +0200258 return @pg_query($this->conn_id, 'COMMIT');
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260
261 // --------------------------------------------------------------------
262
263 /**
264 * Rollback Transaction
265 *
Barry Mienydd671972010-10-04 16:33:58 +0200266 * @return bool
267 */
Andrey Andreeva19beb02012-03-03 04:20:50 +0200268 public function trans_rollback()
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreeva19beb02012-03-03 04:20:50 +0200271 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 return TRUE;
274 }
275
Andrey Andreeva19beb02012-03-03 04:20:50 +0200276 return @pg_query($this->conn_id, 'ROLLBACK');
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Escape String
283 *
284 * @access public
285 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000286 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 * @return string
288 */
Derek Jonese4ed5832009-02-20 21:44:59 +0000289 function escape_str($str, $like = FALSE)
290 {
291 if (is_array($str))
292 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500293 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200294 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000295 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200296 }
297
298 return $str;
299 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000300
301 $str = pg_escape_string($str);
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Jonese4ed5832009-02-20 21:44:59 +0000303 // escape LIKE condition wildcards
304 if ($like === TRUE)
305 {
306 $str = str_replace( array('%', '_', $this->_like_escape_chr),
307 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
308 $str);
309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Jonese4ed5832009-02-20 21:44:59 +0000311 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // --------------------------------------------------------------------
315
316 /**
317 * Affected Rows
318 *
319 * @access public
320 * @return integer
321 */
322 function affected_rows()
323 {
324 return @pg_affected_rows($this->result_id);
325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // --------------------------------------------------------------------
328
329 /**
330 * Insert ID
331 *
332 * @access public
333 * @return integer
334 */
335 function insert_id()
336 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200337 $v = $this->version();
Barry Mienydd671972010-10-04 16:33:58 +0200338
Pascal Kriete8761ef52011-02-14 13:13:52 -0500339 $table = func_num_args() > 0 ? func_get_arg(0) : NULL;
340 $column = func_num_args() > 1 ? func_get_arg(1) : NULL;
Barry Mienydd671972010-10-04 16:33:58 +0200341
Pascal Kriete8761ef52011-02-14 13:13:52 -0500342 if ($table == NULL && $v >= '8.1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 {
344 $sql='SELECT LASTVAL() as ins_id';
345 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500346 elseif ($table != NULL && $column != NULL && $v >= '8.0')
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 $sql = sprintf("SELECT pg_get_serial_sequence('%s','%s') as seq", $table, $column);
349 $query = $this->query($sql);
350 $row = $query->row();
351 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $row->seq);
352 }
Pascal Kriete8761ef52011-02-14 13:13:52 -0500353 elseif ($table != NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 // seq_name passed in table parameter
356 $sql = sprintf("SELECT CURRVAL('%s') as ins_id", $table);
357 }
358 else
359 {
360 return pg_last_oid($this->result_id);
361 }
362 $query = $this->query($sql);
363 $row = $query->row();
364 return $row->ins_id;
365 }
366
367 // --------------------------------------------------------------------
368
369 /**
370 * "Count All" query
371 *
372 * Generates a platform-specific query string that counts all records in
373 * the specified database
374 *
375 * @access public
376 * @param string
377 * @return string
378 */
379 function count_all($table = '')
380 {
381 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000382 {
383 return 0;
384 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000385
Derek Allarde37ab382009-02-03 16:13:57 +0000386 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000389 {
390 return 0;
391 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000392
393 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500394 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000395 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Show table query
402 *
403 * Generates a platform-specific query string so that the table names can be fetched
404 *
405 * @access private
406 * @param boolean
407 * @return string
408 */
409 function _list_tables($prefix_limit = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200410 {
411 $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'";
412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
414 {
Greg Aker0d424892010-01-26 02:14:44 +0000415 $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 +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 *
428 * @access public
429 * @param string the table name
430 * @return string
431 */
432 function _list_columns($table = '')
433 {
434 return "SELECT column_name FROM information_schema.columns WHERE table_name ='".$table."'";
435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * Field data query
441 *
442 * Generates a platform-specific query so that the column data can be retrieved
443 *
444 * @access public
445 * @param string the table name
446 * @return object
447 */
448 function _field_data($table)
449 {
450 return "SELECT * FROM ".$table." LIMIT 1";
451 }
452
453 // --------------------------------------------------------------------
454
455 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200456 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200458 * Returns an array containing code and message of the last
459 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200461 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200463 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200465 return array('code' => '', 'message' => pg_last_error($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
467
468 // --------------------------------------------------------------------
469
470 /**
471 * Escape the SQL Identifiers
472 *
473 * This function escapes column and table names
474 *
475 * @access private
476 * @param string
477 * @return string
478 */
479 function _escape_identifiers($item)
480 {
481 if ($this->_escape_char == '')
482 {
483 return $item;
484 }
485
486 foreach ($this->_reserved_identifiers as $id)
487 {
488 if (strpos($item, '.'.$id) !== FALSE)
489 {
Barry Mienydd671972010-10-04 16:33:58 +0200490 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 // remove duplicates if the user already included the escape
493 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200494 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 if (strpos($item, '.') !== FALSE)
498 {
Barry Mienydd671972010-10-04 16:33:58 +0200499 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
501 else
502 {
503 $str = $this->_escape_char.$item.$this->_escape_char;
504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 // remove duplicates if the user already included the escape
507 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 // --------------------------------------------------------------------
511
512 /**
513 * From Tables
514 *
515 * This function implicitly groups FROM tables so there is no confusion
516 * about operator precedence in harmony with SQL standards
517 *
518 * @access public
519 * @param type
520 * @return type
521 */
522 function _from_tables($tables)
523 {
524 if ( ! is_array($tables))
525 {
526 $tables = array($tables);
527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 return implode(', ', $tables);
530 }
531
532 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 /**
535 * Insert statement
536 *
537 * Generates a platform-specific insert string from the supplied data
538 *
539 * @access public
540 * @param string the table name
541 * @param array the insert keys
542 * @param array the insert values
543 * @return string
544 */
545 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200546 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 // --------------------------------------------------------------------
551
552 /**
Greg Aker60ef4ea2011-04-27 01:45:38 -0500553 * Insert_batch statement
554 *
555 * Generates a platform-specific insert string from the supplied data
556 *
557 * @access public
558 * @param string the table name
559 * @param array the insert keys
560 * @param array the insert values
561 * @return string
562 */
563 function _insert_batch($table, $keys, $values)
564 {
565 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
566 }
567
568 // --------------------------------------------------------------------
569
570 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 * Update statement
572 *
573 * Generates a platform-specific update string from the supplied data
574 *
575 * @access public
576 * @param string the table name
577 * @param array the update data
578 * @param array the where clause
579 * @param array the orderby clause
580 * @param array the limit clause
581 * @return string
582 */
583 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
584 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500585 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 {
587 $valstr[] = $key." = ".$val;
588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200591
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
595
596 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
597
598 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 return $sql;
601 }
602
603 // --------------------------------------------------------------------
604
605 /**
606 * Truncate statement
607 *
608 * Generates a platform-specific truncate string from the supplied data
609 * If the database does not support the truncate() command
610 * This function maps to "DELETE FROM table"
611 *
612 * @access public
613 * @param string the table name
614 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200615 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 function _truncate($table)
617 {
618 return "TRUNCATE ".$table;
619 }
Barry Mienydd671972010-10-04 16:33:58 +0200620
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 // --------------------------------------------------------------------
622
623 /**
624 * Delete statement
625 *
626 * Generates a platform-specific delete string from the supplied data
627 *
628 * @access public
629 * @param string the table name
630 * @param array the where clause
631 * @param string the limit clause
632 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200633 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
635 {
636 $conditions = '';
637
638 if (count($where) > 0 OR count($like) > 0)
639 {
640 $conditions = "\nWHERE ";
641 $conditions .= implode("\n", $this->ar_where);
642
643 if (count($where) > 0 && count($like) > 0)
644 {
645 $conditions .= " AND ";
646 }
647 $conditions .= implode("\n", $like);
648 }
649
650 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 return "DELETE FROM ".$table.$conditions.$limit;
653 }
654
655 // --------------------------------------------------------------------
656 /**
657 * Limit string
658 *
659 * Generates a platform-specific LIMIT clause
660 *
661 * @access public
662 * @param string the sql query string
663 * @param integer the number of rows to limit the query to
664 * @param integer the offset value
665 * @return string
666 */
667 function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200668 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000669 $sql .= "LIMIT ".$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200670
Derek Allard2067d1a2008-11-13 22:59:24 +0000671 if ($offset > 0)
672 {
673 $sql .= " OFFSET ".$offset;
674 }
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 return $sql;
677 }
678
679 // --------------------------------------------------------------------
680
681 /**
682 * Close DB Connection
683 *
684 * @access public
685 * @param resource
686 * @return void
687 */
688 function _close($conn_id)
689 {
690 @pg_close($conn_id);
691 }
692
693
694}
695
696
697/* End of file postgre_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200698/* Location: ./system/database/drivers/postgre/postgre_driver.php */