blob: e55a476bb2cad25e1bafc43c376de99699542bb7 [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
Timothy Warren76e04352012-02-14 11:55:17 -050061
62 // 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 /**
118 * Set client character set
119 *
Timothy Warren7221f942012-02-14 15:02:33 -0500120 * @param string
121 * @param string
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500122 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500123 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500124 public function db_set_charset($charset, $collation)
Timothy Warren76e04352012-02-14 11:55:17 -0500125 {
Timothy Warren3d985a12012-02-15 11:38:00 -0500126 // Must be determined at connection
Timothy Warren76e04352012-02-14 11:55:17 -0500127 return TRUE;
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Version number query string
134 *
Timothy Warren7221f942012-02-14 15:02:33 -0500135 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500136 */
Timothy Warren95562142012-02-20 17:37:21 -0500137 protected function _version()
Timothy Warren76e04352012-02-14 11:55:17 -0500138 {
Timothy Warren3d985a12012-02-15 11:38:00 -0500139 if (($service = ibase_service_attach($this->hostname, $this->username, $this->password)))
140 {
141 $version = ibase_server_info($service, IBASE_SVC_SERVER_VERSION);
Timothy Warrenab189e12012-02-22 10:34:23 -0500142
143 // Don't keep the service open
144 ibase_service_detach($service);
Timothy Warren3d985a12012-02-15 11:38:00 -0500145 return $version;
146 }
147
148 return FALSE;
Timothy Warren76e04352012-02-14 11:55:17 -0500149 }
150
151 // --------------------------------------------------------------------
152
153 /**
154 * Execute the query
155 *
Timothy Warren7221f942012-02-14 15:02:33 -0500156 * @param string an SQL query
157 * @return resource
Timothy Warren76e04352012-02-14 11:55:17 -0500158 */
Timothy Warren95562142012-02-20 17:37:21 -0500159 protected function _execute($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500160 {
161 $sql = $this->_prep_query($sql);
Timothy Warren7221f942012-02-14 15:02:33 -0500162 return @ibase_query($this->conn_id, $sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500163 }
164
165 // --------------------------------------------------------------------
166
167 /**
168 * Prep the query
169 *
170 * If needed, each database adapter can prep the query string
171 *
Timothy Warren7221f942012-02-14 15:02:33 -0500172 * @param string an SQL query
173 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500174 */
Timothy Warren95562142012-02-20 17:37:21 -0500175 protected function _prep_query($sql)
Timothy Warren76e04352012-02-14 11:55:17 -0500176 {
177 return $sql;
178 }
179
180 // --------------------------------------------------------------------
181
182 /**
183 * Begin Transaction
184 *
Timothy Warren7221f942012-02-14 15:02:33 -0500185 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500186 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500187 public function trans_begin($test_mode = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500188 {
189 if ( ! $this->trans_enabled)
190 {
191 return TRUE;
192 }
193
194 // When transactions are nested we only begin/commit/rollback the outermost ones
195 if ($this->_trans_depth > 0)
196 {
197 return TRUE;
198 }
199
200 // Reset the transaction failure flag.
201 // If the $test_mode flag is set to TRUE transactions will be rolled back
202 // even if the queries produce a successful result.
203 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
204
Timothy Warren7221f942012-02-14 15:02:33 -0500205 $this->trans = @ibase_trans($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500206
207 return TRUE;
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Commit Transaction
214 *
Timothy Warren7221f942012-02-14 15:02:33 -0500215 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500216 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500217 public function trans_commit()
Timothy Warren76e04352012-02-14 11:55:17 -0500218 {
219 if ( ! $this->trans_enabled)
220 {
221 return TRUE;
222 }
223
224 // When transactions are nested we only begin/commit/rollback the outermost ones
225 if ($this->_trans_depth > 0)
226 {
227 return TRUE;
228 }
229
Timothy Warren2062a862012-02-20 19:38:10 -0500230 return @ibase_commit($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * Rollback Transaction
237 *
Timothy Warren7221f942012-02-14 15:02:33 -0500238 * @return bool
Timothy Warren76e04352012-02-14 11:55:17 -0500239 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500240 public function trans_rollback()
Timothy Warren76e04352012-02-14 11:55:17 -0500241 {
242 if ( ! $this->trans_enabled)
243 {
244 return TRUE;
245 }
246
247 // When transactions are nested we only begin/commit/rollback the outermost ones
248 if ($this->_trans_depth > 0)
249 {
250 return TRUE;
251 }
252
Timothy Warren2062a862012-02-20 19:38:10 -0500253 return @ibase_rollback($this->trans);
Timothy Warren76e04352012-02-14 11:55:17 -0500254 }
255
256 // --------------------------------------------------------------------
257
258 /**
259 * Escape String
260 *
Timothy Warren7221f942012-02-14 15:02:33 -0500261 * @param string
262 * @param bool whether or not the string will be used in a LIKE condition
263 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500264 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500265 public function escape_str($str, $like = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500266 {
267 if (is_array($str))
268 {
269 foreach ($str as $key => $val)
270 {
271 $str[$key] = $this->escape_str($val, $like);
272 }
273
274 return $str;
275 }
276
277 // escape LIKE condition wildcards
278 if ($like === TRUE)
279 {
280 $str = str_replace( array('%', '_', $this->_like_escape_chr),
281 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
282 $str);
283 }
284
285 return $str;
286 }
287
288 // --------------------------------------------------------------------
289
290 /**
291 * Affected Rows
292 *
Timothy Warren7221f942012-02-14 15:02:33 -0500293 * @return integer
Timothy Warren76e04352012-02-14 11:55:17 -0500294 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500295 public function affected_rows()
Timothy Warren76e04352012-02-14 11:55:17 -0500296 {
Timothy Warren7221f942012-02-14 15:02:33 -0500297 return @ibase_affected_rows($this->conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Insert ID
304 *
Timothy Warren125fe732012-02-21 07:58:25 -0500305 * @param string $generator_name
306 * @param integer $inc_by
Timothy Warren7221f942012-02-14 15:02:33 -0500307 * @return integer
Timothy Warren76e04352012-02-14 11:55:17 -0500308 */
Timothy Warren817af192012-02-16 08:28:00 -0500309 public function insert_id($generator_name, $inc_by=0)
Timothy Warren76e04352012-02-14 11:55:17 -0500310 {
Timothy Warren07b660b2012-02-20 13:23:06 -0500311 //If a generator hasn't been used before it will return 0
Timothy Warrenfed2d1d2012-02-20 15:42:45 -0500312 return ibase_gen_id('"'.$generator_name.'"', $inc_by);
Timothy Warren76e04352012-02-14 11:55:17 -0500313 }
314
315 // --------------------------------------------------------------------
316
317 /**
318 * "Count All" query
319 *
320 * Generates a platform-specific query string that counts all records in
321 * the specified database
322 *
Timothy Warren7221f942012-02-14 15:02:33 -0500323 * @param string
324 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500325 */
Timothy Warren4be822b2012-02-14 12:07:34 -0500326 public function count_all($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500327 {
328 if ($table == '')
329 {
330 return 0;
331 }
332
Timothy Warren817af192012-02-16 08:28:00 -0500333 $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 -0500334
335 if ($query->num_rows() == 0)
336 {
337 return 0;
338 }
339
340 $row = $query->row();
341 $this->_reset_select();
342 return (int) $row->numrows;
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * List table query
349 *
350 * Generates a platform-specific query string so that the table names can be fetched
351 *
Timothy Warren7221f942012-02-14 15:02:33 -0500352 * @param boolean
353 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500354 */
Timothy Warren95562142012-02-20 17:37:21 -0500355 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500356 {
357 $sql = <<<SQL
358 SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
359 WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%'
360 AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%'
361SQL;
362
363 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
364 {
Timothy Warren3d985a12012-02-15 11:38:00 -0500365 $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 -0500366 }
367 return $sql;
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * Show column query
374 *
375 * Generates a platform-specific query string so that the column names can be fetched
376 *
Timothy Warren7221f942012-02-14 15:02:33 -0500377 * @param string the table name
378 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500379 */
Timothy Warren95562142012-02-20 17:37:21 -0500380 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500381 {
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500382 return <<<SQL
Timothy Warreneb6dcf02012-02-15 16:48:51 -0500383 SELECT "RDB\$FIELD_NAME" FROM "RDB\$RELATION_FIELDS"
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500384 WHERE "RDB\$RELATION_NAME"='{$table}';
Timothy Warreneb6dcf02012-02-15 16:48:51 -0500385SQL;
Timothy Warren76e04352012-02-14 11:55:17 -0500386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Field data query
392 *
393 * Generates a platform-specific query so that the column data can be retrieved
394 *
Timothy Warren7221f942012-02-14 15:02:33 -0500395 * @param string the table name
396 * @return object
Timothy Warren76e04352012-02-14 11:55:17 -0500397 */
Timothy Warren95562142012-02-20 17:37:21 -0500398 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500399 {
Timothy Warren8be31a92012-02-15 11:48:57 -0500400 // Need to find a more efficient way to do this
401 // but Interbase/Firebird seems to lack the
402 // limit clause
403 return "SELECT * FROM {$table}";
Timothy Warren76e04352012-02-14 11:55:17 -0500404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * The error message string
410 *
Timothy Warren7221f942012-02-14 15:02:33 -0500411 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500412 */
Timothy Warren95562142012-02-20 17:37:21 -0500413 protected function _error_message()
Timothy Warren76e04352012-02-14 11:55:17 -0500414 {
415 return ibase_errmsg();
416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * The error message number
422 *
Timothy Warren7221f942012-02-14 15:02:33 -0500423 * @return integer
Timothy Warren76e04352012-02-14 11:55:17 -0500424 */
Timothy Warren95562142012-02-20 17:37:21 -0500425 protected function _error_number()
Timothy Warren76e04352012-02-14 11:55:17 -0500426 {
427 return ibase_errcode();
428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * Escape the SQL Identifiers
434 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500435 * This public function escapes column and table names
Timothy Warren76e04352012-02-14 11:55:17 -0500436 *
Timothy Warren7221f942012-02-14 15:02:33 -0500437 * @param string
438 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500439 */
Timothy Warren95562142012-02-20 17:37:21 -0500440 protected function _escape_identifiers($item)
Timothy Warren76e04352012-02-14 11:55:17 -0500441 {
442 foreach ($this->_reserved_identifiers as $id)
443 {
444 if (strpos($item, '.'.$id) !== FALSE)
445 {
446 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
447
448 // remove duplicates if the user already included the escape
449 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
450 }
451 }
452
453 if (strpos($item, '.') !== FALSE)
454 {
455 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
456 }
457 else
458 {
459 $str = $this->_escape_char.$item.$this->_escape_char;
460 }
461
462 // remove duplicates if the user already included the escape
Timothy Warrend19e47a2012-02-14 23:40:40 -0500463 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500464 }
465
466 // --------------------------------------------------------------------
467
468 /**
469 * From Tables
470 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500471 * This public function implicitly groups FROM tables so there is no confusion
Timothy Warren76e04352012-02-14 11:55:17 -0500472 * about operator precedence in harmony with SQL standards
473 *
Timothy Warren7221f942012-02-14 15:02:33 -0500474 * @param type
475 * @return type
Timothy Warren76e04352012-02-14 11:55:17 -0500476 */
Timothy Warren95562142012-02-20 17:37:21 -0500477 protected function _from_tables($tables)
Timothy Warren76e04352012-02-14 11:55:17 -0500478 {
479 if ( ! is_array($tables))
480 {
481 $tables = array($tables);
482 }
483
Timothy Warren817af192012-02-16 08:28:00 -0500484 //Interbase/Firebird doesn't like grouped tables
Timothy Warrend19e47a2012-02-14 23:40:40 -0500485 return implode(', ', $tables);
Timothy Warren76e04352012-02-14 11:55:17 -0500486 }
487
488 // --------------------------------------------------------------------
489
490 /**
491 * Insert statement
492 *
493 * Generates a platform-specific insert string from the supplied data
494 *
Timothy Warren7221f942012-02-14 15:02:33 -0500495 * @param string the table name
496 * @param array the insert keys
497 * @param array the insert values
498 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500499 */
Timothy Warren95562142012-02-20 17:37:21 -0500500 protected function _insert($table, $keys, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500501 {
Timothy Warren817af192012-02-16 08:28:00 -0500502 return "INSERT INTO {$table} (".implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500503 }
504
505 // --------------------------------------------------------------------
506
507 /**
508 * Update statement
509 *
510 * Generates a platform-specific update string from the supplied data
511 *
Timothy Warren7221f942012-02-14 15:02:33 -0500512 * @param string the table name
513 * @param array the update data
514 * @param array the where clause
515 * @param array the orderby clause
516 * @param array the limit clause
517 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500518 */
Timothy Warren95562142012-02-20 17:37:21 -0500519 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500520 {
521 foreach ($values as $key => $val)
522 {
523 $valstr[] = $key." = ".$val;
524 }
525
Timothy Warren8be31a92012-02-15 11:48:57 -0500526 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500527
528 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
529
Timothy Warren817af192012-02-16 08:28:00 -0500530 $sql = "UPDATE {$table} SET ".implode(', ', $valstr);
Timothy Warren76e04352012-02-14 11:55:17 -0500531
Timothy Warren817af192012-02-16 08:28:00 -0500532 $sql .= ($where != '' AND count($where) >=1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren76e04352012-02-14 11:55:17 -0500533
Timothy Warren8be31a92012-02-15 11:48:57 -0500534 $sql .= $orderby;
Timothy Warren76e04352012-02-14 11:55:17 -0500535
536 return $sql;
537 }
538
539
540 // --------------------------------------------------------------------
541
542 /**
543 * Truncate statement
544 *
545 * Generates a platform-specific truncate string from the supplied data
546 * If the database does not support the truncate() command
Timothy Warren4be822b2012-02-14 12:07:34 -0500547 * This public function maps to "DELETE FROM table"
Timothy Warren76e04352012-02-14 11:55:17 -0500548 *
Timothy Warren7221f942012-02-14 15:02:33 -0500549 * @param string the table name
550 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500551 */
Timothy Warren95562142012-02-20 17:37:21 -0500552 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500553 {
554 return $this->_delete($table);
555 }
556
557 // --------------------------------------------------------------------
558
559 /**
560 * Delete statement
561 *
562 * Generates a platform-specific delete string from the supplied data
563 *
Timothy Warren7221f942012-02-14 15:02:33 -0500564 * @param string the table name
565 * @param array the where clause
566 * @param string the limit clause
567 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500568 */
Timothy Warren95562142012-02-20 17:37:21 -0500569 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500570 {
571 $conditions = '';
572
573 if (count($where) > 0 OR count($like) > 0)
574 {
575 $conditions = "\nWHERE ";
576 $conditions .= implode("\n", $this->ar_where);
577
578 if (count($where) > 0 && count($like) > 0)
579 {
Timothy Warren817af192012-02-16 08:28:00 -0500580 $conditions .= ' AND ';
Timothy Warren76e04352012-02-14 11:55:17 -0500581 }
582 $conditions .= implode("\n", $like);
583 }
584
Timothy Warren3d985a12012-02-15 11:38:00 -0500585 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500586
Timothy Warren8be31a92012-02-15 11:48:57 -0500587 return "DELETE FROM {$table}{$conditions}";
Timothy Warren76e04352012-02-14 11:55:17 -0500588 }
589
590 // --------------------------------------------------------------------
591
592 /**
593 * Limit string
594 *
595 * Generates a platform-specific LIMIT clause
596 *
Timothy Warren7221f942012-02-14 15:02:33 -0500597 * @param string the sql query string
598 * @param integer the number of rows to limit the query to
599 * @param integer the offset value
600 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500601 */
Timothy Warren95562142012-02-20 17:37:21 -0500602 protected function _limit($sql, $limit, $offset)
Timothy Warren76e04352012-02-14 11:55:17 -0500603 {
Timothy Warren56784f62012-02-29 11:58:20 -0500604 // Keep the current sql string safe for a moment
605 $orig_sql = $sql;
606
607 // Limit clause depends on if Interbase or Firebird
608 if (stripos($this->_version(), 'firebird') !== FALSE)
609 {
610 $sql = 'FIRST '. (int) $limit;
611
612 if ($offset > 0)
613 {
614 $sql .= ' SKIP'. (int) $offset;
615 }
616 }
617 else
618 {
619 $sql = 'ROWS ' . (int) $limit;
620
621 if ($offset > 0)
622 {
623 $sql = 'ROWS '. (int) $offset . ' TO ' . ($limit + $offset);
624 }
625 }
626
627 $sql = preg_replace("`SELECT`i", "SELECT {$sql}", $orig_sql);
628
Timothy Warren3d985a12012-02-15 11:38:00 -0500629 return $sql;
Timothy Warren76e04352012-02-14 11:55:17 -0500630 }
631
632 // --------------------------------------------------------------------
633
634 /**
635 * Close DB Connection
636 *
Timothy Warren7221f942012-02-14 15:02:33 -0500637 * @param resource
638 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500639 */
Timothy Warren95562142012-02-20 17:37:21 -0500640 protected function _close($conn_id)
Timothy Warren76e04352012-02-14 11:55:17 -0500641 {
Timothy Warren7221f942012-02-14 15:02:33 -0500642 @ibase_close($conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500643 }
Timothy Warren76e04352012-02-14 11:55:17 -0500644}
645
Timothy Warren76e04352012-02-14 11:55:17 -0500646/* End of file interbase_driver.php */
647/* Location: ./system/database/drivers/interbase/interbase_driver.php */