blob: bef6258de263cbae21c6af4244982797a703d9ed [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 *
adminbb1d4392006-09-24 20:14:38 +0000255 * @access private
admin0d011692006-09-24 18:12:58 +0000256 * @return string
257 */
adminbb1d4392006-09-24 20:14:38 +0000258 function _error_message()
admin0d011692006-09-24 18:12:58 +0000259 {
260 return odbc_errormsg($this->conn_id);
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * The error message number
267 *
adminbb1d4392006-09-24 20:14:38 +0000268 * @access private
admin0d011692006-09-24 18:12:58 +0000269 * @return integer
270 */
adminbb1d4392006-09-24 20:14:38 +0000271 function _error_number()
admin0d011692006-09-24 18:12:58 +0000272 {
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 *
adminbb1d4392006-09-24 20:14:38 +0000284 * @access private
admin0d011692006-09-24 18:12:58 +0000285 * @param string the table name
286 * @return string
287 */
adminbb1d4392006-09-24 20:14:38 +0000288 function _escape_table($table)
admin0d011692006-09-24 18:12:58 +0000289 {
290 if (stristr($table, '.'))
291 {
292 $table = preg_replace("/\./", "`.`", $table);
293 }
294
295 return $table;
296 }
adminbb1d4392006-09-24 20:14:38 +0000297
admin0d011692006-09-24 18:12:58 +0000298 // --------------------------------------------------------------------
299
300 /**
301 * Insert statement
302 *
303 * Generates a platform-specific insert string from the supplied data
304 *
305 * @access public
306 * @param string the table name
307 * @param array the insert keys
308 * @param array the insert values
309 * @return string
310 */
311 function _insert($table, $keys, $values)
312 {
adminbb1d4392006-09-24 20:14:38 +0000313 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
admin0d011692006-09-24 18:12:58 +0000314 }
315
316 // --------------------------------------------------------------------
317
318 /**
319 * Update statement
320 *
321 * Generates a platform-specific update string from the supplied data
322 *
323 * @access public
324 * @param string the table name
325 * @param array the update data
326 * @param array the where clause
327 * @return string
328 */
329 function _update($table, $values, $where)
330 {
331 foreach($values as $key => $val)
332 {
333 $valstr[] = $key." = ".$val;
334 }
335
adminbb1d4392006-09-24 20:14:38 +0000336 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
admin0d011692006-09-24 18:12:58 +0000337 }
338
339 // --------------------------------------------------------------------
340
341 /**
342 * Delete statement
343 *
344 * Generates a platform-specific delete string from the supplied data
345 *
346 * @access public
347 * @param string the table name
348 * @param array the where clause
349 * @return string
350 */
351 function _delete($table, $where)
352 {
adminbb1d4392006-09-24 20:14:38 +0000353 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
admin0d011692006-09-24 18:12:58 +0000354 }
adminbb1d4392006-09-24 20:14:38 +0000355
356 // --------------------------------------------------------------------
357
358 /**
359 * Limit string
360 *
361 * Generates a platform-specific LIMIT clause
362 *
363 * @access public
364 * @param string the sql query string
365 * @param integer the number of rows to limit the query to
366 * @param integer the offset value
367 * @return string
368 */
369 function _limit($sql, $limit, $offset)
370 {
371 // Does ODBC doesn't use the LIMIT clause?
372 return $sql;
373 }
374
375 // --------------------------------------------------------------------
376
377 /**
378 * Close DB Connection
379 *
380 * @access public
381 * @param resource
382 * @return void
383 */
384 function _close($conn_id)
385 {
386 odbc_close($conn_id);
387 }
388
admin0d011692006-09-24 18:12:58 +0000389
390}
391
392
393?>