blob: daecabe1339e8a53343cfb48c2b455f8fb8a501e [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 {
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 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 {
427 // This function may get "field >= 1", and need it to return "`field` >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000428 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000429
Derek Allard61579382008-01-16 22:22:42 +0000430 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
431 }
432 else
433 {
434 return "`{$item}`";
Derek Allard39b622d2008-01-16 21:10:09 +0000435 }
436
437 $exceptions = array('AS', '/', '-', '%', '+', '*');
438
439 foreach ($exceptions as $exception)
440 {
Derek Allard61579382008-01-16 22:22:42 +0000441
Derek Allard39b622d2008-01-16 21:10:09 +0000442 if (stristr($item, " `{$exception}` ") !== FALSE)
443 {
444 $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
445 }
446 }
Derek Allard39b622d2008-01-16 21:10:09 +0000447 return $item;
448 }
449
450 // --------------------------------------------------------------------
451
452 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000453 * Insert statement
454 *
455 * Generates a platform-specific insert string from the supplied data
456 *
457 * @access public
458 * @param string the table name
459 * @param array the insert keys
460 * @param array the insert values
461 * @return string
462 */
463 function _insert($table, $keys, $values)
464 {
465 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
466 }
467
468 // --------------------------------------------------------------------
469
470 /**
471 * Update statement
472 *
473 * Generates a platform-specific update string from the supplied data
474 *
475 * @access public
476 * @param string the table name
477 * @param array the update data
478 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000479 * @param array the orderby clause
480 * @param array the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000481 * @return string
482 */
Derek Allard39b622d2008-01-16 21:10:09 +0000483 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000484 {
485 foreach($values as $key => $val)
486 {
487 $valstr[] = $key." = ".$val;
488 }
Derek Allardda6d2402007-12-19 14:49:29 +0000489
490 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000491
492 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allard5c3905b2007-03-24 11:08:55 +0000493
Derek Allard39b622d2008-01-16 21:10:09 +0000494 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$orderby.$limit;
495 }
496
497
498 // --------------------------------------------------------------------
499
500 /**
501 * Truncate statement
502 *
503 * Generates a platform-specific truncate string from the supplied data
504 * If the database does not support the truncate() command
505 * This function maps to "DELETE FROM table"
506 *
507 * @access public
508 * @param string the table name
509 * @return string
510 */
511 function _truncate($table)
512 {
513 return $this->_delete($table);
Derek Allard5c3905b2007-03-24 11:08:55 +0000514 }
515
516 // --------------------------------------------------------------------
517
518 /**
519 * Delete statement
520 *
521 * Generates a platform-specific delete string from the supplied data
522 *
523 * @access public
524 * @param string the table name
525 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000526 * @param string the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000527 * @return string
528 */
Derek Allard39b622d2008-01-16 21:10:09 +0000529 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000530 {
Derek Allard39b622d2008-01-16 21:10:09 +0000531 $conditions = '';
532
533 if (count($where) > 0 || count($like) > 0)
534 {
535 $conditions = "\nWHERE ";
536 $conditions .= implode("\n", $this->ar_where);
537
538 if (count($where) > 0 && count($like) > 0)
539 {
540 $conditions .= " AND ";
541 }
542 $conditions .= implode("\n", $like);
543 }
544
Derek Allarde77d77c2007-12-19 15:01:55 +0000545 $limit = (!$limit) ? '' : ' LIMIT '.$limit;
546
Derek Allard39b622d2008-01-16 21:10:09 +0000547 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allard5c3905b2007-03-24 11:08:55 +0000548 }
549
550 // --------------------------------------------------------------------
551
552 /**
553 * Limit string
554 *
555 * Generates a platform-specific LIMIT clause
556 *
557 * @access public
558 * @param string the sql query string
559 * @param integer the number of rows to limit the query to
560 * @param integer the offset value
561 * @return string
562 */
563 function _limit($sql, $limit, $offset)
564 {
565 // Does ODBC doesn't use the LIMIT clause?
566 return $sql;
567 }
568
569 // --------------------------------------------------------------------
570
571 /**
572 * Close DB Connection
573 *
574 * @access public
575 * @param resource
576 * @return void
577 */
578 function _close($conn_id)
579 {
580 @odbc_close($conn_id);
581 }
582
583
584}
585
586
admin0d011692006-09-24 18:12:58 +0000587?>