blob: cdde2e2eae489e87e864dac1e033f13a57b315c2 [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 }
262
263 // --------------------------------------------------------------------
264
265 /**
admin9cd4e8e2006-09-25 23:26:25 +0000266 * Show columnn query
267 *
268 * Generates a platform-specific query string so that the column names can be fetched
269 *
270 * @access public
271 * @param string the table name
272 * @return string
273 */
274 function _list_columns($table = '')
275 {
276 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Field data query
283 *
284 * Generates a platform-specific query so that the column data can be retrieved
285 *
286 * @access public
287 * @param string the table name
288 * @return object
289 */
290 function _field_data($table)
291 {
292 return "SELECT TOP 1 FROM ".$this->_escape_table($table);
293 }
294
295 // --------------------------------------------------------------------
296
297 /**
admin0d011692006-09-24 18:12:58 +0000298 * The error message string
299 *
adminbb1d4392006-09-24 20:14:38 +0000300 * @access private
admin0d011692006-09-24 18:12:58 +0000301 * @return string
302 */
adminbb1d4392006-09-24 20:14:38 +0000303 function _error_message()
admin0d011692006-09-24 18:12:58 +0000304 {
305 return odbc_errormsg($this->conn_id);
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * The error message number
312 *
adminbb1d4392006-09-24 20:14:38 +0000313 * @access private
admin0d011692006-09-24 18:12:58 +0000314 * @return integer
315 */
adminbb1d4392006-09-24 20:14:38 +0000316 function _error_number()
admin0d011692006-09-24 18:12:58 +0000317 {
318 return odbc_error($this->conn_id);
319 }
320
321 // --------------------------------------------------------------------
322
323 /**
324 * Escape Table Name
325 *
326 * This function adds backticks if the table name has a period
327 * in it. Some DBs will get cranky unless periods are escaped
328 *
adminbb1d4392006-09-24 20:14:38 +0000329 * @access private
admin0d011692006-09-24 18:12:58 +0000330 * @param string the table name
331 * @return string
332 */
adminbb1d4392006-09-24 20:14:38 +0000333 function _escape_table($table)
admin0d011692006-09-24 18:12:58 +0000334 {
335 if (stristr($table, '.'))
336 {
337 $table = preg_replace("/\./", "`.`", $table);
338 }
339
340 return $table;
341 }
adminbb1d4392006-09-24 20:14:38 +0000342
admin0d011692006-09-24 18:12:58 +0000343 // --------------------------------------------------------------------
344
345 /**
346 * Insert statement
347 *
348 * Generates a platform-specific insert string from the supplied data
349 *
350 * @access public
351 * @param string the table name
352 * @param array the insert keys
353 * @param array the insert values
354 * @return string
355 */
356 function _insert($table, $keys, $values)
357 {
adminbb1d4392006-09-24 20:14:38 +0000358 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
admin0d011692006-09-24 18:12:58 +0000359 }
360
361 // --------------------------------------------------------------------
362
363 /**
364 * Update statement
365 *
366 * Generates a platform-specific update string from the supplied data
367 *
368 * @access public
369 * @param string the table name
370 * @param array the update data
371 * @param array the where clause
372 * @return string
373 */
374 function _update($table, $values, $where)
375 {
376 foreach($values as $key => $val)
377 {
378 $valstr[] = $key." = ".$val;
379 }
380
adminbb1d4392006-09-24 20:14:38 +0000381 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
admin0d011692006-09-24 18:12:58 +0000382 }
383
384 // --------------------------------------------------------------------
385
386 /**
387 * Delete statement
388 *
389 * Generates a platform-specific delete string from the supplied data
390 *
391 * @access public
392 * @param string the table name
393 * @param array the where clause
394 * @return string
395 */
396 function _delete($table, $where)
397 {
adminbb1d4392006-09-24 20:14:38 +0000398 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
admin0d011692006-09-24 18:12:58 +0000399 }
adminbb1d4392006-09-24 20:14:38 +0000400
401 // --------------------------------------------------------------------
402
403 /**
404 * Limit string
405 *
406 * Generates a platform-specific LIMIT clause
407 *
408 * @access public
409 * @param string the sql query string
410 * @param integer the number of rows to limit the query to
411 * @param integer the offset value
412 * @return string
413 */
414 function _limit($sql, $limit, $offset)
415 {
416 // Does ODBC doesn't use the LIMIT clause?
417 return $sql;
418 }
419
420 // --------------------------------------------------------------------
421
422 /**
423 * Close DB Connection
424 *
425 * @access public
426 * @param resource
427 * @return void
428 */
429 function _close($conn_id)
430 {
431 odbc_close($conn_id);
432 }
433
admin0d011692006-09-24 18:12:58 +0000434
435}
436
437
438?>