blob: 779b0c62f9f84562cc1d48ab0312b33c386e2a8f [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 *
290 * @access public
291 * @return integer
292 */
293 function insert_id()
294 {
295 return @odbc_insert_id($this->conn_id);
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * "Count All" query
302 *
303 * Generates a platform-specific query string that counts all records in
304 * the specified database
305 *
306 * @access public
307 * @param string
308 * @return string
309 */
310 function count_all($table = '')
311 {
312 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000313 {
314 return 0;
315 }
316
317 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000320 {
321 return 0;
322 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000323
324 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500325 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000326 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328
329 // --------------------------------------------------------------------
330
331 /**
332 * Show table query
333 *
334 * Generates a platform-specific query string so that the table names can be fetched
335 *
336 * @access private
337 * @param boolean
338 * @return string
339 */
340 function _list_tables($prefix_limit = FALSE)
341 {
342 $sql = "SHOW TABLES FROM `".$this->database."`";
343
344 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
345 {
Greg Aker0d424892010-01-26 02:14:44 +0000346 //$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 return FALSE; // not currently supported
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 return $sql;
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // --------------------------------------------------------------------
354
355 /**
356 * Show column query
357 *
358 * Generates a platform-specific query string so that the column names can be fetched
359 *
360 * @access public
361 * @param string the table name
362 * @return string
363 */
364 function _list_columns($table = '')
365 {
366 return "SHOW COLUMNS FROM ".$table;
367 }
368
369 // --------------------------------------------------------------------
370
371 /**
372 * Field data query
373 *
374 * Generates a platform-specific query so that the column data can be retrieved
375 *
376 * @access public
377 * @param string the table name
378 * @return object
379 */
380 function _field_data($table)
381 {
382 return "SELECT TOP 1 FROM ".$table;
383 }
384
385 // --------------------------------------------------------------------
386
387 /**
Andrey Andreev4be5de12012-03-02 15:45:41 +0200388 * Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200390 * Returns an array containing code and message of the last
391 * database error that has occured.
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 *
Andrey Andreev4be5de12012-03-02 15:45:41 +0200393 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 */
Andrey Andreev4be5de12012-03-02 15:45:41 +0200395 public function error()
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Andrey Andreev4be5de12012-03-02 15:45:41 +0200397 return array('code' => odbc_error($this->conn_id), 'message' => odbc_errormsg($this->conn_id));
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
399
400 // --------------------------------------------------------------------
401
402 /**
403 * Escape the SQL Identifiers
404 *
405 * This function escapes column and table names
406 *
407 * @access private
408 * @param string
409 * @return string
410 */
411 function _escape_identifiers($item)
412 {
413 if ($this->_escape_char == '')
414 {
415 return $item;
416 }
417
418 foreach ($this->_reserved_identifiers as $id)
419 {
420 if (strpos($item, '.'.$id) !== FALSE)
421 {
Barry Mienydd671972010-10-04 16:33:58 +0200422 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // remove duplicates if the user already included the escape
425 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200426 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
Barry Mienydd671972010-10-04 16:33:58 +0200428
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 if (strpos($item, '.') !== FALSE)
430 {
Barry Mienydd671972010-10-04 16:33:58 +0200431 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 }
433 else
434 {
435 $str = $this->_escape_char.$item.$this->_escape_char;
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // remove duplicates if the user already included the escape
439 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // --------------------------------------------------------------------
443
444 /**
445 * From Tables
446 *
447 * This function implicitly groups FROM tables so there is no confusion
448 * about operator precedence in harmony with SQL standards
449 *
450 * @access public
451 * @param type
452 * @return type
453 */
454 function _from_tables($tables)
455 {
456 if ( ! is_array($tables))
457 {
458 $tables = array($tables);
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 return '('.implode(', ', $tables).')';
462 }
463
464 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 /**
467 * Insert statement
468 *
469 * Generates a platform-specific insert string from the supplied data
470 *
471 * @access public
472 * @param string the table name
473 * @param array the insert keys
474 * @param array the insert values
475 * @return string
476 */
477 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200478 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 // --------------------------------------------------------------------
483
484 /**
485 * Update statement
486 *
487 * Generates a platform-specific update string from the supplied data
488 *
489 * @access public
490 * @param string the table name
491 * @param array the update data
492 * @param array the where clause
493 * @param array the orderby clause
494 * @param array the limit clause
495 * @return string
496 */
497 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
498 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500499 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
501 $valstr[] = $key." = ".$val;
502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
509
510 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
511
512 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 return $sql;
515 }
516
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 // --------------------------------------------------------------------
519
520 /**
521 * Truncate statement
522 *
523 * Generates a platform-specific truncate string from the supplied data
524 * If the database does not support the truncate() command
525 * This function maps to "DELETE FROM table"
526 *
527 * @access public
528 * @param string the table name
529 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200530 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 function _truncate($table)
532 {
533 return $this->_delete($table);
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 // --------------------------------------------------------------------
537
538 /**
539 * Delete statement
540 *
541 * Generates a platform-specific delete string from the supplied data
542 *
543 * @access public
544 * @param string the table name
545 * @param array the where clause
546 * @param string the limit clause
547 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200548 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
550 {
551 $conditions = '';
552
553 if (count($where) > 0 OR count($like) > 0)
554 {
555 $conditions = "\nWHERE ";
556 $conditions .= implode("\n", $this->ar_where);
557
558 if (count($where) > 0 && count($like) > 0)
559 {
560 $conditions .= " AND ";
561 }
562 $conditions .= implode("\n", $like);
563 }
564
565 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200566
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 return "DELETE FROM ".$table.$conditions.$limit;
568 }
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Limit string
574 *
575 * Generates a platform-specific LIMIT clause
576 *
577 * @access public
578 * @param string the sql query string
579 * @param integer the number of rows to limit the query to
580 * @param integer the offset value
581 * @return string
582 */
583 function _limit($sql, $limit, $offset)
584 {
585 // Does ODBC doesn't use the LIMIT clause?
586 return $sql;
587 }
588
589 // --------------------------------------------------------------------
590
591 /**
592 * Close DB Connection
593 *
594 * @access public
595 * @param resource
596 * @return void
597 */
598 function _close($conn_id)
599 {
600 @odbc_close($conn_id);
601 }
602
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604}
605
606
607
608/* End of file odbc_driver.php */
Andrey Andreev063f5962012-02-27 12:20:52 +0200609/* Location: ./system/database/drivers/odbc/odbc_driver.php */