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