blob: 0e9d46d3ff0ea3d3bc683c2d88ad629ccecc18f2 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * 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 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * ODBC 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 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_odbc_driver extends CI_DB {
44
45 var $dbdriver = 'odbc';
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 // the character used to excape - not necessary for ODBC
48 var $_escape_char = '';
Barry Mienydd671972010-10-04 16:33:58 +020049
Derek Jonese4ed5832009-02-20 21:44:59 +000050 // clause and character used for LIKE escape sequences
51 var $_like_escape_str = " {escape '%s'} ";
52 var $_like_escape_chr = '!';
Barry Mienydd671972010-10-04 16:33:58 +020053
Derek Allard2067d1a2008-11-13 22:59:24 +000054 /**
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 */
59 var $_count_string = "SELECT COUNT(*) AS ";
60 var $_random_keyword;
61
62
Timothy Warrena2097a02011-10-10 10:10:46 -040063 function __construct($params)
Derek Allard2067d1a2008-11-13 22:59:24 +000064 {
Timothy Warrena2097a02011-10-10 10:10:46 -040065 parent::__construct($params);
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
68 }
69
70 /**
71 * Non-persistent database connection
72 *
73 * @access private called by the base class
74 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020075 */
Derek Allard2067d1a2008-11-13 22:59:24 +000076 function db_connect()
77 {
78 return @odbc_connect($this->hostname, $this->username, $this->password);
79 }
Barry Mienydd671972010-10-04 16:33:58 +020080
Derek Allard2067d1a2008-11-13 22:59:24 +000081 // --------------------------------------------------------------------
82
83 /**
84 * Persistent database connection
85 *
86 * @access private called by the base class
87 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020088 */
Derek Allard2067d1a2008-11-13 22:59:24 +000089 function db_pconnect()
90 {
91 return @odbc_pconnect($this->hostname, $this->username, $this->password);
92 }
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // --------------------------------------------------------------------
95
96 /**
Derek Jones87cbafc2009-02-27 16:29:59 +000097 * Reconnect
98 *
99 * Keep / reestablish the db connection if no queries have been
100 * sent for a length of time exceeding the server's idle timeout
101 *
102 * @access public
103 * @return void
104 */
105 function reconnect()
106 {
107 // not implemented in odbc
108 }
109
110 // --------------------------------------------------------------------
111
112 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * Select the database
114 *
115 * @access private called by the base class
116 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200117 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 function db_select()
119 {
120 // Not needed for ODBC
121 return TRUE;
122 }
123
124 // --------------------------------------------------------------------
125
126 /**
127 * Set client character set
128 *
129 * @access public
130 * @param string
131 * @param string
132 * @return resource
133 */
134 function db_set_charset($charset, $collation)
135 {
136 // @todo - add support if needed
137 return TRUE;
138 }
139
140 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 /**
143 * Version number query string
144 *
145 * @access public
146 * @return string
147 */
148 function _version()
149 {
150 return "SELECT version() AS ver";
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Execute the query
157 *
158 * @access private called by the base class
159 * @param string an SQL query
160 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200161 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 function _execute($sql)
163 {
164 $sql = $this->_prep_query($sql);
165 return @odbc_exec($this->conn_id, $sql);
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // --------------------------------------------------------------------
169
170 /**
171 * Prep the query
172 *
173 * If needed, each database adapter can prep the query string
174 *
175 * @access private called by execute()
176 * @param string an SQL query
177 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200178 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 function _prep_query($sql)
180 {
181 return $sql;
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Begin Transaction
188 *
189 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200190 * @return bool
191 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 function trans_begin($test_mode = FALSE)
193 {
194 if ( ! $this->trans_enabled)
195 {
196 return TRUE;
197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 // When transactions are nested we only begin/commit/rollback the outermost ones
200 if ($this->_trans_depth > 0)
201 {
202 return TRUE;
203 }
204
205 // Reset the transaction failure flag.
206 // If the $test_mode flag is set to TRUE transactions will be rolled back
207 // even if the queries produce a successful result.
208 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
209
210 return odbc_autocommit($this->conn_id, FALSE);
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Commit Transaction
217 *
218 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200219 * @return bool
220 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 function trans_commit()
222 {
223 if ( ! $this->trans_enabled)
224 {
225 return TRUE;
226 }
227
228 // When transactions are nested we only begin/commit/rollback the outermost ones
229 if ($this->_trans_depth > 0)
230 {
231 return TRUE;
232 }
233
234 $ret = odbc_commit($this->conn_id);
235 odbc_autocommit($this->conn_id, TRUE);
236 return $ret;
237 }
238
239 // --------------------------------------------------------------------
240
241 /**
242 * Rollback Transaction
243 *
244 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200245 * @return bool
246 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 function trans_rollback()
248 {
249 if ( ! $this->trans_enabled)
250 {
251 return TRUE;
252 }
253
254 // When transactions are nested we only begin/commit/rollback the outermost ones
255 if ($this->_trans_depth > 0)
256 {
257 return TRUE;
258 }
259
260 $ret = odbc_rollback($this->conn_id);
261 odbc_autocommit($this->conn_id, TRUE);
262 return $ret;
263 }
264
265 // --------------------------------------------------------------------
266
267 /**
268 * Escape String
269 *
270 * @access public
271 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000272 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 * @return string
274 */
Derek Jonese4ed5832009-02-20 21:44:59 +0000275 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000277 if (is_array($str))
278 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500279 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200280 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000281 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200282 }
283
284 return $str;
285 }
286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 // ODBC doesn't require escaping
Greg Aker757dda62010-04-14 19:06:19 -0500288 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Jonese4ed5832009-02-20 21:44:59 +0000290 // escape LIKE condition wildcards
291 if ($like === TRUE)
292 {
293 $str = str_replace( array('%', '_', $this->_like_escape_chr),
294 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
295 $str);
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Jonese4ed5832009-02-20 21:44:59 +0000298 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 // --------------------------------------------------------------------
302
303 /**
304 * Affected Rows
305 *
306 * @access public
307 * @return integer
308 */
309 function affected_rows()
310 {
311 return @odbc_num_rows($this->conn_id);
312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // --------------------------------------------------------------------
315
316 /**
317 * Insert ID
318 *
319 * @access public
320 * @return integer
321 */
322 function insert_id()
323 {
324 return @odbc_insert_id($this->conn_id);
325 }
326
327 // --------------------------------------------------------------------
328
329 /**
330 * "Count All" query
331 *
332 * Generates a platform-specific query string that counts all records in
333 * the specified database
334 *
335 * @access public
336 * @param string
337 * @return string
338 */
339 function count_all($table = '')
340 {
341 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000342 {
343 return 0;
344 }
345
346 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000349 {
350 return 0;
351 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
353 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500354 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000355 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * Show table query
362 *
363 * Generates a platform-specific query string so that the table names can be fetched
364 *
365 * @access private
366 * @param boolean
367 * @return string
368 */
369 function _list_tables($prefix_limit = FALSE)
370 {
371 $sql = "SHOW TABLES FROM `".$this->database."`";
372
373 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
374 {
Greg Aker0d424892010-01-26 02:14:44 +0000375 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 return FALSE; // not currently supported
377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 return $sql;
380 }
Barry Mienydd671972010-10-04 16:33:58 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 // --------------------------------------------------------------------
383
384 /**
385 * Show column query
386 *
387 * Generates a platform-specific query string so that the column names can be fetched
388 *
389 * @access public
390 * @param string the table name
391 * @return string
392 */
393 function _list_columns($table = '')
394 {
395 return "SHOW COLUMNS FROM ".$table;
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Field data query
402 *
403 * Generates a platform-specific query so that the column data can be retrieved
404 *
405 * @access public
406 * @param string the table name
407 * @return object
408 */
409 function _field_data($table)
410 {
411 return "SELECT TOP 1 FROM ".$table;
412 }
413
414 // --------------------------------------------------------------------
415
416 /**
417 * The error message string
418 *
419 * @access private
420 * @return string
421 */
422 function _error_message()
423 {
424 return odbc_errormsg($this->conn_id);
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 // --------------------------------------------------------------------
428
429 /**
430 * The error message number
431 *
432 * @access private
433 * @return integer
434 */
435 function _error_number()
436 {
437 return odbc_error($this->conn_id);
438 }
439
440 // --------------------------------------------------------------------
441
442 /**
443 * Escape the SQL Identifiers
444 *
445 * This function escapes column and table names
446 *
447 * @access private
448 * @param string
449 * @return string
450 */
451 function _escape_identifiers($item)
452 {
453 if ($this->_escape_char == '')
454 {
455 return $item;
456 }
457
458 foreach ($this->_reserved_identifiers as $id)
459 {
460 if (strpos($item, '.'.$id) !== FALSE)
461 {
Barry Mienydd671972010-10-04 16:33:58 +0200462 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // remove duplicates if the user already included the escape
465 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200466 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 if (strpos($item, '.') !== FALSE)
470 {
Barry Mienydd671972010-10-04 16:33:58 +0200471 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473 else
474 {
475 $str = $this->_escape_char.$item.$this->_escape_char;
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // remove duplicates if the user already included the escape
479 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * From Tables
486 *
487 * This function implicitly groups FROM tables so there is no confusion
488 * about operator precedence in harmony with SQL standards
489 *
490 * @access public
491 * @param type
492 * @return type
493 */
494 function _from_tables($tables)
495 {
496 if ( ! is_array($tables))
497 {
498 $tables = array($tables);
499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 return '('.implode(', ', $tables).')';
502 }
503
504 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 /**
507 * Insert statement
508 *
509 * Generates a platform-specific insert string from the supplied data
510 *
511 * @access public
512 * @param string the table name
513 * @param array the insert keys
514 * @param array the insert values
515 * @return string
516 */
517 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200518 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // --------------------------------------------------------------------
523
524 /**
525 * Update statement
526 *
527 * Generates a platform-specific update string from the supplied data
528 *
529 * @access public
530 * @param string the table name
531 * @param array the update data
532 * @param array the where clause
533 * @param array the orderby clause
534 * @param array the limit clause
535 * @return string
536 */
537 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
538 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500539 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 {
541 $valstr[] = $key." = ".$val;
542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
549
550 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
551
552 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 return $sql;
555 }
556
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 // --------------------------------------------------------------------
559
560 /**
561 * Truncate statement
562 *
563 * Generates a platform-specific truncate string from the supplied data
564 * If the database does not support the truncate() command
565 * This function maps to "DELETE FROM table"
566 *
567 * @access public
568 * @param string the table name
569 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200570 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 function _truncate($table)
572 {
573 return $this->_delete($table);
574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 // --------------------------------------------------------------------
577
578 /**
579 * Delete statement
580 *
581 * Generates a platform-specific delete string from the supplied data
582 *
583 * @access public
584 * @param string the table name
585 * @param array the where clause
586 * @param string the limit clause
587 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200588 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
590 {
591 $conditions = '';
592
593 if (count($where) > 0 OR count($like) > 0)
594 {
595 $conditions = "\nWHERE ";
596 $conditions .= implode("\n", $this->ar_where);
597
598 if (count($where) > 0 && count($like) > 0)
599 {
600 $conditions .= " AND ";
601 }
602 $conditions .= implode("\n", $like);
603 }
604
605 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200606
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 return "DELETE FROM ".$table.$conditions.$limit;
608 }
609
610 // --------------------------------------------------------------------
611
612 /**
613 * Limit string
614 *
615 * Generates a platform-specific LIMIT clause
616 *
617 * @access public
618 * @param string the sql query string
619 * @param integer the number of rows to limit the query to
620 * @param integer the offset value
621 * @return string
622 */
623 function _limit($sql, $limit, $offset)
624 {
625 // Does ODBC doesn't use the LIMIT clause?
626 return $sql;
627 }
628
629 // --------------------------------------------------------------------
630
631 /**
632 * Close DB Connection
633 *
634 * @access public
635 * @param resource
636 * @return void
637 */
638 function _close($conn_id)
639 {
640 @odbc_close($conn_id);
641 }
642
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644}
645
646
647
648/* End of file odbc_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000649/* Location: ./system/database/drivers/odbc/odbc_driver.php */