blob: bc9cb8f088565ba6f6d98579ef8bf600859124d1 [file] [log] [blame]
Timothy Warren76e04352012-02-14 11:55:17 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
7 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
19 * @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
26 */
27
28// ------------------------------------------------------------------------
29
30
31
32/**
33 * Firebird/Interbase Database Adapter Class
34 *
35 * Note: _DB is an extender class that the app controller
36 * creates dynamically based on whether the active record
37 * class is being used or not.
38 *
39 * @package CodeIgniter
40 * @subpackage Drivers
41 * @category Database
42 * @author EllisLab Dev Team
43 * @link http://codeigniter.com/user_guide/database/
44 */
45class CI_DB_interbase_driver extends CI_DB {
46
47 public $dbdriver = 'interbase';
48
49 // The character used to escape with
50 public $_escape_char = '"';
51
52 // clause and character used for LIKE escape sequences
53 public $_like_escape_str = " ESCAPE '%s' ";
54 public $_like_escape_chr = '!';
55
56 /**
57 * The syntax to count rows is slightly different across different
58 * database engines, so this string appears in each driver and is
59 * used for the count_all() and count_all_results() functions.
60 */
61 public $_count_string = "SELECT COUNT(*) AS ";
62 public $_random_keyword = ' Random()'; // database specific random keyword
63
64 // Keeps track of the resource for the current transaction
65 protected $trans;
66
67 /**
68 * Non-persistent database connection
69 *
70 * @access private called by the base class
71 * @return resource
72 */
73 function db_connect()
74 {
75 if ( ! $conn_id = @ibase_connect($this->database, $this->username, $this->password, $this->charset))
76 {
77 log_message('error', $error);
78
79 if ($this->db_debug)
80 {
81 $this->display_error($error, '', TRUE);
82 }
83
84 return FALSE;
85 }
86
87 return $conn_id;
88 }
89
90 // --------------------------------------------------------------------
91
92 /**
93 * Persistent database connection
94 *
95 * @access private called by the base class
96 * @return resource
97 */
98 function db_pconnect()
99 {
100 if ( ! $conn_id = @ibase_pconnect($this->database, $this->username, $this->password, $this->charset))
101 {
102 log_message('error', $error);
103
104 if ($this->db_debug)
105 {
106 $this->display_error($error, '', TRUE);
107 }
108
109 return FALSE;
110 }
111
112 return $conn_id;
113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Reconnect
119 *
120 * Keep / reestablish the db connection if no queries have been
121 * sent for a length of time exceeding the server's idle timeout
122 *
123 * @access public
124 * @return void
125 */
126 function reconnect()
127 {
128 // not implemented in Interbase/Firebird
129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Select the database
135 *
136 * @access private called by the base class
137 * @return resource
138 */
139 function db_select()
140 {
141 // Connection selects the database
142 return TRUE;
143 }
144
145 // --------------------------------------------------------------------
146
147 /**
148 * Set client character set
149 *
150 * @access public
151 * @param string
152 * @param string
153 * @return resource
154 */
155 function db_set_charset($charset, $collation)
156 {
157 // @todo - add support if needed
158 return TRUE;
159 }
160
161 // --------------------------------------------------------------------
162
163 /**
164 * Version number query string
165 *
166 * @access public
167 * @return string
168 */
169 function _version()
170 {
171 //@todo - add support if needed
172 return TRUE;
173 }
174
175 // --------------------------------------------------------------------
176
177 /**
178 * Execute the query
179 *
180 * @access private called by the base class
181 * @param string an SQL query
182 * @return resource
183 */
184 function _execute($sql)
185 {
186 $sql = $this->_prep_query($sql);
187 return @ibase_query($this->conn_id, $sql);
188 }
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Prep the query
194 *
195 * If needed, each database adapter can prep the query string
196 *
197 * @access private called by execute()
198 * @param string an SQL query
199 * @return string
200 */
201 function _prep_query($sql)
202 {
203 return $sql;
204 }
205
206 // --------------------------------------------------------------------
207
208 /**
209 * Begin Transaction
210 *
211 * @access public
212 * @return bool
213 */
214 function trans_begin($test_mode = FALSE)
215 {
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
227 // Reset the transaction failure flag.
228 // If the $test_mode flag is set to TRUE transactions will be rolled back
229 // even if the queries produce a successful result.
230 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
231
232 $this->trans = @ibase_trans($this->conn_id);
233
234 return TRUE;
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Commit Transaction
241 *
242 * @access public
243 * @return bool
244 */
245 function trans_commit()
246 {
247 if ( ! $this->trans_enabled)
248 {
249 return TRUE;
250 }
251
252 // When transactions are nested we only begin/commit/rollback the outermost ones
253 if ($this->_trans_depth > 0)
254 {
255 return TRUE;
256 }
257
258 @ibase_commit($this->trans);
259
260 return TRUE;
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Rollback Transaction
267 *
268 * @access public
269 * @return bool
270 */
271 function trans_rollback()
272 {
273 if ( ! $this->trans_enabled)
274 {
275 return TRUE;
276 }
277
278 // When transactions are nested we only begin/commit/rollback the outermost ones
279 if ($this->_trans_depth > 0)
280 {
281 return TRUE;
282 }
283
284 @ibase_rollback($this->trans);
285
286 return TRUE;
287 }
288
289 // --------------------------------------------------------------------
290
291 /**
292 * Escape String
293 *
294 * @access public
295 * @param string
296 * @param bool whether or not the string will be used in a LIKE condition
297 * @return string
298 */
299 function escape_str($str, $like = FALSE)
300 {
301 if (is_array($str))
302 {
303 foreach ($str as $key => $val)
304 {
305 $str[$key] = $this->escape_str($val, $like);
306 }
307
308 return $str;
309 }
310
311 // escape LIKE condition wildcards
312 if ($like === TRUE)
313 {
314 $str = str_replace( array('%', '_', $this->_like_escape_chr),
315 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
316 $str);
317 }
318
319 return $str;
320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * Affected Rows
326 *
327 * @access public
328 * @return integer
329 */
330 function affected_rows()
331 {
332 return @ibase_affected_rows($this->conn_id);
333 }
334
335 // --------------------------------------------------------------------
336
337 /**
338 * Insert ID
339 *
340 * @access public
341 * @return integer
342 */
343 function insert_id()
344 {
345 //@todo Implement manually
346 return 0;
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * "Count All" query
353 *
354 * Generates a platform-specific query string that counts all records in
355 * the specified database
356 *
357 * @access public
358 * @param string
359 * @return string
360 */
361 function count_all($table = '')
362 {
363 if ($table == '')
364 {
365 return 0;
366 }
367
368 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
369
370 if ($query->num_rows() == 0)
371 {
372 return 0;
373 }
374
375 $row = $query->row();
376 $this->_reset_select();
377 return (int) $row->numrows;
378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * List table query
384 *
385 * Generates a platform-specific query string so that the table names can be fetched
386 *
387 * @access private
388 * @param boolean
389 * @return string
390 */
391 function _list_tables($prefix_limit = FALSE)
392 {
393 $sql = <<<SQL
394 SELECT "RDB\$RELATION_NAME" FROM "RDB\$RELATIONS"
395 WHERE "RDB\$RELATION_NAME" NOT LIKE 'RDB$%'
396 AND "RDB\$RELATION_NAME" NOT LIKE 'MON$%'
397SQL;
398
399 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
400 {
401 $sql .= " AND 'name' LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
402 }
403 return $sql;
404 }
405
406 // --------------------------------------------------------------------
407
408 /**
409 * Show column query
410 *
411 * Generates a platform-specific query string so that the column names can be fetched
412 *
413 * @access public
414 * @param string the table name
415 * @return string
416 */
417 function _list_columns($table = '')
418 {
419 // Not supported
420 return FALSE;
421 }
422
423 // --------------------------------------------------------------------
424
425 /**
426 * Field data query
427 *
428 * Generates a platform-specific query so that the column data can be retrieved
429 *
430 * @access public
431 * @param string the table name
432 * @return object
433 */
434 function _field_data($table)
435 {
436 return "SELECT * FROM ".$table." LIMIT 1";
437 }
438
439 // --------------------------------------------------------------------
440
441 /**
442 * The error message string
443 *
444 * @access private
445 * @return string
446 */
447 function _error_message()
448 {
449 return ibase_errmsg();
450 }
451
452 // --------------------------------------------------------------------
453
454 /**
455 * The error message number
456 *
457 * @access private
458 * @return integer
459 */
460 function _error_number()
461 {
462 return ibase_errcode();
463 }
464
465 // --------------------------------------------------------------------
466
467 /**
468 * Escape the SQL Identifiers
469 *
470 * This function escapes column and table names
471 *
472 * @access private
473 * @param string
474 * @return string
475 */
476 function _escape_identifiers($item)
477 {
478 foreach ($this->_reserved_identifiers as $id)
479 {
480 if (strpos($item, '.'.$id) !== FALSE)
481 {
482 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
483
484 // remove duplicates if the user already included the escape
485 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
486 }
487 }
488
489 if (strpos($item, '.') !== FALSE)
490 {
491 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
492 }
493 else
494 {
495 $str = $this->_escape_char.$item.$this->_escape_char;
496 }
497
498 // remove duplicates if the user already included the escape
499 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
505 * From Tables
506 *
507 * This function implicitly groups FROM tables so there is no confusion
508 * about operator precedence in harmony with SQL standards
509 *
510 * @access public
511 * @param type
512 * @return type
513 */
514 function _from_tables($tables)
515 {
516 if ( ! is_array($tables))
517 {
518 $tables = array($tables);
519 }
520
521 return '('.implode(', ', $tables).')';
522 }
523
524 // --------------------------------------------------------------------
525
526 /**
527 * Insert statement
528 *
529 * Generates a platform-specific insert string from the supplied data
530 *
531 * @access public
532 * @param string the table name
533 * @param array the insert keys
534 * @param array the insert values
535 * @return string
536 */
537 function _insert($table, $keys, $values)
538 {
539 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
540 }
541
542 // --------------------------------------------------------------------
543
544 /**
545 * Update statement
546 *
547 * Generates a platform-specific update string from the supplied data
548 *
549 * @access public
550 * @param string the table name
551 * @param array the update data
552 * @param array the where clause
553 * @param array the orderby clause
554 * @param array the limit clause
555 * @return string
556 */
557 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
558 {
559 foreach ($values as $key => $val)
560 {
561 $valstr[] = $key." = ".$val;
562 }
563
564 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
565
566 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
567
568 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
569
570 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
571
572 $sql .= $orderby.$limit;
573
574 return $sql;
575 }
576
577
578 // --------------------------------------------------------------------
579
580 /**
581 * Truncate statement
582 *
583 * Generates a platform-specific truncate string from the supplied data
584 * If the database does not support the truncate() command
585 * This function maps to "DELETE FROM table"
586 *
587 * @access public
588 * @param string the table name
589 * @return string
590 */
591 function _truncate($table)
592 {
593 return $this->_delete($table);
594 }
595
596 // --------------------------------------------------------------------
597
598 /**
599 * Delete statement
600 *
601 * Generates a platform-specific delete string from the supplied data
602 *
603 * @access public
604 * @param string the table name
605 * @param array the where clause
606 * @param string the limit clause
607 * @return string
608 */
609 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
610 {
611 $conditions = '';
612
613 if (count($where) > 0 OR count($like) > 0)
614 {
615 $conditions = "\nWHERE ";
616 $conditions .= implode("\n", $this->ar_where);
617
618 if (count($where) > 0 && count($like) > 0)
619 {
620 $conditions .= " AND ";
621 }
622 $conditions .= implode("\n", $like);
623 }
624
625 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
626
627 return "DELETE FROM ".$table.$conditions.$limit;
628 }
629
630 // --------------------------------------------------------------------
631
632 /**
633 * Limit string
634 *
635 * Generates a platform-specific LIMIT clause
636 *
637 * @access public
638 * @param string the sql query string
639 * @param integer the number of rows to limit the query to
640 * @param integer the offset value
641 * @return string
642 */
643 function _limit($sql, $limit, $offset)
644 {
645 if ($offset == 0)
646 {
647 $offset = '';
648 }
649 else
650 {
651 $offset .= ", ";
652 }
653
654 return $sql."LIMIT ".$offset.$limit;
655 }
656
657 // --------------------------------------------------------------------
658
659 /**
660 * Close DB Connection
661 *
662 * @access public
663 * @param resource
664 * @return void
665 */
666 function _close($conn_id)
667 {
668 @ibase_close($conn_id);
669 }
670
671
672}
673
674
675/* End of file interbase_driver.php */
676/* Location: ./system/database/drivers/interbase/interbase_driver.php */