blob: 3b5d8f4c6f137bf381f382272e8d8f26d5fae5c6 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Timothy Warren76e04352012-02-14 11:55:17 -050028
Timothy Warren76e04352012-02-14 11:55:17 -050029/**
30 * Firebird/Interbase Database Adapter Class
31 *
32 * Note: _DB is an extender class that the app controller
Jamie Rumbelowffe7a0a2012-04-26 13:48:18 +010033 * creates dynamically based on whether the query builder
Timothy Warren76e04352012-02-14 11:55:17 -050034 * class is being used or not.
35 *
Timothy Warren7221f942012-02-14 15:02:33 -050036 * @package CodeIgniter
37 * @subpackage Drivers
38 * @category Database
39 * @author EllisLab Dev Team
40 * @link http://codeigniter.com/user_guide/database/
Timothy Warren76e04352012-02-14 11:55:17 -050041 */
Andrey Andreev26086872012-07-05 11:21:58 +030042class CI_DB_ibase_driver extends CI_DB {
Timothy Warren76e04352012-02-14 11:55:17 -050043
Andrey Andreev26086872012-07-05 11:21:58 +030044 public $dbdriver = 'ibase';
Timothy Warren76e04352012-02-14 11:55:17 -050045
46 // The character used to escape with
Timothy Warren95562142012-02-20 17:37:21 -050047 protected $_escape_char = '"';
Timothy Warren76e04352012-02-14 11:55:17 -050048
Andrey Andreev77a5d942012-07-05 15:42:14 +030049 protected $_random_keyword = ' Random()'; // database specific random keyword
Andrey Andreev00df2e32012-03-02 18:37:16 +020050
Timothy Warren76e04352012-02-14 11:55:17 -050051 // Keeps track of the resource for the current transaction
52 protected $trans;
53
54 /**
55 * Non-persistent database connection
56 *
Timothy Warren7221f942012-02-14 15:02:33 -050057 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050058 */
Timothy Warren4be822b2012-02-14 12:07:34 -050059 public function db_connect()
Timothy Warren76e04352012-02-14 11:55:17 -050060 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050061 return @ibase_connect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050062 }
63
64 // --------------------------------------------------------------------
65
66 /**
67 * Persistent database connection
68 *
Timothy Warren7221f942012-02-14 15:02:33 -050069 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050070 */
Timothy Warren4be822b2012-02-14 12:07:34 -050071 public function db_pconnect()
Timothy Warren76e04352012-02-14 11:55:17 -050072 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050073 return @ibase_pconnect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050074 }
75
76 // --------------------------------------------------------------------
77
78 /**
Andrey Andreev08856b82012-03-03 03:19:28 +020079 * Database version number
Timothy Warren76e04352012-02-14 11:55:17 -050080 *
Timothy Warren7221f942012-02-14 15:02:33 -050081 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050082 */
Andrey Andreev08856b82012-03-03 03:19:28 +020083 public function version()
Timothy Warren76e04352012-02-14 11:55:17 -050084 {
Andrey Andreev08856b82012-03-03 03:19:28 +020085 if (isset($this->data_cache['version']))
86 {
87 return $this->data_cache['version'];
88 }
89
Timothy Warren3d985a12012-02-15 11:38:00 -050090 if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
91 {
Andrey Andreev08856b82012-03-03 03:19:28 +020092 $this->data_cache['version'] = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
Andrey Andreev00df2e32012-03-02 18:37:16 +020093
Timothy Warrenab189e12012-02-22 10:34:23 -050094 // Don't keep the service open
95 ibase_service_detach($service);
Andrey Andreev08856b82012-03-03 03:19:28 +020096 return $this->data_cache['version'];
Timothy Warren3d985a12012-02-15 11:38:00 -050097 }
Andrey Andreev00df2e32012-03-02 18:37:16 +020098
Timothy Warren3d985a12012-02-15 11:38:00 -050099 return FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -0500100 }
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Execute the query
106 *
Timothy Warren7221f942012-02-14 15:02:33 -0500107 * @param string an SQL query
108 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -0500109 */
Timothy Warren95562142012-02-20 17:37:21 -0500110 protected function _execute($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500111 {
Timothy Warren7221f942012-02-14 15:02:33 -0500112 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500113 }
114
115 // --------------------------------------------------------------------
116
117 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500118 * Begin Transaction
119 *
Andrey Andreev5fd3ae82012-10-24 14:55:35 +0300120 * @param bool $test_mode = FALSE
Timothy Warren7221f942012-02-14 15:02:33 -0500121 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500122 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500123 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500124 {
Timothy Warren76e04352012-02-14 11:55:17 -0500125 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200126 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500127 {
128 return TRUE;
129 }
130
131 // Reset the transaction failure flag.
132 // If the $test_mode flag is set to TRUE transactions will be rolled back
133 // even if the queries produce a successful result.
Andrey Andreev131772c2012-03-20 23:35:03 +0200134 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren76e04352012-02-14 11:55:17 -0500135
Timothy Warren7221f942012-02-14 15:02:33 -0500136 $this->trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200137
Timothy Warren76e04352012-02-14 11:55:17 -0500138 return TRUE;
139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Commit Transaction
145 *
Timothy Warren7221f942012-02-14 15:02:33 -0500146 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500147 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500148 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500149 {
Timothy Warren76e04352012-02-14 11:55:17 -0500150 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200151 if ( ! $this->trans_enabled OR $this->_trans->depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500152 {
153 return TRUE;
154 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200155
Timothy Warren2062a862012-02-20 19:38:10 -0500156 return @ibase_commit($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Rollback Transaction
163 *
Timothy Warren7221f942012-02-14 15:02:33 -0500164 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500165 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500166 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500167 {
Timothy Warren76e04352012-02-14 11:55:17 -0500168 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200169 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500170 {
171 return TRUE;
172 }
173
Timothy Warren2062a862012-02-20 19:38:10 -0500174 return @ibase_rollback($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Escape String
181 *
Timothy Warren7221f942012-02-14 15:02:33 -0500182 * @param string
183 * @param bool whether or not the string will be used in a LIKE condition
184 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500185 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500186 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500187 {
188 if (is_array($str))
189 {
190 foreach ($str as $key => $val)
191 {
192 $str[$key] = $this->escape_str($val, $like);
193 }
194
195 return $str;
196 }
197
198 // escape LIKE condition wildcards
199 if ($like === TRUE)
200 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200201 return str_replace(array($this->_like_escape_chr, '%', '_'),
202 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
203 $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500204 }
205
206 return $str;
207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Affected Rows
213 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200214 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500215 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500216 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500217 {
Timothy Warren7221f942012-02-14 15:02:33 -0500218 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500219 }
220
221 // --------------------------------------------------------------------
222
223 /**
224 * Insert ID
225 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200226 * @param string $generator_name
227 * @param int $inc_by
228 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500229 */
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300230 public function insert_id($generator_name, $inc_by = 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500231 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500232 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500233 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500234 }
235
236 // --------------------------------------------------------------------
237
238 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500239 * List table query
240 *
241 * Generates a platform-specific query string so that the table names can be fetched
242 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200243 * @param bool
Timothy Warren7221f942012-02-14 15:02:33 -0500244 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500245 */
Timothy Warren95562142012-02-20 17:37:21 -0500246 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500247 {
Andrey Andreev60c9c992012-03-20 23:46:09 +0200248 $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 -0500249
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100250 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Timothy Warren76e04352012-02-14 11:55:17 -0500251 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300252 return $sql.' AND "RDB$RELATION_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
253 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Timothy Warren76e04352012-02-14 11:55:17 -0500254 }
Andrey Andreev131772c2012-03-20 23:35:03 +0200255
Timothy Warren76e04352012-02-14 11:55:17 -0500256 return $sql;
257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * Show column query
263 *
264 * Generates a platform-specific query string so that the column names can be fetched
265 *
Timothy Warren7221f942012-02-14 15:02:33 -0500266 * @param string the table name
267 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500268 */
Timothy Warren95562142012-02-20 17:37:21 -0500269 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500270 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300271 return 'SELECT "RDB$FIELD_NAME" FROM "RDB$RELATION_FIELDS" WHERE "RDB$RELATION_NAME" = '.$this->escape($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Field data query
278 *
279 * Generates a platform-specific query so that the column data can be retrieved
280 *
Timothy Warren7221f942012-02-14 15:02:33 -0500281 * @param string the table name
Andrey Andreev131772c2012-03-20 23:35:03 +0200282 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500283 */
Timothy Warren95562142012-02-20 17:37:21 -0500284 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500285 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300286 $this->qb_limit = 1;
287 $sql = $this->_limit('SELECT * FROM '.$this->protect_identifiers($table));
288 $this->qb_limit = 0;
289 return $sql;
Timothy Warren76e04352012-02-14 11:55:17 -0500290 }
291
292 // --------------------------------------------------------------------
293
294 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200295 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500296 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200297 * Returns an array containing code and message of the last
298 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500299 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200300 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500301 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200302 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500303 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200304 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500305 }
306
307 // --------------------------------------------------------------------
308
309 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500310 * Update statement
311 *
312 * Generates a platform-specific update string from the supplied data
313 *
Timothy Warren7221f942012-02-14 15:02:33 -0500314 * @param string the table name
315 * @param array the update data
Timothy Warren7221f942012-02-14 15:02:33 -0500316 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500317 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300318 protected function _update($table, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500319 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300320 $this->qb_limit = FALSE;
321 return parent::_update($table, $values);
Timothy Warren76e04352012-02-14 11:55:17 -0500322 }
323
Timothy Warren76e04352012-02-14 11:55:17 -0500324 // --------------------------------------------------------------------
325
326 /**
327 * Truncate statement
328 *
329 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300330 *
331 * If the database does not support the truncate() command,
332 * then this method maps to 'DELETE FROM table'
Timothy Warren76e04352012-02-14 11:55:17 -0500333 *
Timothy Warren7221f942012-02-14 15:02:33 -0500334 * @param string the table name
335 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500336 */
Timothy Warren95562142012-02-20 17:37:21 -0500337 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500338 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300339 return 'DELETE FROM '.$table;
Timothy Warren76e04352012-02-14 11:55:17 -0500340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Delete statement
346 *
347 * Generates a platform-specific delete string from the supplied data
348 *
Timothy Warren7221f942012-02-14 15:02:33 -0500349 * @param string the table name
Timothy Warren7221f942012-02-14 15:02:33 -0500350 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500351 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300352 protected function _delete($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500353 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300354 $this->qb_limit = FALSE;
355 return parent::_delete($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * Limit string
362 *
363 * Generates a platform-specific LIMIT clause
364 *
Timothy Warren7221f942012-02-14 15:02:33 -0500365 * @param string the sql query string
Timothy Warren7221f942012-02-14 15:02:33 -0500366 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500367 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300368 protected function _limit($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500369 {
Timothy Warren56784f62012-02-29 11:58:20 -0500370 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200371 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500372 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300373 $select = 'FIRST '.$this->qb_limit
374 .($this->qb_offset ? ' SKIP '.$this->qb_offset : '');
Timothy Warren56784f62012-02-29 11:58:20 -0500375 }
376 else
377 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200378 $select = 'ROWS '
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300379 .($this->qb_offset ? $this->qb_offset.' TO '.($this->qb_limit + $this->qb_offset) : $this->qb_limit);
Timothy Warren56784f62012-02-29 11:58:20 -0500380 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200381
Andrey Andreev131772c2012-03-20 23:35:03 +0200382 return preg_replace('`SELECT`i', 'SELECT '.$select, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * Close DB Connection
389 *
Timothy Warren7221f942012-02-14 15:02:33 -0500390 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500391 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300392 protected function _close()
Timothy Warren76e04352012-02-14 11:55:17 -0500393 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300394 @ibase_close($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500395 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200396
Timothy Warren76e04352012-02-14 11:55:17 -0500397}
398
Andrey Andreev26086872012-07-05 11:21:58 +0300399/* End of file ibase_driver.php */
400/* Location: ./system/database/drivers/ibase/ibase_driver.php */