blob: 0f002cc1b4a01dbca25a035786911a58d1623ab9 [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.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @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 /**
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 */
110 function _prep_query($sql)
111 {
112 return $sql;
113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Begin Transaction
119 *
120 * @access public
121 * @return bool
122 */
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.
137 // If the $test_mode flag is set to TRUE transactions will be rolled back
138 // even if the queries produce a successful result.
139 $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
148 *
149 * @access public
150 * @return bool
151 */
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
174 *
175 * @access public
176 * @return bool
177 */
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 /**
266 * List databases
267 *
268 * @access private
269 * @return bool
270 */
271 function _list_databases()
272 {
273 // Not sure if ODBC lets you list all databases...
274 if ($this->db_debug)
275 {
276 return $this->display_error('db_unsuported_feature');
277 }
278 return FALSE;
279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * Show table query
285 *
286 * Generates a platform-specific query string so that the table names can be fetched
287 *
288 * @access private
289 * @return string
290 */
291 function _list_tables()
292 {
293 return "SHOW TABLES FROM `".$this->database."`";
294 }
admin0d011692006-09-24 18:12:58 +0000295
296 // --------------------------------------------------------------------
297
298 /**
admin9cd4e8e2006-09-25 23:26:25 +0000299 * Show columnn query
300 *
301 * Generates a platform-specific query string so that the column names can be fetched
302 *
303 * @access public
304 * @param string the table name
305 * @return string
306 */
307 function _list_columns($table = '')
308 {
309 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
310 }
311
312 // --------------------------------------------------------------------
313
314 /**
315 * Field data query
316 *
317 * Generates a platform-specific query so that the column data can be retrieved
318 *
319 * @access public
320 * @param string the table name
321 * @return object
322 */
323 function _field_data($table)
324 {
325 return "SELECT TOP 1 FROM ".$this->_escape_table($table);
326 }
327
328 // --------------------------------------------------------------------
329
330 /**
admin0d011692006-09-24 18:12:58 +0000331 * The error message string
332 *
adminbb1d4392006-09-24 20:14:38 +0000333 * @access private
admin0d011692006-09-24 18:12:58 +0000334 * @return string
335 */
adminbb1d4392006-09-24 20:14:38 +0000336 function _error_message()
admin0d011692006-09-24 18:12:58 +0000337 {
338 return odbc_errormsg($this->conn_id);
339 }
340
341 // --------------------------------------------------------------------
342
343 /**
344 * The error message number
345 *
adminbb1d4392006-09-24 20:14:38 +0000346 * @access private
admin0d011692006-09-24 18:12:58 +0000347 * @return integer
348 */
adminbb1d4392006-09-24 20:14:38 +0000349 function _error_number()
admin0d011692006-09-24 18:12:58 +0000350 {
351 return odbc_error($this->conn_id);
352 }
353
354 // --------------------------------------------------------------------
355
356 /**
357 * Escape Table Name
358 *
359 * This function adds backticks if the table name has a period
360 * in it. Some DBs will get cranky unless periods are escaped
361 *
adminbb1d4392006-09-24 20:14:38 +0000362 * @access private
admin0d011692006-09-24 18:12:58 +0000363 * @param string the table name
364 * @return string
365 */
adminbb1d4392006-09-24 20:14:38 +0000366 function _escape_table($table)
admin0d011692006-09-24 18:12:58 +0000367 {
368 if (stristr($table, '.'))
369 {
370 $table = preg_replace("/\./", "`.`", $table);
371 }
372
373 return $table;
374 }
adminbb1d4392006-09-24 20:14:38 +0000375
admin0d011692006-09-24 18:12:58 +0000376 // --------------------------------------------------------------------
377
378 /**
379 * Insert statement
380 *
381 * Generates a platform-specific insert string from the supplied data
382 *
383 * @access public
384 * @param string the table name
385 * @param array the insert keys
386 * @param array the insert values
387 * @return string
388 */
389 function _insert($table, $keys, $values)
390 {
adminbb1d4392006-09-24 20:14:38 +0000391 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
admin0d011692006-09-24 18:12:58 +0000392 }
393
394 // --------------------------------------------------------------------
395
396 /**
397 * Update statement
398 *
399 * Generates a platform-specific update string from the supplied data
400 *
401 * @access public
402 * @param string the table name
403 * @param array the update data
404 * @param array the where clause
405 * @return string
406 */
407 function _update($table, $values, $where)
408 {
409 foreach($values as $key => $val)
410 {
411 $valstr[] = $key." = ".$val;
412 }
413
adminbb1d4392006-09-24 20:14:38 +0000414 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
admin0d011692006-09-24 18:12:58 +0000415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Delete statement
421 *
422 * Generates a platform-specific delete string from the supplied data
423 *
424 * @access public
425 * @param string the table name
426 * @param array the where clause
427 * @return string
428 */
429 function _delete($table, $where)
430 {
adminbb1d4392006-09-24 20:14:38 +0000431 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
admin0d011692006-09-24 18:12:58 +0000432 }
adminbb1d4392006-09-24 20:14:38 +0000433
434 // --------------------------------------------------------------------
435
436 /**
437 * Limit string
438 *
439 * Generates a platform-specific LIMIT clause
440 *
441 * @access public
442 * @param string the sql query string
443 * @param integer the number of rows to limit the query to
444 * @param integer the offset value
445 * @return string
446 */
447 function _limit($sql, $limit, $offset)
448 {
449 // Does ODBC doesn't use the LIMIT clause?
450 return $sql;
451 }
452
453 // --------------------------------------------------------------------
454
455 /**
456 * Close DB Connection
457 *
458 * @access public
459 * @param resource
460 * @return void
461 */
462 function _close($conn_id)
463 {
464 odbc_close($conn_id);
465 }
466
admin0d011692006-09-24 18:12:58 +0000467
468}
469
470
471?>