Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 6 | * |
| 7 | * NOTICE OF LICENSE |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 8 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 10 | * |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -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 |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 17 | * licensing@ellislab.com so we can send you a copy immediately. |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 18 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 23 | * @link http://codeigniter.com |
| 24 | * @since Version 3.0 |
| 25 | * @filesource |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 30 | /** |
| 31 | * Firebird/Interbase 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 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 37 | * @package CodeIgniter |
| 38 | * @subpackage Drivers |
| 39 | * @category Database |
| 40 | * @author EllisLab Dev Team |
| 41 | * @link http://codeigniter.com/user_guide/database/ |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 42 | */ |
| 43 | class CI_DB_interbase_driver extends CI_DB { |
| 44 | |
| 45 | public $dbdriver = 'interbase'; |
| 46 | |
| 47 | // The character used to escape with |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 48 | protected $_escape_char = '"'; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 49 | |
| 50 | // clause and character used for LIKE escape sequences |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 51 | protected $_like_escape_str = " ESCAPE '%s' "; |
| 52 | protected $_like_escape_chr = '!'; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * The syntax to count rows is slightly different across different |
| 56 | * database engines, so this string appears in each driver and is |
| 57 | * used for the count_all() and count_all_results() functions. |
| 58 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 59 | protected $_count_string = "SELECT COUNT(*) AS "; |
| 60 | protected $_random_keyword = ' Random()'; // database specific random keyword |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 61 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 62 | // Keeps track of the resource for the current transaction |
| 63 | protected $trans; |
| 64 | |
| 65 | /** |
| 66 | * Non-persistent database connection |
| 67 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 68 | * @return resource |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 69 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 70 | public function db_connect() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 71 | { |
Timothy Warren | 3c4281f | 2012-02-16 19:00:10 -0500 | [diff] [blame] | 72 | return @ibase_connect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // -------------------------------------------------------------------- |
| 76 | |
| 77 | /** |
| 78 | * Persistent database connection |
| 79 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 80 | * @return resource |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 81 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 82 | public function db_pconnect() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 83 | { |
Timothy Warren | 3c4281f | 2012-02-16 19:00:10 -0500 | [diff] [blame] | 84 | return @ibase_pconnect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // -------------------------------------------------------------------- |
| 88 | |
| 89 | /** |
| 90 | * Reconnect |
| 91 | * |
| 92 | * Keep / reestablish the db connection if no queries have been |
| 93 | * sent for a length of time exceeding the server's idle timeout |
| 94 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 95 | * @return void |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 96 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 97 | public function reconnect() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 98 | { |
| 99 | // not implemented in Interbase/Firebird |
| 100 | } |
| 101 | |
| 102 | // -------------------------------------------------------------------- |
| 103 | |
| 104 | /** |
| 105 | * Select the database |
| 106 | * |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 107 | * @return bool |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 108 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 109 | public function db_select() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 110 | { |
| 111 | // Connection selects the database |
| 112 | return TRUE; |
| 113 | } |
| 114 | |
| 115 | // -------------------------------------------------------------------- |
| 116 | |
| 117 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame^] | 118 | * Database version number |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 119 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 120 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 121 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame^] | 122 | public function version() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 123 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame^] | 124 | if (isset($this->data_cache['version'])) |
| 125 | { |
| 126 | return $this->data_cache['version']; |
| 127 | } |
| 128 | |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 129 | if (($service = ibase_service_attach($this->hostname, $this->username, $this->password))) |
| 130 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame^] | 131 | $this->data_cache['version'] = ibase_server_info($service, IBASE_SVC_SERVER_VERSION); |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 132 | |
Timothy Warren | ab189e1 | 2012-02-22 10:34:23 -0500 | [diff] [blame] | 133 | // Don't keep the service open |
| 134 | ibase_service_detach($service); |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame^] | 135 | return $this->data_cache['version']; |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 136 | } |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 137 | |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 138 | return FALSE; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // -------------------------------------------------------------------- |
| 142 | |
| 143 | /** |
| 144 | * Execute the query |
| 145 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 146 | * @param string an SQL query |
| 147 | * @return resource |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 148 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 149 | protected function _execute($sql) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 150 | { |
| 151 | $sql = $this->_prep_query($sql); |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 152 | return @ibase_query($this->conn_id, $sql); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // -------------------------------------------------------------------- |
| 156 | |
| 157 | /** |
| 158 | * Prep the query |
| 159 | * |
| 160 | * If needed, each database adapter can prep the query string |
| 161 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 162 | * @param string an SQL query |
| 163 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 164 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 165 | protected function _prep_query($sql) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 166 | { |
| 167 | return $sql; |
| 168 | } |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
| 171 | |
| 172 | /** |
| 173 | * Begin Transaction |
| 174 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 175 | * @return bool |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 176 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 177 | public function trans_begin($test_mode = FALSE) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 178 | { |
| 179 | if ( ! $this->trans_enabled) |
| 180 | { |
| 181 | return TRUE; |
| 182 | } |
| 183 | |
| 184 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 185 | if ($this->_trans_depth > 0) |
| 186 | { |
| 187 | return TRUE; |
| 188 | } |
| 189 | |
| 190 | // Reset the transaction failure flag. |
| 191 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 192 | // even if the queries produce a successful result. |
| 193 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 194 | |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 195 | $this->trans = @ibase_trans($this->conn_id); |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 196 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 197 | return TRUE; |
| 198 | } |
| 199 | |
| 200 | // -------------------------------------------------------------------- |
| 201 | |
| 202 | /** |
| 203 | * Commit Transaction |
| 204 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 205 | * @return bool |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 206 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 207 | public function trans_commit() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 208 | { |
| 209 | if ( ! $this->trans_enabled) |
| 210 | { |
| 211 | return TRUE; |
| 212 | } |
| 213 | |
| 214 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 215 | if ($this->_trans_depth > 0) |
| 216 | { |
| 217 | return TRUE; |
| 218 | } |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 219 | |
Timothy Warren | 2062a86 | 2012-02-20 19:38:10 -0500 | [diff] [blame] | 220 | return @ibase_commit($this->trans); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // -------------------------------------------------------------------- |
| 224 | |
| 225 | /** |
| 226 | * Rollback Transaction |
| 227 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 228 | * @return bool |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 229 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 230 | public function trans_rollback() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 231 | { |
| 232 | if ( ! $this->trans_enabled) |
| 233 | { |
| 234 | return TRUE; |
| 235 | } |
| 236 | |
| 237 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 238 | if ($this->_trans_depth > 0) |
| 239 | { |
| 240 | return TRUE; |
| 241 | } |
| 242 | |
Timothy Warren | 2062a86 | 2012-02-20 19:38:10 -0500 | [diff] [blame] | 243 | return @ibase_rollback($this->trans); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // -------------------------------------------------------------------- |
| 247 | |
| 248 | /** |
| 249 | * Escape String |
| 250 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 251 | * @param string |
| 252 | * @param bool whether or not the string will be used in a LIKE condition |
| 253 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 254 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 255 | public function escape_str($str, $like = FALSE) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 256 | { |
| 257 | if (is_array($str)) |
| 258 | { |
| 259 | foreach ($str as $key => $val) |
| 260 | { |
| 261 | $str[$key] = $this->escape_str($val, $like); |
| 262 | } |
| 263 | |
| 264 | return $str; |
| 265 | } |
| 266 | |
| 267 | // escape LIKE condition wildcards |
| 268 | if ($like === TRUE) |
| 269 | { |
| 270 | $str = str_replace( array('%', '_', $this->_like_escape_chr), |
| 271 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 272 | $str); |
| 273 | } |
| 274 | |
| 275 | return $str; |
| 276 | } |
| 277 | |
| 278 | // -------------------------------------------------------------------- |
| 279 | |
| 280 | /** |
| 281 | * Affected Rows |
| 282 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 283 | * @return integer |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 284 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 285 | public function affected_rows() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 286 | { |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 287 | return @ibase_affected_rows($this->conn_id); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // -------------------------------------------------------------------- |
| 291 | |
| 292 | /** |
| 293 | * Insert ID |
| 294 | * |
Timothy Warren | 125fe73 | 2012-02-21 07:58:25 -0500 | [diff] [blame] | 295 | * @param string $generator_name |
| 296 | * @param integer $inc_by |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 297 | * @return integer |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 298 | */ |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 299 | public function insert_id($generator_name, $inc_by=0) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 300 | { |
Timothy Warren | 07b660b | 2012-02-20 13:23:06 -0500 | [diff] [blame] | 301 | //If a generator hasn't been used before it will return 0 |
Timothy Warren | fed2d1d | 2012-02-20 15:42:45 -0500 | [diff] [blame] | 302 | return ibase_gen_id('"'.$generator_name.'"', $inc_by); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // -------------------------------------------------------------------- |
| 306 | |
| 307 | /** |
| 308 | * "Count All" query |
| 309 | * |
| 310 | * Generates a platform-specific query string that counts all records in |
| 311 | * the specified database |
| 312 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 313 | * @param string |
| 314 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 315 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 316 | public function count_all($table = '') |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 317 | { |
| 318 | if ($table == '') |
| 319 | { |
| 320 | return 0; |
| 321 | } |
| 322 | |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 323 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . ' FROM ' . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 324 | |
| 325 | if ($query->num_rows() == 0) |
| 326 | { |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | $row = $query->row(); |
| 331 | $this->_reset_select(); |
| 332 | return (int) $row->numrows; |
| 333 | } |
| 334 | |
| 335 | // -------------------------------------------------------------------- |
| 336 | |
| 337 | /** |
| 338 | * List table query |
| 339 | * |
| 340 | * Generates a platform-specific query string so that the table names can be fetched |
| 341 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 342 | * @param boolean |
| 343 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 344 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 345 | protected function _list_tables($prefix_limit = FALSE) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 346 | { |
| 347 | $sql = <<<SQL |
| 348 | SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS" |
| 349 | WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%' |
| 350 | AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%' |
| 351 | SQL; |
| 352 | |
| 353 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 354 | { |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 355 | $sql .= ' AND "RDB$RELATION_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 356 | } |
| 357 | return $sql; |
| 358 | } |
| 359 | |
| 360 | // -------------------------------------------------------------------- |
| 361 | |
| 362 | /** |
| 363 | * Show column query |
| 364 | * |
| 365 | * Generates a platform-specific query string so that the column names can be fetched |
| 366 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 367 | * @param string the table name |
| 368 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 369 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 370 | protected function _list_columns($table = '') |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 371 | { |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 372 | return <<<SQL |
Timothy Warren | eb6dcf0 | 2012-02-15 16:48:51 -0500 | [diff] [blame] | 373 | SELECT "RDB\$FIELD_NAME" FROM "RDB\$RELATION_FIELDS" |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 374 | WHERE "RDB\$RELATION_NAME"='{$table}'; |
Timothy Warren | eb6dcf0 | 2012-02-15 16:48:51 -0500 | [diff] [blame] | 375 | SQL; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // -------------------------------------------------------------------- |
| 379 | |
| 380 | /** |
| 381 | * Field data query |
| 382 | * |
| 383 | * Generates a platform-specific query so that the column data can be retrieved |
| 384 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 385 | * @param string the table name |
| 386 | * @return object |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 387 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 388 | protected function _field_data($table) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 389 | { |
Timothy Warren | 8be31a9 | 2012-02-15 11:48:57 -0500 | [diff] [blame] | 390 | // Need to find a more efficient way to do this |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 391 | // but Interbase/Firebird seems to lack the |
Timothy Warren | 8be31a9 | 2012-02-15 11:48:57 -0500 | [diff] [blame] | 392 | // limit clause |
| 393 | return "SELECT * FROM {$table}"; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // -------------------------------------------------------------------- |
| 397 | |
| 398 | /** |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 399 | * Error |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 400 | * |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 401 | * Returns an array containing code and message of the last |
| 402 | * database error that has occured. |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 403 | * |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 404 | * @return array |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 405 | */ |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 406 | public function error() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 407 | { |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 408 | return array('code' => ibase_errcode(), 'message' => ibase_errmsg()); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // -------------------------------------------------------------------- |
| 412 | |
| 413 | /** |
| 414 | * Escape the SQL Identifiers |
| 415 | * |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 416 | * This public function escapes column and table names |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 417 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 418 | * @param string |
| 419 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 420 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 421 | protected function _escape_identifiers($item) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 422 | { |
| 423 | foreach ($this->_reserved_identifiers as $id) |
| 424 | { |
| 425 | if (strpos($item, '.'.$id) !== FALSE) |
| 426 | { |
| 427 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 428 | |
| 429 | // remove duplicates if the user already included the escape |
| 430 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (strpos($item, '.') !== FALSE) |
| 435 | { |
| 436 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 441 | } |
| 442 | |
| 443 | // remove duplicates if the user already included the escape |
Timothy Warren | d19e47a | 2012-02-14 23:40:40 -0500 | [diff] [blame] | 444 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // -------------------------------------------------------------------- |
| 448 | |
| 449 | /** |
| 450 | * From Tables |
| 451 | * |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 452 | * This public function implicitly groups FROM tables so there is no confusion |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 453 | * about operator precedence in harmony with SQL standards |
| 454 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 455 | * @param type |
| 456 | * @return type |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 457 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 458 | protected function _from_tables($tables) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 459 | { |
| 460 | if ( ! is_array($tables)) |
| 461 | { |
| 462 | $tables = array($tables); |
| 463 | } |
| 464 | |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 465 | //Interbase/Firebird doesn't like grouped tables |
Timothy Warren | d19e47a | 2012-02-14 23:40:40 -0500 | [diff] [blame] | 466 | return implode(', ', $tables); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // -------------------------------------------------------------------- |
| 470 | |
| 471 | /** |
| 472 | * Insert statement |
| 473 | * |
| 474 | * Generates a platform-specific insert string from the supplied data |
| 475 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 476 | * @param string the table name |
| 477 | * @param array the insert keys |
| 478 | * @param array the insert values |
| 479 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 480 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 481 | protected function _insert($table, $keys, $values) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 482 | { |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 483 | return "INSERT INTO {$table} (".implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | // -------------------------------------------------------------------- |
| 487 | |
| 488 | /** |
| 489 | * Update statement |
| 490 | * |
| 491 | * Generates a platform-specific update string from the supplied data |
| 492 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 493 | * @param string the table name |
| 494 | * @param array the update data |
| 495 | * @param array the where clause |
| 496 | * @param array the orderby clause |
| 497 | * @param array the limit clause |
| 498 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 499 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 500 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 501 | { |
| 502 | foreach ($values as $key => $val) |
| 503 | { |
| 504 | $valstr[] = $key." = ".$val; |
| 505 | } |
| 506 | |
Timothy Warren | 8be31a9 | 2012-02-15 11:48:57 -0500 | [diff] [blame] | 507 | //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 508 | |
| 509 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
| 510 | |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 511 | $sql = "UPDATE {$table} SET ".implode(', ', $valstr); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 512 | |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 513 | $sql .= ($where != '' AND count($where) >=1) ? ' WHERE '.implode(' ', $where) : ''; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 514 | |
Timothy Warren | 8be31a9 | 2012-02-15 11:48:57 -0500 | [diff] [blame] | 515 | $sql .= $orderby; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 516 | |
| 517 | return $sql; |
| 518 | } |
| 519 | |
| 520 | |
| 521 | // -------------------------------------------------------------------- |
| 522 | |
| 523 | /** |
| 524 | * Truncate statement |
| 525 | * |
| 526 | * Generates a platform-specific truncate string from the supplied data |
| 527 | * If the database does not support the truncate() command |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 528 | * This public function maps to "DELETE FROM table" |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 529 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 530 | * @param string the table name |
| 531 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 532 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 533 | protected function _truncate($table) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 534 | { |
| 535 | return $this->_delete($table); |
| 536 | } |
| 537 | |
| 538 | // -------------------------------------------------------------------- |
| 539 | |
| 540 | /** |
| 541 | * Delete statement |
| 542 | * |
| 543 | * Generates a platform-specific delete string from the supplied data |
| 544 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 545 | * @param string the table name |
| 546 | * @param array the where clause |
| 547 | * @param string the limit clause |
| 548 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 549 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 550 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 551 | { |
| 552 | $conditions = ''; |
| 553 | |
| 554 | if (count($where) > 0 OR count($like) > 0) |
| 555 | { |
| 556 | $conditions = "\nWHERE "; |
| 557 | $conditions .= implode("\n", $this->ar_where); |
| 558 | |
| 559 | if (count($where) > 0 && count($like) > 0) |
| 560 | { |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 561 | $conditions .= ' AND '; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 562 | } |
| 563 | $conditions .= implode("\n", $like); |
| 564 | } |
| 565 | |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 566 | //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 567 | |
Timothy Warren | 8be31a9 | 2012-02-15 11:48:57 -0500 | [diff] [blame] | 568 | return "DELETE FROM {$table}{$conditions}"; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // -------------------------------------------------------------------- |
| 572 | |
| 573 | /** |
| 574 | * Limit string |
| 575 | * |
| 576 | * Generates a platform-specific LIMIT clause |
| 577 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 578 | * @param string the sql query string |
| 579 | * @param integer the number of rows to limit the query to |
| 580 | * @param integer the offset value |
| 581 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 582 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 583 | protected function _limit($sql, $limit, $offset) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 584 | { |
Timothy Warren | 56784f6 | 2012-02-29 11:58:20 -0500 | [diff] [blame] | 585 | // Keep the current sql string safe for a moment |
| 586 | $orig_sql = $sql; |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 587 | |
Timothy Warren | 56784f6 | 2012-02-29 11:58:20 -0500 | [diff] [blame] | 588 | // Limit clause depends on if Interbase or Firebird |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame^] | 589 | if (stripos($this->version(), 'firebird') !== FALSE) |
Timothy Warren | 56784f6 | 2012-02-29 11:58:20 -0500 | [diff] [blame] | 590 | { |
| 591 | $sql = 'FIRST '. (int) $limit; |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 592 | |
Timothy Warren | 56784f6 | 2012-02-29 11:58:20 -0500 | [diff] [blame] | 593 | if ($offset > 0) |
| 594 | { |
Timothy Warren | 9a96082 | 2012-02-29 22:53:35 -0500 | [diff] [blame] | 595 | $sql .= ' SKIP '. (int) $offset; |
Timothy Warren | 56784f6 | 2012-02-29 11:58:20 -0500 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | else |
| 599 | { |
| 600 | $sql = 'ROWS ' . (int) $limit; |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 601 | |
Timothy Warren | 56784f6 | 2012-02-29 11:58:20 -0500 | [diff] [blame] | 602 | if ($offset > 0) |
| 603 | { |
| 604 | $sql = 'ROWS '. (int) $offset . ' TO ' . ($limit + $offset); |
| 605 | } |
| 606 | } |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 607 | |
| 608 | return preg_replace('`SELECT`i', "SELECT {$sql}", $orig_sql); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | // -------------------------------------------------------------------- |
| 612 | |
| 613 | /** |
| 614 | * Close DB Connection |
| 615 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 616 | * @param resource |
| 617 | * @return void |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 618 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame] | 619 | protected function _close($conn_id) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 620 | { |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 621 | @ibase_close($conn_id); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 622 | } |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 623 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 624 | } |
| 625 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 626 | /* End of file interbase_driver.php */ |
Andrey Andreev | 00df2e3 | 2012-03-02 18:37:16 +0200 | [diff] [blame] | 627 | /* Location: ./system/database/drivers/interbase/interbase_driver.php */ |