blob: 96d6f652699b869706ac7087c605312f472c0e7f [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 *
Timothy Warren7221f942012-02-14 15:02:33 -0500119 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500120 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500121 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500122 {
Timothy Warren76e04352012-02-14 11:55:17 -0500123 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200124 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500125 {
126 return TRUE;
127 }
128
129 // Reset the transaction failure flag.
130 // If the $test_mode flag is set to TRUE transactions will be rolled back
131 // even if the queries produce a successful result.
Andrey Andreev131772c2012-03-20 23:35:03 +0200132 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren76e04352012-02-14 11:55:17 -0500133
Timothy Warren7221f942012-02-14 15:02:33 -0500134 $this->trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200135
Timothy Warren76e04352012-02-14 11:55:17 -0500136 return TRUE;
137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Commit Transaction
143 *
Timothy Warren7221f942012-02-14 15:02:33 -0500144 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500145 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500146 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500147 {
Timothy Warren76e04352012-02-14 11:55:17 -0500148 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200149 if ( ! $this->trans_enabled OR $this->_trans->depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500150 {
151 return TRUE;
152 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200153
Timothy Warren2062a862012-02-20 19:38:10 -0500154 return @ibase_commit($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Rollback Transaction
161 *
Timothy Warren7221f942012-02-14 15:02:33 -0500162 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500163 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500164 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500165 {
Timothy Warren76e04352012-02-14 11:55:17 -0500166 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200167 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500168 {
169 return TRUE;
170 }
171
Timothy Warren2062a862012-02-20 19:38:10 -0500172 return @ibase_rollback($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500173 }
174
175 // --------------------------------------------------------------------
176
177 /**
178 * Escape String
179 *
Timothy Warren7221f942012-02-14 15:02:33 -0500180 * @param string
181 * @param bool whether or not the string will be used in a LIKE condition
182 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500183 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500184 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500185 {
186 if (is_array($str))
187 {
188 foreach ($str as $key => $val)
189 {
190 $str[$key] = $this->escape_str($val, $like);
191 }
192
193 return $str;
194 }
195
196 // escape LIKE condition wildcards
197 if ($like === TRUE)
198 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200199 return str_replace(array($this->_like_escape_chr, '%', '_'),
200 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
201 $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500202 }
203
204 return $str;
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Affected Rows
211 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200212 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500213 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500214 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500215 {
Timothy Warren7221f942012-02-14 15:02:33 -0500216 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * Insert ID
223 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200224 * @param string $generator_name
225 * @param int $inc_by
226 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500227 */
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300228 public function insert_id($generator_name, $inc_by = 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500229 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500230 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500231 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500232 }
233
234 // --------------------------------------------------------------------
235
236 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500237 * List table query
238 *
239 * Generates a platform-specific query string so that the table names can be fetched
240 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200241 * @param bool
Timothy Warren7221f942012-02-14 15:02:33 -0500242 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500243 */
Timothy Warren95562142012-02-20 17:37:21 -0500244 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500245 {
Andrey Andreev60c9c992012-03-20 23:46:09 +0200246 $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 -0500247
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100248 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Timothy Warren76e04352012-02-14 11:55:17 -0500249 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300250 return $sql.' AND "RDB$RELATION_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
251 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Timothy Warren76e04352012-02-14 11:55:17 -0500252 }
Andrey Andreev131772c2012-03-20 23:35:03 +0200253
Timothy Warren76e04352012-02-14 11:55:17 -0500254 return $sql;
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Show column query
261 *
262 * Generates a platform-specific query string so that the column names can be fetched
263 *
Timothy Warren7221f942012-02-14 15:02:33 -0500264 * @param string the table name
265 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500266 */
Timothy Warren95562142012-02-20 17:37:21 -0500267 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500268 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300269 return 'SELECT "RDB$FIELD_NAME" FROM "RDB$RELATION_FIELDS" WHERE "RDB$RELATION_NAME" = '.$this->escape($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Field data query
276 *
277 * Generates a platform-specific query so that the column data can be retrieved
278 *
Timothy Warren7221f942012-02-14 15:02:33 -0500279 * @param string the table name
Andrey Andreev131772c2012-03-20 23:35:03 +0200280 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500281 */
Timothy Warren95562142012-02-20 17:37:21 -0500282 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500283 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300284 $this->qb_limit = 1;
285 $sql = $this->_limit('SELECT * FROM '.$this->protect_identifiers($table));
286 $this->qb_limit = 0;
287 return $sql;
Timothy Warren76e04352012-02-14 11:55:17 -0500288 }
289
290 // --------------------------------------------------------------------
291
292 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200293 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500294 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200295 * Returns an array containing code and message of the last
296 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500297 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200298 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500299 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200300 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500301 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200302 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500303 }
304
305 // --------------------------------------------------------------------
306
307 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500308 * Update statement
309 *
310 * Generates a platform-specific update string from the supplied data
311 *
Timothy Warren7221f942012-02-14 15:02:33 -0500312 * @param string the table name
313 * @param array the update data
Timothy Warren7221f942012-02-14 15:02:33 -0500314 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500315 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300316 protected function _update($table, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500317 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300318 $this->qb_limit = FALSE;
319 return parent::_update($table, $values);
Timothy Warren76e04352012-02-14 11:55:17 -0500320 }
321
Timothy Warren76e04352012-02-14 11:55:17 -0500322 // --------------------------------------------------------------------
323
324 /**
325 * Truncate statement
326 *
327 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300328 *
329 * If the database does not support the truncate() command,
330 * then this method maps to 'DELETE FROM table'
Timothy Warren76e04352012-02-14 11:55:17 -0500331 *
Timothy Warren7221f942012-02-14 15:02:33 -0500332 * @param string the table name
333 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500334 */
Timothy Warren95562142012-02-20 17:37:21 -0500335 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500336 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300337 return 'DELETE FROM '.$table;
Timothy Warren76e04352012-02-14 11:55:17 -0500338 }
339
340 // --------------------------------------------------------------------
341
342 /**
343 * Delete statement
344 *
345 * Generates a platform-specific delete string from the supplied data
346 *
Timothy Warren7221f942012-02-14 15:02:33 -0500347 * @param string the table name
Timothy Warren7221f942012-02-14 15:02:33 -0500348 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500349 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300350 protected function _delete($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500351 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300352 $this->qb_limit = FALSE;
353 return parent::_delete($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Limit string
360 *
361 * Generates a platform-specific LIMIT clause
362 *
Timothy Warren7221f942012-02-14 15:02:33 -0500363 * @param string the sql query string
Timothy Warren7221f942012-02-14 15:02:33 -0500364 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500365 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300366 protected function _limit($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500367 {
Timothy Warren56784f62012-02-29 11:58:20 -0500368 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200369 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500370 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300371 $select = 'FIRST '.$this->qb_limit
372 .($this->qb_offset ? ' SKIP '.$this->qb_offset : '');
Timothy Warren56784f62012-02-29 11:58:20 -0500373 }
374 else
375 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200376 $select = 'ROWS '
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300377 .($this->qb_offset ? $this->qb_offset.' TO '.($this->qb_limit + $this->qb_offset) : $this->qb_limit);
Timothy Warren56784f62012-02-29 11:58:20 -0500378 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200379
Andrey Andreev131772c2012-03-20 23:35:03 +0200380 return preg_replace('`SELECT`i', 'SELECT '.$select, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Close DB Connection
387 *
Timothy Warren7221f942012-02-14 15:02:33 -0500388 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500389 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300390 protected function _close()
Timothy Warren76e04352012-02-14 11:55:17 -0500391 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300392 @ibase_close($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500393 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200394
Timothy Warren76e04352012-02-14 11:55:17 -0500395}
396
Andrey Andreev26086872012-07-05 11:21:58 +0300397/* End of file ibase_driver.php */
398/* Location: ./system/database/drivers/ibase/ibase_driver.php */