blob: bacb6688ccc3502a11f3d99ff2cf56b969368bef [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 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
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
28// ------------------------------------------------------------------------
29
Timothy Warren76e04352012-02-14 11:55:17 -050030/**
31 * Firebird/Interbase Database Adapter Class
32 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
Timothy Warren7221f942012-02-14 15:02:33 -050037 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
40 * @author EllisLab Dev Team
41 * @link http://codeigniter.com/user_guide/database/
Timothy Warren76e04352012-02-14 11:55:17 -050042 */
43class CI_DB_interbase_driver extends CI_DB {
44
45 public $dbdriver = 'interbase';
46
47 // The character used to escape with
Timothy Warren95562142012-02-20 17:37:21 -050048 protected $_escape_char = '"';
Timothy Warren76e04352012-02-14 11:55:17 -050049
50 // clause and character used for LIKE escape sequences
Timothy Warren95562142012-02-20 17:37:21 -050051 protected $_like_escape_str = " ESCAPE '%s' ";
52 protected $_like_escape_chr = '!';
Timothy Warren76e04352012-02-14 11:55:17 -050053
54 /**
55 * The syntax to count rows is slightly different across different
56 * database engines, so this string appears in each driver and is
57 * used for the count_all() and count_all_results() functions.
58 */
Timothy Warren95562142012-02-20 17:37:21 -050059 protected $_count_string = "SELECT COUNT(*) AS ";
60 protected $_random_keyword = ' Random()'; // database specific random keyword
Andrey Andreev00df2e32012-03-02 18:37:16 +020061
Timothy Warren76e04352012-02-14 11:55:17 -050062 // Keeps track of the resource for the current transaction
63 protected $trans;
64
65 /**
66 * Non-persistent database connection
67 *
Timothy Warren7221f942012-02-14 15:02:33 -050068 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050069 */
Timothy Warren4be822b2012-02-14 12:07:34 -050070 public function db_connect()
Timothy Warren76e04352012-02-14 11:55:17 -050071 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050072 return @ibase_connect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050073 }
74
75 // --------------------------------------------------------------------
76
77 /**
78 * Persistent database connection
79 *
Timothy Warren7221f942012-02-14 15:02:33 -050080 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -050081 */
Timothy Warren4be822b2012-02-14 12:07:34 -050082 public function db_pconnect()
Timothy Warren76e04352012-02-14 11:55:17 -050083 {
Timothy Warren3c4281f2012-02-16 19:00:10 -050084 return @ibase_pconnect($this->hostname.':'.$this->database, $this->username, $this->password, $this->char_set);
Timothy Warren76e04352012-02-14 11:55:17 -050085 }
86
87 // --------------------------------------------------------------------
88
89 /**
90 * Reconnect
91 *
92 * Keep / reestablish the db connection if no queries have been
93 * sent for a length of time exceeding the server's idle timeout
94 *
Timothy Warren7221f942012-02-14 15:02:33 -050095 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -050096 */
Timothy Warren4be822b2012-02-14 12:07:34 -050097 public function reconnect()
Timothy Warren76e04352012-02-14 11:55:17 -050098 {
99 // not implemented in Interbase/Firebird
100 }
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Select the database
106 *
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500107 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500108 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500109 public function db_select()
Timothy Warren76e04352012-02-14 11:55:17 -0500110 {
111 // Connection selects the database
112 return TRUE;
113 }
114
115 // --------------------------------------------------------------------
116
117 /**
Andrey Andreev08856b82012-03-03 03:19:28 +0200118 * Database version number
Timothy Warren76e04352012-02-14 11:55:17 -0500119 *
Timothy Warren7221f942012-02-14 15:02:33 -0500120 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500121 */
Andrey Andreev08856b82012-03-03 03:19:28 +0200122 public function version()
Timothy Warren76e04352012-02-14 11:55:17 -0500123 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200124 if (isset($this->data_cache['version']))
125 {
126 return $this->data_cache['version'];
127 }
128
Timothy Warren3d985a12012-02-15 11:38:00 -0500129 if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
130 {
Andrey Andreev08856b82012-03-03 03:19:28 +0200131 $this->data_cache['version'] = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200132
Timothy Warrenab189e12012-02-22 10:34:23 -0500133 // Don't keep the service open
134 ibase_service_detach($service);
Andrey Andreev08856b82012-03-03 03:19:28 +0200135 return $this->data_cache['version'];
Timothy Warren3d985a12012-02-15 11:38:00 -0500136 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200137
Timothy Warren3d985a12012-02-15 11:38:00 -0500138 return FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -0500139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Execute the query
145 *
Timothy Warren7221f942012-02-14 15:02:33 -0500146 * @param string an SQL query
147 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -0500148 */
Timothy Warren95562142012-02-20 17:37:21 -0500149 protected function _execute($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500150 {
151 $sql = $this->_prep_query($sql);
Timothy Warren7221f942012-02-14 15:02:33 -0500152 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500153 }
154
155 // --------------------------------------------------------------------
156
157 /**
158 * Prep the query
159 *
160 * If needed, each database adapter can prep the query string
161 *
Timothy Warren7221f942012-02-14 15:02:33 -0500162 * @param string an SQL query
163 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500164 */
Timothy Warren95562142012-02-20 17:37:21 -0500165 protected function _prep_query($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500166 {
167 return $sql;
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Begin Transaction
174 *
Timothy Warren7221f942012-02-14 15:02:33 -0500175 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500176 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500177 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500178 {
179 if ( ! $this->trans_enabled)
180 {
181 return TRUE;
182 }
183
184 // When transactions are nested we only begin/commit/rollback the outermost ones
185 if ($this->_trans_depth > 0)
186 {
187 return TRUE;
188 }
189
190 // Reset the transaction failure flag.
191 // If the $test_mode flag is set to TRUE transactions will be rolled back
192 // even if the queries produce a successful result.
193 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
194
Timothy Warren7221f942012-02-14 15:02:33 -0500195 $this->trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200196
Timothy Warren76e04352012-02-14 11:55:17 -0500197 return TRUE;
198 }
199
200 // --------------------------------------------------------------------
201
202 /**
203 * Commit Transaction
204 *
Timothy Warren7221f942012-02-14 15:02:33 -0500205 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500206 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500207 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500208 {
209 if ( ! $this->trans_enabled)
210 {
211 return TRUE;
212 }
213
214 // When transactions are nested we only begin/commit/rollback the outermost ones
215 if ($this->_trans_depth > 0)
216 {
217 return TRUE;
218 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200219
Timothy Warren2062a862012-02-20 19:38:10 -0500220 return @ibase_commit($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Rollback Transaction
227 *
Timothy Warren7221f942012-02-14 15:02:33 -0500228 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500229 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500230 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500231 {
232 if ( ! $this->trans_enabled)
233 {
234 return TRUE;
235 }
236
237 // When transactions are nested we only begin/commit/rollback the outermost ones
238 if ($this->_trans_depth > 0)
239 {
240 return TRUE;
241 }
242
Timothy Warren2062a862012-02-20 19:38:10 -0500243 return @ibase_rollback($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * Escape String
250 *
Timothy Warren7221f942012-02-14 15:02:33 -0500251 * @param string
252 * @param bool whether or not the string will be used in a LIKE condition
253 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500254 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500255 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500256 {
257 if (is_array($str))
258 {
259 foreach ($str as $key => $val)
260 {
261 $str[$key] = $this->escape_str($val, $like);
262 }
263
264 return $str;
265 }
266
267 // escape LIKE condition wildcards
268 if ($like === TRUE)
269 {
270 $str = str_replace( array('%', '_', $this->_like_escape_chr),
271 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
272 $str);
273 }
274
275 return $str;
276 }
277
278 // --------------------------------------------------------------------
279
280 /**
281 * Affected Rows
282 *
Timothy Warren7221f942012-02-14 15:02:33 -0500283 * @return integer
Timothy Warren76e04352012-02-14 11:55:17 -0500284 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500285 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500286 {
Timothy Warren7221f942012-02-14 15:02:33 -0500287 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * Insert ID
294 *
Timothy Warren125fe732012-02-21 07:58:25 -0500295 * @param string $generator_name
296 * @param integer $inc_by
Timothy Warren7221f942012-02-14 15:02:33 -0500297 * @return integer
Timothy Warren76e04352012-02-14 11:55:17 -0500298 */
Timothy Warren817af192012-02-16 08:28:00 -0500299 public function insert_id($generator_name, $inc_by=0)
Timothy Warren76e04352012-02-14 11:55:17 -0500300 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500301 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500302 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500303 }
304
305 // --------------------------------------------------------------------
306
307 /**
308 * "Count All" query
309 *
310 * Generates a platform-specific query string that counts all records in
311 * the specified database
312 *
Timothy Warren7221f942012-02-14 15:02:33 -0500313 * @param string
314 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500315 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500316 public function count_all($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500317 {
318 if ($table == '')
319 {
320 return 0;
321 }
322
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200323 $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 -0500324 if ($query->num_rows() == 0)
325 {
326 return 0;
327 }
328
329 $row = $query->row();
330 $this->_reset_select();
331 return (int) $row->numrows;
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * List table query
338 *
339 * Generates a platform-specific query string so that the table names can be fetched
340 *
Timothy Warren7221f942012-02-14 15:02:33 -0500341 * @param boolean
342 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500343 */
Timothy Warren95562142012-02-20 17:37:21 -0500344 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500345 {
346 $sql = <<<SQL
347 SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
348 WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%'
349 AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%'
350SQL;
351
352 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
353 {
Timothy Warren3d985a12012-02-15 11:38:00 -0500354 $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 -0500355 }
356 return $sql;
357 }
358
359 // --------------------------------------------------------------------
360
361 /**
362 * Show column query
363 *
364 * Generates a platform-specific query string so that the column names can be fetched
365 *
Timothy Warren7221f942012-02-14 15:02:33 -0500366 * @param string the table name
367 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500368 */
Timothy Warren95562142012-02-20 17:37:21 -0500369 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500370 {
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500371 return <<<SQL
Timothy Warreneb6dcf02012-02-15 16:48:51 -0500372 SELECT "RDB\$FIELD_NAME" FROM "RDB\$RELATION_FIELDS"
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500373 WHERE "RDB\$RELATION_NAME"='{$table}';
Timothy Warreneb6dcf02012-02-15 16:48:51 -0500374SQL;
Timothy Warren76e04352012-02-14 11:55:17 -0500375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Field data query
381 *
382 * Generates a platform-specific query so that the column data can be retrieved
383 *
Timothy Warren7221f942012-02-14 15:02:33 -0500384 * @param string the table name
385 * @return object
Timothy Warren76e04352012-02-14 11:55:17 -0500386 */
Timothy Warren95562142012-02-20 17:37:21 -0500387 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500388 {
Timothy Warren8be31a92012-02-15 11:48:57 -0500389 // Need to find a more efficient way to do this
Andrey Andreev00df2e32012-03-02 18:37:16 +0200390 // but Interbase/Firebird seems to lack the
Timothy Warren8be31a92012-02-15 11:48:57 -0500391 // limit clause
392 return "SELECT * FROM {$table}";
Timothy Warren76e04352012-02-14 11:55:17 -0500393 }
394
395 // --------------------------------------------------------------------
396
397 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200398 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500399 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200400 * Returns an array containing code and message of the last
401 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500402 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200403 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500404 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200405 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500406 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200407 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Escape the SQL Identifiers
414 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500415 * This public function escapes column and table names
Timothy Warren76e04352012-02-14 11:55:17 -0500416 *
Timothy Warren7221f942012-02-14 15:02:33 -0500417 * @param string
418 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500419 */
Timothy Warren95562142012-02-20 17:37:21 -0500420 protected function _escape_identifiers($item)
Timothy Warren76e04352012-02-14 11:55:17 -0500421 {
422 foreach ($this->_reserved_identifiers as $id)
423 {
424 if (strpos($item, '.'.$id) !== FALSE)
425 {
426 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
427
428 // remove duplicates if the user already included the escape
429 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
430 }
431 }
432
433 if (strpos($item, '.') !== FALSE)
434 {
435 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
436 }
437 else
438 {
439 $str = $this->_escape_char.$item.$this->_escape_char;
440 }
441
442 // remove duplicates if the user already included the escape
Timothy Warrend19e47a2012-02-14 23:40:40 -0500443 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500444 }
445
446 // --------------------------------------------------------------------
447
448 /**
449 * From Tables
450 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500451 * This public function implicitly groups FROM tables so there is no confusion
Timothy Warren76e04352012-02-14 11:55:17 -0500452 * about operator precedence in harmony with SQL standards
453 *
Timothy Warren7221f942012-02-14 15:02:33 -0500454 * @param type
455 * @return type
Timothy Warren76e04352012-02-14 11:55:17 -0500456 */
Timothy Warren95562142012-02-20 17:37:21 -0500457 protected function _from_tables($tables)
Timothy Warren76e04352012-02-14 11:55:17 -0500458 {
459 if ( ! is_array($tables))
460 {
461 $tables = array($tables);
462 }
463
Timothy Warren817af192012-02-16 08:28:00 -0500464 //Interbase/Firebird doesn't like grouped tables
Timothy Warrend19e47a2012-02-14 23:40:40 -0500465 return implode(', ', $tables);
Timothy Warren76e04352012-02-14 11:55:17 -0500466 }
467
468 // --------------------------------------------------------------------
469
470 /**
471 * Insert statement
472 *
473 * Generates a platform-specific insert string from the supplied data
474 *
Timothy Warren7221f942012-02-14 15:02:33 -0500475 * @param string the table name
476 * @param array the insert keys
477 * @param array the insert values
478 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500479 */
Timothy Warren95562142012-02-20 17:37:21 -0500480 protected function _insert($table, $keys, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500481 {
Timothy Warren817af192012-02-16 08:28:00 -0500482 return "INSERT INTO {$table} (".implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500483 }
484
485 // --------------------------------------------------------------------
486
487 /**
488 * Update statement
489 *
490 * Generates a platform-specific update string from the supplied data
491 *
Timothy Warren7221f942012-02-14 15:02:33 -0500492 * @param string the table name
493 * @param array the update data
494 * @param array the where clause
495 * @param array the orderby clause
496 * @param array the limit clause
497 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500498 */
Timothy Warren95562142012-02-20 17:37:21 -0500499 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500500 {
501 foreach ($values as $key => $val)
502 {
503 $valstr[] = $key." = ".$val;
504 }
505
Timothy Warren8be31a92012-02-15 11:48:57 -0500506 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500507
508 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
509
Timothy Warren817af192012-02-16 08:28:00 -0500510 $sql = "UPDATE {$table} SET ".implode(', ', $valstr);
Timothy Warren76e04352012-02-14 11:55:17 -0500511
Timothy Warren817af192012-02-16 08:28:00 -0500512 $sql .= ($where != '' AND count($where) >=1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren76e04352012-02-14 11:55:17 -0500513
Timothy Warren8be31a92012-02-15 11:48:57 -0500514 $sql .= $orderby;
Timothy Warren76e04352012-02-14 11:55:17 -0500515
516 return $sql;
517 }
518
519
520 // --------------------------------------------------------------------
521
522 /**
523 * Truncate statement
524 *
525 * Generates a platform-specific truncate string from the supplied data
526 * If the database does not support the truncate() command
Timothy Warren4be822b2012-02-14 12:07:34 -0500527 * This public function maps to "DELETE FROM table"
Timothy Warren76e04352012-02-14 11:55:17 -0500528 *
Timothy Warren7221f942012-02-14 15:02:33 -0500529 * @param string the table name
530 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500531 */
Timothy Warren95562142012-02-20 17:37:21 -0500532 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500533 {
534 return $this->_delete($table);
535 }
536
537 // --------------------------------------------------------------------
538
539 /**
540 * Delete statement
541 *
542 * Generates a platform-specific delete string from the supplied data
543 *
Timothy Warren7221f942012-02-14 15:02:33 -0500544 * @param string the table name
545 * @param array the where clause
546 * @param string the limit clause
547 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500548 */
Timothy Warren95562142012-02-20 17:37:21 -0500549 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500550 {
551 $conditions = '';
552
553 if (count($where) > 0 OR count($like) > 0)
554 {
555 $conditions = "\nWHERE ";
556 $conditions .= implode("\n", $this->ar_where);
557
558 if (count($where) > 0 && count($like) > 0)
559 {
Timothy Warren817af192012-02-16 08:28:00 -0500560 $conditions .= ' AND ';
Timothy Warren76e04352012-02-14 11:55:17 -0500561 }
562 $conditions .= implode("\n", $like);
563 }
564
Timothy Warren3d985a12012-02-15 11:38:00 -0500565 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500566
Timothy Warren8be31a92012-02-15 11:48:57 -0500567 return "DELETE FROM {$table}{$conditions}";
Timothy Warren76e04352012-02-14 11:55:17 -0500568 }
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Limit string
574 *
575 * Generates a platform-specific LIMIT clause
576 *
Timothy Warren7221f942012-02-14 15:02:33 -0500577 * @param string the sql query string
578 * @param integer the number of rows to limit the query to
579 * @param integer the offset value
580 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500581 */
Timothy Warren95562142012-02-20 17:37:21 -0500582 protected function _limit($sql, $limit, $offset)
Timothy Warren76e04352012-02-14 11:55:17 -0500583 {
Timothy Warren56784f62012-02-29 11:58:20 -0500584 // Keep the current sql string safe for a moment
585 $orig_sql = $sql;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200586
Timothy Warren56784f62012-02-29 11:58:20 -0500587 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200588 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500589 {
590 $sql = 'FIRST '. (int) $limit;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200591
Timothy Warren56784f62012-02-29 11:58:20 -0500592 if ($offset > 0)
593 {
Timothy Warren9a960822012-02-29 22:53:35 -0500594 $sql .= ' SKIP '. (int) $offset;
Timothy Warren56784f62012-02-29 11:58:20 -0500595 }
596 }
597 else
598 {
599 $sql = 'ROWS ' . (int) $limit;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200600
Timothy Warren56784f62012-02-29 11:58:20 -0500601 if ($offset > 0)
602 {
603 $sql = 'ROWS '. (int) $offset . ' TO ' . ($limit + $offset);
604 }
605 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200606
607 return preg_replace('`SELECT`i', "SELECT {$sql}", $orig_sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500608 }
609
610 // --------------------------------------------------------------------
611
612 /**
613 * Close DB Connection
614 *
Timothy Warren7221f942012-02-14 15:02:33 -0500615 * @param resource
616 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500617 */
Timothy Warren95562142012-02-20 17:37:21 -0500618 protected function _close($conn_id)
Timothy Warren76e04352012-02-14 11:55:17 -0500619 {
Timothy Warren7221f942012-02-14 15:02:33 -0500620 @ibase_close($conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500621 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200622
Timothy Warren76e04352012-02-14 11:55:17 -0500623}
624
Timothy Warren76e04352012-02-14 11:55:17 -0500625/* End of file interbase_driver.php */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200626/* Location: ./system/database/drivers/interbase/interbase_driver.php */