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