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