blob: a32e9e07261ccfb16af3c4f2d6e41f7be782cdd8 [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 */
110 function trans_begin()
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 return odbc_autocommit($this->conn_id, FALSE);
124 }
125
126 // --------------------------------------------------------------------
127
128 /**
129 * Commit Transaction
130 *
131 * @access public
132 * @return bool
133 */
134 function trans_commit()
135 {
136 if ( ! $this->trans_enabled)
137 {
138 return TRUE;
139 }
140
141 // When transactions are nested we only begin/commit/rollback the outermost ones
142 if ($this->_trans_depth > 0)
143 {
144 return TRUE;
145 }
146
147 $ret = odbc_commit($this->conn_id);
148 odbc_autocommit($this->conn_id, TRUE);
149 return $ret;
150 }
151
152 // --------------------------------------------------------------------
153
154 /**
155 * Rollback Transaction
156 *
157 * @access public
158 * @return bool
159 */
160 function trans_rollback()
161 {
162 if ( ! $this->trans_enabled)
163 {
164 return TRUE;
165 }
166
167 // When transactions are nested we only begin/commit/rollback the outermost ones
168 if ($this->_trans_depth > 0)
169 {
170 return TRUE;
171 }
172
173 $ret = odbc_rollback($this->conn_id);
174 odbc_autocommit($this->conn_id, TRUE);
175 return $ret;
176 }
177
adminb0dd10f2006-08-25 17:25:49 +0000178 // --------------------------------------------------------------------
179
180 /**
181 * Escape String
182 *
183 * @access public
184 * @param string
185 * @return string
186 */
187 function escape_str($str)
188 {
189 // ODBC doesn't require escaping
190 return $str;
191 }
192
193 // --------------------------------------------------------------------
194
195 /**
196 * Close DB Connection
197 *
198 * @access public
199 * @param resource
200 * @return void
201 */
202 function destroy($conn_id)
203 {
204 odbc_close($conn_id);
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Affected Rows
211 *
212 * @access public
213 * @return integer
214 */
215 function affected_rows()
216 {
217 return @odbc_num_rows($this->conn_id);
218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Insert ID
224 *
225 * @access public
226 * @return integer
227 */
228 function insert_id()
229 {
230 return @odbc_insert_id($this->conn_id);
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * "Count All" query
237 *
238 * Generates a platform-specific query string that counts all records in
239 * the specified database
240 *
241 * @access public
242 * @param string
243 * @return string
244 */
245 function count_all($table = '')
246 {
247 if ($table == '')
248 return '0';
249
250 $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`");
251
252 if ($query->num_rows() == 0)
253 return '0';
254
255 $row = $query->row();
256 return $row->numrows;
257 }
258
259 // --------------------------------------------------------------------
260
261 /**
262 * The error message string
263 *
264 * @access public
265 * @return string
266 */
267 function error_message()
268 {
269 return odbc_errormsg($this->conn_id);
270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * The error message number
276 *
277 * @access public
278 * @return integer
279 */
280 function error_number()
281 {
282 return odbc_error($this->conn_id);
283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * Escape Table Name
289 *
290 * This function adds backticks if the table name has a period
291 * in it. Some DBs will get cranky unless periods are escaped
292 *
293 * @access public
294 * @param string the table name
295 * @return string
296 */
297 function escape_table($table)
298 {
299 if (stristr($table, '.'))
300 {
301 $table = preg_replace("/\./", "`.`", $table);
302 }
303
304 return $table;
305 }
306
307 // --------------------------------------------------------------------
308
309 /**
310 * Field data query
311 *
312 * Generates a platform-specific query so that the column data can be retrieved
313 *
314 * @access public
315 * @param string the table name
316 * @return object
317 */
318 function _field_data($table)
319 {
320 $sql = "SELECT TOP 1 FROM ".$this->escape_table($table);
321 $query = $this->query($sql);
322 return $query->field_data();
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Insert statement
329 *
330 * Generates a platform-specific insert string from the supplied data
331 *
332 * @access public
333 * @param string the table name
334 * @param array the insert keys
335 * @param array the insert values
336 * @return string
337 */
338 function _insert($table, $keys, $values)
339 {
340 return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
341 }
342
343 // --------------------------------------------------------------------
344
345 /**
346 * Update statement
347 *
348 * Generates a platform-specific update string from the supplied data
349 *
350 * @access public
351 * @param string the table name
352 * @param array the update data
353 * @param array the where clause
354 * @return string
355 */
356 function _update($table, $values, $where)
357 {
358 foreach($values as $key => $val)
359 {
360 $valstr[] = $key." = ".$val;
361 }
362
363 return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
364 }
365
366 // --------------------------------------------------------------------
367
368 /**
369 * Delete statement
370 *
371 * Generates a platform-specific delete string from the supplied data
372 *
373 * @access public
374 * @param string the table name
375 * @param array the where clause
376 * @return string
377 */
378 function _delete($table, $where)
379 {
380 return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where);
381 }
382
383 // --------------------------------------------------------------------
384
385 /**
386 * Version number query string
387 *
388 * @access public
389 * @return string
390 */
391 function _version()
392 {
393 return "SELECT version() AS ver";
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Show table query
400 *
401 * Generates a platform-specific query string so that the table names can be fetched
402 *
403 * @access public
404 * @return string
405 */
406 function _show_tables()
407 {
408 return "SHOW TABLES FROM `".$this->database."`";
409 }
410
411 // --------------------------------------------------------------------
412
413 /**
414 * Show columnn query
415 *
416 * Generates a platform-specific query string so that the column names can be fetched
417 *
418 * @access public
419 * @param string the table name
420 * @return string
421 */
422 function _show_columns($table = '')
423 {
424 return "SHOW COLUMNS FROM ".$this->escape_table($table);
425 }
426
427 // --------------------------------------------------------------------
428
429 /**
430 * Limit string
431 *
432 * Generates a platform-specific LIMIT clause
433 *
434 * @access public
435 * @param string the sql query string
436 * @param integer the number of rows to limit the query to
437 * @param integer the offset value
438 * @return string
439 */
440 function _limit($sql, $limit, $offset)
441 {
442 // Does ODBC doesn't use the LIMIT clause?
443 return $sql;
444 }
445
446}
447
448
449/**
450 * ODBC Result Class
451 *
452 * This class extends the parent result class: CI_DB_result
453 *
454 * @category Database
455 * @author Rick Ellis
456 * @link http://www.codeigniter.com/user_guide/libraries/database/
457 */
458class CI_DB_odbc_result extends CI_DB_result {
459
460 /**
461 * Number of rows in the result set
462 *
463 * @access public
464 * @return integer
465 */
466 function num_rows()
467 {
468 return @odbc_num_rows($this->result_id);
469 }
470
471 // --------------------------------------------------------------------
472
473 /**
474 * Number of fields in the result set
475 *
476 * @access public
477 * @return integer
478 */
479 function num_fields()
480 {
481 return @odbc_num_fields($this->result_id);
482 }
483
484 // --------------------------------------------------------------------
485
486 /**
487 * Field data
488 *
489 * Generates an array of objects containing field meta-data
490 *
491 * @access public
492 * @return array
493 */
494 function field_data()
495 {
496 $retval = array();
497 for ($i = 0; $i < $this->num_fields(); $i++)
498 {
admine348efb2006-09-20 21:13:26 +0000499 $F = new stdClass();
adminb0dd10f2006-08-25 17:25:49 +0000500 $F->name = odbc_field_name($this->result_id, $i);
501 $F->type = odbc_field_type($this->result_id, $i);
502 $F->max_length = odbc_field_len($this->result_id, $i);
503 $F->primary_key = 0;
504 $F->default = '';
505
506 $retval[] = $F;
507 }
508
509 return $retval;
510 }
511
512 // --------------------------------------------------------------------
513
514 /**
515 * Result - associative array
516 *
517 * Returns the result set as an array
518 *
519 * @access private
520 * @return array
521 */
522 function _fetch_assoc()
523 {
adminfdd92812006-09-06 01:57:41 +0000524 if (function_exists('odbc_fetch_object'))
525 {
526 return odbc_fetch_array($this->result_id);
527 }
528 else
529 {
530 return $this->_odbc_fetch_array($this->result_id);
531 }
adminb0dd10f2006-08-25 17:25:49 +0000532 }
adminfdd92812006-09-06 01:57:41 +0000533
adminb0dd10f2006-08-25 17:25:49 +0000534 // --------------------------------------------------------------------
535
536 /**
537 * Result - object
538 *
539 * Returns the result set as an object
540 *
541 * @access private
542 * @return object
543 */
544 function _fetch_object()
545 {
adminfdd92812006-09-06 01:57:41 +0000546 if (function_exists('odbc_fetch_object'))
547 {
548 return odbc_fetch_object($this->result_id);
549 }
550 else
551 {
552 return $this->_odbc_fetch_object($this->result_id);
553 }
adminb0dd10f2006-08-25 17:25:49 +0000554 }
adminfdd92812006-09-06 01:57:41 +0000555
556
557 /**
558 * Result - object
559 *
560 * subsititutes the odbc_fetch_object function when
561 * not available (odbc_fetch_object requires unixODBC)
562 *
563 * @access private
564 * @return object
565 */
566
567 function _odbc_fetch_object(& $odbc_result) {
568 $rs = array();
569 $rs_obj = false;
570 if (odbc_fetch_into($odbc_result, $rs)) {
571 foreach ($rs as $k=>$v) {
572 $field_name= odbc_field_name($odbc_result, $k+1);
573 $rs_obj->$field_name = $v;
574 }
575 }
576 return $rs_obj;
577 }
578
579
580 /**
581 * Result - array
582 *
583 * subsititutes the odbc_fetch_array function when
584 * not available (odbc_fetch_array requires unixODBC)
585 *
586 * @access private
587 * @return array
588 */
589
590 function _odbc_fetch_array(& $odbc_result) {
591 $rs = array();
592 $rs_assoc = false;
593 if (odbc_fetch_into($odbc_result, $rs)) {
594 $rs_assoc=array();
595 foreach ($rs as $k=>$v) {
596 $field_name= odbc_field_name($odbc_result, $k+1);
597 $rs_assoc[$field_name] = $v;
598 }
599 }
600 return $rs_assoc;
601 }
602
adminb0dd10f2006-08-25 17:25:49 +0000603}
604
605?>