blob: 51e814e16e52fb657a4024becafedc825c613fd4 [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 /**
Timothy Warren76e04352012-02-14 11:55:17 -0500118 * Version number query string
119 *
Timothy Warren7221f942012-02-14 15:02:33 -0500120 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500121 */
Timothy Warren95562142012-02-20 17:37:21 -0500122 protected function _version()
Timothy Warren76e04352012-02-14 11:55:17 -0500123 {
Timothy Warren3d985a12012-02-15 11:38:00 -0500124 if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
125 {
126 $version = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200127
Timothy Warrenab189e12012-02-22 10:34:23 -0500128 // Don't keep the service open
129 ibase_service_detach($service);
Timothy Warren3d985a12012-02-15 11:38:00 -0500130 return $version;
131 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200132
Timothy Warren3d985a12012-02-15 11:38:00 -0500133 return FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -0500134 }
135
136 // --------------------------------------------------------------------
137
138 /**
139 * Execute the query
140 *
Timothy Warren7221f942012-02-14 15:02:33 -0500141 * @param string an SQL query
142 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -0500143 */
Timothy Warren95562142012-02-20 17:37:21 -0500144 protected function _execute($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500145 {
146 $sql = $this->_prep_query($sql);
Timothy Warren7221f942012-02-14 15:02:33 -0500147 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500148 }
149
150 // --------------------------------------------------------------------
151
152 /**
153 * Prep the query
154 *
155 * If needed, each database adapter can prep the query string
156 *
Timothy Warren7221f942012-02-14 15:02:33 -0500157 * @param string an SQL query
158 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500159 */
Timothy Warren95562142012-02-20 17:37:21 -0500160 protected function _prep_query($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500161 {
162 return $sql;
163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Begin Transaction
169 *
Timothy Warren7221f942012-02-14 15:02:33 -0500170 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500171 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500172 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500173 {
174 if ( ! $this->trans_enabled)
175 {
176 return TRUE;
177 }
178
179 // When transactions are nested we only begin/commit/rollback the outermost ones
180 if ($this->_trans_depth > 0)
181 {
182 return TRUE;
183 }
184
185 // Reset the transaction failure flag.
186 // If the $test_mode flag is set to TRUE transactions will be rolled back
187 // even if the queries produce a successful result.
188 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
189
Timothy Warren7221f942012-02-14 15:02:33 -0500190 $this->trans = @ibase_trans($this->conn_id);
Andrey Andreev00df2e32012-03-02 18:37:16 +0200191
Timothy Warren76e04352012-02-14 11:55:17 -0500192 return TRUE;
193 }
194
195 // --------------------------------------------------------------------
196
197 /**
198 * Commit 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_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500203 {
204 if ( ! $this->trans_enabled)
205 {
206 return TRUE;
207 }
208
209 // When transactions are nested we only begin/commit/rollback the outermost ones
210 if ($this->_trans_depth > 0)
211 {
212 return TRUE;
213 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200214
Timothy Warren2062a862012-02-20 19:38:10 -0500215 return @ibase_commit($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Rollback Transaction
222 *
Timothy Warren7221f942012-02-14 15:02:33 -0500223 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500224 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500225 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500226 {
227 if ( ! $this->trans_enabled)
228 {
229 return TRUE;
230 }
231
232 // When transactions are nested we only begin/commit/rollback the outermost ones
233 if ($this->_trans_depth > 0)
234 {
235 return TRUE;
236 }
237
Timothy Warren2062a862012-02-20 19:38:10 -0500238 return @ibase_rollback($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500239 }
240
241 // --------------------------------------------------------------------
242
243 /**
244 * Escape String
245 *
Timothy Warren7221f942012-02-14 15:02:33 -0500246 * @param string
247 * @param bool whether or not the string will be used in a LIKE condition
248 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500249 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500250 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500251 {
252 if (is_array($str))
253 {
254 foreach ($str as $key => $val)
255 {
256 $str[$key] = $this->escape_str($val, $like);
257 }
258
259 return $str;
260 }
261
262 // escape LIKE condition wildcards
263 if ($like === TRUE)
264 {
265 $str = str_replace( array('%', '_', $this->_like_escape_chr),
266 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
267 $str);
268 }
269
270 return $str;
271 }
272
273 // --------------------------------------------------------------------
274
275 /**
276 * Affected Rows
277 *
Timothy Warren7221f942012-02-14 15:02:33 -0500278 * @return integer
Timothy Warren76e04352012-02-14 11:55:17 -0500279 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500280 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500281 {
Timothy Warren7221f942012-02-14 15:02:33 -0500282 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * Insert ID
289 *
Timothy Warren125fe732012-02-21 07:58:25 -0500290 * @param string $generator_name
291 * @param integer $inc_by
Timothy Warren7221f942012-02-14 15:02:33 -0500292 * @return integer
Timothy Warren76e04352012-02-14 11:55:17 -0500293 */
Timothy Warren817af192012-02-16 08:28:00 -0500294 public function insert_id($generator_name, $inc_by=0)
Timothy Warren76e04352012-02-14 11:55:17 -0500295 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500296 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500297 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * "Count All" query
304 *
305 * Generates a platform-specific query string that counts all records in
306 * the specified database
307 *
Timothy Warren7221f942012-02-14 15:02:33 -0500308 * @param string
309 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500310 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500311 public function count_all($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500312 {
313 if ($table == '')
314 {
315 return 0;
316 }
317
Timothy Warren817af192012-02-16 08:28:00 -0500318 $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 -0500319
320 if ($query->num_rows() == 0)
321 {
322 return 0;
323 }
324
325 $row = $query->row();
326 $this->_reset_select();
327 return (int) $row->numrows;
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * List table query
334 *
335 * Generates a platform-specific query string so that the table names can be fetched
336 *
Timothy Warren7221f942012-02-14 15:02:33 -0500337 * @param boolean
338 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500339 */
Timothy Warren95562142012-02-20 17:37:21 -0500340 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500341 {
342 $sql = <<<SQL
343 SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
344 WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%'
345 AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%'
346SQL;
347
348 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
349 {
Timothy Warren3d985a12012-02-15 11:38:00 -0500350 $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 -0500351 }
352 return $sql;
353 }
354
355 // --------------------------------------------------------------------
356
357 /**
358 * Show column query
359 *
360 * Generates a platform-specific query string so that the column names can be fetched
361 *
Timothy Warren7221f942012-02-14 15:02:33 -0500362 * @param string the table name
363 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500364 */
Timothy Warren95562142012-02-20 17:37:21 -0500365 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500366 {
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500367 return <<<SQL
Timothy Warreneb6dcf02012-02-15 16:48:51 -0500368 SELECT "RDB\$FIELD_NAME" FROM "RDB\$RELATION_FIELDS"
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500369 WHERE "RDB\$RELATION_NAME"='{$table}';
Timothy Warreneb6dcf02012-02-15 16:48:51 -0500370SQL;
Timothy Warren76e04352012-02-14 11:55:17 -0500371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * Field data query
377 *
378 * Generates a platform-specific query so that the column data can be retrieved
379 *
Timothy Warren7221f942012-02-14 15:02:33 -0500380 * @param string the table name
381 * @return object
Timothy Warren76e04352012-02-14 11:55:17 -0500382 */
Timothy Warren95562142012-02-20 17:37:21 -0500383 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500384 {
Timothy Warren8be31a92012-02-15 11:48:57 -0500385 // Need to find a more efficient way to do this
Andrey Andreev00df2e32012-03-02 18:37:16 +0200386 // but Interbase/Firebird seems to lack the
Timothy Warren8be31a92012-02-15 11:48:57 -0500387 // limit clause
388 return "SELECT * FROM {$table}";
Timothy Warren76e04352012-02-14 11:55:17 -0500389 }
390
391 // --------------------------------------------------------------------
392
393 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200394 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500395 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200396 * Returns an array containing code and message of the last
397 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500398 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200399 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500400 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200401 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500402 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200403 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * Escape the SQL Identifiers
410 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500411 * This public function escapes column and table names
Timothy Warren76e04352012-02-14 11:55:17 -0500412 *
Timothy Warren7221f942012-02-14 15:02:33 -0500413 * @param string
414 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500415 */
Timothy Warren95562142012-02-20 17:37:21 -0500416 protected function _escape_identifiers($item)
Timothy Warren76e04352012-02-14 11:55:17 -0500417 {
418 foreach ($this->_reserved_identifiers as $id)
419 {
420 if (strpos($item, '.'.$id) !== FALSE)
421 {
422 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
423
424 // remove duplicates if the user already included the escape
425 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
426 }
427 }
428
429 if (strpos($item, '.') !== FALSE)
430 {
431 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
432 }
433 else
434 {
435 $str = $this->_escape_char.$item.$this->_escape_char;
436 }
437
438 // remove duplicates if the user already included the escape
Timothy Warrend19e47a2012-02-14 23:40:40 -0500439 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * From Tables
446 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500447 * This public function implicitly groups FROM tables so there is no confusion
Timothy Warren76e04352012-02-14 11:55:17 -0500448 * about operator precedence in harmony with SQL standards
449 *
Timothy Warren7221f942012-02-14 15:02:33 -0500450 * @param type
451 * @return type
Timothy Warren76e04352012-02-14 11:55:17 -0500452 */
Timothy Warren95562142012-02-20 17:37:21 -0500453 protected function _from_tables($tables)
Timothy Warren76e04352012-02-14 11:55:17 -0500454 {
455 if ( ! is_array($tables))
456 {
457 $tables = array($tables);
458 }
459
Timothy Warren817af192012-02-16 08:28:00 -0500460 //Interbase/Firebird doesn't like grouped tables
Timothy Warrend19e47a2012-02-14 23:40:40 -0500461 return implode(', ', $tables);
Timothy Warren76e04352012-02-14 11:55:17 -0500462 }
463
464 // --------------------------------------------------------------------
465
466 /**
467 * Insert statement
468 *
469 * Generates a platform-specific insert string from the supplied data
470 *
Timothy Warren7221f942012-02-14 15:02:33 -0500471 * @param string the table name
472 * @param array the insert keys
473 * @param array the insert values
474 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500475 */
Timothy Warren95562142012-02-20 17:37:21 -0500476 protected function _insert($table, $keys, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500477 {
Timothy Warren817af192012-02-16 08:28:00 -0500478 return "INSERT INTO {$table} (".implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500479 }
480
481 // --------------------------------------------------------------------
482
483 /**
484 * Update statement
485 *
486 * Generates a platform-specific update string from the supplied data
487 *
Timothy Warren7221f942012-02-14 15:02:33 -0500488 * @param string the table name
489 * @param array the update data
490 * @param array the where clause
491 * @param array the orderby clause
492 * @param array the limit clause
493 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500494 */
Timothy Warren95562142012-02-20 17:37:21 -0500495 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500496 {
497 foreach ($values as $key => $val)
498 {
499 $valstr[] = $key." = ".$val;
500 }
501
Timothy Warren8be31a92012-02-15 11:48:57 -0500502 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500503
504 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
505
Timothy Warren817af192012-02-16 08:28:00 -0500506 $sql = "UPDATE {$table} SET ".implode(', ', $valstr);
Timothy Warren76e04352012-02-14 11:55:17 -0500507
Timothy Warren817af192012-02-16 08:28:00 -0500508 $sql .= ($where != '' AND count($where) >=1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren76e04352012-02-14 11:55:17 -0500509
Timothy Warren8be31a92012-02-15 11:48:57 -0500510 $sql .= $orderby;
Timothy Warren76e04352012-02-14 11:55:17 -0500511
512 return $sql;
513 }
514
515
516 // --------------------------------------------------------------------
517
518 /**
519 * Truncate statement
520 *
521 * Generates a platform-specific truncate string from the supplied data
522 * If the database does not support the truncate() command
Timothy Warren4be822b2012-02-14 12:07:34 -0500523 * This public function maps to "DELETE FROM table"
Timothy Warren76e04352012-02-14 11:55:17 -0500524 *
Timothy Warren7221f942012-02-14 15:02:33 -0500525 * @param string the table name
526 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500527 */
Timothy Warren95562142012-02-20 17:37:21 -0500528 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500529 {
530 return $this->_delete($table);
531 }
532
533 // --------------------------------------------------------------------
534
535 /**
536 * Delete statement
537 *
538 * Generates a platform-specific delete string from the supplied data
539 *
Timothy Warren7221f942012-02-14 15:02:33 -0500540 * @param string the table name
541 * @param array the where clause
542 * @param string the limit clause
543 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500544 */
Timothy Warren95562142012-02-20 17:37:21 -0500545 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500546 {
547 $conditions = '';
548
549 if (count($where) > 0 OR count($like) > 0)
550 {
551 $conditions = "\nWHERE ";
552 $conditions .= implode("\n", $this->ar_where);
553
554 if (count($where) > 0 && count($like) > 0)
555 {
Timothy Warren817af192012-02-16 08:28:00 -0500556 $conditions .= ' AND ';
Timothy Warren76e04352012-02-14 11:55:17 -0500557 }
558 $conditions .= implode("\n", $like);
559 }
560
Timothy Warren3d985a12012-02-15 11:38:00 -0500561 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500562
Timothy Warren8be31a92012-02-15 11:48:57 -0500563 return "DELETE FROM {$table}{$conditions}";
Timothy Warren76e04352012-02-14 11:55:17 -0500564 }
565
566 // --------------------------------------------------------------------
567
568 /**
569 * Limit string
570 *
571 * Generates a platform-specific LIMIT clause
572 *
Timothy Warren7221f942012-02-14 15:02:33 -0500573 * @param string the sql query string
574 * @param integer the number of rows to limit the query to
575 * @param integer the offset value
576 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500577 */
Timothy Warren95562142012-02-20 17:37:21 -0500578 protected function _limit($sql, $limit, $offset)
Timothy Warren76e04352012-02-14 11:55:17 -0500579 {
Timothy Warren56784f62012-02-29 11:58:20 -0500580 // Keep the current sql string safe for a moment
581 $orig_sql = $sql;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200582
Timothy Warren56784f62012-02-29 11:58:20 -0500583 // Limit clause depends on if Interbase or Firebird
584 if (stripos($this->_version(), 'firebird') !== FALSE)
585 {
586 $sql = 'FIRST '. (int) $limit;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200587
Timothy Warren56784f62012-02-29 11:58:20 -0500588 if ($offset > 0)
589 {
Timothy Warren9a960822012-02-29 22:53:35 -0500590 $sql .= ' SKIP '. (int) $offset;
Timothy Warren56784f62012-02-29 11:58:20 -0500591 }
592 }
593 else
594 {
595 $sql = 'ROWS ' . (int) $limit;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200596
Timothy Warren56784f62012-02-29 11:58:20 -0500597 if ($offset > 0)
598 {
599 $sql = 'ROWS '. (int) $offset . ' TO ' . ($limit + $offset);
600 }
601 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200602
603 return preg_replace('`SELECT`i', "SELECT {$sql}", $orig_sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500604 }
605
606 // --------------------------------------------------------------------
607
608 /**
609 * Close DB Connection
610 *
Timothy Warren7221f942012-02-14 15:02:33 -0500611 * @param resource
612 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500613 */
Timothy Warren95562142012-02-20 17:37:21 -0500614 protected function _close($conn_id)
Timothy Warren76e04352012-02-14 11:55:17 -0500615 {
Timothy Warren7221f942012-02-14 15:02:33 -0500616 @ibase_close($conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500617 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200618
Timothy Warren76e04352012-02-14 11:55:17 -0500619}
620
Timothy Warren76e04352012-02-14 11:55:17 -0500621/* End of file interbase_driver.php */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200622/* Location: ./system/database/drivers/interbase/interbase_driver.php */