blob: cc8d3347baf42ec873969db86ae4b8f9a53634cd [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard5c3905b2007-03-24 11:08:55 +00002/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allard5c3905b2007-03-24 11:08:55 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Rick Ellis9ba2bf22008-09-12 23:34:39 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allard5c3905b2007-03-24 11:08:55 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * ODBC Database Adapter Class
20 *
21 * Note: _DB is an extender class that the app controller
22 * creates dynamically based on whether the active record
23 * class is being used or not.
24 *
25 * @package CodeIgniter
26 * @subpackage Drivers
27 * @category Database
Derek Allard3d879d52008-01-18 19:41:32 +000028 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000029 * @link http://codeigniter.com/user_guide/database/
Derek Allard5c3905b2007-03-24 11:08:55 +000030 */
31class CI_DB_odbc_driver extends CI_DB {
32
Rick Ellis5aa8c602008-10-07 01:24:07 +000033 var $dbdriver = 'odbc';
34
Derek Allard5c3905b2007-03-24 11:08:55 +000035 /**
Derek Allard694b5b82007-12-18 15:58:03 +000036 * The syntax to count rows is slightly different across different
37 * database engines, so this string appears in each driver and is
38 * used for the count_all() and count_all_results() functions.
39 */
Derek Allard39b622d2008-01-16 21:10:09 +000040 var $_count_string = "SELECT COUNT(*) AS ";
41 var $_random_keyword;
42
43
Rick Ellisceb6f0b2008-10-07 00:48:19 +000044 function CI_DB_odbc_driver($params)
Derek Allard39b622d2008-01-16 21:10:09 +000045 {
Rick Ellisceb6f0b2008-10-07 00:48:19 +000046 if (is_array($params))
47 {
48 foreach ($params as $key => $val)
49 {
50 $this->$key = $val;
51 }
52 }
53
Derek Allardb52bdc42008-01-27 14:58:24 +000054 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Derek Allard39b622d2008-01-16 21:10:09 +000055 }
Derek Allard694b5b82007-12-18 15:58:03 +000056
57 /**
Derek Allard5c3905b2007-03-24 11:08:55 +000058 * Non-persistent database connection
59 *
60 * @access private called by the base class
61 * @return resource
62 */
63 function db_connect()
64 {
65 return @odbc_connect($this->hostname, $this->username, $this->password);
66 }
67
68 // --------------------------------------------------------------------
69
70 /**
71 * Persistent database connection
72 *
73 * @access private called by the base class
74 * @return resource
75 */
76 function db_pconnect()
77 {
78 return @odbc_pconnect($this->hostname, $this->username, $this->password);
79 }
80
81 // --------------------------------------------------------------------
82
83 /**
84 * Select the database
85 *
86 * @access private called by the base class
87 * @return resource
88 */
89 function db_select()
90 {
91 // Not needed for ODBC
92 return TRUE;
93 }
94
95 // --------------------------------------------------------------------
96
97 /**
Derek Allard39b622d2008-01-16 21:10:09 +000098 * Set client character set
99 *
100 * @access public
101 * @param string
102 * @param string
103 * @return resource
104 */
105 function db_set_charset($charset, $collation)
106 {
Rick Ellisff734012008-09-30 20:38:12 +0000107 // @todo - add support if needed
Derek Allard39b622d2008-01-16 21:10:09 +0000108 return TRUE;
109 }
110
111 // --------------------------------------------------------------------
112
113 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000114 * Version number query string
115 *
116 * @access public
117 * @return string
118 */
119 function _version()
120 {
121 return "SELECT version() AS ver";
122 }
123
124 // --------------------------------------------------------------------
125
126 /**
127 * Execute the query
128 *
129 * @access private called by the base class
130 * @param string an SQL query
131 * @return resource
132 */
133 function _execute($sql)
134 {
135 $sql = $this->_prep_query($sql);
136 return @odbc_exec($this->conn_id, $sql);
137 }
138
139 // --------------------------------------------------------------------
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
149 */
150 function _prep_query($sql)
151 {
152 return $sql;
153 }
154
155 // --------------------------------------------------------------------
156
157 /**
158 * Begin Transaction
159 *
160 * @access public
161 * @return bool
162 */
163 function trans_begin($test_mode = FALSE)
164 {
Derek Jones0b59f272008-05-13 04:22:33 +0000165 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000166 {
167 return TRUE;
168 }
169
170 // 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
190 * @return bool
191 */
192 function trans_commit()
193 {
Derek Jones0b59f272008-05-13 04:22:33 +0000194 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000195 {
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
216 * @return bool
217 */
218 function trans_rollback()
219 {
Derek Jones0b59f272008-05-13 04:22:33 +0000220 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000221 {
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
243 * @return string
244 */
245 function escape_str($str)
246 {
Rick Ellis06a2e742008-10-07 01:04:15 +0000247 // Access the CI object
Rick Ellisca86a7c2008-10-07 01:16:57 +0000248 $CI =& get_instance();
Rick Ellis06a2e742008-10-07 01:04:15 +0000249
Derek Allard5c3905b2007-03-24 11:08:55 +0000250 // ODBC doesn't require escaping
Rick Ellis06a2e742008-10-07 01:04:15 +0000251 return $CI->_remove_invisible_characters($str);
Derek Allard5c3905b2007-03-24 11:08:55 +0000252 }
253
254 // --------------------------------------------------------------------
255
256 /**
257 * Affected Rows
258 *
259 * @access public
260 * @return integer
261 */
262 function affected_rows()
263 {
264 return @odbc_num_rows($this->conn_id);
265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Insert ID
271 *
272 * @access public
273 * @return integer
274 */
275 function insert_id()
276 {
277 return @odbc_insert_id($this->conn_id);
278 }
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 == '')
295 return '0';
296
Derek Allardf6cd45c2008-01-18 14:31:51 +0000297 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
298
Derek Allard5c3905b2007-03-24 11:08:55 +0000299 if ($query->num_rows() == 0)
300 return '0';
301
302 $row = $query->row();
303 return $row->numrows;
304 }
305
306 // --------------------------------------------------------------------
307
308 /**
309 * Show table query
310 *
311 * Generates a platform-specific query string so that the table names can be fetched
312 *
313 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000314 * @param boolean
Derek Allard5c3905b2007-03-24 11:08:55 +0000315 * @return string
316 */
Derek Allard39b622d2008-01-16 21:10:09 +0000317 function _list_tables($prefix_limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000318 {
Derek Allard39b622d2008-01-16 21:10:09 +0000319 $sql = "SHOW TABLES FROM `".$this->database."`";
320
321 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
322 {
323 //$sql .= " LIKE '".$this->dbprefix."%'";
324 return FALSE; // not currently supported
325 }
326
327 return $sql;
Derek Allard5c3905b2007-03-24 11:08:55 +0000328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * Show column query
334 *
335 * Generates a platform-specific query string so that the column names can be fetched
336 *
337 * @access public
338 * @param string the table name
339 * @return string
340 */
341 function _list_columns($table = '')
342 {
343 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
344 }
345
346 // --------------------------------------------------------------------
347
348 /**
349 * Field data query
350 *
351 * Generates a platform-specific query so that the column data can be retrieved
352 *
353 * @access public
354 * @param string the table name
355 * @return object
356 */
357 function _field_data($table)
358 {
359 return "SELECT TOP 1 FROM ".$this->_escape_table($table);
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * The error message string
366 *
367 * @access private
368 * @return string
369 */
370 function _error_message()
371 {
372 return odbc_errormsg($this->conn_id);
373 }
374
375 // --------------------------------------------------------------------
376
377 /**
378 * The error message number
379 *
380 * @access private
381 * @return integer
382 */
383 function _error_number()
384 {
385 return odbc_error($this->conn_id);
386 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000387 // --------------------------------------------------------------------
388
389 /**
390 * Escape Column Name
391 *
392 * This function adds backticks around supplied column name
393 *
394 * @access private
395 * @param string the column name
396 * @return string
397 */
398 function _escape_column($column)
399 {
400 // Not necessary with ODBC so we simply return the value
401 return $column;
402 }
403
Derek Allard5c3905b2007-03-24 11:08:55 +0000404 // --------------------------------------------------------------------
405
406 /**
407 * Escape Table Name
408 *
409 * This function adds backticks if the table name has a period
410 * in it. Some DBs will get cranky unless periods are escaped
411 *
412 * @access private
413 * @param string the table name
414 * @return string
415 */
416 function _escape_table($table)
417 {
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000418 // Not necessary with ODBC so we simply return the value
Derek Allard5c3905b2007-03-24 11:08:55 +0000419 return $table;
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000420 }
Derek Allard5c3905b2007-03-24 11:08:55 +0000421
422 // --------------------------------------------------------------------
423
424 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000425 * Protect Identifiers
426 *
427 * This function adds backticks if appropriate based on db type
428 *
429 * @access private
430 * @param mixed the item to escape
431 * @param boolean only affect the first word
432 * @return mixed the item with backticks
433 */
434 function _protect_identifiers($item, $first_word_only = FALSE)
435 {
436 if (is_array($item))
437 {
438 $escaped_array = array();
439
440 foreach($item as $k=>$v)
441 {
442 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
443 }
444
445 return $escaped_array;
446 }
447
448 // This function may get "item1 item2" as a string, and so
449 // we may need "`item1` `item2`" and not "`item1 item2`"
Derek Allard61579382008-01-16 22:22:42 +0000450 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000451 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000452 if (strpos($item, '.') !== FALSE)
453 {
454 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
455 $table_name = substr($item, 0, strpos($item, '.')+1);
456 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
457 }
458
Derek Allard39b622d2008-01-16 21:10:09 +0000459 // This function may get "field >= 1", and need it to return "`field` >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000460 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000461
Derek Allard61579382008-01-16 22:22:42 +0000462 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
463 }
464 else
465 {
Derek Allard903cc982008-02-11 06:06:59 +0000466 return "{$item}";
Derek Allard39b622d2008-01-16 21:10:09 +0000467 }
468
Derek Allard9a4d1da2008-02-25 14:18:38 +0000469 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard39b622d2008-01-16 21:10:09 +0000470
471 foreach ($exceptions as $exception)
472 {
Derek Allard61579382008-01-16 22:22:42 +0000473
Derek Allard903cc982008-02-11 06:06:59 +0000474 if (stristr($item, " {$exception} ") !== FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000475 {
Derek Allard903cc982008-02-11 06:06:59 +0000476 $item = preg_replace('/ ('.preg_quote($exception).') /i', ' $1 ', $item);
Derek Allard39b622d2008-01-16 21:10:09 +0000477 }
478 }
Derek Allard39b622d2008-01-16 21:10:09 +0000479 return $item;
480 }
481
482 // --------------------------------------------------------------------
483
484 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000485 * From Tables
486 *
487 * This function implicitly groups FROM tables so there is no confusion
488 * about operator precedence in harmony with SQL standards
489 *
490 * @access public
491 * @param type
492 * @return type
493 */
494 function _from_tables($tables)
495 {
Derek Jones0b59f272008-05-13 04:22:33 +0000496 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000497 {
498 $tables = array($tables);
499 }
500
501 return '('.implode(', ', $tables).')';
502 }
503
504 // --------------------------------------------------------------------
505
506 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000507 * Insert statement
508 *
509 * Generates a platform-specific insert string from the supplied data
510 *
511 * @access public
512 * @param string the table name
513 * @param array the insert keys
514 * @param array the insert values
515 * @return string
516 */
517 function _insert($table, $keys, $values)
518 {
519 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
520 }
521
522 // --------------------------------------------------------------------
523
524 /**
525 * Update statement
526 *
527 * Generates a platform-specific update string from the supplied data
528 *
529 * @access public
530 * @param string the table name
531 * @param array the update data
532 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000533 * @param array the orderby clause
534 * @param array the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000535 * @return string
536 */
Derek Allard39b622d2008-01-16 21:10:09 +0000537 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000538 {
539 foreach($values as $key => $val)
540 {
541 $valstr[] = $key." = ".$val;
542 }
Derek Allardda6d2402007-12-19 14:49:29 +0000543
Derek Jones0b59f272008-05-13 04:22:33 +0000544 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000545
546 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allard5c3905b2007-03-24 11:08:55 +0000547
Derek Allard32cf7eb2008-02-05 16:03:50 +0000548 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
549 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
550 $sql .= $orderby.$limit;
551
552 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000553 }
554
555
556 // --------------------------------------------------------------------
557
558 /**
559 * Truncate statement
560 *
561 * Generates a platform-specific truncate string from the supplied data
562 * If the database does not support the truncate() command
563 * This function maps to "DELETE FROM table"
564 *
565 * @access public
566 * @param string the table name
567 * @return string
568 */
569 function _truncate($table)
570 {
571 return $this->_delete($table);
Derek Allard5c3905b2007-03-24 11:08:55 +0000572 }
573
574 // --------------------------------------------------------------------
575
576 /**
577 * Delete statement
578 *
579 * Generates a platform-specific delete string from the supplied data
580 *
581 * @access public
582 * @param string the table name
583 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000584 * @param string the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000585 * @return string
586 */
Derek Allard39b622d2008-01-16 21:10:09 +0000587 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000588 {
Derek Allard39b622d2008-01-16 21:10:09 +0000589 $conditions = '';
590
Derek Jones0b59f272008-05-13 04:22:33 +0000591 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000592 {
593 $conditions = "\nWHERE ";
594 $conditions .= implode("\n", $this->ar_where);
595
596 if (count($where) > 0 && count($like) > 0)
597 {
598 $conditions .= " AND ";
599 }
600 $conditions .= implode("\n", $like);
601 }
602
Derek Jones0b59f272008-05-13 04:22:33 +0000603 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000604
Derek Allard39b622d2008-01-16 21:10:09 +0000605 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allard5c3905b2007-03-24 11:08:55 +0000606 }
607
608 // --------------------------------------------------------------------
609
610 /**
611 * Limit string
612 *
613 * Generates a platform-specific LIMIT clause
614 *
615 * @access public
616 * @param string the sql query string
617 * @param integer the number of rows to limit the query to
618 * @param integer the offset value
619 * @return string
620 */
621 function _limit($sql, $limit, $offset)
622 {
623 // Does ODBC doesn't use the LIMIT clause?
624 return $sql;
625 }
626
627 // --------------------------------------------------------------------
628
629 /**
630 * Close DB Connection
631 *
632 * @access public
633 * @param resource
634 * @return void
635 */
636 function _close($conn_id)
637 {
638 @odbc_close($conn_id);
639 }
640
641
642}
643
644
Derek Jones0b59f272008-05-13 04:22:33 +0000645
646/* End of file odbc_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000647/* Location: ./system/database/drivers/odbc/odbc_driver.php */