blob: 326841dc27268f0c7332ef5263b1b55a27b582fb [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 /**
88 * Reconnect
89 *
90 * Keep / reestablish the db connection if no queries have been
91 * sent for a length of time exceeding the server's idle timeout
92 *
Timothy Warren7221f942012-02-14 15:02:33 -050093 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -050094 */
Timothy Warren4be822b2012-02-14 12:07:34 -050095 public function reconnect()
Timothy Warren76e04352012-02-14 11:55:17 -050096 {
97 // not implemented in Interbase/Firebird
98 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Select the database
104 *
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500105 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500106 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500107 public function db_select()
Timothy Warren76e04352012-02-14 11:55:17 -0500108 {
109 // Connection selects the database
110 return TRUE;
111 }
112
113 // --------------------------------------------------------------------
114
115 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200116 * Database version number
Timothy Warren76e04352012-02-14 11:55:17 -0500117 *
Timothy Warren7221f942012-02-14 15:02:33 -0500118 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500119 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200120 public function version()
Timothy Warren76e04352012-02-14 11:55:17 -0500121 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200122 if (isset($this->data_cache['version']))
123 {
124 return $this->data_cache['version'];
125 }
126
Timothy Warren3d985a12012-02-15 11:38:00 -0500127 if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
128 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200129 $this->data_cache['version'] = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200130
Timothy Warrenab189e12012-02-22 10:34:23 -0500131 // Don't keep the service open
132 ibase_service_detach($service);
Andrey Andreev08856b82012-03-03 03:19:28 +0200133 return $this->data_cache['version'];
Timothy Warren3d985a12012-02-15 11:38:00 -0500134 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200135
Timothy Warren3d985a12012-02-15 11:38:00 -0500136 return FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -0500137 }
138
139 // --------------------------------------------------------------------
140
141 /**
142 * Execute the query
143 *
Timothy Warren7221f942012-02-14 15:02:33 -0500144 * @param string an SQL query
145 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -0500146 */
Timothy Warren95562142012-02-20 17:37:21 -0500147 protected function _execute($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500148 {
Timothy Warren7221f942012-02-14 15:02:33 -0500149 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500150 }
151
152 // --------------------------------------------------------------------
153
154 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500155 * Begin Transaction
156 *
Timothy Warren7221f942012-02-14 15:02:33 -0500157 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500158 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500159 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500160 {
Timothy Warren76e04352012-02-14 11:55:17 -0500161 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200162 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500163 {
164 return TRUE;
165 }
166
167 // Reset the transaction failure flag.
168 // If the $test_mode flag is set to TRUE transactions will be rolled back
169 // even if the queries produce a successful result.
Andrey Andreev131772c2012-03-20 23:35:03 +0200170 $this->_trans_failure = ($test_mode === TRUE);
Timothy Warren76e04352012-02-14 11:55:17 -0500171
Timothy Warren7221f942012-02-14 15:02:33 -0500172 $this->trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200173
Timothy Warren76e04352012-02-14 11:55:17 -0500174 return TRUE;
175 }
176
177 // --------------------------------------------------------------------
178
179 /**
180 * Commit Transaction
181 *
Timothy Warren7221f942012-02-14 15:02:33 -0500182 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500183 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500184 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500185 {
Timothy Warren76e04352012-02-14 11:55:17 -0500186 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200187 if ( ! $this->trans_enabled OR $this->_trans->depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500188 {
189 return TRUE;
190 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200191
Timothy Warren2062a862012-02-20 19:38:10 -0500192 return @ibase_commit($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Rollback Transaction
199 *
Timothy Warren7221f942012-02-14 15:02:33 -0500200 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500201 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500202 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500203 {
Timothy Warren76e04352012-02-14 11:55:17 -0500204 // When transactions are nested we only begin/commit/rollback the outermost ones
Andrey Andreev131772c2012-03-20 23:35:03 +0200205 if ( ! $this->trans_enabled OR $this->_trans_depth > 0)
Timothy Warren76e04352012-02-14 11:55:17 -0500206 {
207 return TRUE;
208 }
209
Timothy Warren2062a862012-02-20 19:38:10 -0500210 return @ibase_rollback($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Escape String
217 *
Timothy Warren7221f942012-02-14 15:02:33 -0500218 * @param string
219 * @param bool whether or not the string will be used in a LIKE condition
220 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500221 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500222 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500223 {
224 if (is_array($str))
225 {
226 foreach ($str as $key => $val)
227 {
228 $str[$key] = $this->escape_str($val, $like);
229 }
230
231 return $str;
232 }
233
234 // escape LIKE condition wildcards
235 if ($like === TRUE)
236 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200237 return str_replace(array($this->_like_escape_chr, '%', '_'),
238 array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'),
239 $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500240 }
241
242 return $str;
243 }
244
245 // --------------------------------------------------------------------
246
247 /**
248 * Affected Rows
249 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200250 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500251 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500252 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500253 {
Timothy Warren7221f942012-02-14 15:02:33 -0500254 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Insert ID
261 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200262 * @param string $generator_name
263 * @param int $inc_by
264 * @return int
Timothy Warren76e04352012-02-14 11:55:17 -0500265 */
Timothy Warren817af192012-02-16 08:28:00 -0500266 public function insert_id($generator_name, $inc_by=0)
Timothy Warren76e04352012-02-14 11:55:17 -0500267 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500268 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500269 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * "Count All" query
276 *
277 * Generates a platform-specific query string that counts all records in
278 * the specified database
279 *
Timothy Warren7221f942012-02-14 15:02:33 -0500280 * @param string
281 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500282 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500283 public function count_all($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500284 {
285 if ($table == '')
286 {
287 return 0;
288 }
289
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200290 $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 -0500291 if ($query->num_rows() == 0)
292 {
293 return 0;
294 }
295
Andrey Andreev131772c2012-03-20 23:35:03 +0200296 $query = $query->row();
Timothy Warren76e04352012-02-14 11:55:17 -0500297 $this->_reset_select();
Andrey Andreev131772c2012-03-20 23:35:03 +0200298 return (int) $query->numrows;
Timothy Warren76e04352012-02-14 11:55:17 -0500299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * List table query
305 *
306 * Generates a platform-specific query string so that the table names can be fetched
307 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200308 * @param bool
Timothy Warren7221f942012-02-14 15:02:33 -0500309 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500310 */
Timothy Warren95562142012-02-20 17:37:21 -0500311 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500312 {
Andrey Andreev60c9c992012-03-20 23:46:09 +0200313 $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 -0500314
Andrey Andreev131772c2012-03-20 23:35:03 +0200315 if ($prefix_limit !== FALSE && $this->dbprefix != '')
Timothy Warren76e04352012-02-14 11:55:17 -0500316 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200317 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 -0500318 }
Andrey Andreev131772c2012-03-20 23:35:03 +0200319
Timothy Warren76e04352012-02-14 11:55:17 -0500320 return $sql;
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * Show column query
327 *
328 * Generates a platform-specific query string so that the column names can be fetched
329 *
Timothy Warren7221f942012-02-14 15:02:33 -0500330 * @param string the table name
331 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500332 */
Timothy Warren95562142012-02-20 17:37:21 -0500333 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500334 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200335 return 'SELECT "RDB$FIELD_NAME" FROM "RDB$RELATION_FIELDS" WHERE "RDB$RELATION_NAME" = \''.$this->escape_str($table)."'";
Timothy Warren76e04352012-02-14 11:55:17 -0500336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * Field data query
342 *
343 * Generates a platform-specific query so that the column data can be retrieved
344 *
Timothy Warren7221f942012-02-14 15:02:33 -0500345 * @param string the table name
Andrey Andreev131772c2012-03-20 23:35:03 +0200346 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500347 */
Timothy Warren95562142012-02-20 17:37:21 -0500348 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500349 {
Timothy Warren8be31a92012-02-15 11:48:57 -0500350 // Need to find a more efficient way to do this
Andrey Andreev00df2e32012-03-02 18:37:16 +0200351 // but Interbase/Firebird seems to lack the
Timothy Warren8be31a92012-02-15 11:48:57 -0500352 // limit clause
Andrey Andreev131772c2012-03-20 23:35:03 +0200353 return 'SELECT * FROM '.$table;
Timothy Warren76e04352012-02-14 11:55:17 -0500354 }
355
356 // --------------------------------------------------------------------
357
358 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200359 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500360 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200361 * Returns an array containing code and message of the last
362 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500363 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200364 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500365 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200366 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500367 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200368 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500369 }
370
371 // --------------------------------------------------------------------
372
373 /**
374 * Escape the SQL Identifiers
375 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500376 * This public function escapes column and table names
Timothy Warren76e04352012-02-14 11:55:17 -0500377 *
Timothy Warren7221f942012-02-14 15:02:33 -0500378 * @param string
379 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500380 */
Timothy Warren95562142012-02-20 17:37:21 -0500381 protected function _escape_identifiers($item)
Timothy Warren76e04352012-02-14 11:55:17 -0500382 {
383 foreach ($this->_reserved_identifiers as $id)
384 {
385 if (strpos($item, '.'.$id) !== FALSE)
386 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200387 $item = str_replace('.', $this->_escape_char.'.', $item);
Timothy Warren76e04352012-02-14 11:55:17 -0500388
389 // remove duplicates if the user already included the escape
Andrey Andreev131772c2012-03-20 23:35:03 +0200390 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
Timothy Warren76e04352012-02-14 11:55:17 -0500391 }
392 }
393
394 if (strpos($item, '.') !== FALSE)
395 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200396 $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
Timothy Warren76e04352012-02-14 11:55:17 -0500397 }
398
399 // remove duplicates if the user already included the escape
Andrey Andreev131772c2012-03-20 23:35:03 +0200400 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
Timothy Warren76e04352012-02-14 11:55:17 -0500401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * From Tables
407 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500408 * This public function implicitly groups FROM tables so there is no confusion
Timothy Warren76e04352012-02-14 11:55:17 -0500409 * about operator precedence in harmony with SQL standards
410 *
Andrey Andreev131772c2012-03-20 23:35:03 +0200411 * @param array
412 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500413 */
Timothy Warren95562142012-02-20 17:37:21 -0500414 protected function _from_tables($tables)
Timothy Warren76e04352012-02-14 11:55:17 -0500415 {
416 if ( ! is_array($tables))
417 {
418 $tables = array($tables);
419 }
420
Timothy Warren817af192012-02-16 08:28:00 -0500421 //Interbase/Firebird doesn't like grouped tables
Timothy Warrend19e47a2012-02-14 23:40:40 -0500422 return implode(', ', $tables);
Timothy Warren76e04352012-02-14 11:55:17 -0500423 }
424
425 // --------------------------------------------------------------------
426
427 /**
428 * Insert statement
429 *
430 * Generates a platform-specific insert string from the supplied data
431 *
Timothy Warren7221f942012-02-14 15:02:33 -0500432 * @param string the table name
433 * @param array the insert keys
434 * @param array the insert values
435 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500436 */
Timothy Warren95562142012-02-20 17:37:21 -0500437 protected function _insert($table, $keys, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500438 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200439 return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * Update statement
446 *
447 * Generates a platform-specific update string from the supplied data
448 *
Timothy Warren7221f942012-02-14 15:02:33 -0500449 * @param string the table name
450 * @param array the update data
451 * @param array the where clause
452 * @param array the orderby clause
453 * @param array the limit clause
454 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500455 */
Timothy Warren95562142012-02-20 17:37:21 -0500456 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500457 {
458 foreach ($values as $key => $val)
459 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200460 $valstr[] = $key.' = '.$val;
Timothy Warren76e04352012-02-14 11:55:17 -0500461 }
462
Timothy Warren8be31a92012-02-15 11:48:57 -0500463 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500464
Andrey Andreev131772c2012-03-20 23:35:03 +0200465 return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
466 .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
467 .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '');
Timothy Warren76e04352012-02-14 11:55:17 -0500468 }
469
470
471 // --------------------------------------------------------------------
472
473 /**
474 * Truncate statement
475 *
476 * Generates a platform-specific truncate string from the supplied data
477 * If the database does not support the truncate() command
Timothy Warren4be822b2012-02-14 12:07:34 -0500478 * This public function maps to "DELETE FROM table"
Timothy Warren76e04352012-02-14 11:55:17 -0500479 *
Timothy Warren7221f942012-02-14 15:02:33 -0500480 * @param string the table name
481 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500482 */
Timothy Warren95562142012-02-20 17:37:21 -0500483 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500484 {
485 return $this->_delete($table);
486 }
487
488 // --------------------------------------------------------------------
489
490 /**
491 * Delete statement
492 *
493 * Generates a platform-specific delete string from the supplied data
494 *
Timothy Warren7221f942012-02-14 15:02:33 -0500495 * @param string the table name
496 * @param array the where clause
497 * @param string the limit clause
498 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500499 */
Timothy Warren95562142012-02-20 17:37:21 -0500500 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500501 {
Timothy Warren76e04352012-02-14 11:55:17 -0500502 if (count($where) > 0 OR count($like) > 0)
503 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200504 $conditions = "\nWHERE ".implode("\n", $where)
505 .((count($where) > 0 && count($like) > 0) ? ' AND ' : '')
506 .implode("\n", $like);
507 }
508 else
509 {
510 $conditions = '';
Timothy Warren76e04352012-02-14 11:55:17 -0500511 }
512
Timothy Warren3d985a12012-02-15 11:38:00 -0500513 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500514
Andrey Andreev131772c2012-03-20 23:35:03 +0200515 return 'DELETE FROM '.$table.' '.$conditions;
Timothy Warren76e04352012-02-14 11:55:17 -0500516 }
517
518 // --------------------------------------------------------------------
519
520 /**
521 * Limit string
522 *
523 * Generates a platform-specific LIMIT clause
524 *
Timothy Warren7221f942012-02-14 15:02:33 -0500525 * @param string the sql query string
Andrey Andreev131772c2012-03-20 23:35:03 +0200526 * @param int the number of rows to limit the query to
527 * @param int the offset value
Timothy Warren7221f942012-02-14 15:02:33 -0500528 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500529 */
Timothy Warren95562142012-02-20 17:37:21 -0500530 protected function _limit($sql, $limit, $offset)
Timothy Warren76e04352012-02-14 11:55:17 -0500531 {
Timothy Warren56784f62012-02-29 11:58:20 -0500532 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200533 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500534 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200535 $select = 'FIRST '. (int) $limit
536 .($offset > 0 ? ' SKIP '. (int) $offset : '');
Timothy Warren56784f62012-02-29 11:58:20 -0500537 }
538 else
539 {
Andrey Andreev131772c2012-03-20 23:35:03 +0200540 $select = 'ROWS '
541 .($offset > 0 ? (int) $offset.' TO '.($limit + $offset) : (int) $limit);
Timothy Warren56784f62012-02-29 11:58:20 -0500542 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200543
Andrey Andreev131772c2012-03-20 23:35:03 +0200544 return preg_replace('`SELECT`i', 'SELECT '.$select, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500545 }
546
547 // --------------------------------------------------------------------
548
549 /**
550 * Close DB Connection
551 *
Timothy Warren7221f942012-02-14 15:02:33 -0500552 * @param resource
553 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500554 */
Timothy Warren95562142012-02-20 17:37:21 -0500555 protected function _close($conn_id)
Timothy Warren76e04352012-02-14 11:55:17 -0500556 {
Timothy Warren7221f942012-02-14 15:02:33 -0500557 @ibase_close($conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500558 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200559
Timothy Warren76e04352012-02-14 11:55:17 -0500560}
561
Timothy Warren76e04352012-02-14 11:55:17 -0500562/* End of file interbase_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400563/* Location: ./system/database/drivers/interbase/interbase_driver.php */