blob: f4bd9e271dd8fb7072ed73bad72687e31e6c292e [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
Timothy Warren817af192012-02-16 08:28:00 -0500323 $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
325 if ($query->num_rows() == 0)
326 {
327 return 0;
328 }
329
330 $row = $query->row();
331 $this->_reset_select();
332 return (int) $row->numrows;
333 }
334
335 // --------------------------------------------------------------------
336
337 /**
338 * List table query
339 *
340 * Generates a platform-specific query string so that the table names can be fetched
341 *
Timothy Warren7221f942012-02-14 15:02:33 -0500342 * @param boolean
343 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500344 */
Timothy Warren95562142012-02-20 17:37:21 -0500345 protected function _list_tables($prefix_limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500346 {
347 $sql = <<<SQL
348 SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
349 WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%'
350 AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%'
351SQL;
352
353 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
354 {
Timothy Warren3d985a12012-02-15 11:38:00 -0500355 $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 -0500356 }
357 return $sql;
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * Show column query
364 *
365 * Generates a platform-specific query string so that the column names can be fetched
366 *
Timothy Warren7221f942012-02-14 15:02:33 -0500367 * @param string the table name
368 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500369 */
Timothy Warren95562142012-02-20 17:37:21 -0500370 protected function _list_columns($table = '')
Timothy Warren76e04352012-02-14 11:55:17 -0500371 {
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500372 return <<<SQL
Timothy Warreneb6dcf02012-02-15 16:48:51 -0500373 SELECT "RDB\$FIELD_NAME" FROM "RDB\$RELATION_FIELDS"
Timothy Warrenc2b712e2012-02-17 15:58:08 -0500374 WHERE "RDB\$RELATION_NAME"='{$table}';
Timothy Warreneb6dcf02012-02-15 16:48:51 -0500375SQL;
Timothy Warren76e04352012-02-14 11:55:17 -0500376 }
377
378 // --------------------------------------------------------------------
379
380 /**
381 * Field data query
382 *
383 * Generates a platform-specific query so that the column data can be retrieved
384 *
Timothy Warren7221f942012-02-14 15:02:33 -0500385 * @param string the table name
386 * @return object
Timothy Warren76e04352012-02-14 11:55:17 -0500387 */
Timothy Warren95562142012-02-20 17:37:21 -0500388 protected function _field_data($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500389 {
Timothy Warren8be31a92012-02-15 11:48:57 -0500390 // Need to find a more efficient way to do this
Andrey Andreev00df2e32012-03-02 18:37:16 +0200391 // but Interbase/Firebird seems to lack the
Timothy Warren8be31a92012-02-15 11:48:57 -0500392 // limit clause
393 return "SELECT * FROM {$table}";
Timothy Warren76e04352012-02-14 11:55:17 -0500394 }
395
396 // --------------------------------------------------------------------
397
398 /**
Andrey Andreev00df2e32012-03-02 18:37:16 +0200399 * Error
Timothy Warren76e04352012-02-14 11:55:17 -0500400 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200401 * Returns an array containing code and message of the last
402 * database error that has occured.
Timothy Warren76e04352012-02-14 11:55:17 -0500403 *
Andrey Andreev00df2e32012-03-02 18:37:16 +0200404 * @return array
Timothy Warren76e04352012-02-14 11:55:17 -0500405 */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200406 public function error()
Timothy Warren76e04352012-02-14 11:55:17 -0500407 {
Andrey Andreev00df2e32012-03-02 18:37:16 +0200408 return array('code' => ibase_errcode(), 'message' => ibase_errmsg());
Timothy Warren76e04352012-02-14 11:55:17 -0500409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Escape the SQL Identifiers
415 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500416 * This public function escapes column and table names
Timothy Warren76e04352012-02-14 11:55:17 -0500417 *
Timothy Warren7221f942012-02-14 15:02:33 -0500418 * @param string
419 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500420 */
Timothy Warren95562142012-02-20 17:37:21 -0500421 protected function _escape_identifiers($item)
Timothy Warren76e04352012-02-14 11:55:17 -0500422 {
423 foreach ($this->_reserved_identifiers as $id)
424 {
425 if (strpos($item, '.'.$id) !== FALSE)
426 {
427 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
428
429 // remove duplicates if the user already included the escape
430 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
431 }
432 }
433
434 if (strpos($item, '.') !== FALSE)
435 {
436 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
437 }
438 else
439 {
440 $str = $this->_escape_char.$item.$this->_escape_char;
441 }
442
443 // remove duplicates if the user already included the escape
Timothy Warrend19e47a2012-02-14 23:40:40 -0500444 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Timothy Warren76e04352012-02-14 11:55:17 -0500445 }
446
447 // --------------------------------------------------------------------
448
449 /**
450 * From Tables
451 *
Timothy Warren4be822b2012-02-14 12:07:34 -0500452 * This public function implicitly groups FROM tables so there is no confusion
Timothy Warren76e04352012-02-14 11:55:17 -0500453 * about operator precedence in harmony with SQL standards
454 *
Timothy Warren7221f942012-02-14 15:02:33 -0500455 * @param type
456 * @return type
Timothy Warren76e04352012-02-14 11:55:17 -0500457 */
Timothy Warren95562142012-02-20 17:37:21 -0500458 protected function _from_tables($tables)
Timothy Warren76e04352012-02-14 11:55:17 -0500459 {
460 if ( ! is_array($tables))
461 {
462 $tables = array($tables);
463 }
464
Timothy Warren817af192012-02-16 08:28:00 -0500465 //Interbase/Firebird doesn't like grouped tables
Timothy Warrend19e47a2012-02-14 23:40:40 -0500466 return implode(', ', $tables);
Timothy Warren76e04352012-02-14 11:55:17 -0500467 }
468
469 // --------------------------------------------------------------------
470
471 /**
472 * Insert statement
473 *
474 * Generates a platform-specific insert string from the supplied data
475 *
Timothy Warren7221f942012-02-14 15:02:33 -0500476 * @param string the table name
477 * @param array the insert keys
478 * @param array the insert values
479 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500480 */
Timothy Warren95562142012-02-20 17:37:21 -0500481 protected function _insert($table, $keys, $values)
Timothy Warren76e04352012-02-14 11:55:17 -0500482 {
Timothy Warren817af192012-02-16 08:28:00 -0500483 return "INSERT INTO {$table} (".implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
Timothy Warren76e04352012-02-14 11:55:17 -0500484 }
485
486 // --------------------------------------------------------------------
487
488 /**
489 * Update statement
490 *
491 * Generates a platform-specific update string from the supplied data
492 *
Timothy Warren7221f942012-02-14 15:02:33 -0500493 * @param string the table name
494 * @param array the update data
495 * @param array the where clause
496 * @param array the orderby clause
497 * @param array the limit clause
498 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500499 */
Timothy Warren95562142012-02-20 17:37:21 -0500500 protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500501 {
502 foreach ($values as $key => $val)
503 {
504 $valstr[] = $key." = ".$val;
505 }
506
Timothy Warren8be31a92012-02-15 11:48:57 -0500507 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500508
509 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
510
Timothy Warren817af192012-02-16 08:28:00 -0500511 $sql = "UPDATE {$table} SET ".implode(', ', $valstr);
Timothy Warren76e04352012-02-14 11:55:17 -0500512
Timothy Warren817af192012-02-16 08:28:00 -0500513 $sql .= ($where != '' AND count($where) >=1) ? ' WHERE '.implode(' ', $where) : '';
Timothy Warren76e04352012-02-14 11:55:17 -0500514
Timothy Warren8be31a92012-02-15 11:48:57 -0500515 $sql .= $orderby;
Timothy Warren76e04352012-02-14 11:55:17 -0500516
517 return $sql;
518 }
519
520
521 // --------------------------------------------------------------------
522
523 /**
524 * Truncate statement
525 *
526 * Generates a platform-specific truncate string from the supplied data
527 * If the database does not support the truncate() command
Timothy Warren4be822b2012-02-14 12:07:34 -0500528 * This public function maps to "DELETE FROM table"
Timothy Warren76e04352012-02-14 11:55:17 -0500529 *
Timothy Warren7221f942012-02-14 15:02:33 -0500530 * @param string the table name
531 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500532 */
Timothy Warren95562142012-02-20 17:37:21 -0500533 protected function _truncate($table)
Timothy Warren76e04352012-02-14 11:55:17 -0500534 {
535 return $this->_delete($table);
536 }
537
538 // --------------------------------------------------------------------
539
540 /**
541 * Delete statement
542 *
543 * Generates a platform-specific delete string from the supplied data
544 *
Timothy Warren7221f942012-02-14 15:02:33 -0500545 * @param string the table name
546 * @param array the where clause
547 * @param string the limit clause
548 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500549 */
Timothy Warren95562142012-02-20 17:37:21 -0500550 protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Timothy Warren76e04352012-02-14 11:55:17 -0500551 {
552 $conditions = '';
553
554 if (count($where) > 0 OR count($like) > 0)
555 {
556 $conditions = "\nWHERE ";
557 $conditions .= implode("\n", $this->ar_where);
558
559 if (count($where) > 0 && count($like) > 0)
560 {
Timothy Warren817af192012-02-16 08:28:00 -0500561 $conditions .= ' AND ';
Timothy Warren76e04352012-02-14 11:55:17 -0500562 }
563 $conditions .= implode("\n", $like);
564 }
565
Timothy Warren3d985a12012-02-15 11:38:00 -0500566 //$limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Timothy Warren76e04352012-02-14 11:55:17 -0500567
Timothy Warren8be31a92012-02-15 11:48:57 -0500568 return "DELETE FROM {$table}{$conditions}";
Timothy Warren76e04352012-02-14 11:55:17 -0500569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Limit string
575 *
576 * Generates a platform-specific LIMIT clause
577 *
Timothy Warren7221f942012-02-14 15:02:33 -0500578 * @param string the sql query string
579 * @param integer the number of rows to limit the query to
580 * @param integer the offset value
581 * @return string
Timothy Warren76e04352012-02-14 11:55:17 -0500582 */
Timothy Warren95562142012-02-20 17:37:21 -0500583 protected function _limit($sql, $limit, $offset)
Timothy Warren76e04352012-02-14 11:55:17 -0500584 {
Timothy Warren56784f62012-02-29 11:58:20 -0500585 // Keep the current sql string safe for a moment
586 $orig_sql = $sql;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200587
Timothy Warren56784f62012-02-29 11:58:20 -0500588 // Limit clause depends on if Interbase or Firebird
Andrey Andreev08856b82012-03-03 03:19:28 +0200589 if (stripos($this->version(), 'firebird') !== FALSE)
Timothy Warren56784f62012-02-29 11:58:20 -0500590 {
591 $sql = 'FIRST '. (int) $limit;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200592
Timothy Warren56784f62012-02-29 11:58:20 -0500593 if ($offset > 0)
594 {
Timothy Warren9a960822012-02-29 22:53:35 -0500595 $sql .= ' SKIP '. (int) $offset;
Timothy Warren56784f62012-02-29 11:58:20 -0500596 }
597 }
598 else
599 {
600 $sql = 'ROWS ' . (int) $limit;
Andrey Andreev00df2e32012-03-02 18:37:16 +0200601
Timothy Warren56784f62012-02-29 11:58:20 -0500602 if ($offset > 0)
603 {
604 $sql = 'ROWS '. (int) $offset . ' TO ' . ($limit + $offset);
605 }
606 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200607
608 return preg_replace('`SELECT`i', "SELECT {$sql}", $orig_sql);
Timothy Warren76e04352012-02-14 11:55:17 -0500609 }
610
611 // --------------------------------------------------------------------
612
613 /**
614 * Close DB Connection
615 *
Timothy Warren7221f942012-02-14 15:02:33 -0500616 * @param resource
617 * @return void
Timothy Warren76e04352012-02-14 11:55:17 -0500618 */
Timothy Warren95562142012-02-20 17:37:21 -0500619 protected function _close($conn_id)
Timothy Warren76e04352012-02-14 11:55:17 -0500620 {
Timothy Warren7221f942012-02-14 15:02:33 -0500621 @ibase_close($conn_id);
Timothy Warren76e04352012-02-14 11:55:17 -0500622 }
Andrey Andreev00df2e32012-03-02 18:37:16 +0200623
Timothy Warren76e04352012-02-14 11:55:17 -0500624}
625
Timothy Warren76e04352012-02-14 11:55:17 -0500626/* End of file interbase_driver.php */
Andrey Andreev00df2e32012-03-02 18:37:16 +0200627/* Location: ./system/database/drivers/interbase/interbase_driver.php */