blob: fc1c28f31db8c0e891fa17d85f5c7386db4a99f7 [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 Andreeva24e52e2012-11-02 03:54:12 +020044 /**
45 * Database driver
46 *
47 * @var string
48 */
Andrey Andreev26086872012-07-05 11:21:58 +030049 public $dbdriver = 'ibase';
Timothy Warren76e04352012-02-14 11:55:17 -050050
Andrey Andreeva24e52e2012-11-02 03:54:12 +020051 // --------------------------------------------------------------------
Timothy Warren76e04352012-02-14 11:55:17 -050052
Andrey Andreeva24e52e2012-11-02 03:54:12 +020053 /**
54 * ORDER BY random keyword
55 *
56 * @var string
57 */
58 protected $_random_keyword = ' Random()';
Andrey Andreev00df2e32012-03-02 18:37:16 +020059
Andrey Andreeva24e52e2012-11-02 03:54:12 +020060 /**
61 * IBase Transaction status flag
62 *
63 * @var resource
64 */
65 protected $_ibase_trans;
66
67 // --------------------------------------------------------------------
Timothy Warren76e04352012-02-14 11:55:17 -050068
69 /**
70 * Non-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_connect()
Timothy Warren76e04352012-02-14 11:55:17 -050075 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050076 return @ibase_connect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050077 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Persistent database connection
83 *
Timothy Warren7221f942012-02-14 15:02:33 -050084 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050085 */
Timothy Warren4be822b2012-02-14 12:07:34 -050086 public function db_pconnect()
Timothy Warren76e04352012-02-14 11:55:17 -050087 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050088 return @ibase_pconnect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050089 }
90
91 // --------------------------------------------------------------------
92
93 /**
Andrey Andreev08856b82012-03-03 03:19:28 +020094 * Database version number
Timothy Warren76e04352012-02-14 11:55:17 -050095 *
Timothy Warren7221f942012-02-14 15:02:33 -050096 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050097 */
Andrey Andreev08856b82012-03-03 03:19:28 +020098 public function version()
Timothy Warren76e04352012-02-14 11:55:17 -050099 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200100 if (isset($this->data_cache['version']))
101 {
102 return $this->data_cache['version'];
103 }
104
Timothy Warren3d985a12012-02-15 11:38:00 -0500105 if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
106 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200107 $this->data_cache['version'] = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200108
Timothy Warrenab189e12012-02-22 10:34:23 -0500109 // Don't keep the service open
110 ibase_service_detach($service);
Andrey Andreev08856b82012-03-03 03:19:28 +0200111 return $this->data_cache['version'];
Timothy Warren3d985a12012-02-15 11:38:00 -0500112 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200113
Timothy Warren3d985a12012-02-15 11:38:00 -0500114 return FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -0500115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Execute the query
121 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200122 * @param string $sql an SQL query
Timothy Warren7221f942012-02-14 15:02:33 -0500123 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -0500124 */
Timothy Warren95562142012-02-20 17:37:21 -0500125 protected function _execute($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500126 {
Timothy Warren7221f942012-02-14 15:02:33 -0500127 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500128 }
129
130 // --------------------------------------------------------------------
131
132 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500133 * Begin Transaction
134 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200135 * @param bool $test_mode
Timothy Warren7221f942012-02-14 15:02:33 -0500136 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500137 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500138 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500139 {
Timothy Warren76e04352012-02-14 11:55:17 -0500140 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200141 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500142 {
143 return TRUE;
144 }
145
146 // Reset the transaction failure flag.
147 // If the $test_mode flag is set to TRUE transactions will be rolled back
148 // even if the queries produce a successful result.
Andrey Andreev131772c2012-03-20 23:35:03 +0200149 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren76e04352012-02-14 11:55:17 -0500150
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200151 $this->_ibase_trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200152
Timothy Warren76e04352012-02-14 11:55:17 -0500153 return TRUE;
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Commit Transaction
160 *
Timothy Warren7221f942012-02-14 15:02:33 -0500161 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500162 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500163 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500164 {
Timothy Warren76e04352012-02-14 11:55:17 -0500165 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200166 if ( ! $this->trans_enabled OR $this->_trans->depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500167 {
168 return TRUE;
169 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200170
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200171 return @ibase_commit($this->_ibase_trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Rollback Transaction
178 *
Timothy Warren7221f942012-02-14 15:02:33 -0500179 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500180 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500181 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500182 {
Timothy Warren76e04352012-02-14 11:55:17 -0500183 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200184 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500185 {
186 return TRUE;
187 }
188
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200189 return @ibase_rollback($this->_ibase_trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500190 }
191
192 // --------------------------------------------------------------------
193
194 /**
195 * Escape String
196 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200197 * @param string $str
198 * @param bool $like Whether or not the string will be used in a LIKE condition
Timothy Warren7221f942012-02-14 15:02:33 -0500199 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500200 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500201 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500202 {
203 if (is_array($str))
204 {
205 foreach ($str as $key => $val)
206 {
207 $str[$key] = $this->escape_str($val, $like);
208 }
209
210 return $str;
211 }
212
213 // escape LIKE condition wildcards
214 if ($like === TRUE)
215 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200216 return str_replace(array($this->_like_escape_chr, '%', '_'),
217 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
218 $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500219 }
220
221 return $str;
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Affected Rows
228 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200229 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500230 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500231 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500232 {
Timothy Warren7221f942012-02-14 15:02:33 -0500233 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Insert ID
240 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200241 * @param string $generator_name
242 * @param int $inc_by
243 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500244 */
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300245 public function insert_id($generator_name, $inc_by = 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500246 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500247 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500248 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500249 }
250
251 // --------------------------------------------------------------------
252
253 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500254 * List table query
255 *
256 * Generates a platform-specific query string so that the table names can be fetched
257 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200258 * @param bool $prefix_limit
Timothy Warren7221f942012-02-14 15:02:33 -0500259 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500260 */
Timothy Warren95562142012-02-20 17:37:21 -0500261 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500262 {
Andrey Andreev60c9c992012-03-20 23:46:09 +0200263 $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 -0500264
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100265 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Timothy Warren76e04352012-02-14 11:55:17 -0500266 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300267 return $sql.' AND "RDB$RELATION_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
268 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Timothy Warren76e04352012-02-14 11:55:17 -0500269 }
Andrey Andreev131772c2012-03-20 23:35:03 +0200270
Timothy Warren76e04352012-02-14 11:55:17 -0500271 return $sql;
272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Show column query
278 *
279 * Generates a platform-specific query string so that the column names can be fetched
280 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200281 * @param string $table
Timothy Warren7221f942012-02-14 15:02:33 -0500282 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500283 */
Timothy Warren95562142012-02-20 17:37:21 -0500284 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500285 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300286 return 'SELECT "RDB$FIELD_NAME" FROM "RDB$RELATION_FIELDS" WHERE "RDB$RELATION_NAME" = '.$this->escape($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Field data query
293 *
294 * Generates a platform-specific query so that the column data can be retrieved
295 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200296 * @param string $table
Andrey Andreev131772c2012-03-20 23:35:03 +0200297 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500298 */
Timothy Warren95562142012-02-20 17:37:21 -0500299 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500300 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300301 $this->qb_limit = 1;
302 $sql = $this->_limit('SELECT * FROM '.$this->protect_identifiers($table));
303 $this->qb_limit = 0;
304 return $sql;
Timothy Warren76e04352012-02-14 11:55:17 -0500305 }
306
307 // --------------------------------------------------------------------
308
309 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200310 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500311 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200312 * Returns an array containing code and message of the last
313 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500314 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200315 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500316 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200317 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500318 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200319 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500320 }
321
322 // --------------------------------------------------------------------
323
324 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500325 * Update statement
326 *
327 * Generates a platform-specific update string from the supplied data
328 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200329 * @param string $table
330 * @param array $values
Timothy Warren7221f942012-02-14 15:02:33 -0500331 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500332 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300333 protected function _update($table, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500334 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300335 $this->qb_limit = FALSE;
336 return parent::_update($table, $values);
Timothy Warren76e04352012-02-14 11:55:17 -0500337 }
338
Timothy Warren76e04352012-02-14 11:55:17 -0500339 // --------------------------------------------------------------------
340
341 /**
342 * Truncate statement
343 *
344 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300345 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200346 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300347 * then this method maps to 'DELETE FROM table'
Timothy Warren76e04352012-02-14 11:55:17 -0500348 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200349 * @param string $table
Timothy Warren7221f942012-02-14 15:02:33 -0500350 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500351 */
Timothy Warren95562142012-02-20 17:37:21 -0500352 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500353 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300354 return 'DELETE FROM '.$table;
Timothy Warren76e04352012-02-14 11:55:17 -0500355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * Delete statement
361 *
362 * Generates a platform-specific delete string from the supplied data
363 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200364 * @param string $table
Timothy Warren7221f942012-02-14 15:02:33 -0500365 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500366 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300367 protected function _delete($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500368 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300369 $this->qb_limit = FALSE;
370 return parent::_delete($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500371 }
372
373 // --------------------------------------------------------------------
374
375 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200376 * LIMIT
Timothy Warren76e04352012-02-14 11:55:17 -0500377 *
378 * Generates a platform-specific LIMIT clause
379 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200380 * @param string $sql SQL Query
Timothy Warren7221f942012-02-14 15:02:33 -0500381 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500382 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300383 protected function _limit($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500384 {
Timothy Warren56784f62012-02-29 11:58:20 -0500385 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200386 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500387 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300388 $select = 'FIRST '.$this->qb_limit
389 .($this->qb_offset ? ' SKIP '.$this->qb_offset : '');
Timothy Warren56784f62012-02-29 11:58:20 -0500390 }
391 else
392 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200393 $select = 'ROWS '
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300394 .($this->qb_offset ? $this->qb_offset.' TO '.($this->qb_limit + $this->qb_offset) : $this->qb_limit);
Timothy Warren56784f62012-02-29 11:58:20 -0500395 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200396
Andrey Andreev131772c2012-03-20 23:35:03 +0200397 return preg_replace('`SELECT`i', 'SELECT '.$select, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500398 }
399
400 // --------------------------------------------------------------------
401
402 /**
403 * Close DB Connection
404 *
Timothy Warren7221f942012-02-14 15:02:33 -0500405 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500406 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300407 protected function _close()
Timothy Warren76e04352012-02-14 11:55:17 -0500408 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300409 @ibase_close($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500410 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200411
Timothy Warren76e04352012-02-14 11:55:17 -0500412}
413
Andrey Andreev26086872012-07-05 11:21:58 +0300414/* End of file ibase_driver.php */
415/* Location: ./system/database/drivers/ibase/ibase_driver.php */