blob: d8b6ae571db3ac66dec74a82b36eda0db080da98 [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
32 * creates dynamically based on whether the active record
33 * 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 */
41class CI_DB_interbase_driver extends CI_DB {
42
43 public $dbdriver = 'interbase';
44
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
48 // clause and character used for LIKE escape sequences
Timothy Warren95562142012-02-20 17:37:21 -050049 protected $_like_escape_str = " ESCAPE '%s' ";
50 protected $_like_escape_chr = '!';
Timothy Warren76e04352012-02-14 11:55:17 -050051
52 /**
53 * The syntax to count rows is slightly different across different
54 * database engines, so this string appears in each driver and is
55 * used for the count_all() and count_all_results() functions.
56 */
Andrey Andreev131772c2012-03-20 23:35:03 +020057 protected $_count_string = 'SELECT COUNT(*) AS ';
58 protected $_random_keyword = ' Random()'; // database specific random keyword
Andrey Andreev00df2e32012-03-02 18:37:16 +020059
Timothy Warren76e04352012-02-14 11:55:17 -050060 // Keeps track of the resource for the current transaction
61 protected $trans;
62
63 /**
64 * Non-persistent database connection
65 *
Timothy Warren7221f942012-02-14 15:02:33 -050066 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050067 */
Timothy Warren4be822b2012-02-14 12:07:34 -050068 public function db_connect()
Timothy Warren76e04352012-02-14 11:55:17 -050069 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050070 return @ibase_connect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050071 }
72
73 // --------------------------------------------------------------------
74
75 /**
76 * Persistent database connection
77 *
Timothy Warren7221f942012-02-14 15:02:33 -050078 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050079 */
Timothy Warren4be822b2012-02-14 12:07:34 -050080 public function db_pconnect()
Timothy Warren76e04352012-02-14 11:55:17 -050081 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050082 return @ibase_pconnect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050083 }
84
85 // --------------------------------------------------------------------
86
87 /**
Timothy Warren76e04352012-02-14 11:55:17 -050088 * Select the database
89 *
Timothy Warrenc2b712e2012-02-17 15:58:08 -050090 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -050091 */
Timothy Warren4be822b2012-02-14 12:07:34 -050092 public function db_select()
Timothy Warren76e04352012-02-14 11:55:17 -050093 {
94 // Connection selects the database
95 return TRUE;
96 }
97
98 // --------------------------------------------------------------------
99
100 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200101 * Database version number
Timothy Warren76e04352012-02-14 11:55:17 -0500102 *
Timothy Warren7221f942012-02-14 15:02:33 -0500103 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500104 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200105 public function version()
Timothy Warren76e04352012-02-14 11:55:17 -0500106 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200107 if (isset($this->data_cache['version']))
108 {
109 return $this->data_cache['version'];
110 }
111
Timothy Warren3d985a12012-02-15 11:38:00 -0500112 if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
113 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200114 $this->data_cache['version'] = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200115
Timothy Warrenab189e12012-02-22 10:34:23 -0500116 // Don't keep the service open
117 ibase_service_detach($service);
Andrey Andreev08856b82012-03-03 03:19:28 +0200118 return $this->data_cache['version'];
Timothy Warren3d985a12012-02-15 11:38:00 -0500119 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200120
Timothy Warren3d985a12012-02-15 11:38:00 -0500121 return FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -0500122 }
123
124 // --------------------------------------------------------------------
125
126 /**
127 * Execute the query
128 *
Timothy Warren7221f942012-02-14 15:02:33 -0500129 * @param string an SQL query
130 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -0500131 */
Timothy Warren95562142012-02-20 17:37:21 -0500132 protected function _execute($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500133 {
Timothy Warren7221f942012-02-14 15:02:33 -0500134 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500135 }
136
137 // --------------------------------------------------------------------
138
139 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500140 * Begin Transaction
141 *
Timothy Warren7221f942012-02-14 15:02:33 -0500142 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500143 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500144 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500145 {
Timothy Warren76e04352012-02-14 11:55:17 -0500146 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200147 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500148 {
149 return TRUE;
150 }
151
152 // Reset the transaction failure flag.
153 // If the $test_mode flag is set to TRUE transactions will be rolled back
154 // even if the queries produce a successful result.
Andrey Andreev131772c2012-03-20 23:35:03 +0200155 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren76e04352012-02-14 11:55:17 -0500156
Timothy Warren7221f942012-02-14 15:02:33 -0500157 $this->trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200158
Timothy Warren76e04352012-02-14 11:55:17 -0500159 return TRUE;
160 }
161
162 // --------------------------------------------------------------------
163
164 /**
165 * Commit Transaction
166 *
Timothy Warren7221f942012-02-14 15:02:33 -0500167 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500168 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500169 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500170 {
Timothy Warren76e04352012-02-14 11:55:17 -0500171 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200172 if ( ! $this->trans_enabled OR $this->_trans->depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500173 {
174 return TRUE;
175 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200176
Timothy Warren2062a862012-02-20 19:38:10 -0500177 return @ibase_commit($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Rollback Transaction
184 *
Timothy Warren7221f942012-02-14 15:02:33 -0500185 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500186 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500187 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500188 {
Timothy Warren76e04352012-02-14 11:55:17 -0500189 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200190 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500191 {
192 return TRUE;
193 }
194
Timothy Warren2062a862012-02-20 19:38:10 -0500195 return @ibase_rollback($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Escape String
202 *
Timothy Warren7221f942012-02-14 15:02:33 -0500203 * @param string
204 * @param bool whether or not the string will be used in a LIKE condition
205 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500206 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500207 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500208 {
209 if (is_array($str))
210 {
211 foreach ($str as $key => $val)
212 {
213 $str[$key] = $this->escape_str($val, $like);
214 }
215
216 return $str;
217 }
218
219 // escape LIKE condition wildcards
220 if ($like === TRUE)
221 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200222 return str_replace(array($this->_like_escape_chr, '%', '_'),
223 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
224 $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500225 }
226
227 return $str;
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * Affected Rows
234 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200235 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500236 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500237 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500238 {
Timothy Warren7221f942012-02-14 15:02:33 -0500239 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Insert ID
246 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200247 * @param string $generator_name
248 * @param int $inc_by
249 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500250 */
Timothy Warren817af192012-02-16 08:28:00 -0500251 public function insert_id($generator_name, $inc_by=0)
Timothy Warren76e04352012-02-14 11:55:17 -0500252 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500253 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500254 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * "Count All" query
261 *
262 * Generates a platform-specific query string that counts all records in
263 * the specified database
264 *
Timothy Warren7221f942012-02-14 15:02:33 -0500265 * @param string
266 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500267 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500268 public function count_all($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500269 {
270 if ($table == '')
271 {
272 return 0;
273 }
274
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200275 $query = $this->query($this->_count_string.$this->protect_identifiers('numrows').' FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE));
Timothy Warren76e04352012-02-14 11:55:17 -0500276 if ($query->num_rows() == 0)
277 {
278 return 0;
279 }
280
Andrey Andreev131772c2012-03-20 23:35:03 +0200281 $query = $query->row();
Timothy Warren76e04352012-02-14 11:55:17 -0500282 $this->_reset_select();
Andrey Andreev131772c2012-03-20 23:35:03 +0200283 return (int) $query->numrows;
Timothy Warren76e04352012-02-14 11:55:17 -0500284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * List table query
290 *
291 * Generates a platform-specific query string so that the table names can be fetched
292 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200293 * @param bool
Timothy Warren7221f942012-02-14 15:02:33 -0500294 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500295 */
Timothy Warren95562142012-02-20 17:37:21 -0500296 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500297 {
Andrey Andreev60c9c992012-03-20 23:46:09 +0200298 $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 -0500299
Andrey Andreev131772c2012-03-20 23:35:03 +0200300 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Timothy Warren76e04352012-02-14 11:55:17 -0500301 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200302 return $sql.' AND "RDB$RELATION_NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Timothy Warren76e04352012-02-14 11:55:17 -0500303 }
Andrey Andreev131772c2012-03-20 23:35:03 +0200304
Timothy Warren76e04352012-02-14 11:55:17 -0500305 return $sql;
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * Show column query
312 *
313 * Generates a platform-specific query string so that the column names can be fetched
314 *
Timothy Warren7221f942012-02-14 15:02:33 -0500315 * @param string the table name
316 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500317 */
Timothy Warren95562142012-02-20 17:37:21 -0500318 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500319 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200320 return 'SELECT "RDB$FIELD_NAME" FROM "RDB$RELATION_FIELDS" WHERE "RDB$RELATION_NAME" = \''.$this->escape_str($table)."'";
Timothy Warren76e04352012-02-14 11:55:17 -0500321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * Field data query
327 *
328 * Generates a platform-specific query so that the column data can be retrieved
329 *
Timothy Warren7221f942012-02-14 15:02:33 -0500330 * @param string the table name
Andrey Andreev131772c2012-03-20 23:35:03 +0200331 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500332 */
Timothy Warren95562142012-02-20 17:37:21 -0500333 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500334 {
Timothy Warren8be31a92012-02-15 11:48:57 -0500335 // Need to find a more efficient way to do this
Andrey Andreev00df2e32012-03-02 18:37:16 +0200336 // but Interbase/Firebird seems to lack the
Timothy Warren8be31a92012-02-15 11:48:57 -0500337 // limit clause
Andrey Andreev131772c2012-03-20 23:35:03 +0200338 return 'SELECT * FROM '.$table;
Timothy Warren76e04352012-02-14 11:55:17 -0500339 }
340
341 // --------------------------------------------------------------------
342
343 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200344 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500345 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200346 * Returns an array containing code and message of the last
347 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500348 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200349 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500350 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200351 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500352 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200353 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Escape the SQL Identifiers
360 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500361 * This public function escapes column and table names
Timothy Warren76e04352012-02-14 11:55:17 -0500362 *
Timothy Warren7221f942012-02-14 15:02:33 -0500363 * @param string
364 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500365 */
Timothy Warren95562142012-02-20 17:37:21 -0500366 protected function _escape_identifiers($item)
Timothy Warren76e04352012-02-14 11:55:17 -0500367 {
368 foreach ($this->_reserved_identifiers as $id)
369 {
370 if (strpos($item, '.'.$id) !== FALSE)
371 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200372 $item = str_replace('.', $this->_escape_char.'.', $item);
Timothy Warren76e04352012-02-14 11:55:17 -0500373
374 // remove duplicates if the user already included the escape
Andrey Andreev131772c2012-03-20 23:35:03 +0200375 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Timothy Warren76e04352012-02-14 11:55:17 -0500376 }
377 }
378
379 if (strpos($item, '.') !== FALSE)
380 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200381 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Timothy Warren76e04352012-02-14 11:55:17 -0500382 }
383
384 // remove duplicates if the user already included the escape
Andrey Andreev131772c2012-03-20 23:35:03 +0200385 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Timothy Warren76e04352012-02-14 11:55:17 -0500386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * From Tables
392 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500393 * This public function implicitly groups FROM tables so there is no confusion
Timothy Warren76e04352012-02-14 11:55:17 -0500394 * about operator precedence in harmony with SQL standards
395 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200396 * @param array
397 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500398 */
Timothy Warren95562142012-02-20 17:37:21 -0500399 protected function _from_tables($tables)
Timothy Warren76e04352012-02-14 11:55:17 -0500400 {
401 if ( ! is_array($tables))
402 {
403 $tables = array($tables);
404 }
405
Timothy Warren817af192012-02-16 08:28:00 -0500406 //Interbase/Firebird doesn't like grouped tables
Timothy Warrend19e47a2012-02-14 23:40:40 -0500407 return implode(', ', $tables);
Timothy Warren76e04352012-02-14 11:55:17 -0500408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Insert statement
414 *
415 * Generates a platform-specific insert string from the supplied data
416 *
Timothy Warren7221f942012-02-14 15:02:33 -0500417 * @param string the table name
418 * @param array the insert keys
419 * @param array the insert values
420 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500421 */
Timothy Warren95562142012-02-20 17:37:21 -0500422 protected function _insert($table, $keys, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500423 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200424 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Update statement
431 *
432 * Generates a platform-specific update string from the supplied data
433 *
Timothy Warren7221f942012-02-14 15:02:33 -0500434 * @param string the table name
435 * @param array the update data
436 * @param array the where clause
437 * @param array the orderby clause
438 * @param array the limit clause
439 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500440 */
Timothy Warren95562142012-02-20 17:37:21 -0500441 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500442 {
443 foreach ($values as $key => $val)
444 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200445 $valstr[] = $key.' = '.$val;
Timothy Warren76e04352012-02-14 11:55:17 -0500446 }
447
Timothy Warren8be31a92012-02-15 11:48:57 -0500448 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500449
Andrey Andreev131772c2012-03-20 23:35:03 +0200450 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
451 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
452 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '');
Timothy Warren76e04352012-02-14 11:55:17 -0500453 }
454
455
456 // --------------------------------------------------------------------
457
458 /**
459 * Truncate statement
460 *
461 * Generates a platform-specific truncate string from the supplied data
462 * If the database does not support the truncate() command
Timothy Warren4be822b2012-02-14 12:07:34 -0500463 * This public function maps to "DELETE FROM table"
Timothy Warren76e04352012-02-14 11:55:17 -0500464 *
Timothy Warren7221f942012-02-14 15:02:33 -0500465 * @param string the table name
466 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500467 */
Timothy Warren95562142012-02-20 17:37:21 -0500468 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500469 {
470 return $this->_delete($table);
471 }
472
473 // --------------------------------------------------------------------
474
475 /**
476 * Delete statement
477 *
478 * Generates a platform-specific delete string from the supplied data
479 *
Timothy Warren7221f942012-02-14 15:02:33 -0500480 * @param string the table name
481 * @param array the where clause
482 * @param string the limit clause
483 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500484 */
Timothy Warren95562142012-02-20 17:37:21 -0500485 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500486 {
Timothy Warren76e04352012-02-14 11:55:17 -0500487 if (count($where) > 0 OR count($like) > 0)
488 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200489 $conditions = "\nWHERE ".implode("\n", $where)
490 .((count($where) > 0 && count($like) > 0) ? ' AND ' : '')
491 .implode("\n", $like);
492 }
493 else
494 {
495 $conditions = '';
Timothy Warren76e04352012-02-14 11:55:17 -0500496 }
497
Timothy Warren3d985a12012-02-15 11:38:00 -0500498 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500499
Andrey Andreev131772c2012-03-20 23:35:03 +0200500 return 'DELETE FROM '.$table.' '.$conditions;
Timothy Warren76e04352012-02-14 11:55:17 -0500501 }
502
503 // --------------------------------------------------------------------
504
505 /**
506 * Limit string
507 *
508 * Generates a platform-specific LIMIT clause
509 *
Timothy Warren7221f942012-02-14 15:02:33 -0500510 * @param string the sql query string
Andrey Andreev131772c2012-03-20 23:35:03 +0200511 * @param int the number of rows to limit the query to
512 * @param int the offset value
Timothy Warren7221f942012-02-14 15:02:33 -0500513 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500514 */
Timothy Warren95562142012-02-20 17:37:21 -0500515 protected function _limit($sql, $limit, $offset)
Timothy Warren76e04352012-02-14 11:55:17 -0500516 {
Timothy Warren56784f62012-02-29 11:58:20 -0500517 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200518 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500519 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200520 $select = 'FIRST '. (int) $limit
521 .($offset > 0 ? ' SKIP '. (int) $offset : '');
Timothy Warren56784f62012-02-29 11:58:20 -0500522 }
523 else
524 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200525 $select = 'ROWS '
526 .($offset > 0 ? (int) $offset.' TO '.($limit + $offset) : (int) $limit);
Timothy Warren56784f62012-02-29 11:58:20 -0500527 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200528
Andrey Andreev131772c2012-03-20 23:35:03 +0200529 return preg_replace('`SELECT`i', 'SELECT '.$select, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500530 }
531
532 // --------------------------------------------------------------------
533
534 /**
535 * Close DB Connection
536 *
Timothy Warren7221f942012-02-14 15:02:33 -0500537 * @param resource
538 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500539 */
Timothy Warren95562142012-02-20 17:37:21 -0500540 protected function _close($conn_id)
Timothy Warren76e04352012-02-14 11:55:17 -0500541 {
Timothy Warren7221f942012-02-14 15:02:33 -0500542 @ibase_close($conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500543 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200544
Timothy Warren76e04352012-02-14 11:55:17 -0500545}
546
Timothy Warren76e04352012-02-14 11:55:17 -0500547/* End of file interbase_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400548/* Location: ./system/database/drivers/interbase/interbase_driver.php */