blob: a6e08cf2fff25d89779524e84d628595cdf94e67 [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 * 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 {
135 $sql = $this->_prep_query($sql);
136 return @odbc_exec($this->conn_id, $sql);
137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 // --------------------------------------------------------------------
140
141 /**
142 * Prep the query
143 *
144 * If needed, each database adapter can prep the query string
145 *
146 * @access private called by execute()
147 * @param string an SQL query
148 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200149 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 function _prep_query($sql)
151 {
152 return $sql;
153 }
154
155 // --------------------------------------------------------------------
156
157 /**
158 * Begin Transaction
159 *
160 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200161 * @return bool
162 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 function trans_begin($test_mode = FALSE)
164 {
165 if ( ! $this->trans_enabled)
166 {
167 return TRUE;
168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 // When transactions are nested we only begin/commit/rollback the outermost ones
171 if ($this->_trans_depth > 0)
172 {
173 return TRUE;
174 }
175
176 // Reset the transaction failure flag.
177 // If the $test_mode flag is set to TRUE transactions will be rolled back
178 // even if the queries produce a successful result.
179 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
180
181 return odbc_autocommit($this->conn_id, FALSE);
182 }
183
184 // --------------------------------------------------------------------
185
186 /**
187 * Commit 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_commit()
193 {
194 if ( ! $this->trans_enabled)
195 {
196 return TRUE;
197 }
198
199 // 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 $ret = odbc_commit($this->conn_id);
206 odbc_autocommit($this->conn_id, TRUE);
207 return $ret;
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Rollback Transaction
214 *
215 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200216 * @return bool
217 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 function trans_rollback()
219 {
220 if ( ! $this->trans_enabled)
221 {
222 return TRUE;
223 }
224
225 // When transactions are nested we only begin/commit/rollback the outermost ones
226 if ($this->_trans_depth > 0)
227 {
228 return TRUE;
229 }
230
231 $ret = odbc_rollback($this->conn_id);
232 odbc_autocommit($this->conn_id, TRUE);
233 return $ret;
234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Escape String
240 *
241 * @access public
242 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000243 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 * @return string
245 */
Derek Jonese4ed5832009-02-20 21:44:59 +0000246 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000248 if (is_array($str))
249 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500250 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200251 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000252 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200253 }
254
255 return $str;
256 }
257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // ODBC doesn't require escaping
Greg Aker757dda62010-04-14 19:06:19 -0500259 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Jonese4ed5832009-02-20 21:44:59 +0000261 // escape LIKE condition wildcards
262 if ($like === TRUE)
263 {
264 $str = str_replace( array('%', '_', $this->_like_escape_chr),
265 array($this->_like_escape_chr.'%', $this->_like_escape_chr.'_', $this->_like_escape_chr.$this->_like_escape_chr),
266 $str);
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Jonese4ed5832009-02-20 21:44:59 +0000269 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // --------------------------------------------------------------------
273
274 /**
275 * Affected Rows
276 *
277 * @access public
278 * @return integer
279 */
280 function affected_rows()
281 {
282 return @odbc_num_rows($this->conn_id);
283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // --------------------------------------------------------------------
286
287 /**
288 * Insert ID
289 *
Andrey Andreev8af76662012-03-05 14:33:41 +0200290 * @return bool
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 */
Andrey Andreev8af76662012-03-05 14:33:41 +0200292 public function insert_id()
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
Andrey Andreev8af76662012-03-05 14:33:41 +0200294 return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296
297 // --------------------------------------------------------------------
298
299 /**
300 * "Count All" query
301 *
302 * Generates a platform-specific query string that counts all records in
303 * the specified database
304 *
305 * @access public
306 * @param string
307 * @return string
308 */
309 function count_all($table = '')
310 {
311 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000312 {
313 return 0;
314 }
315
316 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000319 {
320 return 0;
321 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000322
323 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500324 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000325 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327
328 // --------------------------------------------------------------------
329
330 /**
331 * Show table query
332 *
333 * Generates a platform-specific query string so that the table names can be fetched
334 *
335 * @access private
336 * @param boolean
337 * @return string
338 */
339 function _list_tables($prefix_limit = FALSE)
340 {
341 $sql = "SHOW TABLES FROM `".$this->database."`";
342
343 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
344 {
Greg Aker0d424892010-01-26 02:14:44 +0000345 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 return FALSE; // not currently supported
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 return $sql;
350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // --------------------------------------------------------------------
353
354 /**
355 * Show column query
356 *
357 * Generates a platform-specific query string so that the column names can be fetched
358 *
359 * @access public
360 * @param string the table name
361 * @return string
362 */
363 function _list_columns($table = '')
364 {
365 return "SHOW COLUMNS FROM ".$table;
366 }
367
368 // --------------------------------------------------------------------
369
370 /**
371 * Field data query
372 *
373 * Generates a platform-specific query so that the column data can be retrieved
374 *
375 * @access public
376 * @param string the table name
377 * @return object
378 */
379 function _field_data($table)
380 {
381 return "SELECT TOP 1 FROM ".$table;
382 }
383
384 // --------------------------------------------------------------------
385
386 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200387 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200389 * Returns an array containing code and message of the last
390 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200392 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200394 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200396 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Escape the SQL Identifiers
403 *
404 * This function escapes column and table names
405 *
406 * @access private
407 * @param string
408 * @return string
409 */
410 function _escape_identifiers($item)
411 {
412 if ($this->_escape_char == '')
413 {
414 return $item;
415 }
416
417 foreach ($this->_reserved_identifiers as $id)
418 {
419 if (strpos($item, '.'.$id) !== FALSE)
420 {
Barry Mienydd671972010-10-04 16:33:58 +0200421 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 // remove duplicates if the user already included the escape
424 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200425 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 if (strpos($item, '.') !== FALSE)
429 {
Barry Mienydd671972010-10-04 16:33:58 +0200430 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432 else
433 {
434 $str = $this->_escape_char.$item.$this->_escape_char;
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
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);
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 // --------------------------------------------------------------------
442
443 /**
444 * From Tables
445 *
446 * This function implicitly groups FROM tables so there is no confusion
447 * about operator precedence in harmony with SQL standards
448 *
449 * @access public
450 * @param type
451 * @return type
452 */
453 function _from_tables($tables)
454 {
455 if ( ! is_array($tables))
456 {
457 $tables = array($tables);
458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 return '('.implode(', ', $tables).')';
461 }
462
463 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 /**
466 * Insert statement
467 *
468 * Generates a platform-specific insert string from the supplied data
469 *
470 * @access public
471 * @param string the table name
472 * @param array the insert keys
473 * @param array the insert values
474 * @return string
475 */
476 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200477 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 // --------------------------------------------------------------------
482
483 /**
484 * Update statement
485 *
486 * Generates a platform-specific update string from the supplied data
487 *
488 * @access public
489 * @param string the table name
490 * @param array the update data
491 * @param array the where clause
492 * @param array the orderby clause
493 * @param array the limit clause
494 * @return string
495 */
496 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
497 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500498 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
500 $valstr[] = $key." = ".$val;
501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
508
509 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
510
511 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 return $sql;
514 }
515
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 // --------------------------------------------------------------------
518
519 /**
520 * Truncate statement
521 *
522 * Generates a platform-specific truncate string from the supplied data
523 * If the database does not support the truncate() command
524 * This function maps to "DELETE FROM table"
525 *
526 * @access public
527 * @param string the table name
528 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200529 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 function _truncate($table)
531 {
532 return $this->_delete($table);
533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 // --------------------------------------------------------------------
536
537 /**
538 * Delete statement
539 *
540 * Generates a platform-specific delete string from the supplied data
541 *
542 * @access public
543 * @param string the table name
544 * @param array the where clause
545 * @param string the limit clause
546 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200547 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
549 {
550 $conditions = '';
551
552 if (count($where) > 0 OR count($like) > 0)
553 {
554 $conditions = "\nWHERE ";
555 $conditions .= implode("\n", $this->ar_where);
556
557 if (count($where) > 0 && count($like) > 0)
558 {
559 $conditions .= " AND ";
560 }
561 $conditions .= implode("\n", $like);
562 }
563
564 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 return "DELETE FROM ".$table.$conditions.$limit;
567 }
568
569 // --------------------------------------------------------------------
570
571 /**
572 * Limit string
573 *
574 * Generates a platform-specific LIMIT clause
575 *
576 * @access public
577 * @param string the sql query string
578 * @param integer the number of rows to limit the query to
579 * @param integer the offset value
580 * @return string
581 */
582 function _limit($sql, $limit, $offset)
583 {
584 // Does ODBC doesn't use the LIMIT clause?
585 return $sql;
586 }
587
588 // --------------------------------------------------------------------
589
590 /**
591 * Close DB Connection
592 *
593 * @access public
594 * @param resource
595 * @return void
596 */
597 function _close($conn_id)
598 {
599 @odbc_close($conn_id);
600 }
601
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603}
604
605
606
607/* End of file odbc_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200608/* Location: ./system/database/drivers/odbc/odbc_driver.php */