blob: dd10fbdd33e9714a9275d73efe5427649814cef1 [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 {
155 if ( ! $this->trans_enabled)
156 {
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 {
184 if ( ! $this->trans_enabled)
185 {
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 {
210 if ( ! $this->trans_enabled)
211 {
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 {
389 if (stristr($table, '.'))
390 {
391 $table = preg_replace("/\./", "`.`", $table);
392 }
393
394 return $table;
395 }
396
397 // --------------------------------------------------------------------
398
399 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000400 * Protect Identifiers
401 *
402 * This function adds backticks if appropriate based on db type
403 *
404 * @access private
405 * @param mixed the item to escape
406 * @param boolean only affect the first word
407 * @return mixed the item with backticks
408 */
409 function _protect_identifiers($item, $first_word_only = FALSE)
410 {
411 if (is_array($item))
412 {
413 $escaped_array = array();
414
415 foreach($item as $k=>$v)
416 {
417 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
418 }
419
420 return $escaped_array;
421 }
422
423 // This function may get "item1 item2" as a string, and so
424 // we may need "`item1` `item2`" and not "`item1 item2`"
Derek Allard61579382008-01-16 22:22:42 +0000425 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000426 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000427 if (strpos($item, '.') !== FALSE)
428 {
429 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
430 $table_name = substr($item, 0, strpos($item, '.')+1);
431 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
432 }
433
Derek Allard39b622d2008-01-16 21:10:09 +0000434 // This function may get "field >= 1", and need it to return "`field` >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000435 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000436
Derek Allard61579382008-01-16 22:22:42 +0000437 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
438 }
439 else
440 {
441 return "`{$item}`";
Derek Allard39b622d2008-01-16 21:10:09 +0000442 }
443
444 $exceptions = array('AS', '/', '-', '%', '+', '*');
445
446 foreach ($exceptions as $exception)
447 {
Derek Allard61579382008-01-16 22:22:42 +0000448
Derek Allard39b622d2008-01-16 21:10:09 +0000449 if (stristr($item, " `{$exception}` ") !== FALSE)
450 {
451 $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
452 }
453 }
Derek Allard39b622d2008-01-16 21:10:09 +0000454 return $item;
455 }
456
457 // --------------------------------------------------------------------
458
459 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000460 * From Tables
461 *
462 * This function implicitly groups FROM tables so there is no confusion
463 * about operator precedence in harmony with SQL standards
464 *
465 * @access public
466 * @param type
467 * @return type
468 */
469 function _from_tables($tables)
470 {
471 if (! is_array($tables))
472 {
473 $tables = array($tables);
474 }
475
476 return '('.implode(', ', $tables).')';
477 }
478
479 // --------------------------------------------------------------------
480
481 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000482 * Insert statement
483 *
484 * Generates a platform-specific insert string from the supplied data
485 *
486 * @access public
487 * @param string the table name
488 * @param array the insert keys
489 * @param array the insert values
490 * @return string
491 */
492 function _insert($table, $keys, $values)
493 {
494 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
495 }
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Update statement
501 *
502 * Generates a platform-specific update string from the supplied data
503 *
504 * @access public
505 * @param string the table name
506 * @param array the update data
507 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000508 * @param array the orderby clause
509 * @param array the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000510 * @return string
511 */
Derek Allard39b622d2008-01-16 21:10:09 +0000512 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000513 {
514 foreach($values as $key => $val)
515 {
516 $valstr[] = $key." = ".$val;
517 }
Derek Allardda6d2402007-12-19 14:49:29 +0000518
519 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000520
521 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allard5c3905b2007-03-24 11:08:55 +0000522
Derek Allard32cf7eb2008-02-05 16:03:50 +0000523 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
524 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
525 $sql .= $orderby.$limit;
526
527 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000528 }
529
530
531 // --------------------------------------------------------------------
532
533 /**
534 * Truncate statement
535 *
536 * Generates a platform-specific truncate string from the supplied data
537 * If the database does not support the truncate() command
538 * This function maps to "DELETE FROM table"
539 *
540 * @access public
541 * @param string the table name
542 * @return string
543 */
544 function _truncate($table)
545 {
546 return $this->_delete($table);
Derek Allard5c3905b2007-03-24 11:08:55 +0000547 }
548
549 // --------------------------------------------------------------------
550
551 /**
552 * Delete statement
553 *
554 * Generates a platform-specific delete string from the supplied data
555 *
556 * @access public
557 * @param string the table name
558 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000559 * @param string the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000560 * @return string
561 */
Derek Allard39b622d2008-01-16 21:10:09 +0000562 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000563 {
Derek Allard39b622d2008-01-16 21:10:09 +0000564 $conditions = '';
565
566 if (count($where) > 0 || count($like) > 0)
567 {
568 $conditions = "\nWHERE ";
569 $conditions .= implode("\n", $this->ar_where);
570
571 if (count($where) > 0 && count($like) > 0)
572 {
573 $conditions .= " AND ";
574 }
575 $conditions .= implode("\n", $like);
576 }
577
Derek Allarde77d77c2007-12-19 15:01:55 +0000578 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
579
Derek Allard39b622d2008-01-16 21:10:09 +0000580 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allard5c3905b2007-03-24 11:08:55 +0000581 }
582
583 // --------------------------------------------------------------------
584
585 /**
586 * Limit string
587 *
588 * Generates a platform-specific LIMIT clause
589 *
590 * @access public
591 * @param string the sql query string
592 * @param integer the number of rows to limit the query to
593 * @param integer the offset value
594 * @return string
595 */
596 function _limit($sql, $limit, $offset)
597 {
598 // Does ODBC doesn't use the LIMIT clause?
599 return $sql;
600 }
601
602 // --------------------------------------------------------------------
603
604 /**
605 * Close DB Connection
606 *
607 * @access public
608 * @param resource
609 * @return void
610 */
611 function _close($conn_id)
612 {
613 @odbc_close($conn_id);
614 }
615
616
617}
618
619
admin0d011692006-09-24 18:12:58 +0000620?>