blob: ed8f81cb90a5d6bbfdb07b72f203885b36f473cf [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
42 function CI_DB_odbc_driver()
43 {
Derek Allardb52bdc42008-01-27 14:58:24 +000044 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Derek Allard39b622d2008-01-16 21:10:09 +000045 }
Derek Allard694b5b82007-12-18 15:58:03 +000046
47 /**
Derek Allard5c3905b2007-03-24 11:08:55 +000048 * Non-persistent database connection
49 *
50 * @access private called by the base class
51 * @return resource
52 */
53 function db_connect()
54 {
55 return @odbc_connect($this->hostname, $this->username, $this->password);
56 }
57
58 // --------------------------------------------------------------------
59
60 /**
61 * Persistent database connection
62 *
63 * @access private called by the base class
64 * @return resource
65 */
66 function db_pconnect()
67 {
68 return @odbc_pconnect($this->hostname, $this->username, $this->password);
69 }
70
71 // --------------------------------------------------------------------
72
73 /**
74 * Select the database
75 *
76 * @access private called by the base class
77 * @return resource
78 */
79 function db_select()
80 {
81 // Not needed for ODBC
82 return TRUE;
83 }
84
85 // --------------------------------------------------------------------
86
87 /**
Derek Allard39b622d2008-01-16 21:10:09 +000088 * Set client character set
89 *
90 * @access public
91 * @param string
92 * @param string
93 * @return resource
94 */
95 function db_set_charset($charset, $collation)
96 {
97 // TODO - add support if needed
98 return TRUE;
99 }
100
101 // --------------------------------------------------------------------
102
103 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000104 * Version number query string
105 *
106 * @access public
107 * @return string
108 */
109 function _version()
110 {
111 return "SELECT version() AS ver";
112 }
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Execute the query
118 *
119 * @access private called by the base class
120 * @param string an SQL query
121 * @return resource
122 */
123 function _execute($sql)
124 {
125 $sql = $this->_prep_query($sql);
126 return @odbc_exec($this->conn_id, $sql);
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
132 * Prep the query
133 *
134 * If needed, each database adapter can prep the query string
135 *
136 * @access private called by execute()
137 * @param string an SQL query
138 * @return string
139 */
140 function _prep_query($sql)
141 {
142 return $sql;
143 }
144
145 // --------------------------------------------------------------------
146
147 /**
148 * Begin Transaction
149 *
150 * @access public
151 * @return bool
152 */
153 function trans_begin($test_mode = FALSE)
154 {
Derek Jones0b59f272008-05-13 04:22:33 +0000155 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000156 {
157 return TRUE;
158 }
159
160 // When transactions are nested we only begin/commit/rollback the outermost ones
161 if ($this->_trans_depth > 0)
162 {
163 return TRUE;
164 }
165
166 // Reset the transaction failure flag.
167 // If the $test_mode flag is set to TRUE transactions will be rolled back
168 // even if the queries produce a successful result.
169 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
170
171 return odbc_autocommit($this->conn_id, FALSE);
172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Commit Transaction
178 *
179 * @access public
180 * @return bool
181 */
182 function trans_commit()
183 {
Derek Jones0b59f272008-05-13 04:22:33 +0000184 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000185 {
186 return TRUE;
187 }
188
189 // When transactions are nested we only begin/commit/rollback the outermost ones
190 if ($this->_trans_depth > 0)
191 {
192 return TRUE;
193 }
194
195 $ret = odbc_commit($this->conn_id);
196 odbc_autocommit($this->conn_id, TRUE);
197 return $ret;
198 }
199
200 // --------------------------------------------------------------------
201
202 /**
203 * Rollback Transaction
204 *
205 * @access public
206 * @return bool
207 */
208 function trans_rollback()
209 {
Derek Jones0b59f272008-05-13 04:22:33 +0000210 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000211 {
212 return TRUE;
213 }
214
215 // When transactions are nested we only begin/commit/rollback the outermost ones
216 if ($this->_trans_depth > 0)
217 {
218 return TRUE;
219 }
220
221 $ret = odbc_rollback($this->conn_id);
222 odbc_autocommit($this->conn_id, TRUE);
223 return $ret;
224 }
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Escape String
230 *
231 * @access public
232 * @param string
233 * @return string
234 */
235 function escape_str($str)
236 {
237 // ODBC doesn't require escaping
Derek Jonesd16bab12008-09-24 18:22:03 +0000238 return $this->input->_remove_invisible_characters($str);
Derek Allard5c3905b2007-03-24 11:08:55 +0000239 }
240
241 // --------------------------------------------------------------------
242
243 /**
244 * Affected Rows
245 *
246 * @access public
247 * @return integer
248 */
249 function affected_rows()
250 {
251 return @odbc_num_rows($this->conn_id);
252 }
253
254 // --------------------------------------------------------------------
255
256 /**
257 * Insert ID
258 *
259 * @access public
260 * @return integer
261 */
262 function insert_id()
263 {
264 return @odbc_insert_id($this->conn_id);
265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * "Count All" query
271 *
272 * Generates a platform-specific query string that counts all records in
273 * the specified database
274 *
275 * @access public
276 * @param string
277 * @return string
278 */
279 function count_all($table = '')
280 {
281 if ($table == '')
282 return '0';
283
Derek Allardf6cd45c2008-01-18 14:31:51 +0000284 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
285
Derek Allard5c3905b2007-03-24 11:08:55 +0000286 if ($query->num_rows() == 0)
287 return '0';
288
289 $row = $query->row();
290 return $row->numrows;
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Show table query
297 *
298 * Generates a platform-specific query string so that the table names can be fetched
299 *
300 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000301 * @param boolean
Derek Allard5c3905b2007-03-24 11:08:55 +0000302 * @return string
303 */
Derek Allard39b622d2008-01-16 21:10:09 +0000304 function _list_tables($prefix_limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000305 {
Derek Allard39b622d2008-01-16 21:10:09 +0000306 $sql = "SHOW TABLES FROM `".$this->database."`";
307
308 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
309 {
310 //$sql .= " LIKE '".$this->dbprefix."%'";
311 return FALSE; // not currently supported
312 }
313
314 return $sql;
Derek Allard5c3905b2007-03-24 11:08:55 +0000315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * Show column query
321 *
322 * Generates a platform-specific query string so that the column names can be fetched
323 *
324 * @access public
325 * @param string the table name
326 * @return string
327 */
328 function _list_columns($table = '')
329 {
330 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * Field data query
337 *
338 * Generates a platform-specific query so that the column data can be retrieved
339 *
340 * @access public
341 * @param string the table name
342 * @return object
343 */
344 function _field_data($table)
345 {
346 return "SELECT TOP 1 FROM ".$this->_escape_table($table);
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * The error message string
353 *
354 * @access private
355 * @return string
356 */
357 function _error_message()
358 {
359 return odbc_errormsg($this->conn_id);
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * The error message number
366 *
367 * @access private
368 * @return integer
369 */
370 function _error_number()
371 {
372 return odbc_error($this->conn_id);
373 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000374 // --------------------------------------------------------------------
375
376 /**
377 * Escape Column Name
378 *
379 * This function adds backticks around supplied column name
380 *
381 * @access private
382 * @param string the column name
383 * @return string
384 */
385 function _escape_column($column)
386 {
387 // Not necessary with ODBC so we simply return the value
388 return $column;
389 }
390
Derek Allard5c3905b2007-03-24 11:08:55 +0000391 // --------------------------------------------------------------------
392
393 /**
394 * Escape Table Name
395 *
396 * This function adds backticks if the table name has a period
397 * in it. Some DBs will get cranky unless periods are escaped
398 *
399 * @access private
400 * @param string the table name
401 * @return string
402 */
403 function _escape_table($table)
404 {
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000405 // Not necessary with ODBC so we simply return the value
Derek Allard5c3905b2007-03-24 11:08:55 +0000406 return $table;
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000407 }
Derek Allard5c3905b2007-03-24 11:08:55 +0000408
409 // --------------------------------------------------------------------
410
411 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000412 * Protect Identifiers
413 *
414 * This function adds backticks if appropriate based on db type
415 *
416 * @access private
417 * @param mixed the item to escape
418 * @param boolean only affect the first word
419 * @return mixed the item with backticks
420 */
421 function _protect_identifiers($item, $first_word_only = FALSE)
422 {
423 if (is_array($item))
424 {
425 $escaped_array = array();
426
427 foreach($item as $k=>$v)
428 {
429 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
430 }
431
432 return $escaped_array;
433 }
434
435 // This function may get "item1 item2" as a string, and so
436 // we may need "`item1` `item2`" and not "`item1 item2`"
Derek Allard61579382008-01-16 22:22:42 +0000437 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000438 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000439 if (strpos($item, '.') !== FALSE)
440 {
441 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
442 $table_name = substr($item, 0, strpos($item, '.')+1);
443 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
444 }
445
Derek Allard39b622d2008-01-16 21:10:09 +0000446 // This function may get "field >= 1", and need it to return "`field` >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000447 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000448
Derek Allard61579382008-01-16 22:22:42 +0000449 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
450 }
451 else
452 {
Derek Allard903cc982008-02-11 06:06:59 +0000453 return "{$item}";
Derek Allard39b622d2008-01-16 21:10:09 +0000454 }
455
Derek Allard9a4d1da2008-02-25 14:18:38 +0000456 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard39b622d2008-01-16 21:10:09 +0000457
458 foreach ($exceptions as $exception)
459 {
Derek Allard61579382008-01-16 22:22:42 +0000460
Derek Allard903cc982008-02-11 06:06:59 +0000461 if (stristr($item, " {$exception} ") !== FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000462 {
Derek Allard903cc982008-02-11 06:06:59 +0000463 $item = preg_replace('/ ('.preg_quote($exception).') /i', ' $1 ', $item);
Derek Allard39b622d2008-01-16 21:10:09 +0000464 }
465 }
Derek Allard39b622d2008-01-16 21:10:09 +0000466 return $item;
467 }
468
469 // --------------------------------------------------------------------
470
471 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000472 * From Tables
473 *
474 * This function implicitly groups FROM tables so there is no confusion
475 * about operator precedence in harmony with SQL standards
476 *
477 * @access public
478 * @param type
479 * @return type
480 */
481 function _from_tables($tables)
482 {
Derek Jones0b59f272008-05-13 04:22:33 +0000483 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000484 {
485 $tables = array($tables);
486 }
487
488 return '('.implode(', ', $tables).')';
489 }
490
491 // --------------------------------------------------------------------
492
493 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000494 * Insert statement
495 *
496 * Generates a platform-specific insert string from the supplied data
497 *
498 * @access public
499 * @param string the table name
500 * @param array the insert keys
501 * @param array the insert values
502 * @return string
503 */
504 function _insert($table, $keys, $values)
505 {
506 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
507 }
508
509 // --------------------------------------------------------------------
510
511 /**
512 * Update statement
513 *
514 * Generates a platform-specific update string from the supplied data
515 *
516 * @access public
517 * @param string the table name
518 * @param array the update data
519 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000520 * @param array the orderby clause
521 * @param array the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000522 * @return string
523 */
Derek Allard39b622d2008-01-16 21:10:09 +0000524 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000525 {
526 foreach($values as $key => $val)
527 {
528 $valstr[] = $key." = ".$val;
529 }
Derek Allardda6d2402007-12-19 14:49:29 +0000530
Derek Jones0b59f272008-05-13 04:22:33 +0000531 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000532
533 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allard5c3905b2007-03-24 11:08:55 +0000534
Derek Allard32cf7eb2008-02-05 16:03:50 +0000535 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
536 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
537 $sql .= $orderby.$limit;
538
539 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000540 }
541
542
543 // --------------------------------------------------------------------
544
545 /**
546 * Truncate statement
547 *
548 * Generates a platform-specific truncate string from the supplied data
549 * If the database does not support the truncate() command
550 * This function maps to "DELETE FROM table"
551 *
552 * @access public
553 * @param string the table name
554 * @return string
555 */
556 function _truncate($table)
557 {
558 return $this->_delete($table);
Derek Allard5c3905b2007-03-24 11:08:55 +0000559 }
560
561 // --------------------------------------------------------------------
562
563 /**
564 * Delete statement
565 *
566 * Generates a platform-specific delete string from the supplied data
567 *
568 * @access public
569 * @param string the table name
570 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000571 * @param string the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000572 * @return string
573 */
Derek Allard39b622d2008-01-16 21:10:09 +0000574 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000575 {
Derek Allard39b622d2008-01-16 21:10:09 +0000576 $conditions = '';
577
Derek Jones0b59f272008-05-13 04:22:33 +0000578 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000579 {
580 $conditions = "\nWHERE ";
581 $conditions .= implode("\n", $this->ar_where);
582
583 if (count($where) > 0 && count($like) > 0)
584 {
585 $conditions .= " AND ";
586 }
587 $conditions .= implode("\n", $like);
588 }
589
Derek Jones0b59f272008-05-13 04:22:33 +0000590 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000591
Derek Allard39b622d2008-01-16 21:10:09 +0000592 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allard5c3905b2007-03-24 11:08:55 +0000593 }
594
595 // --------------------------------------------------------------------
596
597 /**
598 * Limit string
599 *
600 * Generates a platform-specific LIMIT clause
601 *
602 * @access public
603 * @param string the sql query string
604 * @param integer the number of rows to limit the query to
605 * @param integer the offset value
606 * @return string
607 */
608 function _limit($sql, $limit, $offset)
609 {
610 // Does ODBC doesn't use the LIMIT clause?
611 return $sql;
612 }
613
614 // --------------------------------------------------------------------
615
616 /**
617 * Close DB Connection
618 *
619 * @access public
620 * @param resource
621 * @return void
622 */
623 function _close($conn_id)
624 {
625 @odbc_close($conn_id);
626 }
627
628
629}
630
631
Derek Jones0b59f272008-05-13 04:22:33 +0000632
633/* End of file odbc_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000634/* Location: ./system/database/drivers/odbc/odbc_driver.php */