blob: 7c59c306047cbb8f4e422d632189fa0177e55670 [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 {
245 // ODBC doesn't require escaping
Rick Ellis2cad6e92008-10-07 00:19:34 +0000246 return $this->input->CI->_remove_invisible_characters($str);
Derek Allard5c3905b2007-03-24 11:08:55 +0000247 }
248
249 // --------------------------------------------------------------------
250
251 /**
252 * Affected Rows
253 *
254 * @access public
255 * @return integer
256 */
257 function affected_rows()
258 {
259 return @odbc_num_rows($this->conn_id);
260 }
261
262 // --------------------------------------------------------------------
263
264 /**
265 * Insert ID
266 *
267 * @access public
268 * @return integer
269 */
270 function insert_id()
271 {
272 return @odbc_insert_id($this->conn_id);
273 }
274
275 // --------------------------------------------------------------------
276
277 /**
278 * "Count All" query
279 *
280 * Generates a platform-specific query string that counts all records in
281 * the specified database
282 *
283 * @access public
284 * @param string
285 * @return string
286 */
287 function count_all($table = '')
288 {
289 if ($table == '')
290 return '0';
291
Derek Allardf6cd45c2008-01-18 14:31:51 +0000292 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
293
Derek Allard5c3905b2007-03-24 11:08:55 +0000294 if ($query->num_rows() == 0)
295 return '0';
296
297 $row = $query->row();
298 return $row->numrows;
299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * Show table query
305 *
306 * Generates a platform-specific query string so that the table names can be fetched
307 *
308 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000309 * @param boolean
Derek Allard5c3905b2007-03-24 11:08:55 +0000310 * @return string
311 */
Derek Allard39b622d2008-01-16 21:10:09 +0000312 function _list_tables($prefix_limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000313 {
Derek Allard39b622d2008-01-16 21:10:09 +0000314 $sql = "SHOW TABLES FROM `".$this->database."`";
315
316 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
317 {
318 //$sql .= " LIKE '".$this->dbprefix."%'";
319 return FALSE; // not currently supported
320 }
321
322 return $sql;
Derek Allard5c3905b2007-03-24 11:08:55 +0000323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Show column query
329 *
330 * Generates a platform-specific query string so that the column names can be fetched
331 *
332 * @access public
333 * @param string the table name
334 * @return string
335 */
336 function _list_columns($table = '')
337 {
338 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
339 }
340
341 // --------------------------------------------------------------------
342
343 /**
344 * Field data query
345 *
346 * Generates a platform-specific query so that the column data can be retrieved
347 *
348 * @access public
349 * @param string the table name
350 * @return object
351 */
352 function _field_data($table)
353 {
354 return "SELECT TOP 1 FROM ".$this->_escape_table($table);
355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * The error message string
361 *
362 * @access private
363 * @return string
364 */
365 function _error_message()
366 {
367 return odbc_errormsg($this->conn_id);
368 }
369
370 // --------------------------------------------------------------------
371
372 /**
373 * The error message number
374 *
375 * @access private
376 * @return integer
377 */
378 function _error_number()
379 {
380 return odbc_error($this->conn_id);
381 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000382 // --------------------------------------------------------------------
383
384 /**
385 * Escape Column Name
386 *
387 * This function adds backticks around supplied column name
388 *
389 * @access private
390 * @param string the column name
391 * @return string
392 */
393 function _escape_column($column)
394 {
395 // Not necessary with ODBC so we simply return the value
396 return $column;
397 }
398
Derek Allard5c3905b2007-03-24 11:08:55 +0000399 // --------------------------------------------------------------------
400
401 /**
402 * Escape Table Name
403 *
404 * This function adds backticks if the table name has a period
405 * in it. Some DBs will get cranky unless periods are escaped
406 *
407 * @access private
408 * @param string the table name
409 * @return string
410 */
411 function _escape_table($table)
412 {
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000413 // Not necessary with ODBC so we simply return the value
Derek Allard5c3905b2007-03-24 11:08:55 +0000414 return $table;
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000415 }
Derek Allard5c3905b2007-03-24 11:08:55 +0000416
417 // --------------------------------------------------------------------
418
419 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000420 * Protect Identifiers
421 *
422 * This function adds backticks if appropriate based on db type
423 *
424 * @access private
425 * @param mixed the item to escape
426 * @param boolean only affect the first word
427 * @return mixed the item with backticks
428 */
429 function _protect_identifiers($item, $first_word_only = FALSE)
430 {
431 if (is_array($item))
432 {
433 $escaped_array = array();
434
435 foreach($item as $k=>$v)
436 {
437 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
438 }
439
440 return $escaped_array;
441 }
442
443 // This function may get "item1 item2" as a string, and so
444 // we may need "`item1` `item2`" and not "`item1 item2`"
Derek Allard61579382008-01-16 22:22:42 +0000445 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000446 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000447 if (strpos($item, '.') !== FALSE)
448 {
449 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
450 $table_name = substr($item, 0, strpos($item, '.')+1);
451 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
452 }
453
Derek Allard39b622d2008-01-16 21:10:09 +0000454 // This function may get "field >= 1", and need it to return "`field` >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000455 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000456
Derek Allard61579382008-01-16 22:22:42 +0000457 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
458 }
459 else
460 {
Derek Allard903cc982008-02-11 06:06:59 +0000461 return "{$item}";
Derek Allard39b622d2008-01-16 21:10:09 +0000462 }
463
Derek Allard9a4d1da2008-02-25 14:18:38 +0000464 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard39b622d2008-01-16 21:10:09 +0000465
466 foreach ($exceptions as $exception)
467 {
Derek Allard61579382008-01-16 22:22:42 +0000468
Derek Allard903cc982008-02-11 06:06:59 +0000469 if (stristr($item, " {$exception} ") !== FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000470 {
Derek Allard903cc982008-02-11 06:06:59 +0000471 $item = preg_replace('/ ('.preg_quote($exception).') /i', ' $1 ', $item);
Derek Allard39b622d2008-01-16 21:10:09 +0000472 }
473 }
Derek Allard39b622d2008-01-16 21:10:09 +0000474 return $item;
475 }
476
477 // --------------------------------------------------------------------
478
479 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000480 * From Tables
481 *
482 * This function implicitly groups FROM tables so there is no confusion
483 * about operator precedence in harmony with SQL standards
484 *
485 * @access public
486 * @param type
487 * @return type
488 */
489 function _from_tables($tables)
490 {
Derek Jones0b59f272008-05-13 04:22:33 +0000491 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000492 {
493 $tables = array($tables);
494 }
495
496 return '('.implode(', ', $tables).')';
497 }
498
499 // --------------------------------------------------------------------
500
501 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000502 * Insert statement
503 *
504 * Generates a platform-specific insert string from the supplied data
505 *
506 * @access public
507 * @param string the table name
508 * @param array the insert keys
509 * @param array the insert values
510 * @return string
511 */
512 function _insert($table, $keys, $values)
513 {
514 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
515 }
516
517 // --------------------------------------------------------------------
518
519 /**
520 * Update statement
521 *
522 * Generates a platform-specific update string from the supplied data
523 *
524 * @access public
525 * @param string the table name
526 * @param array the update data
527 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000528 * @param array the orderby clause
529 * @param array the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000530 * @return string
531 */
Derek Allard39b622d2008-01-16 21:10:09 +0000532 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000533 {
534 foreach($values as $key => $val)
535 {
536 $valstr[] = $key." = ".$val;
537 }
Derek Allardda6d2402007-12-19 14:49:29 +0000538
Derek Jones0b59f272008-05-13 04:22:33 +0000539 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000540
541 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allard5c3905b2007-03-24 11:08:55 +0000542
Derek Allard32cf7eb2008-02-05 16:03:50 +0000543 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
544 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
545 $sql .= $orderby.$limit;
546
547 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000548 }
549
550
551 // --------------------------------------------------------------------
552
553 /**
554 * Truncate statement
555 *
556 * Generates a platform-specific truncate string from the supplied data
557 * If the database does not support the truncate() command
558 * This function maps to "DELETE FROM table"
559 *
560 * @access public
561 * @param string the table name
562 * @return string
563 */
564 function _truncate($table)
565 {
566 return $this->_delete($table);
Derek Allard5c3905b2007-03-24 11:08:55 +0000567 }
568
569 // --------------------------------------------------------------------
570
571 /**
572 * Delete statement
573 *
574 * Generates a platform-specific delete string from the supplied data
575 *
576 * @access public
577 * @param string the table name
578 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000579 * @param string the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000580 * @return string
581 */
Derek Allard39b622d2008-01-16 21:10:09 +0000582 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000583 {
Derek Allard39b622d2008-01-16 21:10:09 +0000584 $conditions = '';
585
Derek Jones0b59f272008-05-13 04:22:33 +0000586 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000587 {
588 $conditions = "\nWHERE ";
589 $conditions .= implode("\n", $this->ar_where);
590
591 if (count($where) > 0 && count($like) > 0)
592 {
593 $conditions .= " AND ";
594 }
595 $conditions .= implode("\n", $like);
596 }
597
Derek Jones0b59f272008-05-13 04:22:33 +0000598 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000599
Derek Allard39b622d2008-01-16 21:10:09 +0000600 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allard5c3905b2007-03-24 11:08:55 +0000601 }
602
603 // --------------------------------------------------------------------
604
605 /**
606 * Limit string
607 *
608 * Generates a platform-specific LIMIT clause
609 *
610 * @access public
611 * @param string the sql query string
612 * @param integer the number of rows to limit the query to
613 * @param integer the offset value
614 * @return string
615 */
616 function _limit($sql, $limit, $offset)
617 {
618 // Does ODBC doesn't use the LIMIT clause?
619 return $sql;
620 }
621
622 // --------------------------------------------------------------------
623
624 /**
625 * Close DB Connection
626 *
627 * @access public
628 * @param resource
629 * @return void
630 */
631 function _close($conn_id)
632 {
633 @odbc_close($conn_id);
634 }
635
636
637}
638
639
Derek Jones0b59f272008-05-13 04:22:33 +0000640
641/* End of file odbc_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000642/* Location: ./system/database/drivers/odbc/odbc_driver.php */