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 |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 61 | |
| 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 | /** |
| 118 | * Set client character set |
| 119 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 120 | * @param string |
| 121 | * @param string |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 122 | * @return bool |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 123 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 124 | public function db_set_charset($charset, $collation) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 125 | { |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 126 | // Must be determined at connection |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 127 | return TRUE; |
| 128 | } |
| 129 | |
| 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | /** |
| 133 | * Version number query string |
| 134 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 135 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 136 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 137 | protected function _version() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 138 | { |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 139 | if (($service = ibase_service_attach($this->hostname, $this->username, $this->password))) |
| 140 | { |
| 141 | $version = ibase_server_info($service, IBASE_SVC_SERVER_VERSION); |
| 142 | return $version; |
| 143 | } |
| 144 | |
| 145 | return FALSE; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // -------------------------------------------------------------------- |
| 149 | |
| 150 | /** |
| 151 | * Execute the query |
| 152 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 153 | * @param string an SQL query |
| 154 | * @return resource |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 155 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 156 | protected function _execute($sql) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 157 | { |
| 158 | $sql = $this->_prep_query($sql); |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 159 | return @ibase_query($this->conn_id, $sql); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Prep the query |
| 166 | * |
| 167 | * If needed, each database adapter can prep the query string |
| 168 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 169 | * @param string an SQL query |
| 170 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 171 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 172 | protected function _prep_query($sql) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 173 | { |
| 174 | return $sql; |
| 175 | } |
| 176 | |
| 177 | // -------------------------------------------------------------------- |
| 178 | |
| 179 | /** |
| 180 | * Begin 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_begin($test_mode = FALSE) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 185 | { |
| 186 | if ( ! $this->trans_enabled) |
| 187 | { |
| 188 | return TRUE; |
| 189 | } |
| 190 | |
| 191 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 192 | if ($this->_trans_depth > 0) |
| 193 | { |
| 194 | return TRUE; |
| 195 | } |
| 196 | |
| 197 | // Reset the transaction failure flag. |
| 198 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 199 | // even if the queries produce a successful result. |
| 200 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
| 201 | |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 202 | $this->trans = @ibase_trans($this->conn_id); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 203 | |
| 204 | return TRUE; |
| 205 | } |
| 206 | |
| 207 | // -------------------------------------------------------------------- |
| 208 | |
| 209 | /** |
| 210 | * Commit Transaction |
| 211 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 212 | * @return bool |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 213 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 214 | public function trans_commit() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 215 | { |
| 216 | if ( ! $this->trans_enabled) |
| 217 | { |
| 218 | return TRUE; |
| 219 | } |
| 220 | |
| 221 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 222 | if ($this->_trans_depth > 0) |
| 223 | { |
| 224 | return TRUE; |
| 225 | } |
| 226 | |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 227 | @ibase_commit($this->trans); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 228 | |
| 229 | return TRUE; |
| 230 | } |
| 231 | |
| 232 | // -------------------------------------------------------------------- |
| 233 | |
| 234 | /** |
| 235 | * Rollback Transaction |
| 236 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 237 | * @return bool |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 238 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 239 | public function trans_rollback() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 240 | { |
| 241 | if ( ! $this->trans_enabled) |
| 242 | { |
| 243 | return TRUE; |
| 244 | } |
| 245 | |
| 246 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 247 | if ($this->_trans_depth > 0) |
| 248 | { |
| 249 | return TRUE; |
| 250 | } |
| 251 | |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 252 | @ibase_rollback($this->trans); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 253 | |
| 254 | return TRUE; |
| 255 | } |
| 256 | |
| 257 | // -------------------------------------------------------------------- |
| 258 | |
| 259 | /** |
| 260 | * Escape String |
| 261 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 262 | * @param string |
| 263 | * @param bool whether or not the string will be used in a LIKE condition |
| 264 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 265 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 266 | public function escape_str($str, $like = FALSE) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 267 | { |
| 268 | if (is_array($str)) |
| 269 | { |
| 270 | foreach ($str as $key => $val) |
| 271 | { |
| 272 | $str[$key] = $this->escape_str($val, $like); |
| 273 | } |
| 274 | |
| 275 | return $str; |
| 276 | } |
| 277 | |
| 278 | // escape LIKE condition wildcards |
| 279 | if ($like === TRUE) |
| 280 | { |
| 281 | $str = str_replace( array('%', '_', $this->_like_escape_chr), |
| 282 | array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr), |
| 283 | $str); |
| 284 | } |
| 285 | |
| 286 | return $str; |
| 287 | } |
| 288 | |
| 289 | // -------------------------------------------------------------------- |
| 290 | |
| 291 | /** |
| 292 | * Affected Rows |
| 293 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 294 | * @return integer |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 295 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 296 | public function affected_rows() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 297 | { |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 298 | return @ibase_affected_rows($this->conn_id); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // -------------------------------------------------------------------- |
| 302 | |
| 303 | /** |
| 304 | * Insert ID |
| 305 | * |
Timothy Warren | 6e821ce | 2012-02-15 18:40:39 -0500 | [diff] [blame] | 306 | * @param string $generator_name |
| 307 | * @param integer $inc_by |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 308 | * @return integer |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 309 | */ |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 310 | public function insert_id($generator_name, $inc_by=0) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 311 | { |
Timothy Warren | 07b660b | 2012-02-20 13:23:06 -0500 | [diff] [blame] | 312 | //If a generator hasn't been used before it will return 0 |
Timothy Warren | fed2d1d | 2012-02-20 15:42:45 -0500 | [diff] [blame] | 313 | return ibase_gen_id('"'.$generator_name.'"', $inc_by); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // -------------------------------------------------------------------- |
| 317 | |
| 318 | /** |
| 319 | * "Count All" query |
| 320 | * |
| 321 | * Generates a platform-specific query string that counts all records in |
| 322 | * the specified database |
| 323 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 324 | * @param string |
| 325 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 326 | */ |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 327 | public function count_all($table = '') |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 328 | { |
| 329 | if ($table == '') |
| 330 | { |
| 331 | return 0; |
| 332 | } |
| 333 | |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 334 | $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] | 335 | |
| 336 | if ($query->num_rows() == 0) |
| 337 | { |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | $row = $query->row(); |
| 342 | $this->_reset_select(); |
| 343 | return (int) $row->numrows; |
| 344 | } |
| 345 | |
| 346 | // -------------------------------------------------------------------- |
| 347 | |
| 348 | /** |
| 349 | * List table query |
| 350 | * |
| 351 | * Generates a platform-specific query string so that the table names can be fetched |
| 352 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 353 | * @param boolean |
| 354 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 355 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 356 | protected function _list_tables($prefix_limit = FALSE) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 357 | { |
| 358 | $sql = <<<SQL |
| 359 | SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS" |
| 360 | WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%' |
| 361 | AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%' |
| 362 | SQL; |
| 363 | |
| 364 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 365 | { |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 366 | $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] | 367 | } |
| 368 | return $sql; |
| 369 | } |
| 370 | |
| 371 | // -------------------------------------------------------------------- |
| 372 | |
| 373 | /** |
| 374 | * Show column query |
| 375 | * |
| 376 | * Generates a platform-specific query string so that the column names can be fetched |
| 377 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 378 | * @param string the table name |
| 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 _list_columns($table = '') |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 382 | { |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 383 | return <<<SQL |
Timothy Warren | eb6dcf0 | 2012-02-15 16:48:51 -0500 | [diff] [blame] | 384 | SELECT "RDB\$FIELD_NAME" FROM "RDB\$RELATION_FIELDS" |
Timothy Warren | c2b712e | 2012-02-17 15:58:08 -0500 | [diff] [blame] | 385 | WHERE "RDB\$RELATION_NAME"='{$table}'; |
Timothy Warren | eb6dcf0 | 2012-02-15 16:48:51 -0500 | [diff] [blame] | 386 | SQL; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // -------------------------------------------------------------------- |
| 390 | |
| 391 | /** |
| 392 | * Field data query |
| 393 | * |
| 394 | * Generates a platform-specific query so that the column data can be retrieved |
| 395 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 396 | * @param string the table name |
| 397 | * @return object |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 398 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 399 | protected function _field_data($table) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 400 | { |
Timothy Warren | 8be31a9 | 2012-02-15 11:48:57 -0500 | [diff] [blame] | 401 | // Need to find a more efficient way to do this |
| 402 | // but Interbase/Firebird seems to lack the |
| 403 | // limit clause |
| 404 | return "SELECT * FROM {$table}"; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | // -------------------------------------------------------------------- |
| 408 | |
| 409 | /** |
| 410 | * The error message string |
| 411 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 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 _error_message() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 415 | { |
| 416 | return ibase_errmsg(); |
| 417 | } |
| 418 | |
| 419 | // -------------------------------------------------------------------- |
| 420 | |
| 421 | /** |
| 422 | * The error message number |
| 423 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 424 | * @return integer |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 425 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 426 | protected function _error_number() |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 427 | { |
| 428 | return ibase_errcode(); |
| 429 | } |
| 430 | |
| 431 | // -------------------------------------------------------------------- |
| 432 | |
| 433 | /** |
| 434 | * Escape the SQL Identifiers |
| 435 | * |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 436 | * This public function escapes column and table names |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 437 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 438 | * @param string |
| 439 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 440 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 441 | protected function _escape_identifiers($item) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 442 | { |
| 443 | foreach ($this->_reserved_identifiers as $id) |
| 444 | { |
| 445 | if (strpos($item, '.'.$id) !== FALSE) |
| 446 | { |
| 447 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 448 | |
| 449 | // remove duplicates if the user already included the escape |
| 450 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | if (strpos($item, '.') !== FALSE) |
| 455 | { |
| 456 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 461 | } |
| 462 | |
| 463 | // remove duplicates if the user already included the escape |
Timothy Warren | d19e47a | 2012-02-14 23:40:40 -0500 | [diff] [blame] | 464 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // -------------------------------------------------------------------- |
| 468 | |
| 469 | /** |
| 470 | * From Tables |
| 471 | * |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 472 | * This public function implicitly groups FROM tables so there is no confusion |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 473 | * about operator precedence in harmony with SQL standards |
| 474 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 475 | * @param type |
| 476 | * @return type |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 477 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 478 | protected function _from_tables($tables) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 479 | { |
| 480 | if ( ! is_array($tables)) |
| 481 | { |
| 482 | $tables = array($tables); |
| 483 | } |
| 484 | |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 485 | //Interbase/Firebird doesn't like grouped tables |
Timothy Warren | d19e47a | 2012-02-14 23:40:40 -0500 | [diff] [blame] | 486 | return implode(', ', $tables); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // -------------------------------------------------------------------- |
| 490 | |
| 491 | /** |
| 492 | * Insert statement |
| 493 | * |
| 494 | * Generates a platform-specific insert string from the supplied data |
| 495 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 496 | * @param string the table name |
| 497 | * @param array the insert keys |
| 498 | * @param array the insert values |
| 499 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 500 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 501 | protected function _insert($table, $keys, $values) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 502 | { |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 503 | return "INSERT INTO {$table} (".implode(', ', $keys).') VALUES ('.implode(', ', $values).')'; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // -------------------------------------------------------------------- |
| 507 | |
| 508 | /** |
| 509 | * Update statement |
| 510 | * |
| 511 | * Generates a platform-specific update string from the supplied data |
| 512 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 513 | * @param string the table name |
| 514 | * @param array the update data |
| 515 | * @param array the where clause |
| 516 | * @param array the orderby clause |
| 517 | * @param array the limit clause |
| 518 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 519 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 520 | protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 521 | { |
| 522 | foreach ($values as $key => $val) |
| 523 | { |
| 524 | $valstr[] = $key." = ".$val; |
| 525 | } |
| 526 | |
Timothy Warren | 8be31a9 | 2012-02-15 11:48:57 -0500 | [diff] [blame] | 527 | //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 528 | |
| 529 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
| 530 | |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 531 | $sql = "UPDATE {$table} SET ".implode(', ', $valstr); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 532 | |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 533 | $sql .= ($where != '' AND count($where) >=1) ? ' WHERE '.implode(' ', $where) : ''; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 534 | |
Timothy Warren | 8be31a9 | 2012-02-15 11:48:57 -0500 | [diff] [blame] | 535 | $sql .= $orderby; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 536 | |
| 537 | return $sql; |
| 538 | } |
| 539 | |
| 540 | |
| 541 | // -------------------------------------------------------------------- |
| 542 | |
| 543 | /** |
| 544 | * Truncate statement |
| 545 | * |
| 546 | * Generates a platform-specific truncate string from the supplied data |
| 547 | * If the database does not support the truncate() command |
Timothy Warren | 4be822b | 2012-02-14 12:07:34 -0500 | [diff] [blame] | 548 | * This public function maps to "DELETE FROM table" |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 549 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 550 | * @param string the table name |
| 551 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 552 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 553 | protected function _truncate($table) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 554 | { |
| 555 | return $this->_delete($table); |
| 556 | } |
| 557 | |
| 558 | // -------------------------------------------------------------------- |
| 559 | |
| 560 | /** |
| 561 | * Delete statement |
| 562 | * |
| 563 | * Generates a platform-specific delete string from the supplied data |
| 564 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 565 | * @param string the table name |
| 566 | * @param array the where clause |
| 567 | * @param string the limit clause |
| 568 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 569 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 570 | protected function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 571 | { |
| 572 | $conditions = ''; |
| 573 | |
| 574 | if (count($where) > 0 OR count($like) > 0) |
| 575 | { |
| 576 | $conditions = "\nWHERE "; |
| 577 | $conditions .= implode("\n", $this->ar_where); |
| 578 | |
| 579 | if (count($where) > 0 && count($like) > 0) |
| 580 | { |
Timothy Warren | 817af19 | 2012-02-16 08:28:00 -0500 | [diff] [blame] | 581 | $conditions .= ' AND '; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 582 | } |
| 583 | $conditions .= implode("\n", $like); |
| 584 | } |
| 585 | |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 586 | //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 587 | |
Timothy Warren | 8be31a9 | 2012-02-15 11:48:57 -0500 | [diff] [blame] | 588 | return "DELETE FROM {$table}{$conditions}"; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | // -------------------------------------------------------------------- |
| 592 | |
| 593 | /** |
| 594 | * Limit string |
| 595 | * |
| 596 | * Generates a platform-specific LIMIT clause |
| 597 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 598 | * @param string the sql query string |
| 599 | * @param integer the number of rows to limit the query to |
| 600 | * @param integer the offset value |
| 601 | * @return string |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 602 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 603 | protected function _limit($sql, $limit, $offset) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 604 | { |
Timothy Warren | 3d985a1 | 2012-02-15 11:38:00 -0500 | [diff] [blame] | 605 | //There doesn't seem to be a limit clause? |
| 606 | return $sql; |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | // -------------------------------------------------------------------- |
| 610 | |
| 611 | /** |
| 612 | * Close DB Connection |
| 613 | * |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 614 | * @param resource |
| 615 | * @return void |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 616 | */ |
Timothy Warren | 9556214 | 2012-02-20 17:37:21 -0500 | [diff] [blame^] | 617 | protected function _close($conn_id) |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 618 | { |
Timothy Warren | 7221f94 | 2012-02-14 15:02:33 -0500 | [diff] [blame] | 619 | @ibase_close($conn_id); |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 620 | } |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 621 | } |
| 622 | |
Timothy Warren | 76e0435 | 2012-02-14 11:55:17 -0500 | [diff] [blame] | 623 | /* End of file interbase_driver.php */ |
| 624 | /* Location: ./system/database/drivers/interbase/interbase_driver.php */ |