blob: 50412685de5bc097a3d7f7a8c1c14ccbf07f15c0 [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
Rick Ellis5aa8c602008-10-07 01:24:07 +000033 var $dbdriver = 'odbc';
Rick Ellis605a07a2008-10-17 04:07:54 +000034
35 // the character used to excape - not necessary for ODBC
36 var $_escape_char = '';
Rick Ellis5aa8c602008-10-07 01:24:07 +000037
Derek Allard5c3905b2007-03-24 11:08:55 +000038 /**
Derek Allard694b5b82007-12-18 15:58:03 +000039 * The syntax to count rows is slightly different across different
40 * database engines, so this string appears in each driver and is
41 * used for the count_all() and count_all_results() functions.
42 */
Derek Allard39b622d2008-01-16 21:10:09 +000043 var $_count_string = "SELECT COUNT(*) AS ";
44 var $_random_keyword;
45
46
Rick Ellisceb6f0b2008-10-07 00:48:19 +000047 function CI_DB_odbc_driver($params)
Derek Allard39b622d2008-01-16 21:10:09 +000048 {
Rick Ellisceb6f0b2008-10-07 00:48:19 +000049 if (is_array($params))
50 {
51 foreach ($params as $key => $val)
52 {
53 $this->$key = $val;
54 }
55 }
56
Derek Allardb52bdc42008-01-27 14:58:24 +000057 $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
Derek Allard39b622d2008-01-16 21:10:09 +000058 }
Derek Allard694b5b82007-12-18 15:58:03 +000059
60 /**
Derek Allard5c3905b2007-03-24 11:08:55 +000061 * Non-persistent database connection
62 *
63 * @access private called by the base class
64 * @return resource
65 */
66 function db_connect()
67 {
68 return @odbc_connect($this->hostname, $this->username, $this->password);
69 }
70
71 // --------------------------------------------------------------------
72
73 /**
74 * Persistent database connection
75 *
76 * @access private called by the base class
77 * @return resource
78 */
79 function db_pconnect()
80 {
81 return @odbc_pconnect($this->hostname, $this->username, $this->password);
82 }
83
84 // --------------------------------------------------------------------
85
86 /**
87 * Select the database
88 *
89 * @access private called by the base class
90 * @return resource
91 */
92 function db_select()
93 {
94 // Not needed for ODBC
95 return TRUE;
96 }
97
98 // --------------------------------------------------------------------
99
100 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000101 * Set client character set
102 *
103 * @access public
104 * @param string
105 * @param string
106 * @return resource
107 */
108 function db_set_charset($charset, $collation)
109 {
Rick Ellisff734012008-09-30 20:38:12 +0000110 // @todo - add support if needed
Derek Allard39b622d2008-01-16 21:10:09 +0000111 return TRUE;
112 }
113
114 // --------------------------------------------------------------------
115
116 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000117 * Version number query string
118 *
119 * @access public
120 * @return string
121 */
122 function _version()
123 {
124 return "SELECT version() AS ver";
125 }
126
127 // --------------------------------------------------------------------
128
129 /**
130 * Execute the query
131 *
132 * @access private called by the base class
133 * @param string an SQL query
134 * @return resource
135 */
136 function _execute($sql)
137 {
138 $sql = $this->_prep_query($sql);
139 return @odbc_exec($this->conn_id, $sql);
140 }
141
142 // --------------------------------------------------------------------
143
144 /**
145 * Prep the query
146 *
147 * If needed, each database adapter can prep the query string
148 *
149 * @access private called by execute()
150 * @param string an SQL query
151 * @return string
152 */
153 function _prep_query($sql)
154 {
155 return $sql;
156 }
157
158 // --------------------------------------------------------------------
159
160 /**
161 * Begin Transaction
162 *
163 * @access public
164 * @return bool
165 */
166 function trans_begin($test_mode = FALSE)
167 {
Derek Jones0b59f272008-05-13 04:22:33 +0000168 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000169 {
170 return TRUE;
171 }
172
173 // When transactions are nested we only begin/commit/rollback the outermost ones
174 if ($this->_trans_depth > 0)
175 {
176 return TRUE;
177 }
178
179 // Reset the transaction failure flag.
180 // If the $test_mode flag is set to TRUE transactions will be rolled back
181 // even if the queries produce a successful result.
182 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
183
184 return odbc_autocommit($this->conn_id, FALSE);
185 }
186
187 // --------------------------------------------------------------------
188
189 /**
190 * Commit Transaction
191 *
192 * @access public
193 * @return bool
194 */
195 function trans_commit()
196 {
Derek Jones0b59f272008-05-13 04:22:33 +0000197 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000198 {
199 return TRUE;
200 }
201
202 // When transactions are nested we only begin/commit/rollback the outermost ones
203 if ($this->_trans_depth > 0)
204 {
205 return TRUE;
206 }
207
208 $ret = odbc_commit($this->conn_id);
209 odbc_autocommit($this->conn_id, TRUE);
210 return $ret;
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Rollback Transaction
217 *
218 * @access public
219 * @return bool
220 */
221 function trans_rollback()
222 {
Derek Jones0b59f272008-05-13 04:22:33 +0000223 if ( ! $this->trans_enabled)
Derek Allard5c3905b2007-03-24 11:08:55 +0000224 {
225 return TRUE;
226 }
227
228 // When transactions are nested we only begin/commit/rollback the outermost ones
229 if ($this->_trans_depth > 0)
230 {
231 return TRUE;
232 }
233
234 $ret = odbc_rollback($this->conn_id);
235 odbc_autocommit($this->conn_id, TRUE);
236 return $ret;
237 }
238
239 // --------------------------------------------------------------------
240
241 /**
242 * Escape String
243 *
244 * @access public
245 * @param string
246 * @return string
247 */
248 function escape_str($str)
249 {
Rick Ellis06a2e742008-10-07 01:04:15 +0000250 // Access the CI object
Rick Ellisca86a7c2008-10-07 01:16:57 +0000251 $CI =& get_instance();
Rick Ellis06a2e742008-10-07 01:04:15 +0000252
Derek Allard5c3905b2007-03-24 11:08:55 +0000253 // ODBC doesn't require escaping
Rick Ellis06a2e742008-10-07 01:04:15 +0000254 return $CI->_remove_invisible_characters($str);
Derek Allard5c3905b2007-03-24 11:08:55 +0000255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Affected Rows
261 *
262 * @access public
263 * @return integer
264 */
265 function affected_rows()
266 {
267 return @odbc_num_rows($this->conn_id);
268 }
269
270 // --------------------------------------------------------------------
271
272 /**
273 * Insert ID
274 *
275 * @access public
276 * @return integer
277 */
278 function insert_id()
279 {
280 return @odbc_insert_id($this->conn_id);
281 }
282
283 // --------------------------------------------------------------------
284
285 /**
286 * "Count All" query
287 *
288 * Generates a platform-specific query string that counts all records in
289 * the specified database
290 *
291 * @access public
292 * @param string
293 * @return string
294 */
295 function count_all($table = '')
296 {
297 if ($table == '')
298 return '0';
299
Rick Ellis605a07a2008-10-17 04:07:54 +0000300 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
Derek Allardf6cd45c2008-01-18 14:31:51 +0000301
Derek Allard5c3905b2007-03-24 11:08:55 +0000302 if ($query->num_rows() == 0)
303 return '0';
304
305 $row = $query->row();
306 return $row->numrows;
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Show table query
313 *
314 * Generates a platform-specific query string so that the table names can be fetched
315 *
316 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000317 * @param boolean
Derek Allard5c3905b2007-03-24 11:08:55 +0000318 * @return string
319 */
Derek Allard39b622d2008-01-16 21:10:09 +0000320 function _list_tables($prefix_limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000321 {
Derek Allard39b622d2008-01-16 21:10:09 +0000322 $sql = "SHOW TABLES FROM `".$this->database."`";
323
324 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
325 {
326 //$sql .= " LIKE '".$this->dbprefix."%'";
327 return FALSE; // not currently supported
328 }
329
330 return $sql;
Derek Allard5c3905b2007-03-24 11:08:55 +0000331 }
332
333 // --------------------------------------------------------------------
334
335 /**
336 * Show column query
337 *
338 * Generates a platform-specific query string so that the column names can be fetched
339 *
340 * @access public
341 * @param string the table name
342 * @return string
343 */
344 function _list_columns($table = '')
345 {
Rick Ellis605a07a2008-10-17 04:07:54 +0000346 return "SHOW COLUMNS FROM ".$table;
Derek Allard5c3905b2007-03-24 11:08:55 +0000347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * Field data query
353 *
354 * Generates a platform-specific query so that the column data can be retrieved
355 *
356 * @access public
357 * @param string the table name
358 * @return object
359 */
360 function _field_data($table)
361 {
Rick Ellis605a07a2008-10-17 04:07:54 +0000362 return "SELECT TOP 1 FROM ".$table;
Derek Allard5c3905b2007-03-24 11:08:55 +0000363 }
364
365 // --------------------------------------------------------------------
366
367 /**
368 * The error message string
369 *
370 * @access private
371 * @return string
372 */
373 function _error_message()
374 {
375 return odbc_errormsg($this->conn_id);
376 }
377
378 // --------------------------------------------------------------------
379
380 /**
381 * The error message number
382 *
383 * @access private
384 * @return integer
385 */
386 function _error_number()
387 {
388 return odbc_error($this->conn_id);
389 }
Rick Ellis605a07a2008-10-17 04:07:54 +0000390
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000391 // --------------------------------------------------------------------
392
393 /**
Rick Ellis605a07a2008-10-17 04:07:54 +0000394 * Escape the SQL Identifiers
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000395 *
Rick Ellis605a07a2008-10-17 04:07:54 +0000396 * This function escapes column and table names
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000397 *
398 * @access private
Rick Ellis605a07a2008-10-17 04:07:54 +0000399 * @param string
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000400 * @return string
401 */
Rick Ellis605a07a2008-10-17 04:07:54 +0000402 function _escape_identifiers($item)
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000403 {
Rick Ellis605a07a2008-10-17 04:07:54 +0000404 if ($this->_escape_char == '')
Derek Allard39b622d2008-01-16 21:10:09 +0000405 {
Rick Ellis605a07a2008-10-17 04:07:54 +0000406 return $item;
407 }
408
409 if (strpos($item, '.') !== FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000410 {
Rick Ellis605a07a2008-10-17 04:07:54 +0000411 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard61579382008-01-16 22:22:42 +0000412 }
413 else
414 {
Rick Ellis605a07a2008-10-17 04:07:54 +0000415 $str = $this->_escape_char.$item.$this->_escape_char;
Derek Allard39b622d2008-01-16 21:10:09 +0000416 }
Derek Allard39b622d2008-01-16 21:10:09 +0000417
Rick Ellis605a07a2008-10-17 04:07:54 +0000418 // remove duplicates if the user already included the escape
419 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Derek Allard39b622d2008-01-16 21:10:09 +0000420 }
421
422 // --------------------------------------------------------------------
423
424 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000425 * From Tables
426 *
427 * This function implicitly groups FROM tables so there is no confusion
428 * about operator precedence in harmony with SQL standards
429 *
430 * @access public
431 * @param type
432 * @return type
433 */
434 function _from_tables($tables)
435 {
Derek Jones0b59f272008-05-13 04:22:33 +0000436 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000437 {
438 $tables = array($tables);
439 }
440
441 return '('.implode(', ', $tables).')';
442 }
443
444 // --------------------------------------------------------------------
445
446 /**
Derek Allard5c3905b2007-03-24 11:08:55 +0000447 * Insert statement
448 *
449 * Generates a platform-specific insert string from the supplied data
450 *
451 * @access public
452 * @param string the table name
453 * @param array the insert keys
454 * @param array the insert values
455 * @return string
456 */
457 function _insert($table, $keys, $values)
458 {
Rick Ellis605a07a2008-10-17 04:07:54 +0000459 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard5c3905b2007-03-24 11:08:55 +0000460 }
461
462 // --------------------------------------------------------------------
463
464 /**
465 * Update statement
466 *
467 * Generates a platform-specific update string from the supplied data
468 *
469 * @access public
470 * @param string the table name
471 * @param array the update data
472 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000473 * @param array the orderby clause
474 * @param array the limit clause
Derek Allard5c3905b2007-03-24 11:08:55 +0000475 * @return string
476 */
Derek Allard39b622d2008-01-16 21:10:09 +0000477 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allard5c3905b2007-03-24 11:08:55 +0000478 {
479 foreach($values as $key => $val)
480 {
481 $valstr[] = $key." = ".$val;
482 }
Derek Allardda6d2402007-12-19 14:49:29 +0000483
Derek Jones0b59f272008-05-13 04:22:33 +0000484 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000485
486 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allard5c3905b2007-03-24 11:08:55 +0000487
Rick Ellis605a07a2008-10-17 04:07:54 +0000488 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
489
Derek Allard32cf7eb2008-02-05 16:03:50 +0000490 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
Rick Ellis605a07a2008-10-17 04:07:54 +0000491
Derek Allard32cf7eb2008-02-05 16:03:50 +0000492 $sql .= $orderby.$limit;
493
494 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000495 }
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
Derek Jones0b59f272008-05-13 04:22:33 +0000533 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000534 {
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 Jones0b59f272008-05-13 04:22:33 +0000545 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000546
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
Derek Jones0b59f272008-05-13 04:22:33 +0000587
588/* End of file odbc_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000589/* Location: ./system/database/drivers/odbc/odbc_driver.php */