Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/database/ |
| 40 | */ |
| 41 | class CI_DB_postgre_driver extends CI_DB { |
| 42 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 43 | public $dbdriver = 'postgre'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 44 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 45 | protected $_escape_char = '"'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 47 | // clause and character used for LIKE escape sequences |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 48 | protected $_like_escape_str = ' ESCAPE \'%s\' '; |
| 49 | protected $_like_escape_chr = '!'; |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 50 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | /** |
| 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 Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 56 | protected $_count_string = 'SELECT COUNT(*) AS '; |
| 57 | protected $_random_keyword = ' RANDOM()'; // database specific random keyword |
| 58 | |
| 59 | // Postgre-specific properties |
| 60 | protected $_pg_dsn; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 63 | * Constructor |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | * |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 65 | * Creates a DSN string to be used for db_connect() and db_pconnect() |
| 66 | * |
| 67 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 68 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 69 | private function __construct($params) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 71 | parent::__construct($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 72 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 73 | $components = array( |
| 74 | 'hostname' => 'host', |
| 75 | 'port' => 'port', |
| 76 | 'database' => 'dbname', |
| 77 | 'username' => 'user', |
| 78 | 'password' => 'password' |
| 79 | ); |
| 80 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | foreach ($components as $key => $val) |
| 82 | { |
| 83 | if (isset($this->$key) && $this->$key != '') |
| 84 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 85 | $this->_pg_dsn .= $val.'='.$this->$key.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 88 | |
| 89 | if (strlen($this->_pg_dsn) > 0) |
| 90 | { |
| 91 | $this->_pg_dsn = rtrim($this->_pg_dsn); |
| 92 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // -------------------------------------------------------------------- |
| 96 | |
| 97 | /** |
| 98 | * Non-persistent database connection |
| 99 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 101 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 102 | public function db_connect() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 104 | $ret = @pg_connect($this->_pg_dsn); |
| 105 | if (is_resource($ret) && $this->char_set != '') |
| 106 | { |
| 107 | pg_set_client_encoding($ret, $this->char_set); |
| 108 | } |
| 109 | |
| 110 | return $ret; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // -------------------------------------------------------------------- |
| 114 | |
| 115 | /** |
| 116 | * Persistent database connection |
| 117 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 120 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 122 | $ret = @pg_pconnect($this->_pg_dsn); |
| 123 | if (is_resource($ret) && $this->char_set != '') |
| 124 | { |
| 125 | pg_set_client_encoding($ret, $this->char_set); |
| 126 | } |
| 127 | |
| 128 | return $ret; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 130 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | // -------------------------------------------------------------------- |
| 132 | |
| 133 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 134 | * Reconnect |
| 135 | * |
| 136 | * Keep / reestablish the db connection if no queries have been |
| 137 | * sent for a length of time exceeding the server's idle timeout |
| 138 | * |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 139 | * @return void |
| 140 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 141 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 142 | { |
| 143 | if (pg_ping($this->conn_id) === FALSE) |
| 144 | { |
| 145 | $this->conn_id = FALSE; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // -------------------------------------------------------------------- |
| 150 | |
| 151 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | * Select the database |
| 153 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 156 | public function db_select() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | { |
| 158 | // Not needed for Postgre so we'll return TRUE |
| 159 | return TRUE; |
| 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Set client character set |
| 166 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | * @param string |
| 168 | * @param string |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 169 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 171 | public function db_set_charset($charset, $collation) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 173 | return (pg_set_client_encoding($this->conn_id, $charset) === 0); |
| 174 | } |
| 175 | |
| 176 | // -------------------------------------------------------------------- |
| 177 | |
| 178 | /** |
| 179 | * Database version |
| 180 | * |
| 181 | * This method overrides the one from the base class, as if PHP was |
| 182 | * compiled with PostgreSQL version >= 7.4 libraries, we have a native |
| 183 | * function to get the server's version. |
| 184 | * |
| 185 | * @return string |
| 186 | */ |
| 187 | |
| 188 | public function version() |
| 189 | { |
| 190 | $version = pg_version($this->conn_id); |
| 191 | |
| 192 | /* If we don't have the server version string - fall back to |
| 193 | * the base driver class' method |
| 194 | */ |
| 195 | return isset($version['server']) ? $version['server'] : parent::version(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | /** |
| 201 | * Version number query string |
| 202 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | * @return string |
| 204 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 205 | protected function _version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 207 | return 'SELECT version() AS ver'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // -------------------------------------------------------------------- |
| 211 | |
| 212 | /** |
| 213 | * Execute the query |
| 214 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | * @param string an SQL query |
| 216 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 217 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 218 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 220 | return @pg_query($this->conn_id, $this->_prep_query($sql)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 222 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | // -------------------------------------------------------------------- |
| 224 | |
| 225 | /** |
| 226 | * Prep the query |
| 227 | * |
| 228 | * If needed, each database adapter can prep the query string |
| 229 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | * @param string an SQL query |
| 231 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 233 | protected function _prep_query($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | { |
| 235 | return $sql; |
| 236 | } |
| 237 | |
| 238 | // -------------------------------------------------------------------- |
| 239 | |
| 240 | /** |
| 241 | * Begin Transaction |
| 242 | * |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 243 | * @param bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | * @return bool |
| 245 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 246 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 249 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | { |
| 251 | return TRUE; |
| 252 | } |
| 253 | |
| 254 | // Reset the transaction failure flag. |
| 255 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 256 | // even if the queries produce a successful result. |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 257 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 259 | return (bool) @pg_query($this->conn_id, 'BEGIN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // -------------------------------------------------------------------- |
| 263 | |
| 264 | /** |
| 265 | * Commit Transaction |
| 266 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | * @return bool |
| 268 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 269 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 272 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | { |
| 274 | return TRUE; |
| 275 | } |
| 276 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 277 | return (bool) @pg_query($this->conn_id, 'COMMIT'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // -------------------------------------------------------------------- |
| 281 | |
| 282 | /** |
| 283 | * Rollback Transaction |
| 284 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 285 | * @return bool |
| 286 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 287 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 290 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | { |
| 292 | return TRUE; |
| 293 | } |
| 294 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 295 | return (bool) @pg_query($this->conn_id, 'ROLLBACK'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // -------------------------------------------------------------------- |
| 299 | |
| 300 | /** |
| 301 | * Escape String |
| 302 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 304 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | * @return string |
| 306 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 307 | public function escape_str($str, $like = FALSE) |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 308 | { |
| 309 | if (is_array($str)) |
| 310 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 311 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 313 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | return $str; |
| 317 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 318 | |
| 319 | $str = pg_escape_string($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 321 | // escape LIKE condition wildcards |
| 322 | if ($like === TRUE) |
| 323 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 324 | return str_replace(array('%', '_', $this->_like_escape_chr), |
| 325 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 326 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 327 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 328 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 329 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | // -------------------------------------------------------------------- |
| 333 | |
| 334 | /** |
| 335 | * Affected Rows |
| 336 | * |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 337 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 339 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | { |
| 341 | return @pg_affected_rows($this->result_id); |
| 342 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 343 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | // -------------------------------------------------------------------- |
| 345 | |
| 346 | /** |
| 347 | * Insert ID |
| 348 | * |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 349 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 351 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 353 | $v = pg_version($this->conn_id); |
| 354 | $v = isset($v['server']) ? $v['server'] : 0; // 'server' key is only available since PosgreSQL 7.4 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 355 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 356 | $table = (func_num_args() > 0) ? func_get_arg(0) : NULL; |
| 357 | $column = (func_num_args() > 1) ? func_get_arg(1) : NULL; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 358 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 359 | if ($table == NULL && $v >= '8.1') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 361 | $sql = 'SELECT LASTVAL() AS ins_id'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | } |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 363 | elseif ($table != NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 365 | if ($column != NULL && $v >= '8.0') |
| 366 | { |
| 367 | $sql = 'SELECT pg_get_serial_sequence(\''.$table."', '".$column."') AS seq"; |
| 368 | $query = $this->query($sql); |
| 369 | $query = $query->row(); |
| 370 | $seq = $query->seq; |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | // seq_name passed in table parameter |
| 375 | $seq = $table; |
| 376 | } |
| 377 | |
| 378 | $sql = 'SELECT CURRVAL(\''.$seq."') AS ins_id"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | } |
| 380 | else |
| 381 | { |
| 382 | return pg_last_oid($this->result_id); |
| 383 | } |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 384 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | $query = $this->query($sql); |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 386 | $query = $query->row(); |
| 387 | return (int) $query->ins_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // -------------------------------------------------------------------- |
| 391 | |
| 392 | /** |
| 393 | * "Count All" query |
| 394 | * |
| 395 | * Generates a platform-specific query string that counts all records in |
| 396 | * the specified database |
| 397 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | * @param string |
| 399 | * @return string |
| 400 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 401 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | { |
| 403 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 404 | { |
| 405 | return 0; |
| 406 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 408 | $query = $this->query($this->_count_string.$this->_protect_identifiers('numrows').' FROM '.$this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 410 | { |
| 411 | return 0; |
| 412 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 414 | $query = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 415 | $this->_reset_select(); |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 416 | return (int) $query->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // -------------------------------------------------------------------- |
| 420 | |
| 421 | /** |
| 422 | * Show table query |
| 423 | * |
| 424 | * Generates a platform-specific query string so that the table names can be fetched |
| 425 | * |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 426 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | * @return string |
| 428 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 429 | protected function _list_tables($prefix_limit = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | { |
| 431 | $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'"; |
| 432 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 433 | if ($prefix_limit !== FALSE && $this->dbprefix != '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 435 | return $sql." AND table_name LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 437 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 438 | return $sql; |
| 439 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 440 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | // -------------------------------------------------------------------- |
| 442 | |
| 443 | /** |
| 444 | * Show column query |
| 445 | * |
| 446 | * Generates a platform-specific query string so that the column names can be fetched |
| 447 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | * @param string the table name |
| 449 | * @return string |
| 450 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 451 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 453 | return "SELECT column_name FROM information_schema.columns WHERE table_name = '".$table."'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // -------------------------------------------------------------------- |
| 457 | |
| 458 | /** |
| 459 | * Field data query |
| 460 | * |
| 461 | * Generates a platform-specific query so that the column data can be retrieved |
| 462 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | * @param string the table name |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 464 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 466 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 468 | return 'SELECT * FROM '.$table.' LIMIT 1'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // -------------------------------------------------------------------- |
| 472 | |
| 473 | /** |
| 474 | * The error message string |
| 475 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | * @return string |
| 477 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 478 | protected function _error_message() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | { |
| 480 | return pg_last_error($this->conn_id); |
| 481 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 482 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | // -------------------------------------------------------------------- |
| 484 | |
| 485 | /** |
| 486 | * The error message number |
| 487 | * |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 488 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 490 | protected function _error_number() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 492 | // Not supported in Postgre |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | return ''; |
| 494 | } |
| 495 | |
| 496 | // -------------------------------------------------------------------- |
| 497 | |
| 498 | /** |
| 499 | * Escape the SQL Identifiers |
| 500 | * |
| 501 | * This function escapes column and table names |
| 502 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | * @param string |
| 504 | * @return string |
| 505 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 506 | public function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | { |
| 508 | if ($this->_escape_char == '') |
| 509 | { |
| 510 | return $item; |
| 511 | } |
| 512 | |
| 513 | foreach ($this->_reserved_identifiers as $id) |
| 514 | { |
| 515 | if (strpos($item, '.'.$id) !== FALSE) |
| 516 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 517 | $item = str_replace('.', $this->_escape_char.'.', $item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 518 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 519 | // remove duplicates if the user already included the escape |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 520 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 521 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 522 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 523 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 524 | if (strpos($item, '.') !== FALSE) |
| 525 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 526 | $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 528 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 529 | // remove duplicates if the user already included the escape |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 530 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 531 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 532 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | // -------------------------------------------------------------------- |
| 534 | |
| 535 | /** |
| 536 | * From Tables |
| 537 | * |
| 538 | * This function implicitly groups FROM tables so there is no confusion |
| 539 | * about operator precedence in harmony with SQL standards |
| 540 | * |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 541 | * @param string table name |
| 542 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 543 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 544 | protected function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 545 | { |
| 546 | if ( ! is_array($tables)) |
| 547 | { |
| 548 | $tables = array($tables); |
| 549 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 550 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 551 | return implode(', ', $tables); |
| 552 | } |
| 553 | |
| 554 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 555 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 556 | /** |
| 557 | * Insert statement |
| 558 | * |
| 559 | * Generates a platform-specific insert string from the supplied data |
| 560 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 561 | * @param string the table name |
| 562 | * @param array the insert keys |
| 563 | * @param array the insert values |
| 564 | * @return string |
| 565 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 566 | protected function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 567 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 568 | return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 569 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 570 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 571 | // -------------------------------------------------------------------- |
| 572 | |
| 573 | /** |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 574 | * Insert_batch statement |
| 575 | * |
| 576 | * Generates a platform-specific insert string from the supplied data |
| 577 | * |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 578 | * @param string the table name |
| 579 | * @param array the insert keys |
| 580 | * @param array the insert values |
| 581 | * @return string |
| 582 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 583 | protected function _insert_batch($table, $keys, $values) |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 584 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 585 | return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values); |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // -------------------------------------------------------------------- |
| 589 | |
| 590 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | * Update statement |
| 592 | * |
| 593 | * Generates a platform-specific update string from the supplied data |
| 594 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | * @param string the table name |
| 596 | * @param array the update data |
| 597 | * @param array the where clause |
| 598 | * @param array the orderby clause |
| 599 | * @param array the limit clause |
| 600 | * @return string |
| 601 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 602 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 604 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 605 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 606 | $valstr[] = $key.' = '.$val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 608 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 609 | return 'UPDATE '.$table.' SET '.implode(', ', $valstr) |
| 610 | .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '') |
| 611 | .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '') |
| 612 | .( ! $limit ? '' : ' LIMIT '.$limit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | // -------------------------------------------------------------------- |
| 616 | |
| 617 | /** |
| 618 | * Truncate statement |
| 619 | * |
| 620 | * Generates a platform-specific truncate string from the supplied data |
| 621 | * If the database does not support the truncate() command |
| 622 | * This function maps to "DELETE FROM table" |
| 623 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | * @param string the table name |
| 625 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 626 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 627 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 628 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 629 | return 'TRUNCATE '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 630 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 631 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | // -------------------------------------------------------------------- |
| 633 | |
| 634 | /** |
| 635 | * Delete statement |
| 636 | * |
| 637 | * Generates a platform-specific delete string from the supplied data |
| 638 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 639 | * @param string the table name |
| 640 | * @param array the where clause |
| 641 | * @param string the limit clause |
| 642 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 643 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 644 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 645 | { |
| 646 | $conditions = ''; |
| 647 | |
| 648 | if (count($where) > 0 OR count($like) > 0) |
| 649 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 650 | $conditions = "\nWHERE ".implode("\n", $this->ar_where); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 651 | |
| 652 | if (count($where) > 0 && count($like) > 0) |
| 653 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 654 | $conditions .= ' AND '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | } |
| 656 | $conditions .= implode("\n", $like); |
| 657 | } |
| 658 | |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 659 | return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | // -------------------------------------------------------------------- |
| 663 | /** |
| 664 | * Limit string |
| 665 | * |
| 666 | * Generates a platform-specific LIMIT clause |
| 667 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 668 | * @param string the sql query string |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 669 | * @param int the number of rows to limit the query to |
| 670 | * @param int the offset value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 671 | * @return string |
| 672 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 673 | protected function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 674 | { |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 675 | return $sql.' LIMIT '.$limit.($offset == 0 ? '' : ' OFFSET '.$offset); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | // -------------------------------------------------------------------- |
| 679 | |
| 680 | /** |
| 681 | * Close DB Connection |
| 682 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 683 | * @param resource |
| 684 | * @return void |
| 685 | */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 686 | protected function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 687 | { |
| 688 | @pg_close($conn_id); |
| 689 | } |
| 690 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 693 | /* End of file postgre_driver.php */ |
Andrey Andreev | 214583f | 2012-01-26 16:39:09 +0200 | [diff] [blame^] | 694 | /* Location: ./system/database/drivers/postgre/postgre_driver.php */ |