blob: b9eabd00e92b6a4a177b391321759431db4bdd68 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Timothy Warren7221f942012-02-14 15:02:33 -050022 * @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 *
Andrey Andreev98e46cf2012-11-13 03:01:42 +020056 * @var array
Andrey Andreeva24e52e2012-11-02 03:54:12 +020057 */
Andrey Andreev98e46cf2012-11-13 03:01:42 +020058 protected $_random_keyword = array('RAND()', 'RAND()');
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 *
Andrey Andreev2e171022014-02-25 15:21:41 +020072 * @param bool $persistent
Timothy Warren7221f942012-02-14 15:02:33 -050073 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050074 */
Andrey Andreev2e171022014-02-25 15:21:41 +020075 public function db_connect($persistent = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -050076 {
Andrey Andreev2e171022014-02-25 15:21:41 +020077 return ($persistent === TRUE)
Andrey Andreevf2818bd2014-02-25 15:26:20 +020078 ? ibase_pconnect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set)
79 : ibase_connect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050080 }
81
82 // --------------------------------------------------------------------
83
84 /**
Andrey Andreev08856b82012-03-03 03:19:28 +020085 * Database version number
Timothy Warren76e04352012-02-14 11:55:17 -050086 *
Timothy Warren7221f942012-02-14 15:02:33 -050087 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -050088 */
Andrey Andreev08856b82012-03-03 03:19:28 +020089 public function version()
Timothy Warren76e04352012-02-14 11:55:17 -050090 {
Andrey Andreev08856b82012-03-03 03:19:28 +020091 if (isset($this->data_cache['version']))
92 {
93 return $this->data_cache['version'];
94 }
95
Timothy Warren3d985a12012-02-15 11:38:00 -050096 if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
97 {
Andrey Andreev08856b82012-03-03 03:19:28 +020098 $this->data_cache['version'] = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
Andrey Andreev00df2e32012-03-02 18:37:16 +020099
Timothy Warrenab189e12012-02-22 10:34:23 -0500100 // Don't keep the service open
101 ibase_service_detach($service);
Andrey Andreev08856b82012-03-03 03:19:28 +0200102 return $this->data_cache['version'];
Timothy Warren3d985a12012-02-15 11:38:00 -0500103 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200104
Timothy Warren3d985a12012-02-15 11:38:00 -0500105 return FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -0500106 }
107
108 // --------------------------------------------------------------------
109
110 /**
111 * Execute the query
112 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200113 * @param string $sql an SQL query
Timothy Warren7221f942012-02-14 15:02:33 -0500114 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -0500115 */
Timothy Warren95562142012-02-20 17:37:21 -0500116 protected function _execute($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500117 {
Timothy Warren7221f942012-02-14 15:02:33 -0500118 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500119 }
120
121 // --------------------------------------------------------------------
122
123 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500124 * Begin Transaction
125 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200126 * @param bool $test_mode
Timothy Warren7221f942012-02-14 15:02:33 -0500127 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500128 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500129 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500130 {
Timothy Warren76e04352012-02-14 11:55:17 -0500131 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200132 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500133 {
134 return TRUE;
135 }
136
137 // Reset the transaction failure flag.
138 // If the $test_mode flag is set to TRUE transactions will be rolled back
139 // even if the queries produce a successful result.
Andrey Andreev131772c2012-03-20 23:35:03 +0200140 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren76e04352012-02-14 11:55:17 -0500141
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200142 $this->_ibase_trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200143
Timothy Warren76e04352012-02-14 11:55:17 -0500144 return TRUE;
145 }
146
147 // --------------------------------------------------------------------
148
149 /**
150 * Commit Transaction
151 *
Timothy Warren7221f942012-02-14 15:02:33 -0500152 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500153 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500154 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500155 {
Timothy Warren76e04352012-02-14 11:55:17 -0500156 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200157 if ( ! $this->trans_enabled OR $this->_trans->depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500158 {
159 return TRUE;
160 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200161
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200162 return @ibase_commit($this->_ibase_trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Rollback Transaction
169 *
Timothy Warren7221f942012-02-14 15:02:33 -0500170 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500171 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500172 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500173 {
Timothy Warren76e04352012-02-14 11:55:17 -0500174 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200175 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500176 {
177 return TRUE;
178 }
179
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200180 return @ibase_rollback($this->_ibase_trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500181 }
182
183 // --------------------------------------------------------------------
184
185 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500186 * Affected Rows
187 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200188 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500189 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500190 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500191 {
Timothy Warren7221f942012-02-14 15:02:33 -0500192 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Insert ID
199 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200200 * @param string $generator_name
201 * @param int $inc_by
202 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500203 */
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300204 public function insert_id($generator_name, $inc_by = 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500205 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500206 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500207 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500208 }
209
210 // --------------------------------------------------------------------
211
212 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500213 * List table query
214 *
215 * Generates a platform-specific query string so that the table names can be fetched
216 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200217 * @param bool $prefix_limit
Timothy Warren7221f942012-02-14 15:02:33 -0500218 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500219 */
Timothy Warren95562142012-02-20 17:37:21 -0500220 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500221 {
Andrey Andreev60c9c992012-03-20 23:46:09 +0200222 $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 -0500223
Alex Bilbie48a2baf2012-06-02 11:09:54 +0100224 if ($prefix_limit !== FALSE && $this->dbprefix !== '')
Timothy Warren76e04352012-02-14 11:55:17 -0500225 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300226 return $sql.' AND "RDB$RELATION_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
227 .sprintf($this->_like_escape_str, $this->_like_escape_chr);
Timothy Warren76e04352012-02-14 11:55:17 -0500228 }
Andrey Andreev131772c2012-03-20 23:35:03 +0200229
Timothy Warren76e04352012-02-14 11:55:17 -0500230 return $sql;
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * Show column query
237 *
238 * Generates a platform-specific query string so that the column names can be fetched
239 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200240 * @param string $table
Timothy Warren7221f942012-02-14 15:02:33 -0500241 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500242 */
Timothy Warren95562142012-02-20 17:37:21 -0500243 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500244 {
Andrey Andreev5382b1b2012-06-25 01:26:48 +0300245 return 'SELECT "RDB$FIELD_NAME" FROM "RDB$RELATION_FIELDS" WHERE "RDB$RELATION_NAME" = '.$this->escape($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500246 }
247
248 // --------------------------------------------------------------------
249
250 /**
Andrey Andreev02e7a9d2012-11-16 01:49:46 +0200251 * Returns an object with field data
Timothy Warren76e04352012-02-14 11:55:17 -0500252 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200253 * @param string $table
Andrey Andreev02e7a9d2012-11-16 01:49:46 +0200254 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500255 */
Andrey Andreev02e7a9d2012-11-16 01:49:46 +0200256 public function field_data($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500257 {
Andrey Andreev02e7a9d2012-11-16 01:49:46 +0200258 if ($table === '')
259 {
260 return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE;
261 }
262
263 $sql = 'SELECT "rfields"."RDB$FIELD_NAME" AS "name",
264 CASE "fields"."RDB$FIELD_TYPE"
265 WHEN 7 THEN \'SMALLINT\'
266 WHEN 8 THEN \'INTEGER\'
267 WHEN 9 THEN \'QUAD\'
268 WHEN 10 THEN \'FLOAT\'
269 WHEN 11 THEN \'DFLOAT\'
270 WHEN 12 THEN \'DATE\'
271 WHEN 13 THEN \'TIME\'
272 WHEN 14 THEN \'CHAR\'
273 WHEN 16 THEN \'INT64\'
274 WHEN 27 THEN \'DOUBLE\'
275 WHEN 35 THEN \'TIMESTAMP\'
276 WHEN 37 THEN \'VARCHAR\'
277 WHEN 40 THEN \'CSTRING\'
278 WHEN 261 THEN \'BLOB\'
279 ELSE NULL
280 END AS "type",
281 "fields"."RDB$FIELD_LENGTH" AS "max_length",
282 "rfields"."RDB$DEFAULT_VALUE" AS "default"
283 FROM "RDB$RELATION_FIELDS" "rfields"
284 JOIN "RDB$FIELDS" "fields" ON "rfields"."RDB$FIELD_SOURCE" = "fields"."RDB$FIELD_NAME"
285 WHERE "rfields"."RDB$RELATION_NAME" = '.$this->escape($table).'
286 ORDER BY "rfields"."RDB$FIELD_POSITION"';
287
288 return (($query = $this->query($sql)) !== FALSE)
289 ? $query->result_object()
290 : FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -0500291 }
292
293 // --------------------------------------------------------------------
294
295 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200296 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500297 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200298 * Returns an array containing code and message of the last
299 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500300 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200301 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500302 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200303 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500304 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200305 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500306 }
307
308 // --------------------------------------------------------------------
309
310 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500311 * Update statement
312 *
313 * Generates a platform-specific update string from the supplied data
314 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200315 * @param string $table
316 * @param array $values
Timothy Warren7221f942012-02-14 15:02:33 -0500317 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500318 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300319 protected function _update($table, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500320 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300321 $this->qb_limit = FALSE;
322 return parent::_update($table, $values);
Timothy Warren76e04352012-02-14 11:55:17 -0500323 }
324
Timothy Warren76e04352012-02-14 11:55:17 -0500325 // --------------------------------------------------------------------
326
327 /**
328 * Truncate statement
329 *
330 * Generates a platform-specific truncate string from the supplied data
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300331 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200332 * If the database does not support the TRUNCATE statement,
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300333 * then this method maps to 'DELETE FROM table'
Timothy Warren76e04352012-02-14 11:55:17 -0500334 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200335 * @param string $table
Timothy Warren7221f942012-02-14 15:02:33 -0500336 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500337 */
Timothy Warren95562142012-02-20 17:37:21 -0500338 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500339 {
Andrey Andreeva6fe36e2012-04-05 16:00:32 +0300340 return 'DELETE FROM '.$table;
Timothy Warren76e04352012-02-14 11:55:17 -0500341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * Delete statement
347 *
348 * Generates a platform-specific delete string from the supplied data
349 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200350 * @param string $table
Timothy Warren7221f942012-02-14 15:02:33 -0500351 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500352 */
Andrey Andreevb0478652012-07-18 15:34:46 +0300353 protected function _delete($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500354 {
Andrey Andreevb0478652012-07-18 15:34:46 +0300355 $this->qb_limit = FALSE;
356 return parent::_delete($table);
Timothy Warren76e04352012-02-14 11:55:17 -0500357 }
358
359 // --------------------------------------------------------------------
360
361 /**
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200362 * LIMIT
Timothy Warren76e04352012-02-14 11:55:17 -0500363 *
364 * Generates a platform-specific LIMIT clause
365 *
Andrey Andreeva24e52e2012-11-02 03:54:12 +0200366 * @param string $sql SQL Query
Timothy Warren7221f942012-02-14 15:02:33 -0500367 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500368 */
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300369 protected function _limit($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500370 {
Timothy Warren56784f62012-02-29 11:58:20 -0500371 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200372 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500373 {
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300374 $select = 'FIRST '.$this->qb_limit
375 .($this->qb_offset ? ' SKIP '.$this->qb_offset : '');
Timothy Warren56784f62012-02-29 11:58:20 -0500376 }
377 else
378 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200379 $select = 'ROWS '
Andrey Andreevc9b924c2012-07-19 13:06:02 +0300380 .($this->qb_offset ? $this->qb_offset.' TO '.($this->qb_limit + $this->qb_offset) : $this->qb_limit);
Timothy Warren56784f62012-02-29 11:58:20 -0500381 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200382
Timothy Warrena5410872013-01-09 10:44:14 -0500383 return preg_replace('`SELECT`i', 'SELECT '.$select, $sql, 1);
Timothy Warren76e04352012-02-14 11:55:17 -0500384 }
385
386 // --------------------------------------------------------------------
387
388 /**
389 * Close DB Connection
390 *
Timothy Warren7221f942012-02-14 15:02:33 -0500391 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500392 */
Andrey Andreev79922c02012-05-23 12:27:17 +0300393 protected function _close()
Timothy Warren76e04352012-02-14 11:55:17 -0500394 {
Andrey Andreev79922c02012-05-23 12:27:17 +0300395 @ibase_close($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500396 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200397
Timothy Warren76e04352012-02-14 11:55:17 -0500398}
399
Andrey Andreev26086872012-07-05 11:21:58 +0300400/* End of file ibase_driver.php */
401/* Location: ./system/database/drivers/ibase/ibase_driver.php */