blob: 1725ed74395ec50ed5994a6fbc848c0a79ad43e3 [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 }
70
71 // --------------------------------------------------------------------
72
73 /**
74 * Execute the query
75 *
76 * @access private called by the base class
77 * @param string an SQL query
78 * @return resource
79 */
80 function _execute($sql)
81 {
82 $sql = $this->_prep_query($sql);
83 return @odbc_exec($this->conn_id, $sql);
84 }
85
86 // --------------------------------------------------------------------
87
88 /**
89 * Prep the query
90 *
91 * If needed, each database adapter can prep the query string
92 *
93 * @access private called by execute()
94 * @param string an SQL query
95 * @return string
96 */
97 function _prep_query($sql)
98 {
99 return $sql;
100 }
101
102 // --------------------------------------------------------------------
103
104 /**
105 * Begin Transaction
106 *
107 * @access public
108 * @return bool
109 */
110 function trans_begin($test_mode = FALSE)
111 {
112 if ( ! $this->trans_enabled)
113 {
114 return TRUE;
115 }
116
117 // When transactions are nested we only begin/commit/rollback the outermost ones
118 if ($this->_trans_depth > 0)
119 {
120 return TRUE;
121 }
122
123 // Reset the transaction failure flag.
124 // If the $test_mode flag is set to TRUE transactions will be rolled back
125 // even if the queries produce a successful result.
126 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
127
128 return odbc_autocommit($this->conn_id, FALSE);
129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Commit Transaction
135 *
136 * @access public
137 * @return bool
138 */
139 function trans_commit()
140 {
141 if ( ! $this->trans_enabled)
142 {
143 return TRUE;
144 }
145
146 // When transactions are nested we only begin/commit/rollback the outermost ones
147 if ($this->_trans_depth > 0)
148 {
149 return TRUE;
150 }
151
152 $ret = odbc_commit($this->conn_id);
153 odbc_autocommit($this->conn_id, TRUE);
154 return $ret;
155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Rollback Transaction
161 *
162 * @access public
163 * @return bool
164 */
165 function trans_rollback()
166 {
167 if ( ! $this->trans_enabled)
168 {
169 return TRUE;
170 }
171
172 // When transactions are nested we only begin/commit/rollback the outermost ones
173 if ($this->_trans_depth > 0)
174 {
175 return TRUE;
176 }
177
178 $ret = odbc_rollback($this->conn_id);
179 odbc_autocommit($this->conn_id, TRUE);
180 return $ret;
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Escape String
187 *
188 * @access public
189 * @param string
190 * @return string
191 */
192 function escape_str($str)
193 {
194 // ODBC doesn't require escaping
195 return $str;
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Affected Rows
202 *
203 * @access public
204 * @return integer
205 */
206 function affected_rows()
207 {
208 return @odbc_num_rows($this->conn_id);
209 }
210
211 // --------------------------------------------------------------------
212
213 /**
214 * Insert ID
215 *
216 * @access public
217 * @return integer
218 */
219 function insert_id()
220 {
221 return @odbc_insert_id($this->conn_id);
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
227 * "Count All" query
228 *
229 * Generates a platform-specific query string that counts all records in
230 * the specified database
231 *
232 * @access public
233 * @param string
234 * @return string
235 */
236 function count_all($table = '')
237 {
238 if ($table == '')
239 return '0';
240
241 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
242
243 if ($query->num_rows() == 0)
244 return '0';
245
246 $row = $query->row();
247 return $row->numrows;
248 }
249
250 // --------------------------------------------------------------------
251
252 /**
253 * The error message string
254 *
255 * @access public
256 * @return string
257 */
258 function error_message()
259 {
260 return odbc_errormsg($this->conn_id);
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * The error message number
267 *
268 * @access public
269 * @return integer
270 */
271 function error_number()
272 {
273 return odbc_error($this->conn_id);
274 }
275
276 // --------------------------------------------------------------------
277
278 /**
279 * Escape Table Name
280 *
281 * This function adds backticks if the table name has a period
282 * in it. Some DBs will get cranky unless periods are escaped
283 *
284 * @access public
285 * @param string the table name
286 * @return string
287 */
288 function escape_table($table)
289 {
290 if (stristr($table, '.'))
291 {
292 $table = preg_replace("/\./", "`.`", $table);
293 }
294
295 return $table;
296 }
297
298 // --------------------------------------------------------------------
299
300 /**
301 * Field data query
302 *
303 * Generates a platform-specific query so that the column data can be retrieved
304 *
305 * @access public
306 * @param string the table name
307 * @return object
308 */
309 function _field_data($table)
310 {
311 $sql = "SELECT TOP 1 FROM ".$this->escape_table($table);
312 $query = $this->query($sql);
313 return $query->field_data();
314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * Insert statement
320 *
321 * Generates a platform-specific insert string from the supplied data
322 *
323 * @access public
324 * @param string the table name
325 * @param array the insert keys
326 * @param array the insert values
327 * @return string
328 */
329 function _insert($table, $keys, $values)
330 {
331 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
337 * Update statement
338 *
339 * Generates a platform-specific update string from the supplied data
340 *
341 * @access public
342 * @param string the table name
343 * @param array the update data
344 * @param array the where clause
345 * @return string
346 */
347 function _update($table, $values, $where)
348 {
349 foreach($values as $key => $val)
350 {
351 $valstr[] = $key." = ".$val;
352 }
353
354 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * Delete statement
361 *
362 * Generates a platform-specific delete string from the supplied data
363 *
364 * @access public
365 * @param string the table name
366 * @param array the where clause
367 * @return string
368 */
369 function _delete($table, $where)
370 {
371 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
372 }
373
374 // --------------------------------------------------------------------
375
376 /**
377 * Version number query string
378 *
379 * @access public
380 * @return string
381 */
382 function _version()
383 {
384 return "SELECT version() AS ver";
385 }
386
387 // --------------------------------------------------------------------
388
389 /**
390 * Show table query
391 *
392 * Generates a platform-specific query string so that the table names can be fetched
393 *
394 * @access public
395 * @return string
396 */
397 function _show_tables()
398 {
399 return "SHOW TABLES FROM `".$this->database."`";
400 }
401
402 // --------------------------------------------------------------------
403
404 /**
405 * Show columnn query
406 *
407 * Generates a platform-specific query string so that the column names can be fetched
408 *
409 * @access public
410 * @param string the table name
411 * @return string
412 */
413 function _show_columns($table = '')
414 {
415 return "SHOW COLUMNS FROM ".$this->escape_table($table);
416 }
417
418 // --------------------------------------------------------------------
419
420 /**
421 * Limit string
422 *
423 * Generates a platform-specific LIMIT clause
424 *
425 * @access public
426 * @param string the sql query string
427 * @param integer the number of rows to limit the query to
428 * @param integer the offset value
429 * @return string
430 */
431 function _limit($sql, $limit, $offset)
432 {
433 // Does ODBC doesn't use the LIMIT clause?
434 return $sql;
435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * Close DB Connection
441 *
442 * @access public
443 * @param resource
444 * @return void
445 */
446 function _close($conn_id)
447 {
448 odbc_close($conn_id);
449 }
450
451}
452
453
454?>