blob: 09ca07ee4607709007b67a931cda9443f32370f5 [file] [log] [blame]
admin0d011692006-09-24 18:12:58 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
admin0d011692006-09-24 18:12:58 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
admin0d011692006-09-24 18:12:58 +000016// ------------------------------------------------------------------------
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.
admine334c472006-10-21 19:44:22 +000024 *
admin0d011692006-09-24 18:12:58 +000025 * @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 /**
34 * Non-persistent database connection
35 *
36 * @access private called by the base class
37 * @return resource
38 */
39 function db_connect()
40 {
41 return odbc_connect($this->database, $this->username, $this->password);
42 }
43
44 // --------------------------------------------------------------------
45
46 /**
47 * Persistent database connection
48 *
49 * @access private called by the base class
50 * @return resource
51 */
52 function db_pconnect()
53 {
54 return odbc_pconnect($this->database, $this->username, $this->password);
55 }
56
57 // --------------------------------------------------------------------
58
59 /**
60 * Select the database
61 *
62 * @access private called by the base class
63 * @return resource
64 */
65 function db_select()
66 {
67 // Not needed for ODBC
68 return TRUE;
69 }
admin9cd4e8e2006-09-25 23:26:25 +000070
71 // --------------------------------------------------------------------
72
73 /**
74 * Version number query string
75 *
76 * @access public
77 * @return string
78 */
79 function _version()
80 {
81 return "SELECT version() AS ver";
82 }
83
admin0d011692006-09-24 18:12:58 +000084 // --------------------------------------------------------------------
85
86 /**
87 * Execute the query
88 *
89 * @access private called by the base class
90 * @param string an SQL query
91 * @return resource
92 */
93 function _execute($sql)
94 {
95 $sql = $this->_prep_query($sql);
96 return @odbc_exec($this->conn_id, $sql);
97 }
98
99 // --------------------------------------------------------------------
100
101 /**
102 * Prep the query
103 *
104 * If needed, each database adapter can prep the query string
105 *
106 * @access private called by execute()
107 * @param string an SQL query
108 * @return string
109 */
admine334c472006-10-21 19:44:22 +0000110 function _prep_query($sql)
111 {
admin0d011692006-09-24 18:12:58 +0000112 return $sql;
admine334c472006-10-21 19:44:22 +0000113 }
admin0d011692006-09-24 18:12:58 +0000114
115 // --------------------------------------------------------------------
116
117 /**
118 * Begin Transaction
admine334c472006-10-21 19:44:22 +0000119 *
admin0d011692006-09-24 18:12:58 +0000120 * @access public
admine334c472006-10-21 19:44:22 +0000121 * @return bool
admin0d011692006-09-24 18:12:58 +0000122 */
123 function trans_begin($test_mode = FALSE)
124 {
125 if ( ! $this->trans_enabled)
126 {
127 return TRUE;
128 }
129
130 // When transactions are nested we only begin/commit/rollback the outermost ones
131 if ($this->_trans_depth > 0)
132 {
133 return TRUE;
134 }
135
136 // Reset the transaction failure flag.
admine334c472006-10-21 19:44:22 +0000137 // If the $test_mode flag is set to TRUE transactions will be rolled back
138 // even if the queries produce a successful result.
admin0d011692006-09-24 18:12:58 +0000139 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
140
141 return odbc_autocommit($this->conn_id, FALSE);
142 }
143
144 // --------------------------------------------------------------------
145
146 /**
147 * Commit Transaction
admine334c472006-10-21 19:44:22 +0000148 *
admin0d011692006-09-24 18:12:58 +0000149 * @access public
admine334c472006-10-21 19:44:22 +0000150 * @return bool
admin0d011692006-09-24 18:12:58 +0000151 */
152 function trans_commit()
153 {
154 if ( ! $this->trans_enabled)
155 {
156 return TRUE;
157 }
158
159 // When transactions are nested we only begin/commit/rollback the outermost ones
160 if ($this->_trans_depth > 0)
161 {
162 return TRUE;
163 }
164
165 $ret = odbc_commit($this->conn_id);
166 odbc_autocommit($this->conn_id, TRUE);
167 return $ret;
168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Rollback Transaction
admine334c472006-10-21 19:44:22 +0000174 *
admin0d011692006-09-24 18:12:58 +0000175 * @access public
admine334c472006-10-21 19:44:22 +0000176 * @return bool
admin0d011692006-09-24 18:12:58 +0000177 */
178 function trans_rollback()
179 {
180 if ( ! $this->trans_enabled)
181 {
182 return TRUE;
183 }
184
185 // When transactions are nested we only begin/commit/rollback the outermost ones
186 if ($this->_trans_depth > 0)
187 {
188 return TRUE;
189 }
190
191 $ret = odbc_rollback($this->conn_id);
192 odbc_autocommit($this->conn_id, TRUE);
193 return $ret;
194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Escape String
200 *
201 * @access public
202 * @param string
203 * @return string
204 */
205 function escape_str($str)
206 {
207 // ODBC doesn't require escaping
208 return $str;
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Affected Rows
215 *
216 * @access public
217 * @return integer
218 */
219 function affected_rows()
220 {
221 return @odbc_num_rows($this->conn_id);
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * Insert ID
228 *
229 * @access public
230 * @return integer
231 */
232 function insert_id()
233 {
234 return @odbc_insert_id($this->conn_id);
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * "Count All" query
241 *
242 * Generates a platform-specific query string that counts all records in
243 * the specified database
244 *
245 * @access public
246 * @param string
247 * @return string
248 */
249 function count_all($table = '')
250 {
251 if ($table == '')
252 return '0';
253
254 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
255
256 if ($query->num_rows() == 0)
257 return '0';
258
259 $row = $query->row();
260 return $row->numrows;
261 }
admin3dd978f2006-09-30 19:24:45 +0000262
263 // --------------------------------------------------------------------
264
265 /**
admin3dd978f2006-09-30 19:24:45 +0000266 * Show table query
267 *
268 * Generates a platform-specific query string so that the table names can be fetched
269 *
270 * @access private
271 * @return string
272 */
273 function _list_tables()
274 {
275 return "SHOW TABLES FROM `".$this->database."`";
276 }
admin0d011692006-09-24 18:12:58 +0000277
278 // --------------------------------------------------------------------
279
280 /**
adminfafe28b2006-10-21 19:08:17 +0000281 * Show column query
admin9cd4e8e2006-09-25 23:26:25 +0000282 *
283 * Generates a platform-specific query string so that the column names can be fetched
284 *
285 * @access public
286 * @param string the table name
287 * @return string
288 */
289 function _list_columns($table = '')
290 {
291 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
292 }
293
294 // --------------------------------------------------------------------
295
296 /**
297 * Field data query
298 *
299 * Generates a platform-specific query so that the column data can be retrieved
300 *
301 * @access public
302 * @param string the table name
303 * @return object
304 */
305 function _field_data($table)
306 {
307 return "SELECT TOP 1 FROM ".$this->_escape_table($table);
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
admin0d011692006-09-24 18:12:58 +0000313 * The error message string
314 *
adminbb1d4392006-09-24 20:14:38 +0000315 * @access private
admin0d011692006-09-24 18:12:58 +0000316 * @return string
317 */
adminbb1d4392006-09-24 20:14:38 +0000318 function _error_message()
admin0d011692006-09-24 18:12:58 +0000319 {
320 return odbc_errormsg($this->conn_id);
321 }
322
323 // --------------------------------------------------------------------
324
325 /**
326 * The error message number
327 *
adminbb1d4392006-09-24 20:14:38 +0000328 * @access private
admin0d011692006-09-24 18:12:58 +0000329 * @return integer
330 */
adminbb1d4392006-09-24 20:14:38 +0000331 function _error_number()
admin0d011692006-09-24 18:12:58 +0000332 {
333 return odbc_error($this->conn_id);
334 }
335
336 // --------------------------------------------------------------------
337
338 /**
339 * Escape Table Name
340 *
341 * This function adds backticks if the table name has a period
342 * in it. Some DBs will get cranky unless periods are escaped
343 *
adminbb1d4392006-09-24 20:14:38 +0000344 * @access private
admin0d011692006-09-24 18:12:58 +0000345 * @param string the table name
346 * @return string
347 */
adminbb1d4392006-09-24 20:14:38 +0000348 function _escape_table($table)
admin0d011692006-09-24 18:12:58 +0000349 {
350 if (stristr($table, '.'))
351 {
352 $table = preg_replace("/\./", "`.`", $table);
353 }
354
355 return $table;
356 }
adminbb1d4392006-09-24 20:14:38 +0000357
admin0d011692006-09-24 18:12:58 +0000358 // --------------------------------------------------------------------
359
360 /**
361 * Insert statement
362 *
363 * Generates a platform-specific insert string from the supplied data
364 *
365 * @access public
366 * @param string the table name
367 * @param array the insert keys
368 * @param array the insert values
369 * @return string
370 */
371 function _insert($table, $keys, $values)
372 {
adminbb1d4392006-09-24 20:14:38 +0000373 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
admin0d011692006-09-24 18:12:58 +0000374 }
375
376 // --------------------------------------------------------------------
377
378 /**
379 * Update statement
380 *
381 * Generates a platform-specific update string from the supplied data
382 *
383 * @access public
384 * @param string the table name
385 * @param array the update data
386 * @param array the where clause
387 * @return string
388 */
389 function _update($table, $values, $where)
390 {
391 foreach($values as $key => $val)
392 {
393 $valstr[] = $key." = ".$val;
394 }
395
adminbb1d4392006-09-24 20:14:38 +0000396 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
admin0d011692006-09-24 18:12:58 +0000397 }
398
399 // --------------------------------------------------------------------
400
401 /**
402 * Delete statement
403 *
404 * Generates a platform-specific delete string from the supplied data
405 *
406 * @access public
407 * @param string the table name
408 * @param array the where clause
409 * @return string
410 */
411 function _delete($table, $where)
412 {
adminbb1d4392006-09-24 20:14:38 +0000413 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
admin0d011692006-09-24 18:12:58 +0000414 }
adminbb1d4392006-09-24 20:14:38 +0000415
416 // --------------------------------------------------------------------
417
418 /**
419 * Limit string
420 *
421 * Generates a platform-specific LIMIT clause
422 *
423 * @access public
424 * @param string the sql query string
425 * @param integer the number of rows to limit the query to
426 * @param integer the offset value
427 * @return string
428 */
429 function _limit($sql, $limit, $offset)
430 {
431 // Does ODBC doesn't use the LIMIT clause?
432 return $sql;
433 }
434
435 // --------------------------------------------------------------------
436
437 /**
438 * Close DB Connection
439 *
440 * @access public
441 * @param resource
442 * @return void
443 */
444 function _close($conn_id)
445 {
446 odbc_close($conn_id);
447 }
448
admin0d011692006-09-24 18:12:58 +0000449
450}
451
452
453?>