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