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