Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 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 |
| 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 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 | |
| 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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 40 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | * @link http://codeigniter.com/user_guide/database/ |
| 42 | */ |
| 43 | class CI_DB_postgre_driver extends CI_DB { |
| 44 | |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 45 | public $dbdriver = 'postgre'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 46 | |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 47 | protected $_escape_char = '"'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 49 | // clause and character used for LIKE escape sequences |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 50 | protected $_like_escape_str = " ESCAPE '%s' "; |
| 51 | protected $_like_escape_chr = '!'; |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 52 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | /** |
| 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 Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 58 | protected $_count_string = "SELECT COUNT(*) AS "; |
| 59 | protected $_random_keyword = ' RANDOM()'; // database specific random keyword |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Connection String |
| 63 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 65 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 66 | protected function _connect_string() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | { |
| 68 | $components = array( |
| 69 | 'hostname' => 'host', |
| 70 | 'port' => 'port', |
| 71 | 'database' => 'dbname', |
| 72 | 'username' => 'user', |
| 73 | 'password' => 'password' |
| 74 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 75 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 93 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 94 | protected function db_connect() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | return @pg_connect($this->_connect_string()); |
| 97 | } |
| 98 | |
| 99 | // -------------------------------------------------------------------- |
| 100 | |
| 101 | /** |
| 102 | * Persistent database connection |
| 103 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 106 | protected function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | { |
| 108 | return @pg_pconnect($this->_connect_string()); |
| 109 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 110 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | // -------------------------------------------------------------------- |
| 112 | |
| 113 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 114 | * 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 Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 119 | * @return void |
| 120 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 121 | public function reconnect() |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 122 | { |
| 123 | if (pg_ping($this->conn_id) === FALSE) |
| 124 | { |
| 125 | $this->conn_id = FALSE; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // -------------------------------------------------------------------- |
| 130 | |
| 131 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | * Select the database |
| 133 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 136 | protected function db_select() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | { |
| 138 | // Not needed for Postgre so we'll return TRUE |
| 139 | return TRUE; |
| 140 | } |
| 141 | |
| 142 | // -------------------------------------------------------------------- |
| 143 | |
| 144 | /** |
Andrey Andreev | 8e89df8 | 2012-03-03 03:48:12 +0200 | [diff] [blame] | 145 | * 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 Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 158 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | * @return string |
| 161 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 162 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 164 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // -------------------------------------------------------------------- |
| 185 | |
| 186 | /** |
| 187 | * Execute the query |
| 188 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | * @param string an SQL query |
| 190 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 192 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | return @pg_query($this->conn_id, $sql); |
| 195 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | // -------------------------------------------------------------------- |
| 198 | |
| 199 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | * Begin Transaction |
| 201 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | * @return bool |
| 203 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 204 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 207 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | { |
| 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 Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 215 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 217 | return @pg_query($this->conn_id, 'BEGIN'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // -------------------------------------------------------------------- |
| 221 | |
| 222 | /** |
| 223 | * Commit Transaction |
| 224 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | * @return bool |
| 226 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 227 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 230 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | { |
| 232 | return TRUE; |
| 233 | } |
| 234 | |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 235 | return @pg_query($this->conn_id, 'COMMIT'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // -------------------------------------------------------------------- |
| 239 | |
| 240 | /** |
| 241 | * Rollback Transaction |
| 242 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 243 | * @return bool |
| 244 | */ |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 245 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 248 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | { |
| 250 | return TRUE; |
| 251 | } |
| 252 | |
Andrey Andreev | a19beb0 | 2012-03-03 04:20:50 +0200 | [diff] [blame] | 253 | return @pg_query($this->conn_id, 'ROLLBACK'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // -------------------------------------------------------------------- |
| 257 | |
| 258 | /** |
| 259 | * Escape String |
| 260 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 262 | * @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] | 263 | * @return string |
| 264 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 265 | public function escape_str($str, $like = FALSE) |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 266 | { |
| 267 | if (is_array($str)) |
| 268 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 269 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 271 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | return $str; |
| 275 | } |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 276 | |
| 277 | $str = pg_escape_string($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 278 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 279 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 286 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 287 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | // -------------------------------------------------------------------- |
| 291 | |
| 292 | /** |
| 293 | * Affected Rows |
| 294 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 295 | * @return integer |
| 296 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 297 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | { |
| 299 | return @pg_affected_rows($this->result_id); |
| 300 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 301 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | // -------------------------------------------------------------------- |
| 303 | |
| 304 | /** |
| 305 | * Insert ID |
| 306 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | * @return integer |
| 308 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 309 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 311 | $v = $this->version(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 313 | $table = func_num_args() > 0 ? func_get_arg(0) : NULL; |
| 314 | $column = func_num_args() > 1 ? func_get_arg(1) : NULL; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 315 | |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 316 | if ($table == NULL && $v >= '8.1') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | { |
| 318 | $sql='SELECT LASTVAL() as ins_id'; |
| 319 | } |
Pascal Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 320 | elseif ($table != NULL && $column != NULL && $v >= '8.0') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | { |
| 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 Kriete | 8761ef5 | 2011-02-14 13:13:52 -0500 | [diff] [blame] | 327 | elseif ($table != NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | * @param string |
| 350 | * @return string |
| 351 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 352 | public function count_all($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | { |
| 354 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 355 | { |
| 356 | return 0; |
| 357 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | |
Andrey Andreev | 032e7ea | 2012-03-06 19:48:35 +0200 | [diff] [blame] | 359 | $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] | 360 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 361 | { |
| 362 | return 0; |
| 363 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | |
| 365 | $row = $query->row(); |
Greg Aker | 90248ab | 2011-08-20 14:23:14 -0500 | [diff] [blame] | 366 | $this->_reset_select(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 367 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | } |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | * @param boolean |
| 378 | * @return string |
| 379 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 380 | protected function _list_tables($prefix_limit = FALSE) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | { |
| 382 | $sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'"; |
| 383 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 385 | { |
Greg Aker | 0d42489 | 2010-01-26 02:14:44 +0000 | [diff] [blame] | 386 | $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] | 387 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 388 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | return $sql; |
| 390 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 391 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | // -------------------------------------------------------------------- |
| 393 | |
| 394 | /** |
| 395 | * Show column query |
| 396 | * |
| 397 | * Generates a platform-specific query string so that the column names can be fetched |
| 398 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | * @param string the table name |
| 400 | * @return string |
| 401 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 402 | public function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | { |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | * @param string the table name |
| 415 | * @return object |
| 416 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 417 | public function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | { |
| 419 | return "SELECT * FROM ".$table." LIMIT 1"; |
| 420 | } |
| 421 | |
| 422 | // -------------------------------------------------------------------- |
| 423 | |
| 424 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 425 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 427 | * Returns an array containing code and message of the last |
| 428 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 430 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 432 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 434 | return array('code' => '', 'message' => pg_last_error($this->conn_id)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // -------------------------------------------------------------------- |
| 438 | |
| 439 | /** |
| 440 | * Escape the SQL Identifiers |
| 441 | * |
| 442 | * This function escapes column and table names |
| 443 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | * @param string |
| 445 | * @return string |
| 446 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 447 | protected function _escape_identifiers($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | { |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 458 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 459 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | // remove duplicates if the user already included the escape |
| 461 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 464 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | if (strpos($item, '.') !== FALSE) |
| 466 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 467 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | } |
| 469 | else |
| 470 | { |
| 471 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 472 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 473 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | // remove duplicates if the user already included the escape |
| 475 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 476 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 477 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | // -------------------------------------------------------------------- |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | * @param type |
| 487 | * @return type |
| 488 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 489 | public function _from_tables($tables) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | { |
| 491 | if ( ! is_array($tables)) |
| 492 | { |
| 493 | $tables = array($tables); |
| 494 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 495 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | return implode(', ', $tables); |
| 497 | } |
| 498 | |
| 499 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 500 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | /** |
| 502 | * Insert statement |
| 503 | * |
| 504 | * Generates a platform-specific insert string from the supplied data |
| 505 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | * @param string the table name |
| 507 | * @param array the insert keys |
| 508 | * @param array the insert values |
| 509 | * @return string |
| 510 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 511 | public function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 512 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 513 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 514 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 515 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 516 | // -------------------------------------------------------------------- |
| 517 | |
| 518 | /** |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 519 | * Insert_batch statement |
| 520 | * |
| 521 | * Generates a platform-specific insert string from the supplied data |
| 522 | * |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 523 | * @param string the table name |
| 524 | * @param array the insert keys |
| 525 | * @param array the insert values |
| 526 | * @return string |
| 527 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 528 | public function _insert_batch($table, $keys, $values) |
Greg Aker | 60ef4ea | 2011-04-27 01:45:38 -0500 | [diff] [blame] | 529 | { |
| 530 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values); |
| 531 | } |
| 532 | |
| 533 | // -------------------------------------------------------------------- |
| 534 | |
| 535 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | * Update statement |
| 537 | * |
| 538 | * Generates a platform-specific update string from the supplied data |
| 539 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | * @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 Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 547 | public function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 548 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 549 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 550 | { |
| 551 | $valstr[] = $key." = ".$val; |
| 552 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 553 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 554 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 555 | |
| 556 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 557 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 558 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | * @param string the table name |
| 571 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 572 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 573 | public function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 574 | { |
| 575 | return "TRUNCATE ".$table; |
| 576 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 577 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | // -------------------------------------------------------------------- |
| 579 | |
| 580 | /** |
| 581 | * Delete statement |
| 582 | * |
| 583 | * Generates a platform-specific delete string from the supplied data |
| 584 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 585 | * @param string the table name |
| 586 | * @param array the where clause |
| 587 | * @param string the limit clause |
| 588 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 589 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 590 | public function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | { |
| 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 | |
SammyK | 1bf8eeb | 2012-03-05 16:56:06 -0600 | [diff] [blame] | 606 | return "DELETE FROM ".$table.$conditions; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | // -------------------------------------------------------------------- |
| 610 | /** |
| 611 | * Limit string |
| 612 | * |
| 613 | * Generates a platform-specific LIMIT clause |
| 614 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 615 | * @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 Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 620 | public function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 621 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | $sql .= "LIMIT ".$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 623 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 624 | if ($offset > 0) |
| 625 | { |
| 626 | $sql .= " OFFSET ".$offset; |
| 627 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 628 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 629 | return $sql; |
| 630 | } |
| 631 | |
| 632 | // -------------------------------------------------------------------- |
| 633 | |
| 634 | /** |
| 635 | * Close DB Connection |
| 636 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 637 | * @param resource |
| 638 | * @return void |
| 639 | */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 640 | public function _close($conn_id) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 641 | { |
| 642 | @pg_close($conn_id); |
| 643 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 646 | /* End of file postgre_driver.php */ |
Timothy Warren | 7a3f897 | 2012-03-19 18:18:27 -0400 | [diff] [blame] | 647 | /* Location: ./system/database/drivers/postgre/postgre_driver.php */ |