blob: 9fa03adc7bf733a6d1a17bfc41e23ad0dbc6c458 [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
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 {
Timothy Warren7221f942012-02-14 15:02:33 -0500151 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500152 }
153
154 // --------------------------------------------------------------------
155
156 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500157 * Begin Transaction
158 *
Timothy Warren7221f942012-02-14 15:02:33 -0500159 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500160 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500161 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500162 {
163 if ( ! $this->trans_enabled)
164 {
165 return TRUE;
166 }
167
168 // When transactions are nested we only begin/commit/rollback the outermost ones
169 if ($this->_trans_depth > 0)
170 {
171 return TRUE;
172 }
173
174 // Reset the transaction failure flag.
175 // If the $test_mode flag is set to TRUE transactions will be rolled back
176 // even if the queries produce a successful result.
177 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
178
Timothy Warren7221f942012-02-14 15:02:33 -0500179 $this->trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200180
Timothy Warren76e04352012-02-14 11:55:17 -0500181 return TRUE;
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Commit Transaction
188 *
Timothy Warren7221f942012-02-14 15:02:33 -0500189 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500190 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500191 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500192 {
193 if ( ! $this->trans_enabled)
194 {
195 return TRUE;
196 }
197
198 // When transactions are nested we only begin/commit/rollback the outermost ones
199 if ($this->_trans_depth > 0)
200 {
201 return TRUE;
202 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200203
Timothy Warren2062a862012-02-20 19:38:10 -0500204 return @ibase_commit($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Rollback Transaction
211 *
Timothy Warren7221f942012-02-14 15:02:33 -0500212 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500213 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500214 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500215 {
216 if ( ! $this->trans_enabled)
217 {
218 return TRUE;
219 }
220
221 // When transactions are nested we only begin/commit/rollback the outermost ones
222 if ($this->_trans_depth > 0)
223 {
224 return TRUE;
225 }
226
Timothy Warren2062a862012-02-20 19:38:10 -0500227 return @ibase_rollback($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * Escape String
234 *
Timothy Warren7221f942012-02-14 15:02:33 -0500235 * @param string
236 * @param bool whether or not the string will be used in a LIKE condition
237 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500238 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500239 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500240 {
241 if (is_array($str))
242 {
243 foreach ($str as $key => $val)
244 {
245 $str[$key] = $this->escape_str($val, $like);
246 }
247
248 return $str;
249 }
250
251 // escape LIKE condition wildcards
252 if ($like === TRUE)
253 {
254 $str = str_replace( array('%', '_', $this->_like_escape_chr),
255 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
256 $str);
257 }
258
259 return $str;
260 }
261
262 // --------------------------------------------------------------------
263
264 /**
265 * Affected Rows
266 *
Timothy Warren7221f942012-02-14 15:02:33 -0500267 * @return integer
Timothy Warren76e04352012-02-14 11:55:17 -0500268 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500269 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500270 {
Timothy Warren7221f942012-02-14 15:02:33 -0500271 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500272 }
273
274 // --------------------------------------------------------------------
275
276 /**
277 * Insert ID
278 *
Timothy Warren125fe732012-02-21 07:58:25 -0500279 * @param string $generator_name
280 * @param integer $inc_by
Timothy Warren7221f942012-02-14 15:02:33 -0500281 * @return integer
Timothy Warren76e04352012-02-14 11:55:17 -0500282 */
Timothy Warren817af192012-02-16 08:28:00 -0500283 public function insert_id($generator_name, $inc_by=0)
Timothy Warren76e04352012-02-14 11:55:17 -0500284 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500285 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500286 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * "Count All" query
293 *
294 * Generates a platform-specific query string that counts all records in
295 * the specified database
296 *
Timothy Warren7221f942012-02-14 15:02:33 -0500297 * @param string
298 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500299 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500300 public function count_all($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500301 {
302 if ($table == '')
303 {
304 return 0;
305 }
306
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200307 $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 -0500308 if ($query->num_rows() == 0)
309 {
310 return 0;
311 }
312
313 $row = $query->row();
314 $this->_reset_select();
315 return (int) $row->numrows;
316 }
317
318 // --------------------------------------------------------------------
319
320 /**
321 * List table query
322 *
323 * Generates a platform-specific query string so that the table names can be fetched
324 *
Timothy Warren7221f942012-02-14 15:02:33 -0500325 * @param boolean
326 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500327 */
Timothy Warren95562142012-02-20 17:37:21 -0500328 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500329 {
330 $sql = <<<SQL
331 SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
332 WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%'
333 AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%'
334SQL;
335
336 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
337 {
Timothy Warren3d985a12012-02-15 11:38:00 -0500338 $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 -0500339 }
340 return $sql;
341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * Show column query
347 *
348 * Generates a platform-specific query string so that the column names can be fetched
349 *
Timothy Warren7221f942012-02-14 15:02:33 -0500350 * @param string the table name
351 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500352 */
Timothy Warren95562142012-02-20 17:37:21 -0500353 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500354 {
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500355 return <<<SQL
Timothy Warreneb6dcf02012-02-15 16:48:51 -0500356 SELECT "RDB\$FIELD_NAME" FROM "RDB\$RELATION_FIELDS"
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500357 WHERE "RDB\$RELATION_NAME"='{$table}';
Timothy Warreneb6dcf02012-02-15 16:48:51 -0500358SQL;
Timothy Warren76e04352012-02-14 11:55:17 -0500359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Field data query
365 *
366 * Generates a platform-specific query so that the column data can be retrieved
367 *
Timothy Warren7221f942012-02-14 15:02:33 -0500368 * @param string the table name
369 * @return object
Timothy Warren76e04352012-02-14 11:55:17 -0500370 */
Timothy Warren95562142012-02-20 17:37:21 -0500371 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500372 {
Timothy Warren8be31a92012-02-15 11:48:57 -0500373 // Need to find a more efficient way to do this
Andrey Andreev00df2e32012-03-02 18:37:16 +0200374 // but Interbase/Firebird seems to lack the
Timothy Warren8be31a92012-02-15 11:48:57 -0500375 // limit clause
376 return "SELECT * FROM {$table}";
Timothy Warren76e04352012-02-14 11:55:17 -0500377 }
378
379 // --------------------------------------------------------------------
380
381 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200382 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500383 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200384 * Returns an array containing code and message of the last
385 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500386 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200387 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500388 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200389 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500390 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200391 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500392 }
393
394 // --------------------------------------------------------------------
395
396 /**
397 * Escape the SQL Identifiers
398 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500399 * This public function escapes column and table names
Timothy Warren76e04352012-02-14 11:55:17 -0500400 *
Timothy Warren7221f942012-02-14 15:02:33 -0500401 * @param string
402 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500403 */
Timothy Warren95562142012-02-20 17:37:21 -0500404 protected function _escape_identifiers($item)
Timothy Warren76e04352012-02-14 11:55:17 -0500405 {
406 foreach ($this->_reserved_identifiers as $id)
407 {
408 if (strpos($item, '.'.$id) !== FALSE)
409 {
410 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
411
412 // remove duplicates if the user already included the escape
413 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
414 }
415 }
416
417 if (strpos($item, '.') !== FALSE)
418 {
419 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
420 }
421 else
422 {
423 $str = $this->_escape_char.$item.$this->_escape_char;
424 }
425
426 // remove duplicates if the user already included the escape
Timothy Warrend19e47a2012-02-14 23:40:40 -0500427 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * From Tables
434 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500435 * This public function implicitly groups FROM tables so there is no confusion
Timothy Warren76e04352012-02-14 11:55:17 -0500436 * about operator precedence in harmony with SQL standards
437 *
Timothy Warren7221f942012-02-14 15:02:33 -0500438 * @param type
439 * @return type
Timothy Warren76e04352012-02-14 11:55:17 -0500440 */
Timothy Warren95562142012-02-20 17:37:21 -0500441 protected function _from_tables($tables)
Timothy Warren76e04352012-02-14 11:55:17 -0500442 {
443 if ( ! is_array($tables))
444 {
445 $tables = array($tables);
446 }
447
Timothy Warren817af192012-02-16 08:28:00 -0500448 //Interbase/Firebird doesn't like grouped tables
Timothy Warrend19e47a2012-02-14 23:40:40 -0500449 return implode(', ', $tables);
Timothy Warren76e04352012-02-14 11:55:17 -0500450 }
451
452 // --------------------------------------------------------------------
453
454 /**
455 * Insert statement
456 *
457 * Generates a platform-specific insert string from the supplied data
458 *
Timothy Warren7221f942012-02-14 15:02:33 -0500459 * @param string the table name
460 * @param array the insert keys
461 * @param array the insert values
462 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500463 */
Timothy Warren95562142012-02-20 17:37:21 -0500464 protected function _insert($table, $keys, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500465 {
Timothy Warren817af192012-02-16 08:28:00 -0500466 return "INSERT INTO {$table} (".implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500467 }
468
469 // --------------------------------------------------------------------
470
471 /**
472 * Update statement
473 *
474 * Generates a platform-specific update string from the supplied data
475 *
Timothy Warren7221f942012-02-14 15:02:33 -0500476 * @param string the table name
477 * @param array the update data
478 * @param array the where clause
479 * @param array the orderby clause
480 * @param array the limit clause
481 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500482 */
Timothy Warren95562142012-02-20 17:37:21 -0500483 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500484 {
485 foreach ($values as $key => $val)
486 {
487 $valstr[] = $key." = ".$val;
488 }
489
Timothy Warren8be31a92012-02-15 11:48:57 -0500490 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500491
492 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
493
Timothy Warren817af192012-02-16 08:28:00 -0500494 $sql = "UPDATE {$table} SET ".implode(', ', $valstr);
Timothy Warren76e04352012-02-14 11:55:17 -0500495
Timothy Warren817af192012-02-16 08:28:00 -0500496 $sql .= ($where != '' AND count($where) >=1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren76e04352012-02-14 11:55:17 -0500497
Timothy Warren8be31a92012-02-15 11:48:57 -0500498 $sql .= $orderby;
Timothy Warren76e04352012-02-14 11:55:17 -0500499
500 return $sql;
501 }
502
503
504 // --------------------------------------------------------------------
505
506 /**
507 * Truncate statement
508 *
509 * Generates a platform-specific truncate string from the supplied data
510 * If the database does not support the truncate() command
Timothy Warren4be822b2012-02-14 12:07:34 -0500511 * This public function maps to "DELETE FROM table"
Timothy Warren76e04352012-02-14 11:55:17 -0500512 *
Timothy Warren7221f942012-02-14 15:02:33 -0500513 * @param string the table name
514 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500515 */
Timothy Warren95562142012-02-20 17:37:21 -0500516 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500517 {
518 return $this->_delete($table);
519 }
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Delete statement
525 *
526 * Generates a platform-specific delete string from the supplied data
527 *
Timothy Warren7221f942012-02-14 15:02:33 -0500528 * @param string the table name
529 * @param array the where clause
530 * @param string the limit clause
531 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500532 */
Timothy Warren95562142012-02-20 17:37:21 -0500533 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500534 {
535 $conditions = '';
536
537 if (count($where) > 0 OR count($like) > 0)
538 {
539 $conditions = "\nWHERE ";
540 $conditions .= implode("\n", $this->ar_where);
541
542 if (count($where) > 0 && count($like) > 0)
543 {
Timothy Warren817af192012-02-16 08:28:00 -0500544 $conditions .= ' AND ';
Timothy Warren76e04352012-02-14 11:55:17 -0500545 }
546 $conditions .= implode("\n", $like);
547 }
548
Timothy Warren3d985a12012-02-15 11:38:00 -0500549 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500550
Timothy Warren8be31a92012-02-15 11:48:57 -0500551 return "DELETE FROM {$table}{$conditions}";
Timothy Warren76e04352012-02-14 11:55:17 -0500552 }
553
554 // --------------------------------------------------------------------
555
556 /**
557 * Limit string
558 *
559 * Generates a platform-specific LIMIT clause
560 *
Timothy Warren7221f942012-02-14 15:02:33 -0500561 * @param string the sql query string
562 * @param integer the number of rows to limit the query to
563 * @param integer the offset value
564 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500565 */
Timothy Warren95562142012-02-20 17:37:21 -0500566 protected function _limit($sql, $limit, $offset)
Timothy Warren76e04352012-02-14 11:55:17 -0500567 {
Timothy Warren56784f62012-02-29 11:58:20 -0500568 // Keep the current sql string safe for a moment
569 $orig_sql = $sql;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200570
Timothy Warren56784f62012-02-29 11:58:20 -0500571 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200572 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500573 {
574 $sql = 'FIRST '. (int) $limit;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200575
Timothy Warren56784f62012-02-29 11:58:20 -0500576 if ($offset > 0)
577 {
Timothy Warren9a960822012-02-29 22:53:35 -0500578 $sql .= ' SKIP '. (int) $offset;
Timothy Warren56784f62012-02-29 11:58:20 -0500579 }
580 }
581 else
582 {
583 $sql = 'ROWS ' . (int) $limit;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200584
Timothy Warren56784f62012-02-29 11:58:20 -0500585 if ($offset > 0)
586 {
587 $sql = 'ROWS '. (int) $offset . ' TO ' . ($limit + $offset);
588 }
589 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200590
591 return preg_replace('`SELECT`i', "SELECT {$sql}", $orig_sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500592 }
593
594 // --------------------------------------------------------------------
595
596 /**
597 * Close DB Connection
598 *
Timothy Warren7221f942012-02-14 15:02:33 -0500599 * @param resource
600 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500601 */
Timothy Warren95562142012-02-20 17:37:21 -0500602 protected function _close($conn_id)
Timothy Warren76e04352012-02-14 11:55:17 -0500603 {
Timothy Warren7221f942012-02-14 15:02:33 -0500604 @ibase_close($conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500605 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200606
Timothy Warren76e04352012-02-14 11:55:17 -0500607}
608
Timothy Warren76e04352012-02-14 11:55:17 -0500609/* End of file interbase_driver.php */
Timothy Warren215890b2012-03-20 09:38:16 -0400610/* Location: ./system/database/drivers/interbase/interbase_driver.php */