blob: 985c975ced67ecb75845dae6e181d3fd4ecf015d [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
33 /**
Derek Allard694b5b82007-12-18 15:58:03 +000034 * The syntax to count rows is slightly different across different
35 * database engines, so this string appears in each driver and is
36 * used for the count_all() and count_all_results() functions.
37 */
Derek Allard39b622d2008-01-16 21:10:09 +000038 var $_count_string = "SELECT COUNT(*) AS ";
39 var $_random_keyword;
40
41
Rick Ellisceb6f0b2008-10-07 00:48:19 +000042 function CI_DB_odbc_driver($params)
Derek Allard39b622d2008-01-16 21:10:09 +000043 {
Rick Ellisceb6f0b2008-10-07 00:48:19 +000044 if (is_array($params))
45 {
46 foreach ($params as $key => $val)
47 {
48 $this->$key = $val;
49 }
50 }
51
Derek Allardb52bdc42008-01-27 14:58:24 +000052 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Derek Allard39b622d2008-01-16 21:10:09 +000053 }
Derek Allard694b5b82007-12-18 15:58:03 +000054
55 /**
Derek Allard5c3905b2007-03-24 11:08:55 +000056 * Non-persistent database connection
57 *
58 * @access private called by the base class
59 * @return resource
60 */
61 function db_connect()
62 {
63 return @odbc_connect($this->hostname, $this->username, $this->password);
64 }
65
66 // --------------------------------------------------------------------
67
68 /**
69 * Persistent database connection
70 *
71 * @access private called by the base class
72 * @return resource
73 */
74 function db_pconnect()
75 {
76 return @odbc_pconnect($this->hostname, $this->username, $this->password);
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Select the database
83 *
84 * @access private called by the base class
85 * @return resource
86 */
87 function db_select()
88 {
89 // Not needed for ODBC
90 return TRUE;
91 }
92
93 // --------------------------------------------------------------------
94
95 /**
Derek Allard39b622d2008-01-16 21:10:09 +000096 * Set client character set
97 *
98 * @access public
99 * @param string
100 * @param string
101 * @return resource
102 */
103 function db_set_charset($charset, $collation)
104 {
Rick Ellisff734012008-09-30 20:38:12 +0000105 // @todo - add support if needed
Derek Allard39b622d2008-01-16 21:10:09 +0000106 return TRUE;
107 }
108
109 // --------------------------------------------------------------------
110
111 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000112 * Version number query string
113 *
114 * @access public
115 * @return string
116 */
117 function _version()
118 {
119 return "SELECT version() AS ver";
120 }
121
122 // --------------------------------------------------------------------
123
124 /**
125 * Execute the query
126 *
127 * @access private called by the base class
128 * @param string an SQL query
129 * @return resource
130 */
131 function _execute($sql)
132 {
133 $sql = $this->_prep_query($sql);
134 return @odbc_exec($this->conn_id, $sql);
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Prep the query
141 *
142 * If needed, each database adapter can prep the query string
143 *
144 * @access private called by execute()
145 * @param string an SQL query
146 * @return string
147 */
148 function _prep_query($sql)
149 {
150 return $sql;
151 }
152
153 // --------------------------------------------------------------------
154
155 /**
156 * Begin Transaction
157 *
158 * @access public
159 * @return bool
160 */
161 function trans_begin($test_mode = FALSE)
162 {
Derek Jones0b59f272008-05-13 04:22:33 +0000163 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000164 {
165 return TRUE;
166 }
167
168 // When transactions are nested we only begin/commit/rollback the outermost ones
169 if ($this->_trans_depth > 0)
170 {
171 return TRUE;
172 }
173
174 // Reset the transaction failure flag.
175 // If the $test_mode flag is set to TRUE transactions will be rolled back
176 // even if the queries produce a successful result.
177 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
178
179 return odbc_autocommit($this->conn_id, FALSE);
180 }
181
182 // --------------------------------------------------------------------
183
184 /**
185 * Commit Transaction
186 *
187 * @access public
188 * @return bool
189 */
190 function trans_commit()
191 {
Derek Jones0b59f272008-05-13 04:22:33 +0000192 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000193 {
194 return TRUE;
195 }
196
197 // When transactions are nested we only begin/commit/rollback the outermost ones
198 if ($this->_trans_depth > 0)
199 {
200 return TRUE;
201 }
202
203 $ret = odbc_commit($this->conn_id);
204 odbc_autocommit($this->conn_id, TRUE);
205 return $ret;
206 }
207
208 // --------------------------------------------------------------------
209
210 /**
211 * Rollback Transaction
212 *
213 * @access public
214 * @return bool
215 */
216 function trans_rollback()
217 {
Derek Jones0b59f272008-05-13 04:22:33 +0000218 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000219 {
220 return TRUE;
221 }
222
223 // When transactions are nested we only begin/commit/rollback the outermost ones
224 if ($this->_trans_depth > 0)
225 {
226 return TRUE;
227 }
228
229 $ret = odbc_rollback($this->conn_id);
230 odbc_autocommit($this->conn_id, TRUE);
231 return $ret;
232 }
233
234 // --------------------------------------------------------------------
235
236 /**
237 * Escape String
238 *
239 * @access public
240 * @param string
241 * @return string
242 */
243 function escape_str($str)
244 {
Rick Ellis06a2e742008-10-07 01:04:15 +0000245 // Access the CI object
246 $CI->get_instance();
247
Derek Allard5c3905b2007-03-24 11:08:55 +0000248 // ODBC doesn't require escaping
Rick Ellis06a2e742008-10-07 01:04:15 +0000249 return $CI->_remove_invisible_characters($str);
Derek Allard5c3905b2007-03-24 11:08:55 +0000250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Affected Rows
256 *
257 * @access public
258 * @return integer
259 */
260 function affected_rows()
261 {
262 return @odbc_num_rows($this->conn_id);
263 }
264
265 // --------------------------------------------------------------------
266
267 /**
268 * Insert ID
269 *
270 * @access public
271 * @return integer
272 */
273 function insert_id()
274 {
275 return @odbc_insert_id($this->conn_id);
276 }
277
278 // --------------------------------------------------------------------
279
280 /**
281 * "Count All" query
282 *
283 * Generates a platform-specific query string that counts all records in
284 * the specified database
285 *
286 * @access public
287 * @param string
288 * @return string
289 */
290 function count_all($table = '')
291 {
292 if ($table == '')
293 return '0';
294
Derek Allardf6cd45c2008-01-18 14:31:51 +0000295 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
296
Derek Allard5c3905b2007-03-24 11:08:55 +0000297 if ($query->num_rows() == 0)
298 return '0';
299
300 $row = $query->row();
301 return $row->numrows;
302 }
303
304 // --------------------------------------------------------------------
305
306 /**
307 * Show table query
308 *
309 * Generates a platform-specific query string so that the table names can be fetched
310 *
311 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000312 * @param boolean
Derek Allard5c3905b2007-03-24 11:08:55 +0000313 * @return string
314 */
Derek Allard39b622d2008-01-16 21:10:09 +0000315 function _list_tables($prefix_limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000316 {
Derek Allard39b622d2008-01-16 21:10:09 +0000317 $sql = "SHOW TABLES FROM `".$this->database."`";
318
319 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
320 {
321 //$sql .= " LIKE '".$this->dbprefix."%'";
322 return FALSE; // not currently supported
323 }
324
325 return $sql;
Derek Allard5c3905b2007-03-24 11:08:55 +0000326 }
327
328 // --------------------------------------------------------------------
329
330 /**
331 * Show column query
332 *
333 * Generates a platform-specific query string so that the column names can be fetched
334 *
335 * @access public
336 * @param string the table name
337 * @return string
338 */
339 function _list_columns($table = '')
340 {
341 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
342 }
343
344 // --------------------------------------------------------------------
345
346 /**
347 * Field data query
348 *
349 * Generates a platform-specific query so that the column data can be retrieved
350 *
351 * @access public
352 * @param string the table name
353 * @return object
354 */
355 function _field_data($table)
356 {
357 return "SELECT TOP 1 FROM ".$this->_escape_table($table);
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * The error message string
364 *
365 * @access private
366 * @return string
367 */
368 function _error_message()
369 {
370 return odbc_errormsg($this->conn_id);
371 }
372
373 // --------------------------------------------------------------------
374
375 /**
376 * The error message number
377 *
378 * @access private
379 * @return integer
380 */
381 function _error_number()
382 {
383 return odbc_error($this->conn_id);
384 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000385 // --------------------------------------------------------------------
386
387 /**
388 * Escape Column Name
389 *
390 * This function adds backticks around supplied column name
391 *
392 * @access private
393 * @param string the column name
394 * @return string
395 */
396 function _escape_column($column)
397 {
398 // Not necessary with ODBC so we simply return the value
399 return $column;
400 }
401
Derek Allard5c3905b2007-03-24 11:08:55 +0000402 // --------------------------------------------------------------------
403
404 /**
405 * Escape Table Name
406 *
407 * This function adds backticks if the table name has a period
408 * in it. Some DBs will get cranky unless periods are escaped
409 *
410 * @access private
411 * @param string the table name
412 * @return string
413 */
414 function _escape_table($table)
415 {
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000416 // Not necessary with ODBC so we simply return the value
Derek Allard5c3905b2007-03-24 11:08:55 +0000417 return $table;
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000418 }
Derek Allard5c3905b2007-03-24 11:08:55 +0000419
420 // --------------------------------------------------------------------
421
422 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000423 * Protect Identifiers
424 *
425 * This function adds backticks if appropriate based on db type
426 *
427 * @access private
428 * @param mixed the item to escape
429 * @param boolean only affect the first word
430 * @return mixed the item with backticks
431 */
432 function _protect_identifiers($item, $first_word_only = FALSE)
433 {
434 if (is_array($item))
435 {
436 $escaped_array = array();
437
438 foreach($item as $k=>$v)
439 {
440 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
441 }
442
443 return $escaped_array;
444 }
445
446 // This function may get "item1 item2" as a string, and so
447 // we may need "`item1` `item2`" and not "`item1 item2`"
Derek Allard61579382008-01-16 22:22:42 +0000448 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000449 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000450 if (strpos($item, '.') !== FALSE)
451 {
452 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
453 $table_name = substr($item, 0, strpos($item, '.')+1);
454 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
455 }
456
Derek Allard39b622d2008-01-16 21:10:09 +0000457 // This function may get "field >= 1", and need it to return "`field` >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000458 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000459
Derek Allard61579382008-01-16 22:22:42 +0000460 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
461 }
462 else
463 {
Derek Allard903cc982008-02-11 06:06:59 +0000464 return "{$item}";
Derek Allard39b622d2008-01-16 21:10:09 +0000465 }
466
Derek Allard9a4d1da2008-02-25 14:18:38 +0000467 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard39b622d2008-01-16 21:10:09 +0000468
469 foreach ($exceptions as $exception)
470 {
Derek Allard61579382008-01-16 22:22:42 +0000471
Derek Allard903cc982008-02-11 06:06:59 +0000472 if (stristr($item, " {$exception} ") !== FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000473 {
Derek Allard903cc982008-02-11 06:06:59 +0000474 $item = preg_replace('/ ('.preg_quote($exception).') /i', ' $1 ', $item);
Derek Allard39b622d2008-01-16 21:10:09 +0000475 }
476 }
Derek Allard39b622d2008-01-16 21:10:09 +0000477 return $item;
478 }
479
480 // --------------------------------------------------------------------
481
482 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000483 * From Tables
484 *
485 * This function implicitly groups FROM tables so there is no confusion
486 * about operator precedence in harmony with SQL standards
487 *
488 * @access public
489 * @param type
490 * @return type
491 */
492 function _from_tables($tables)
493 {
Derek Jones0b59f272008-05-13 04:22:33 +0000494 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000495 {
496 $tables = array($tables);
497 }
498
499 return '('.implode(', ', $tables).')';
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000505 * Insert statement
506 *
507 * Generates a platform-specific insert string from the supplied data
508 *
509 * @access public
510 * @param string the table name
511 * @param array the insert keys
512 * @param array the insert values
513 * @return string
514 */
515 function _insert($table, $keys, $values)
516 {
517 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
518 }
519
520 // --------------------------------------------------------------------
521
522 /**
523 * Update statement
524 *
525 * Generates a platform-specific update string from the supplied data
526 *
527 * @access public
528 * @param string the table name
529 * @param array the update data
530 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000531 * @param array the orderby clause
532 * @param array the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000533 * @return string
534 */
Derek Allard39b622d2008-01-16 21:10:09 +0000535 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000536 {
537 foreach($values as $key => $val)
538 {
539 $valstr[] = $key." = ".$val;
540 }
Derek Allardda6d2402007-12-19 14:49:29 +0000541
Derek Jones0b59f272008-05-13 04:22:33 +0000542 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000543
544 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allard5c3905b2007-03-24 11:08:55 +0000545
Derek Allard32cf7eb2008-02-05 16:03:50 +0000546 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
547 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
548 $sql .= $orderby.$limit;
549
550 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000551 }
552
553
554 // --------------------------------------------------------------------
555
556 /**
557 * Truncate statement
558 *
559 * Generates a platform-specific truncate string from the supplied data
560 * If the database does not support the truncate() command
561 * This function maps to "DELETE FROM table"
562 *
563 * @access public
564 * @param string the table name
565 * @return string
566 */
567 function _truncate($table)
568 {
569 return $this->_delete($table);
Derek Allard5c3905b2007-03-24 11:08:55 +0000570 }
571
572 // --------------------------------------------------------------------
573
574 /**
575 * Delete statement
576 *
577 * Generates a platform-specific delete string from the supplied data
578 *
579 * @access public
580 * @param string the table name
581 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000582 * @param string the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000583 * @return string
584 */
Derek Allard39b622d2008-01-16 21:10:09 +0000585 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000586 {
Derek Allard39b622d2008-01-16 21:10:09 +0000587 $conditions = '';
588
Derek Jones0b59f272008-05-13 04:22:33 +0000589 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000590 {
591 $conditions = "\nWHERE ";
592 $conditions .= implode("\n", $this->ar_where);
593
594 if (count($where) > 0 && count($like) > 0)
595 {
596 $conditions .= " AND ";
597 }
598 $conditions .= implode("\n", $like);
599 }
600
Derek Jones0b59f272008-05-13 04:22:33 +0000601 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000602
Derek Allard39b622d2008-01-16 21:10:09 +0000603 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allard5c3905b2007-03-24 11:08:55 +0000604 }
605
606 // --------------------------------------------------------------------
607
608 /**
609 * Limit string
610 *
611 * Generates a platform-specific LIMIT clause
612 *
613 * @access public
614 * @param string the sql query string
615 * @param integer the number of rows to limit the query to
616 * @param integer the offset value
617 * @return string
618 */
619 function _limit($sql, $limit, $offset)
620 {
621 // Does ODBC doesn't use the LIMIT clause?
622 return $sql;
623 }
624
625 // --------------------------------------------------------------------
626
627 /**
628 * Close DB Connection
629 *
630 * @access public
631 * @param resource
632 * @return void
633 */
634 function _close($conn_id)
635 {
636 @odbc_close($conn_id);
637 }
638
639
640}
641
642
Derek Jones0b59f272008-05-13 04:22:33 +0000643
644/* End of file odbc_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000645/* Location: ./system/database/drivers/odbc/odbc_driver.php */