blob: cdd2f451362edbc44e3035ee837bc587025c22d5 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +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/libraries/database/
30 */
31class CI_DB_odbc 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 */
admine885d782006-09-23 20:25:05 +000080 function _execute($sql)
adminb0dd10f2006-08-25 17:25:49 +000081 {
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 */
adminb071bb52006-08-26 19:28:37 +000097 function _prep_query($sql)
adminb0dd10f2006-08-25 17:25:49 +000098 {
99 return $sql;
100 }
admine885d782006-09-23 20:25:05 +0000101
102 // --------------------------------------------------------------------
103
104 /**
105 * Begin Transaction
106 *
107 * @access public
108 * @return bool
109 */
admin8b180be2006-09-24 01:12:22 +0000110 function trans_begin($test_mode = FALSE)
admine885d782006-09-23 20:25:05 +0000111 {
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
admin8b180be2006-09-24 01:12:22 +0000123 // 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
admine885d782006-09-23 20:25:05 +0000128 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
adminb0dd10f2006-08-25 17:25:49 +0000183 // --------------------------------------------------------------------
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 * Close DB Connection
202 *
203 * @access public
204 * @param resource
205 * @return void
206 */
207 function destroy($conn_id)
208 {
209 odbc_close($conn_id);
210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Affected Rows
216 *
217 * @access public
218 * @return integer
219 */
220 function affected_rows()
221 {
222 return @odbc_num_rows($this->conn_id);
223 }
224
225 // --------------------------------------------------------------------
226
227 /**
228 * Insert ID
229 *
230 * @access public
231 * @return integer
232 */
233 function insert_id()
234 {
235 return @odbc_insert_id($this->conn_id);
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * "Count All" query
242 *
243 * Generates a platform-specific query string that counts all records in
244 * the specified database
245 *
246 * @access public
247 * @param string
248 * @return string
249 */
250 function count_all($table = '')
251 {
252 if ($table == '')
253 return '0';
254
255 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
256
257 if ($query->num_rows() == 0)
258 return '0';
259
260 $row = $query->row();
261 return $row->numrows;
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * The error message string
268 *
269 * @access public
270 * @return string
271 */
272 function error_message()
273 {
274 return odbc_errormsg($this->conn_id);
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * The error message number
281 *
282 * @access public
283 * @return integer
284 */
285 function error_number()
286 {
287 return odbc_error($this->conn_id);
288 }
289
290 // --------------------------------------------------------------------
291
292 /**
293 * Escape Table Name
294 *
295 * This function adds backticks if the table name has a period
296 * in it. Some DBs will get cranky unless periods are escaped
297 *
298 * @access public
299 * @param string the table name
300 * @return string
301 */
302 function escape_table($table)
303 {
304 if (stristr($table, '.'))
305 {
306 $table = preg_replace("/\./", "`.`", $table);
307 }
308
309 return $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 $sql = "SELECT TOP 1 FROM ".$this->escape_table($table);
326 $query = $this->query($sql);
327 return $query->field_data();
328 }
329
330 // --------------------------------------------------------------------
331
332 /**
333 * Insert statement
334 *
335 * Generates a platform-specific insert string from the supplied data
336 *
337 * @access public
338 * @param string the table name
339 * @param array the insert keys
340 * @param array the insert values
341 * @return string
342 */
343 function _insert($table, $keys, $values)
344 {
345 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * Update statement
352 *
353 * Generates a platform-specific update string from the supplied data
354 *
355 * @access public
356 * @param string the table name
357 * @param array the update data
358 * @param array the where clause
359 * @return string
360 */
361 function _update($table, $values, $where)
362 {
363 foreach($values as $key => $val)
364 {
365 $valstr[] = $key." = ".$val;
366 }
367
368 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
369 }
370
371 // --------------------------------------------------------------------
372
373 /**
374 * Delete statement
375 *
376 * Generates a platform-specific delete string from the supplied data
377 *
378 * @access public
379 * @param string the table name
380 * @param array the where clause
381 * @return string
382 */
383 function _delete($table, $where)
384 {
385 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
386 }
387
388 // --------------------------------------------------------------------
389
390 /**
391 * Version number query string
392 *
393 * @access public
394 * @return string
395 */
396 function _version()
397 {
398 return "SELECT version() AS ver";
399 }
400
401 // --------------------------------------------------------------------
402
403 /**
404 * Show table query
405 *
406 * Generates a platform-specific query string so that the table names can be fetched
407 *
408 * @access public
409 * @return string
410 */
411 function _show_tables()
412 {
413 return "SHOW TABLES FROM `".$this->database."`";
414 }
415
416 // --------------------------------------------------------------------
417
418 /**
419 * Show columnn query
420 *
421 * Generates a platform-specific query string so that the column names can be fetched
422 *
423 * @access public
424 * @param string the table name
425 * @return string
426 */
427 function _show_columns($table = '')
428 {
429 return "SHOW COLUMNS FROM ".$this->escape_table($table);
430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * Limit string
436 *
437 * Generates a platform-specific LIMIT clause
438 *
439 * @access public
440 * @param string the sql query string
441 * @param integer the number of rows to limit the query to
442 * @param integer the offset value
443 * @return string
444 */
445 function _limit($sql, $limit, $offset)
446 {
447 // Does ODBC doesn't use the LIMIT clause?
448 return $sql;
449 }
450
451}
452
453
454/**
455 * ODBC Result Class
456 *
457 * This class extends the parent result class: CI_DB_result
458 *
459 * @category Database
460 * @author Rick Ellis
461 * @link http://www.codeigniter.com/user_guide/libraries/database/
462 */
463class CI_DB_odbc_result extends CI_DB_result {
464
465 /**
466 * Number of rows in the result set
467 *
468 * @access public
469 * @return integer
470 */
471 function num_rows()
472 {
473 return @odbc_num_rows($this->result_id);
474 }
475
476 // --------------------------------------------------------------------
477
478 /**
479 * Number of fields in the result set
480 *
481 * @access public
482 * @return integer
483 */
484 function num_fields()
485 {
486 return @odbc_num_fields($this->result_id);
487 }
488
489 // --------------------------------------------------------------------
490
491 /**
492 * Field data
493 *
494 * Generates an array of objects containing field meta-data
495 *
496 * @access public
497 * @return array
498 */
499 function field_data()
500 {
501 $retval = array();
502 for ($i = 0; $i < $this->num_fields(); $i++)
503 {
admine348efb2006-09-20 21:13:26 +0000504 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000505 $F->name = odbc_field_name($this->result_id, $i);
506 $F->type = odbc_field_type($this->result_id, $i);
507 $F->max_length = odbc_field_len($this->result_id, $i);
508 $F->primary_key = 0;
509 $F->default = '';
510
511 $retval[] = $F;
512 }
513
514 return $retval;
515 }
516
517 // --------------------------------------------------------------------
518
519 /**
520 * Result - associative array
521 *
522 * Returns the result set as an array
523 *
524 * @access private
525 * @return array
526 */
527 function _fetch_assoc()
528 {
adminfdd92812006-09-06 01:57:41 +0000529 if (function_exists('odbc_fetch_object'))
530 {
531 return odbc_fetch_array($this->result_id);
532 }
533 else
534 {
535 return $this->_odbc_fetch_array($this->result_id);
536 }
adminb0dd10f2006-08-25 17:25:49 +0000537 }
adminfdd92812006-09-06 01:57:41 +0000538
adminb0dd10f2006-08-25 17:25:49 +0000539 // --------------------------------------------------------------------
540
541 /**
542 * Result - object
543 *
544 * Returns the result set as an object
545 *
546 * @access private
547 * @return object
548 */
549 function _fetch_object()
550 {
adminfdd92812006-09-06 01:57:41 +0000551 if (function_exists('odbc_fetch_object'))
552 {
553 return odbc_fetch_object($this->result_id);
554 }
555 else
556 {
557 return $this->_odbc_fetch_object($this->result_id);
558 }
adminb0dd10f2006-08-25 17:25:49 +0000559 }
adminfdd92812006-09-06 01:57:41 +0000560
561
562 /**
563 * Result - object
564 *
565 * subsititutes the odbc_fetch_object function when
566 * not available (odbc_fetch_object requires unixODBC)
567 *
568 * @access private
569 * @return object
570 */
571
572 function _odbc_fetch_object(& $odbc_result) {
573 $rs = array();
574 $rs_obj = false;
575 if (odbc_fetch_into($odbc_result, $rs)) {
576 foreach ($rs as $k=>$v) {
577 $field_name= odbc_field_name($odbc_result, $k+1);
578 $rs_obj->$field_name = $v;
579 }
580 }
581 return $rs_obj;
582 }
583
584
585 /**
586 * Result - array
587 *
588 * subsititutes the odbc_fetch_array function when
589 * not available (odbc_fetch_array requires unixODBC)
590 *
591 * @access private
592 * @return array
593 */
594
595 function _odbc_fetch_array(& $odbc_result) {
596 $rs = array();
597 $rs_assoc = false;
598 if (odbc_fetch_into($odbc_result, $rs)) {
599 $rs_assoc=array();
600 foreach ($rs as $k=>$v) {
601 $field_name= odbc_field_name($odbc_result, $k+1);
602 $rs_assoc[$field_name] = $v;
603 }
604 }
605 return $rs_assoc;
606 }
607
adminb0dd10f2006-08-25 17:25:49 +0000608}
609
610?>