blob: 58da07818b9bdee5f3d59b035383c333638037a1 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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 * Execute the query
128 *
129 * @access private called by the base class
130 * @param string an SQL query
131 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200132 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 function _execute($sql)
134 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 return @odbc_exec($this->conn_id, $sql);
136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 // --------------------------------------------------------------------
139
140 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * Begin Transaction
142 *
143 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200144 * @return bool
145 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 function trans_begin($test_mode = FALSE)
147 {
148 if ( ! $this->trans_enabled)
149 {
150 return TRUE;
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 // When transactions are nested we only begin/commit/rollback the outermost ones
154 if ($this->_trans_depth > 0)
155 {
156 return TRUE;
157 }
158
159 // Reset the transaction failure flag.
160 // If the $test_mode flag is set to TRUE transactions will be rolled back
161 // even if the queries produce a successful result.
162 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
163
164 return odbc_autocommit($this->conn_id, FALSE);
165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Commit Transaction
171 *
172 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200173 * @return bool
174 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 function trans_commit()
176 {
177 if ( ! $this->trans_enabled)
178 {
179 return TRUE;
180 }
181
182 // When transactions are nested we only begin/commit/rollback the outermost ones
183 if ($this->_trans_depth > 0)
184 {
185 return TRUE;
186 }
187
188 $ret = odbc_commit($this->conn_id);
189 odbc_autocommit($this->conn_id, TRUE);
190 return $ret;
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Rollback Transaction
197 *
198 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200199 * @return bool
200 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 function trans_rollback()
202 {
203 if ( ! $this->trans_enabled)
204 {
205 return TRUE;
206 }
207
208 // When transactions are nested we only begin/commit/rollback the outermost ones
209 if ($this->_trans_depth > 0)
210 {
211 return TRUE;
212 }
213
214 $ret = odbc_rollback($this->conn_id);
215 odbc_autocommit($this->conn_id, TRUE);
216 return $ret;
217 }
218
219 // --------------------------------------------------------------------
220
221 /**
222 * Escape String
223 *
224 * @access public
225 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000226 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 * @return string
228 */
Derek Jonese4ed5832009-02-20 21:44:59 +0000229 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000231 if (is_array($str))
232 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500233 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200234 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000235 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200236 }
237
238 return $str;
239 }
240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 // ODBC doesn't require escaping
Greg Aker757dda62010-04-14 19:06:19 -0500242 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Jonese4ed5832009-02-20 21:44:59 +0000244 // escape LIKE condition wildcards
245 if ($like === TRUE)
246 {
247 $str = str_replace( array('%', '_', $this->_like_escape_chr),
248 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
249 $str);
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Jonese4ed5832009-02-20 21:44:59 +0000252 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 // --------------------------------------------------------------------
256
257 /**
258 * Affected Rows
259 *
260 * @access public
261 * @return integer
262 */
263 function affected_rows()
264 {
265 return @odbc_num_rows($this->conn_id);
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // --------------------------------------------------------------------
269
270 /**
271 * Insert ID
272 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200273 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 */
Andrey Andreev8af76662012-03-05 14:33:41 +0200275 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200277 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * "Count All" query
284 *
285 * Generates a platform-specific query string that counts all records in
286 * the specified database
287 *
288 * @access public
289 * @param string
290 * @return string
291 */
292 function count_all($table = '')
293 {
294 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000295 {
296 return 0;
297 }
298
Andrey Andreev032e7ea2012-03-06 19:48:35 +0200299 $query = $this->query($this->_count_string . $this->protect_identifiers('numrows') . " FROM " . $this->protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allarde37ab382009-02-03 16:13:57 +0000300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000302 {
303 return 0;
304 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000305
306 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500307 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000308 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
310
311 // --------------------------------------------------------------------
312
313 /**
314 * Show table query
315 *
316 * Generates a platform-specific query string so that the table names can be fetched
317 *
318 * @access private
319 * @param boolean
320 * @return string
321 */
322 function _list_tables($prefix_limit = FALSE)
323 {
324 $sql = "SHOW TABLES FROM `".$this->database."`";
325
326 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
327 {
Greg Aker0d424892010-01-26 02:14:44 +0000328 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 return FALSE; // not currently supported
330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 return $sql;
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // --------------------------------------------------------------------
336
337 /**
338 * Show column query
339 *
340 * Generates a platform-specific query string so that the column names can be fetched
341 *
342 * @access public
343 * @param string the table name
344 * @return string
345 */
346 function _list_columns($table = '')
347 {
348 return "SHOW COLUMNS FROM ".$table;
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * Field data query
355 *
356 * Generates a platform-specific query so that the column data can be retrieved
357 *
358 * @access public
359 * @param string the table name
360 * @return object
361 */
362 function _field_data($table)
363 {
364 return "SELECT TOP 1 FROM ".$table;
365 }
366
367 // --------------------------------------------------------------------
368
369 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200370 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200372 * Returns an array containing code and message of the last
373 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200375 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200377 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200379 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * Escape the SQL Identifiers
386 *
387 * This function escapes column and table names
388 *
389 * @access private
390 * @param string
391 * @return string
392 */
393 function _escape_identifiers($item)
394 {
395 if ($this->_escape_char == '')
396 {
397 return $item;
398 }
399
400 foreach ($this->_reserved_identifiers as $id)
401 {
402 if (strpos($item, '.'.$id) !== FALSE)
403 {
Barry Mienydd671972010-10-04 16:33:58 +0200404 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 // remove duplicates if the user already included the escape
407 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200408 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 if (strpos($item, '.') !== FALSE)
412 {
Barry Mienydd671972010-10-04 16:33:58 +0200413 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415 else
416 {
417 $str = $this->_escape_char.$item.$this->_escape_char;
418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // remove duplicates if the user already included the escape
421 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // --------------------------------------------------------------------
425
426 /**
427 * From Tables
428 *
429 * This function implicitly groups FROM tables so there is no confusion
430 * about operator precedence in harmony with SQL standards
431 *
432 * @access public
433 * @param type
434 * @return type
435 */
436 function _from_tables($tables)
437 {
438 if ( ! is_array($tables))
439 {
440 $tables = array($tables);
441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 return '('.implode(', ', $tables).')';
444 }
445
446 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 /**
449 * Insert statement
450 *
451 * Generates a platform-specific insert string from the supplied data
452 *
453 * @access public
454 * @param string the table name
455 * @param array the insert keys
456 * @param array the insert values
457 * @return string
458 */
459 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200460 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // --------------------------------------------------------------------
465
466 /**
467 * Update statement
468 *
469 * Generates a platform-specific update string from the supplied data
470 *
471 * @access public
472 * @param string the table name
473 * @param array the update data
474 * @param array the where clause
475 * @param array the orderby clause
476 * @param array the limit clause
477 * @return string
478 */
479 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
480 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500481 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 $valstr[] = $key." = ".$val;
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
491
492 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
493
494 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 return $sql;
497 }
498
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 // --------------------------------------------------------------------
501
502 /**
503 * Truncate statement
504 *
505 * Generates a platform-specific truncate string from the supplied data
506 * If the database does not support the truncate() command
507 * This function maps to "DELETE FROM table"
508 *
509 * @access public
510 * @param string the table name
511 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200512 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 function _truncate($table)
514 {
515 return $this->_delete($table);
516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 // --------------------------------------------------------------------
519
520 /**
521 * Delete statement
522 *
523 * Generates a platform-specific delete string from the supplied data
524 *
525 * @access public
526 * @param string the table name
527 * @param array the where clause
528 * @param string the limit clause
529 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200530 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
532 {
533 $conditions = '';
534
535 if (count($where) > 0 OR count($like) > 0)
536 {
537 $conditions = "\nWHERE ";
538 $conditions .= implode("\n", $this->ar_where);
539
540 if (count($where) > 0 && count($like) > 0)
541 {
542 $conditions .= " AND ";
543 }
544 $conditions .= implode("\n", $like);
545 }
546
547 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 return "DELETE FROM ".$table.$conditions.$limit;
550 }
551
552 // --------------------------------------------------------------------
553
554 /**
555 * Limit string
556 *
557 * Generates a platform-specific LIMIT clause
558 *
559 * @access public
560 * @param string the sql query string
561 * @param integer the number of rows to limit the query to
562 * @param integer the offset value
563 * @return string
564 */
565 function _limit($sql, $limit, $offset)
566 {
567 // Does ODBC doesn't use the LIMIT clause?
568 return $sql;
569 }
570
571 // --------------------------------------------------------------------
572
573 /**
574 * Close DB Connection
575 *
576 * @access public
577 * @param resource
578 * @return void
579 */
580 function _close($conn_id)
581 {
582 @odbc_close($conn_id);
583 }
584
Barry Mienydd671972010-10-04 16:33:58 +0200585
Derek Allard2067d1a2008-11-13 22:59:24 +0000586}
587
588
589
590/* End of file odbc_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200591/* Location: ./system/database/drivers/odbc/odbc_driver.php */