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