blob: 93a1d4e05a264ee5551417e093c139ee897b9f65 [file] [log] [blame]
Derek Allard5c3905b2007-03-24 11:08:55 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, 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 Allard73274992008-05-05 16:39:18 +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 Allard73274992008-05-05 16:39:18 +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 Allard73274992008-05-05 16:39:18 +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
238 return $str;
239 }
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 }
374
375 // --------------------------------------------------------------------
376
377 /**
378 * Escape Table Name
379 *
380 * This function adds backticks if the table name has a period
381 * in it. Some DBs will get cranky unless periods are escaped
382 *
383 * @access private
384 * @param string the table name
385 * @return string
386 */
387 function _escape_table($table)
388 {
Derek Allard903cc982008-02-11 06:06:59 +0000389 // used to add backticks in other db drivers
Derek Allard5c3905b2007-03-24 11:08:55 +0000390 return $table;
391 }
392
393 // --------------------------------------------------------------------
394
395 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000396 * Protect Identifiers
397 *
398 * This function adds backticks if appropriate based on db type
399 *
400 * @access private
401 * @param mixed the item to escape
402 * @param boolean only affect the first word
403 * @return mixed the item with backticks
404 */
405 function _protect_identifiers($item, $first_word_only = FALSE)
406 {
407 if (is_array($item))
408 {
409 $escaped_array = array();
410
411 foreach($item as $k=>$v)
412 {
413 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
414 }
415
416 return $escaped_array;
417 }
418
419 // This function may get "item1 item2" as a string, and so
420 // we may need "`item1` `item2`" and not "`item1 item2`"
Derek Allard61579382008-01-16 22:22:42 +0000421 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000422 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000423 if (strpos($item, '.') !== FALSE)
424 {
425 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
426 $table_name = substr($item, 0, strpos($item, '.')+1);
427 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
428 }
429
Derek Allard39b622d2008-01-16 21:10:09 +0000430 // This function may get "field >= 1", and need it to return "`field` >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000431 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000432
Derek Allard61579382008-01-16 22:22:42 +0000433 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
434 }
435 else
436 {
Derek Allard903cc982008-02-11 06:06:59 +0000437 return "{$item}";
Derek Allard39b622d2008-01-16 21:10:09 +0000438 }
439
Derek Allard9a4d1da2008-02-25 14:18:38 +0000440 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard39b622d2008-01-16 21:10:09 +0000441
442 foreach ($exceptions as $exception)
443 {
Derek Allard61579382008-01-16 22:22:42 +0000444
Derek Allard903cc982008-02-11 06:06:59 +0000445 if (stristr($item, " {$exception} ") !== FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000446 {
Derek Allard903cc982008-02-11 06:06:59 +0000447 $item = preg_replace('/ ('.preg_quote($exception).') /i', ' $1 ', $item);
Derek Allard39b622d2008-01-16 21:10:09 +0000448 }
449 }
Derek Allard39b622d2008-01-16 21:10:09 +0000450 return $item;
451 }
452
453 // --------------------------------------------------------------------
454
455 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000456 * From Tables
457 *
458 * This function implicitly groups FROM tables so there is no confusion
459 * about operator precedence in harmony with SQL standards
460 *
461 * @access public
462 * @param type
463 * @return type
464 */
465 function _from_tables($tables)
466 {
467 if (! is_array($tables))
468 {
469 $tables = array($tables);
470 }
471
472 return '('.implode(', ', $tables).')';
473 }
474
475 // --------------------------------------------------------------------
476
477 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000478 * Insert statement
479 *
480 * Generates a platform-specific insert string from the supplied data
481 *
482 * @access public
483 * @param string the table name
484 * @param array the insert keys
485 * @param array the insert values
486 * @return string
487 */
488 function _insert($table, $keys, $values)
489 {
490 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
491 }
492
493 // --------------------------------------------------------------------
494
495 /**
496 * Update statement
497 *
498 * Generates a platform-specific update string from the supplied data
499 *
500 * @access public
501 * @param string the table name
502 * @param array the update data
503 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000504 * @param array the orderby clause
505 * @param array the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000506 * @return string
507 */
Derek Allard39b622d2008-01-16 21:10:09 +0000508 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000509 {
510 foreach($values as $key => $val)
511 {
512 $valstr[] = $key." = ".$val;
513 }
Derek Allardda6d2402007-12-19 14:49:29 +0000514
515 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000516
517 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allard5c3905b2007-03-24 11:08:55 +0000518
Derek Allard32cf7eb2008-02-05 16:03:50 +0000519 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
520 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
521 $sql .= $orderby.$limit;
522
523 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000524 }
525
526
527 // --------------------------------------------------------------------
528
529 /**
530 * Truncate statement
531 *
532 * Generates a platform-specific truncate string from the supplied data
533 * If the database does not support the truncate() command
534 * This function maps to "DELETE FROM table"
535 *
536 * @access public
537 * @param string the table name
538 * @return string
539 */
540 function _truncate($table)
541 {
542 return $this->_delete($table);
Derek Allard5c3905b2007-03-24 11:08:55 +0000543 }
544
545 // --------------------------------------------------------------------
546
547 /**
548 * Delete statement
549 *
550 * Generates a platform-specific delete string from the supplied data
551 *
552 * @access public
553 * @param string the table name
554 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000555 * @param string the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000556 * @return string
557 */
Derek Allard39b622d2008-01-16 21:10:09 +0000558 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000559 {
Derek Allard39b622d2008-01-16 21:10:09 +0000560 $conditions = '';
561
562 if (count($where) > 0 || count($like) > 0)
563 {
564 $conditions = "\nWHERE ";
565 $conditions .= implode("\n", $this->ar_where);
566
567 if (count($where) > 0 && count($like) > 0)
568 {
569 $conditions .= " AND ";
570 }
571 $conditions .= implode("\n", $like);
572 }
573
Derek Allarde77d77c2007-12-19 15:01:55 +0000574 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
575
Derek Allard39b622d2008-01-16 21:10:09 +0000576 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allard5c3905b2007-03-24 11:08:55 +0000577 }
578
579 // --------------------------------------------------------------------
580
581 /**
582 * Limit string
583 *
584 * Generates a platform-specific LIMIT clause
585 *
586 * @access public
587 * @param string the sql query string
588 * @param integer the number of rows to limit the query to
589 * @param integer the offset value
590 * @return string
591 */
592 function _limit($sql, $limit, $offset)
593 {
594 // Does ODBC doesn't use the LIMIT clause?
595 return $sql;
596 }
597
598 // --------------------------------------------------------------------
599
600 /**
601 * Close DB Connection
602 *
603 * @access public
604 * @param resource
605 * @return void
606 */
607 function _close($conn_id)
608 {
609 @odbc_close($conn_id);
610 }
611
612
613}
614
615