blob: c3be519bf70cb74dc7961364ef3add7bdc4322c1 [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
Andrey Andreev77a5d942012-07-05 15:42:14 +030048 protected $_random_keyword = ' Random()'; // database specific random keyword
Andrey Andreev00df2e32012-03-02 18:37:16 +020049
Timothy Warren76e04352012-02-14 11:55:17 -050050 // Keeps track of the resource for the current transaction
51 protected $trans;
52
53 /**
54 * Non-persistent database connection
55 *
Timothy Warren7221f942012-02-14 15:02:33 -050056 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050057 */
Timothy Warren4be822b2012-02-14 12:07:34 -050058 public function db_connect()
Timothy Warren76e04352012-02-14 11:55:17 -050059 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050060 return @ibase_connect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050061 }
62
63 // --------------------------------------------------------------------
64
65 /**
66 * Persistent database connection
67 *
Timothy Warren7221f942012-02-14 15:02:33 -050068 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050069 */
Timothy Warren4be822b2012-02-14 12:07:34 -050070 public function db_pconnect()
Timothy Warren76e04352012-02-14 11:55:17 -050071 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050072 return @ibase_pconnect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050073 }
74
75 // --------------------------------------------------------------------
76
77 /**
Andrey Andreev08856b82012-03-03 03:19:28 +020078 * Database version number
Timothy Warren76e04352012-02-14 11:55:17 -050079 *
Timothy Warren7221f942012-02-14 15:02:33 -050080 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050081 */
Andrey Andreev08856b82012-03-03 03:19:28 +020082 public function version()
Timothy Warren76e04352012-02-14 11:55:17 -050083 {
Andrey Andreev08856b82012-03-03 03:19:28 +020084 if (isset($this->data_cache['version']))
85 {
86 return $this->data_cache['version'];
87 }
88
Timothy Warren3d985a12012-02-15 11:38:00 -050089 if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
90 {
Andrey Andreev08856b82012-03-03 03:19:28 +020091 $this->data_cache['version'] = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
Andrey Andreev00df2e32012-03-02 18:37:16 +020092
Timothy Warrenab189e12012-02-22 10:34:23 -050093 // Don't keep the service open
94 ibase_service_detach($service);
Andrey Andreev08856b82012-03-03 03:19:28 +020095 return $this->data_cache['version'];
Timothy Warren3d985a12012-02-15 11:38:00 -050096 }
Andrey Andreev00df2e32012-03-02 18:37:16 +020097
Timothy Warren3d985a12012-02-15 11:38:00 -050098 return FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -050099 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Execute the query
105 *
Timothy Warren7221f942012-02-14 15:02:33 -0500106 * @param string an SQL query
107 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -0500108 */
Timothy Warren95562142012-02-20 17:37:21 -0500109 protected function _execute($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500110 {
Timothy Warren7221f942012-02-14 15:02:33 -0500111 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500112 }
113
114 // --------------------------------------------------------------------
115
116 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500117 * Begin Transaction
118 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300119 * @param bool $test_mode = FALSE
Timothy Warren7221f942012-02-14 15:02:33 -0500120 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500121 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500122 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500123 {
Timothy Warren76e04352012-02-14 11:55:17 -0500124 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200125 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500126 {
127 return TRUE;
128 }
129
130 // Reset the transaction failure flag.
131 // If the $test_mode flag is set to TRUE transactions will be rolled back
132 // even if the queries produce a successful result.
Andrey Andreev131772c2012-03-20 23:35:03 +0200133 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren76e04352012-02-14 11:55:17 -0500134
Timothy Warren7221f942012-02-14 15:02:33 -0500135 $this->trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200136
Timothy Warren76e04352012-02-14 11:55:17 -0500137 return TRUE;
138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Commit Transaction
144 *
Timothy Warren7221f942012-02-14 15:02:33 -0500145 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500146 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500147 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500148 {
Timothy Warren76e04352012-02-14 11:55:17 -0500149 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200150 if ( ! $this->trans_enabled OR $this->_trans->depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500151 {
152 return TRUE;
153 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200154
Timothy Warren2062a862012-02-20 19:38:10 -0500155 return @ibase_commit($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Rollback Transaction
162 *
Timothy Warren7221f942012-02-14 15:02:33 -0500163 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500164 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500165 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500166 {
Timothy Warren76e04352012-02-14 11:55:17 -0500167 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200168 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500169 {
170 return TRUE;
171 }
172
Timothy Warren2062a862012-02-20 19:38:10 -0500173 return @ibase_rollback($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500174 }
175
176 // --------------------------------------------------------------------
177
178 /**
179 * Escape String
180 *
Timothy Warren7221f942012-02-14 15:02:33 -0500181 * @param string
182 * @param bool whether or not the string will be used in a LIKE condition
183 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500184 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500185 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500186 {
187 if (is_array($str))
188 {
189 foreach ($str as $key => $val)
190 {
191 $str[$key] = $this->escape_str($val, $like);
192 }
193
194 return $str;
195 }
196
197 // escape LIKE condition wildcards
198 if ($like === TRUE)
199 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200200 return str_replace(array($this->_like_escape_chr, '%', '_'),
201 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
202 $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500203 }
204
205 return $str;
206 }
207
208 // --------------------------------------------------------------------
209
210 /**
211 * Affected Rows
212 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200213 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500214 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500215 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500216 {
Timothy Warren7221f942012-02-14 15:02:33 -0500217 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Insert ID
224 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200225 * @param string $generator_name
226 * @param int $inc_by
227 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500228 */
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300229 public function insert_id($generator_name, $inc_by = 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500230 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500231 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500232 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500233 }
234
235 // --------------------------------------------------------------------
236
237 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500238 * List table query
239 *
240 * Generates a platform-specific query string so that the table names can be fetched
241 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200242 * @param bool
Timothy Warren7221f942012-02-14 15:02:33 -0500243 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500244 */
Timothy Warren95562142012-02-20 17:37:21 -0500245 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500246 {
Andrey Andreev60c9c992012-03-20 23:46:09 +0200247 $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 -0500248
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100249 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Timothy Warren76e04352012-02-14 11:55:17 -0500250 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300251 return $sql.' AND "RDB$RELATION_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
252 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Timothy Warren76e04352012-02-14 11:55:17 -0500253 }
Andrey Andreev131772c2012-03-20 23:35:03 +0200254
Timothy Warren76e04352012-02-14 11:55:17 -0500255 return $sql;
256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Show column query
262 *
263 * Generates a platform-specific query string so that the column names can be fetched
264 *
Timothy Warren7221f942012-02-14 15:02:33 -0500265 * @param string the table name
266 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500267 */
Timothy Warren95562142012-02-20 17:37:21 -0500268 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500269 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300270 return 'SELECT "RDB$FIELD_NAME" FROM "RDB$RELATION_FIELDS" WHERE "RDB$RELATION_NAME" = '.$this->escape($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Field data query
277 *
278 * Generates a platform-specific query so that the column data can be retrieved
279 *
Timothy Warren7221f942012-02-14 15:02:33 -0500280 * @param string the table name
Andrey Andreev131772c2012-03-20 23:35:03 +0200281 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500282 */
Timothy Warren95562142012-02-20 17:37:21 -0500283 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500284 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300285 $this->qb_limit = 1;
286 $sql = $this->_limit('SELECT * FROM '.$this->protect_identifiers($table));
287 $this->qb_limit = 0;
288 return $sql;
Timothy Warren76e04352012-02-14 11:55:17 -0500289 }
290
291 // --------------------------------------------------------------------
292
293 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200294 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500295 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200296 * Returns an array containing code and message of the last
297 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500298 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200299 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500300 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200301 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500302 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200303 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500304 }
305
306 // --------------------------------------------------------------------
307
308 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500309 * Update statement
310 *
311 * Generates a platform-specific update string from the supplied data
312 *
Timothy Warren7221f942012-02-14 15:02:33 -0500313 * @param string the table name
314 * @param array the update data
Timothy Warren7221f942012-02-14 15:02:33 -0500315 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500316 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300317 protected function _update($table, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500318 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300319 $this->qb_limit = FALSE;
320 return parent::_update($table, $values);
Timothy Warren76e04352012-02-14 11:55:17 -0500321 }
322
Timothy Warren76e04352012-02-14 11:55:17 -0500323 // --------------------------------------------------------------------
324
325 /**
326 * Truncate statement
327 *
328 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300329 *
330 * If the database does not support the truncate() command,
331 * then this method maps to 'DELETE FROM table'
Timothy Warren76e04352012-02-14 11:55:17 -0500332 *
Timothy Warren7221f942012-02-14 15:02:33 -0500333 * @param string the table name
334 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500335 */
Timothy Warren95562142012-02-20 17:37:21 -0500336 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500337 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300338 return 'DELETE FROM '.$table;
Timothy Warren76e04352012-02-14 11:55:17 -0500339 }
340
341 // --------------------------------------------------------------------
342
343 /**
344 * Delete statement
345 *
346 * Generates a platform-specific delete string from the supplied data
347 *
Timothy Warren7221f942012-02-14 15:02:33 -0500348 * @param string the table name
Timothy Warren7221f942012-02-14 15:02:33 -0500349 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500350 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300351 protected function _delete($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500352 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300353 $this->qb_limit = FALSE;
354 return parent::_delete($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * Limit string
361 *
362 * Generates a platform-specific LIMIT clause
363 *
Timothy Warren7221f942012-02-14 15:02:33 -0500364 * @param string the sql query string
Timothy Warren7221f942012-02-14 15:02:33 -0500365 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500366 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300367 protected function _limit($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500368 {
Timothy Warren56784f62012-02-29 11:58:20 -0500369 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200370 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500371 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300372 $select = 'FIRST '.$this->qb_limit
373 .($this->qb_offset ? ' SKIP '.$this->qb_offset : '');
Timothy Warren56784f62012-02-29 11:58:20 -0500374 }
375 else
376 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200377 $select = 'ROWS '
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300378 .($this->qb_offset ? $this->qb_offset.' TO '.($this->qb_limit + $this->qb_offset) : $this->qb_limit);
Timothy Warren56784f62012-02-29 11:58:20 -0500379 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200380
Andrey Andreev131772c2012-03-20 23:35:03 +0200381 return preg_replace('`SELECT`i', 'SELECT '.$select, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * Close DB Connection
388 *
Timothy Warren7221f942012-02-14 15:02:33 -0500389 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500390 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300391 protected function _close()
Timothy Warren76e04352012-02-14 11:55:17 -0500392 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300393 @ibase_close($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500394 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200395
Timothy Warren76e04352012-02-14 11:55:17 -0500396}
397
Andrey Andreev26086872012-07-05 11:21:58 +0300398/* End of file ibase_driver.php */
399/* Location: ./system/database/drivers/ibase/ibase_driver.php */