blob: 0bdfc41d5645ea24cc1bacdc61c6a025b1db32ed [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
8 * @author Rick Ellis
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Allard6838f002007-10-04 19:29:59 +000010 * @license http://www.codeigniter.com/user_guide/license.html
Derek Allard5c3905b2007-03-24 11:08:55 +000011 * @link http://www.codeigniter.com
12 * @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
28 * @author Rick Ellis
29 * @link http://www.codeigniter.com/user_guide/database/
30 */
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 {
44 $_random_keyword = ' RND('.time().')'; // database specific random keyword
45 }
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 Allard6ddb5a12007-12-18 17:22:50 +0000284 $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
Derek Allard5c3905b2007-03-24 11:08:55 +0000285
286 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`"
425 if (strpos($item, ' ') !== FALSE)
426 {
427 // This function may get "field >= 1", and need it to return "`field` >= 1"
428 if ($first_word_only === TRUE)
429 {
430 return '`'.preg_replace('/ /', '` ', $item, 1);
431 }
432
433 $item = preg_replace('/(^|\s|\()([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
434 }
435
436 $exceptions = array('AS', '/', '-', '%', '+', '*');
437
438 foreach ($exceptions as $exception)
439 {
440 if (stristr($item, " `{$exception}` ") !== FALSE)
441 {
442 $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
443 }
444 }
445
446 return $item;
447 }
448
449 // --------------------------------------------------------------------
450
451 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000452 * Insert statement
453 *
454 * Generates a platform-specific insert string from the supplied data
455 *
456 * @access public
457 * @param string the table name
458 * @param array the insert keys
459 * @param array the insert values
460 * @return string
461 */
462 function _insert($table, $keys, $values)
463 {
464 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
465 }
466
467 // --------------------------------------------------------------------
468
469 /**
470 * Update statement
471 *
472 * Generates a platform-specific update string from the supplied data
473 *
474 * @access public
475 * @param string the table name
476 * @param array the update data
477 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000478 * @param array the orderby clause
479 * @param array the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000480 * @return string
481 */
Derek Allard39b622d2008-01-16 21:10:09 +0000482 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000483 {
484 foreach($values as $key => $val)
485 {
486 $valstr[] = $key." = ".$val;
487 }
Derek Allardda6d2402007-12-19 14:49:29 +0000488
489 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000490
491 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allard5c3905b2007-03-24 11:08:55 +0000492
Derek Allard39b622d2008-01-16 21:10:09 +0000493 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
494 }
495
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Truncate statement
501 *
502 * Generates a platform-specific truncate string from the supplied data
503 * If the database does not support the truncate() command
504 * This function maps to "DELETE FROM table"
505 *
506 * @access public
507 * @param string the table name
508 * @return string
509 */
510 function _truncate($table)
511 {
512 return $this->_delete($table);
Derek Allard5c3905b2007-03-24 11:08:55 +0000513 }
514
515 // --------------------------------------------------------------------
516
517 /**
518 * Delete statement
519 *
520 * Generates a platform-specific delete string from the supplied data
521 *
522 * @access public
523 * @param string the table name
524 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000525 * @param string the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000526 * @return string
527 */
Derek Allard39b622d2008-01-16 21:10:09 +0000528 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000529 {
Derek Allard39b622d2008-01-16 21:10:09 +0000530 $conditions = '';
531
532 if (count($where) > 0 || count($like) > 0)
533 {
534 $conditions = "\nWHERE ";
535 $conditions .= implode("\n", $this->ar_where);
536
537 if (count($where) > 0 && count($like) > 0)
538 {
539 $conditions .= " AND ";
540 }
541 $conditions .= implode("\n", $like);
542 }
543
Derek Allarde77d77c2007-12-19 15:01:55 +0000544 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
545
Derek Allard39b622d2008-01-16 21:10:09 +0000546 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allard5c3905b2007-03-24 11:08:55 +0000547 }
548
549 // --------------------------------------------------------------------
550
551 /**
552 * Limit string
553 *
554 * Generates a platform-specific LIMIT clause
555 *
556 * @access public
557 * @param string the sql query string
558 * @param integer the number of rows to limit the query to
559 * @param integer the offset value
560 * @return string
561 */
562 function _limit($sql, $limit, $offset)
563 {
564 // Does ODBC doesn't use the LIMIT clause?
565 return $sql;
566 }
567
568 // --------------------------------------------------------------------
569
570 /**
571 * Close DB Connection
572 *
573 * @access public
574 * @param resource
575 * @return void
576 */
577 function _close($conn_id)
578 {
579 @odbc_close($conn_id);
580 }
581
582
583}
584
585
admin0d011692006-09-24 18:12:58 +0000586?>