blob: abb660324d19e235d4c3f4f8cfe69223b1d2dbac [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * Version number query string
128 *
129 * @access public
130 * @return string
131 */
132 function _version()
133 {
134 return "SELECT version() AS ver";
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Execute the query
141 *
142 * @access private called by the base class
143 * @param string an SQL query
144 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200145 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 function _execute($sql)
147 {
148 $sql = $this->_prep_query($sql);
149 return @odbc_exec($this->conn_id, $sql);
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // --------------------------------------------------------------------
153
154 /**
155 * Prep the query
156 *
157 * If needed, each database adapter can prep the query string
158 *
159 * @access private called by execute()
160 * @param string an SQL query
161 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200162 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 function _prep_query($sql)
164 {
165 return $sql;
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Begin Transaction
172 *
173 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200174 * @return bool
175 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 function trans_begin($test_mode = FALSE)
177 {
178 if ( ! $this->trans_enabled)
179 {
180 return TRUE;
181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // When transactions are nested we only begin/commit/rollback the outermost ones
184 if ($this->_trans_depth > 0)
185 {
186 return TRUE;
187 }
188
189 // Reset the transaction failure flag.
190 // If the $test_mode flag is set to TRUE transactions will be rolled back
191 // even if the queries produce a successful result.
192 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
193
194 return odbc_autocommit($this->conn_id, FALSE);
195 }
196
197 // --------------------------------------------------------------------
198
199 /**
200 * Commit Transaction
201 *
202 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200203 * @return bool
204 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 function trans_commit()
206 {
207 if ( ! $this->trans_enabled)
208 {
209 return TRUE;
210 }
211
212 // When transactions are nested we only begin/commit/rollback the outermost ones
213 if ($this->_trans_depth > 0)
214 {
215 return TRUE;
216 }
217
218 $ret = odbc_commit($this->conn_id);
219 odbc_autocommit($this->conn_id, TRUE);
220 return $ret;
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Rollback Transaction
227 *
228 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200229 * @return bool
230 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 function trans_rollback()
232 {
233 if ( ! $this->trans_enabled)
234 {
235 return TRUE;
236 }
237
238 // When transactions are nested we only begin/commit/rollback the outermost ones
239 if ($this->_trans_depth > 0)
240 {
241 return TRUE;
242 }
243
244 $ret = odbc_rollback($this->conn_id);
245 odbc_autocommit($this->conn_id, TRUE);
246 return $ret;
247 }
248
249 // --------------------------------------------------------------------
250
251 /**
252 * Escape String
253 *
254 * @access public
255 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000256 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 * @return string
258 */
Derek Jonese4ed5832009-02-20 21:44:59 +0000259 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000261 if (is_array($str))
262 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500263 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200264 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000265 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200266 }
267
268 return $str;
269 }
270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // ODBC doesn't require escaping
Greg Aker757dda62010-04-14 19:06:19 -0500272 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Jonese4ed5832009-02-20 21:44:59 +0000274 // escape LIKE condition wildcards
275 if ($like === TRUE)
276 {
277 $str = str_replace( array('%', '_', $this->_like_escape_chr),
278 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
279 $str);
280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Jonese4ed5832009-02-20 21:44:59 +0000282 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // --------------------------------------------------------------------
286
287 /**
288 * Affected Rows
289 *
290 * @access public
291 * @return integer
292 */
293 function affected_rows()
294 {
295 return @odbc_num_rows($this->conn_id);
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // --------------------------------------------------------------------
299
300 /**
301 * Insert ID
302 *
303 * @access public
304 * @return integer
305 */
306 function insert_id()
307 {
308 return @odbc_insert_id($this->conn_id);
309 }
310
311 // --------------------------------------------------------------------
312
313 /**
314 * "Count All" query
315 *
316 * Generates a platform-specific query string that counts all records in
317 * the specified database
318 *
319 * @access public
320 * @param string
321 * @return string
322 */
323 function count_all($table = '')
324 {
325 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000326 {
327 return 0;
328 }
329
330 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000333 {
334 return 0;
335 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000336
337 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500338 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000339 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Show table query
346 *
347 * Generates a platform-specific query string so that the table names can be fetched
348 *
349 * @access private
350 * @param boolean
351 * @return string
352 */
353 function _list_tables($prefix_limit = FALSE)
354 {
355 $sql = "SHOW TABLES FROM `".$this->database."`";
356
357 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
358 {
Greg Aker0d424892010-01-26 02:14:44 +0000359 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 return FALSE; // not currently supported
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 return $sql;
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // --------------------------------------------------------------------
367
368 /**
369 * Show column query
370 *
371 * Generates a platform-specific query string so that the column names can be fetched
372 *
373 * @access public
374 * @param string the table name
375 * @return string
376 */
377 function _list_columns($table = '')
378 {
379 return "SHOW COLUMNS FROM ".$table;
380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Field data query
386 *
387 * Generates a platform-specific query so that the column data can be retrieved
388 *
389 * @access public
390 * @param string the table name
391 * @return object
392 */
393 function _field_data($table)
394 {
395 return "SELECT TOP 1 FROM ".$table;
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * The error message string
402 *
403 * @access private
404 * @return string
405 */
406 function _error_message()
407 {
408 return odbc_errormsg($this->conn_id);
409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // --------------------------------------------------------------------
412
413 /**
414 * The error message number
415 *
416 * @access private
417 * @return integer
418 */
419 function _error_number()
420 {
421 return odbc_error($this->conn_id);
422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Escape the SQL Identifiers
428 *
429 * This function escapes column and table names
430 *
431 * @access private
432 * @param string
433 * @return string
434 */
435 function _escape_identifiers($item)
436 {
437 if ($this->_escape_char == '')
438 {
439 return $item;
440 }
441
442 foreach ($this->_reserved_identifiers as $id)
443 {
444 if (strpos($item, '.'.$id) !== FALSE)
445 {
Barry Mienydd671972010-10-04 16:33:58 +0200446 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // remove duplicates if the user already included the escape
449 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200450 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 if (strpos($item, '.') !== FALSE)
454 {
Barry Mienydd671972010-10-04 16:33:58 +0200455 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
457 else
458 {
459 $str = $this->_escape_char.$item.$this->_escape_char;
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // remove duplicates if the user already included the escape
463 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 // --------------------------------------------------------------------
467
468 /**
469 * From Tables
470 *
471 * This function implicitly groups FROM tables so there is no confusion
472 * about operator precedence in harmony with SQL standards
473 *
474 * @access public
475 * @param type
476 * @return type
477 */
478 function _from_tables($tables)
479 {
480 if ( ! is_array($tables))
481 {
482 $tables = array($tables);
483 }
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 return '('.implode(', ', $tables).')';
486 }
487
488 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 /**
491 * Insert statement
492 *
493 * Generates a platform-specific insert string from the supplied data
494 *
495 * @access public
496 * @param string the table name
497 * @param array the insert keys
498 * @param array the insert values
499 * @return string
500 */
501 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200502 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 // --------------------------------------------------------------------
507
508 /**
509 * Update statement
510 *
511 * Generates a platform-specific update string from the supplied data
512 *
513 * @access public
514 * @param string the table name
515 * @param array the update data
516 * @param array the where clause
517 * @param array the orderby clause
518 * @param array the limit clause
519 * @return string
520 */
521 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
522 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500523 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 $valstr[] = $key." = ".$val;
526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
533
534 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
535
536 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200537
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 return $sql;
539 }
540
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 // --------------------------------------------------------------------
543
544 /**
545 * Truncate statement
546 *
547 * Generates a platform-specific truncate string from the supplied data
548 * If the database does not support the truncate() command
549 * This function maps to "DELETE FROM table"
550 *
551 * @access public
552 * @param string the table name
553 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200554 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 function _truncate($table)
556 {
557 return $this->_delete($table);
558 }
Barry Mienydd671972010-10-04 16:33:58 +0200559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 // --------------------------------------------------------------------
561
562 /**
563 * Delete statement
564 *
565 * Generates a platform-specific delete string from the supplied data
566 *
567 * @access public
568 * @param string the table name
569 * @param array the where clause
570 * @param string the limit clause
571 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200572 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
574 {
575 $conditions = '';
576
577 if (count($where) > 0 OR count($like) > 0)
578 {
579 $conditions = "\nWHERE ";
580 $conditions .= implode("\n", $this->ar_where);
581
582 if (count($where) > 0 && count($like) > 0)
583 {
584 $conditions .= " AND ";
585 }
586 $conditions .= implode("\n", $like);
587 }
588
589 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 return "DELETE FROM ".$table.$conditions.$limit;
592 }
593
594 // --------------------------------------------------------------------
595
596 /**
597 * Limit string
598 *
599 * Generates a platform-specific LIMIT clause
600 *
601 * @access public
602 * @param string the sql query string
603 * @param integer the number of rows to limit the query to
604 * @param integer the offset value
605 * @return string
606 */
607 function _limit($sql, $limit, $offset)
608 {
609 // Does ODBC doesn't use the LIMIT clause?
610 return $sql;
611 }
612
613 // --------------------------------------------------------------------
614
615 /**
616 * Close DB Connection
617 *
618 * @access public
619 * @param resource
620 * @return void
621 */
622 function _close($conn_id)
623 {
624 @odbc_close($conn_id);
625 }
626
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628}
629
630
631
632/* End of file odbc_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200633/* Location: ./system/database/drivers/odbc/odbc_driver.php */