blob: 5a93f7cad70546f5083e60dc0e362b2b5d0faff1 [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 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200401 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200403 * Returns an array containing code and message of the last
404 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200406 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200408 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200410 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
412
413 // --------------------------------------------------------------------
414
415 /**
416 * Escape the SQL Identifiers
417 *
418 * This function escapes column and table names
419 *
420 * @access private
421 * @param string
422 * @return string
423 */
424 function _escape_identifiers($item)
425 {
426 if ($this->_escape_char == '')
427 {
428 return $item;
429 }
430
431 foreach ($this->_reserved_identifiers as $id)
432 {
433 if (strpos($item, '.'.$id) !== FALSE)
434 {
Barry Mienydd671972010-10-04 16:33:58 +0200435 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // remove duplicates if the user already included the escape
438 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200439 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 if (strpos($item, '.') !== FALSE)
443 {
Barry Mienydd671972010-10-04 16:33:58 +0200444 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
446 else
447 {
448 $str = $this->_escape_char.$item.$this->_escape_char;
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 // remove duplicates if the user already included the escape
452 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // --------------------------------------------------------------------
456
457 /**
458 * From Tables
459 *
460 * This function implicitly groups FROM tables so there is no confusion
461 * about operator precedence in harmony with SQL standards
462 *
463 * @access public
464 * @param type
465 * @return type
466 */
467 function _from_tables($tables)
468 {
469 if ( ! is_array($tables))
470 {
471 $tables = array($tables);
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 return '('.implode(', ', $tables).')';
475 }
476
477 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 /**
480 * Insert statement
481 *
482 * Generates a platform-specific insert string from the supplied data
483 *
484 * @access public
485 * @param string the table name
486 * @param array the insert keys
487 * @param array the insert values
488 * @return string
489 */
490 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200491 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 // --------------------------------------------------------------------
496
497 /**
498 * Update statement
499 *
500 * Generates a platform-specific update string from the supplied data
501 *
502 * @access public
503 * @param string the table name
504 * @param array the update data
505 * @param array the where clause
506 * @param array the orderby clause
507 * @param array the limit clause
508 * @return string
509 */
510 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
511 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500512 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 {
514 $valstr[] = $key." = ".$val;
515 }
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
522
523 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
524
525 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 return $sql;
528 }
529
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 // --------------------------------------------------------------------
532
533 /**
534 * Truncate statement
535 *
536 * Generates a platform-specific truncate string from the supplied data
537 * If the database does not support the truncate() command
538 * This function maps to "DELETE FROM table"
539 *
540 * @access public
541 * @param string the table name
542 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200543 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 function _truncate($table)
545 {
546 return $this->_delete($table);
547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 // --------------------------------------------------------------------
550
551 /**
552 * Delete statement
553 *
554 * Generates a platform-specific delete string from the supplied data
555 *
556 * @access public
557 * @param string the table name
558 * @param array the where clause
559 * @param string the limit clause
560 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200561 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
563 {
564 $conditions = '';
565
566 if (count($where) > 0 OR count($like) > 0)
567 {
568 $conditions = "\nWHERE ";
569 $conditions .= implode("\n", $this->ar_where);
570
571 if (count($where) > 0 && count($like) > 0)
572 {
573 $conditions .= " AND ";
574 }
575 $conditions .= implode("\n", $like);
576 }
577
578 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 return "DELETE FROM ".$table.$conditions.$limit;
581 }
582
583 // --------------------------------------------------------------------
584
585 /**
586 * Limit string
587 *
588 * Generates a platform-specific LIMIT clause
589 *
590 * @access public
591 * @param string the sql query string
592 * @param integer the number of rows to limit the query to
593 * @param integer the offset value
594 * @return string
595 */
596 function _limit($sql, $limit, $offset)
597 {
598 // Does ODBC doesn't use the LIMIT clause?
599 return $sql;
600 }
601
602 // --------------------------------------------------------------------
603
604 /**
605 * Close DB Connection
606 *
607 * @access public
608 * @param resource
609 * @return void
610 */
611 function _close($conn_id)
612 {
613 @odbc_close($conn_id);
614 }
615
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617}
618
619
620
621/* End of file odbc_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200622/* Location: ./system/database/drivers/odbc/odbc_driver.php */