blob: 7b37b9999400e849b4bafd6728a0c39bdbb42d5d [file] [log] [blame]
Timothy Warren817af192012-02-16 08:28:00 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Timothy Warren76e04352012-02-14 11:55:17 -05002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Timothy Warren76e04352012-02-14 11:55:17 -05006 *
7 * NOTICE OF LICENSE
Timothy Warren817af192012-02-16 08:28:00 -05008 *
Timothy Warren76e04352012-02-14 11:55:17 -05009 * Licensed under the Open Software License version 3.0
Timothy Warren817af192012-02-16 08:28:00 -050010 *
Timothy Warren76e04352012-02-14 11:55:17 -050011 * 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 Warren7221f942012-02-14 15:02:33 -050017 * licensing@ellislab.com so we can send you a copy immediately.
Timothy Warren76e04352012-02-14 11:55:17 -050018 *
Timothy Warren7221f942012-02-14 15:02:33 -050019 * @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 Warren76e04352012-02-14 11:55:17 -050026 */
27
Timothy Warren76e04352012-02-14 11:55:17 -050028/**
29 * Firebird/Interbase Database Adapter Class
30 *
31 * Note: _DB is an extender class that the app controller
Jamie Rumbelowffe7a0a2012-04-26 13:48:18 +010032 * creates dynamically based on whether the query builder
Timothy Warren76e04352012-02-14 11:55:17 -050033 * class is being used or not.
34 *
Timothy Warren7221f942012-02-14 15:02:33 -050035 * @package CodeIgniter
36 * @subpackage Drivers
37 * @category Database
38 * @author EllisLab Dev Team
39 * @link http://codeigniter.com/user_guide/database/
Timothy Warren76e04352012-02-14 11:55:17 -050040 */
Andrey Andreev26086872012-07-05 11:21:58 +030041class CI_DB_ibase_driver extends CI_DB {
Timothy Warren76e04352012-02-14 11:55:17 -050042
Andrey Andreev26086872012-07-05 11:21:58 +030043 public $dbdriver = 'ibase';
Timothy Warren76e04352012-02-14 11:55:17 -050044
45 // The character used to escape with
Timothy Warren95562142012-02-20 17:37:21 -050046 protected $_escape_char = '"';
Timothy Warren76e04352012-02-14 11:55:17 -050047
48 // clause and character used for LIKE escape sequences
Timothy Warren95562142012-02-20 17:37:21 -050049 protected $_like_escape_str = " ESCAPE '%s' ";
50 protected $_like_escape_chr = '!';
Timothy Warren76e04352012-02-14 11:55:17 -050051
Andrey Andreev77a5d942012-07-05 15:42:14 +030052 protected $_random_keyword = ' Random()'; // database specific random keyword
Andrey Andreev00df2e32012-03-02 18:37:16 +020053
Timothy Warren76e04352012-02-14 11:55:17 -050054 // Keeps track of the resource for the current transaction
55 protected $trans;
56
57 /**
58 * Non-persistent database connection
59 *
Timothy Warren7221f942012-02-14 15:02:33 -050060 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050061 */
Timothy Warren4be822b2012-02-14 12:07:34 -050062 public function db_connect()
Timothy Warren76e04352012-02-14 11:55:17 -050063 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050064 return @ibase_connect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050065 }
66
67 // --------------------------------------------------------------------
68
69 /**
70 * Persistent database connection
71 *
Timothy Warren7221f942012-02-14 15:02:33 -050072 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050073 */
Timothy Warren4be822b2012-02-14 12:07:34 -050074 public function db_pconnect()
Timothy Warren76e04352012-02-14 11:55:17 -050075 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050076 return @ibase_pconnect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050077 }
78
79 // --------------------------------------------------------------------
80
81 /**
Andrey Andreev08856b82012-03-03 03:19:28 +020082 * Database version number
Timothy Warren76e04352012-02-14 11:55:17 -050083 *
Timothy Warren7221f942012-02-14 15:02:33 -050084 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050085 */
Andrey Andreev08856b82012-03-03 03:19:28 +020086 public function version()
Timothy Warren76e04352012-02-14 11:55:17 -050087 {
Andrey Andreev08856b82012-03-03 03:19:28 +020088 if (isset($this->data_cache['version']))
89 {
90 return $this->data_cache['version'];
91 }
92
Timothy Warren3d985a12012-02-15 11:38:00 -050093 if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
94 {
Andrey Andreev08856b82012-03-03 03:19:28 +020095 $this->data_cache['version'] = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
Andrey Andreev00df2e32012-03-02 18:37:16 +020096
Timothy Warrenab189e12012-02-22 10:34:23 -050097 // Don't keep the service open
98 ibase_service_detach($service);
Andrey Andreev08856b82012-03-03 03:19:28 +020099 return $this->data_cache['version'];
Timothy Warren3d985a12012-02-15 11:38:00 -0500100 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200101
Timothy Warren3d985a12012-02-15 11:38:00 -0500102 return FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -0500103 }
104
105 // --------------------------------------------------------------------
106
107 /**
108 * Execute the query
109 *
Timothy Warren7221f942012-02-14 15:02:33 -0500110 * @param string an SQL query
111 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -0500112 */
Timothy Warren95562142012-02-20 17:37:21 -0500113 protected function _execute($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500114 {
Timothy Warren7221f942012-02-14 15:02:33 -0500115 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500116 }
117
118 // --------------------------------------------------------------------
119
120 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500121 * Begin Transaction
122 *
Timothy Warren7221f942012-02-14 15:02:33 -0500123 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500124 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500125 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500126 {
Timothy Warren76e04352012-02-14 11:55:17 -0500127 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200128 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500129 {
130 return TRUE;
131 }
132
133 // Reset the transaction failure flag.
134 // If the $test_mode flag is set to TRUE transactions will be rolled back
135 // even if the queries produce a successful result.
Andrey Andreev131772c2012-03-20 23:35:03 +0200136 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren76e04352012-02-14 11:55:17 -0500137
Timothy Warren7221f942012-02-14 15:02:33 -0500138 $this->trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200139
Timothy Warren76e04352012-02-14 11:55:17 -0500140 return TRUE;
141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Commit Transaction
147 *
Timothy Warren7221f942012-02-14 15:02:33 -0500148 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500149 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500150 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500151 {
Timothy Warren76e04352012-02-14 11:55:17 -0500152 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200153 if ( ! $this->trans_enabled OR $this->_trans->depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500154 {
155 return TRUE;
156 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200157
Timothy Warren2062a862012-02-20 19:38:10 -0500158 return @ibase_commit($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500159 }
160
161 // --------------------------------------------------------------------
162
163 /**
164 * Rollback Transaction
165 *
Timothy Warren7221f942012-02-14 15:02:33 -0500166 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500167 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500168 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500169 {
Timothy Warren76e04352012-02-14 11:55:17 -0500170 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200171 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500172 {
173 return TRUE;
174 }
175
Timothy Warren2062a862012-02-20 19:38:10 -0500176 return @ibase_rollback($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500177 }
178
179 // --------------------------------------------------------------------
180
181 /**
182 * Escape String
183 *
Timothy Warren7221f942012-02-14 15:02:33 -0500184 * @param string
185 * @param bool whether or not the string will be used in a LIKE condition
186 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500187 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500188 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500189 {
190 if (is_array($str))
191 {
192 foreach ($str as $key => $val)
193 {
194 $str[$key] = $this->escape_str($val, $like);
195 }
196
197 return $str;
198 }
199
200 // escape LIKE condition wildcards
201 if ($like === TRUE)
202 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200203 return str_replace(array($this->_like_escape_chr, '%', '_'),
204 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
205 $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500206 }
207
208 return $str;
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Affected Rows
215 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200216 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500217 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500218 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500219 {
Timothy Warren7221f942012-02-14 15:02:33 -0500220 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Insert ID
227 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200228 * @param string $generator_name
229 * @param int $inc_by
230 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500231 */
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300232 public function insert_id($generator_name, $inc_by = 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500233 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500234 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500235 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500236 }
237
238 // --------------------------------------------------------------------
239
240 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500241 * List table query
242 *
243 * Generates a platform-specific query string so that the table names can be fetched
244 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200245 * @param bool
Timothy Warren7221f942012-02-14 15:02:33 -0500246 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500247 */
Timothy Warren95562142012-02-20 17:37:21 -0500248 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500249 {
Andrey Andreev60c9c992012-03-20 23:46:09 +0200250 $sql = 'SELECT "RDB$RELATION_NAME" FROM "RDB$RELATIONS" WHERE "RDB$RELATION_NAME" NOT LIKE \'RDB$%\' AND "RDB$RELATION_NAME" NOT LIKE \'MON$%\'';
Timothy Warren76e04352012-02-14 11:55:17 -0500251
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100252 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Timothy Warren76e04352012-02-14 11:55:17 -0500253 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300254 return $sql.' AND "RDB$RELATION_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
255 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Timothy Warren76e04352012-02-14 11:55:17 -0500256 }
Andrey Andreev131772c2012-03-20 23:35:03 +0200257
Timothy Warren76e04352012-02-14 11:55:17 -0500258 return $sql;
259 }
260
261 // --------------------------------------------------------------------
262
263 /**
264 * Show column query
265 *
266 * Generates a platform-specific query string so that the column names can be fetched
267 *
Timothy Warren7221f942012-02-14 15:02:33 -0500268 * @param string the table name
269 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500270 */
Timothy Warren95562142012-02-20 17:37:21 -0500271 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500272 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300273 return 'SELECT "RDB$FIELD_NAME" FROM "RDB$RELATION_FIELDS" WHERE "RDB$RELATION_NAME" = '.$this->escape($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500274 }
275
276 // --------------------------------------------------------------------
277
278 /**
279 * Field data query
280 *
281 * Generates a platform-specific query so that the column data can be retrieved
282 *
Timothy Warren7221f942012-02-14 15:02:33 -0500283 * @param string the table name
Andrey Andreev131772c2012-03-20 23:35:03 +0200284 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500285 */
Timothy Warren95562142012-02-20 17:37:21 -0500286 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500287 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300288 $this->qb_limit = 1;
289 $sql = $this->_limit('SELECT * FROM '.$this->protect_identifiers($table));
290 $this->qb_limit = 0;
291 return $sql;
Timothy Warren76e04352012-02-14 11:55:17 -0500292 }
293
294 // --------------------------------------------------------------------
295
296 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200297 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500298 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200299 * Returns an array containing code and message of the last
300 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500301 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200302 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500303 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200304 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500305 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200306 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500307 }
308
309 // --------------------------------------------------------------------
310
311 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500312 * From Tables
313 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500314 * This public function implicitly groups FROM tables so there is no confusion
Timothy Warren76e04352012-02-14 11:55:17 -0500315 * about operator precedence in harmony with SQL standards
316 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200317 * @param array
318 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500319 */
Timothy Warren95562142012-02-20 17:37:21 -0500320 protected function _from_tables($tables)
Timothy Warren76e04352012-02-14 11:55:17 -0500321 {
Andrey Andreevc78e56a2012-06-08 02:12:07 +0300322 return is_array($tables) ? implode(', ', $tables) : $tables;
Timothy Warren76e04352012-02-14 11:55:17 -0500323 }
324
325 // --------------------------------------------------------------------
326
327 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500328 * Update statement
329 *
330 * Generates a platform-specific update string from the supplied data
331 *
Timothy Warren7221f942012-02-14 15:02:33 -0500332 * @param string the table name
333 * @param array the update data
Timothy Warren7221f942012-02-14 15:02:33 -0500334 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500335 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300336 protected function _update($table, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500337 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300338 $this->qb_limit = FALSE;
339 return parent::_update($table, $values);
Timothy Warren76e04352012-02-14 11:55:17 -0500340 }
341
Timothy Warren76e04352012-02-14 11:55:17 -0500342 // --------------------------------------------------------------------
343
344 /**
345 * Truncate statement
346 *
347 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300348 *
349 * If the database does not support the truncate() command,
350 * then this method maps to 'DELETE FROM table'
Timothy Warren76e04352012-02-14 11:55:17 -0500351 *
Timothy Warren7221f942012-02-14 15:02:33 -0500352 * @param string the table name
353 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500354 */
Timothy Warren95562142012-02-20 17:37:21 -0500355 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500356 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300357 return 'DELETE FROM '.$table;
Timothy Warren76e04352012-02-14 11:55:17 -0500358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Delete statement
364 *
365 * Generates a platform-specific delete string from the supplied data
366 *
Timothy Warren7221f942012-02-14 15:02:33 -0500367 * @param string the table name
Timothy Warren7221f942012-02-14 15:02:33 -0500368 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500369 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300370 protected function _delete($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500371 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300372 $this->qb_limit = FALSE;
373 return parent::_delete($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Limit string
380 *
381 * Generates a platform-specific LIMIT clause
382 *
Timothy Warren7221f942012-02-14 15:02:33 -0500383 * @param string the sql query string
Timothy Warren7221f942012-02-14 15:02:33 -0500384 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500385 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300386 protected function _limit($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500387 {
Timothy Warren56784f62012-02-29 11:58:20 -0500388 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200389 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500390 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300391 $select = 'FIRST '.$this->qb_limit
392 .($this->qb_offset ? ' SKIP '.$this->qb_offset : '');
Timothy Warren56784f62012-02-29 11:58:20 -0500393 }
394 else
395 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200396 $select = 'ROWS '
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300397 .($this->qb_offset ? $this->qb_offset.' TO '.($this->qb_limit + $this->qb_offset) : $this->qb_limit);
Timothy Warren56784f62012-02-29 11:58:20 -0500398 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200399
Andrey Andreev131772c2012-03-20 23:35:03 +0200400 return preg_replace('`SELECT`i', 'SELECT '.$select, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Close DB Connection
407 *
Timothy Warren7221f942012-02-14 15:02:33 -0500408 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500409 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300410 protected function _close()
Timothy Warren76e04352012-02-14 11:55:17 -0500411 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300412 @ibase_close($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500413 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200414
Timothy Warren76e04352012-02-14 11:55:17 -0500415}
416
Andrey Andreev26086872012-07-05 11:21:58 +0300417/* End of file ibase_driver.php */
418/* Location: ./system/database/drivers/ibase/ibase_driver.php */