admin | 0d01169 | 2006-09-24 18:12:58 +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. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 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. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 24 | * |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 25 | * @package CodeIgniter |
| 26 | * @subpackage Drivers |
| 27 | * @category Database |
| 28 | * @author Rick Ellis |
| 29 | * @link http://www.codeigniter.com/user_guide/database/ |
| 30 | */ |
| 31 | class 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 | { |
admin | 7acd581 | 2006-10-23 21:37:22 +0000 | [diff] [blame] | 41 | return @odbc_connect($this->database, $this->username, $this->password); |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 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 | { |
admin | 7acd581 | 2006-10-23 21:37:22 +0000 | [diff] [blame] | 54 | return @odbc_pconnect($this->database, $this->username, $this->password); |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 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 | } |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 70 | |
| 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 | |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 84 | // -------------------------------------------------------------------- |
| 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 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 110 | function _prep_query($sql) |
| 111 | { |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 112 | return $sql; |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 113 | } |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 114 | |
| 115 | // -------------------------------------------------------------------- |
| 116 | |
| 117 | /** |
| 118 | * Begin Transaction |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 119 | * |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 120 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 121 | * @return bool |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 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. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 137 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 138 | // even if the queries produce a successful result. |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 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 |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 148 | * |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 149 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 150 | * @return bool |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 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 |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 174 | * |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 175 | * @access public |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 176 | * @return bool |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 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 | } |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 262 | |
| 263 | // -------------------------------------------------------------------- |
| 264 | |
| 265 | /** |
admin | 3dd978f | 2006-09-30 19:24:45 +0000 | [diff] [blame] | 266 | * Show table query |
| 267 | * |
| 268 | * Generates a platform-specific query string so that the table names can be fetched |
| 269 | * |
| 270 | * @access private |
| 271 | * @return string |
| 272 | */ |
| 273 | function _list_tables() |
| 274 | { |
| 275 | return "SHOW TABLES FROM `".$this->database."`"; |
| 276 | } |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 277 | |
| 278 | // -------------------------------------------------------------------- |
| 279 | |
| 280 | /** |
admin | fafe28b | 2006-10-21 19:08:17 +0000 | [diff] [blame] | 281 | * Show column query |
admin | 9cd4e8e | 2006-09-25 23:26:25 +0000 | [diff] [blame] | 282 | * |
| 283 | * Generates a platform-specific query string so that the column names can be fetched |
| 284 | * |
| 285 | * @access public |
| 286 | * @param string the table name |
| 287 | * @return string |
| 288 | */ |
| 289 | function _list_columns($table = '') |
| 290 | { |
| 291 | return "SHOW COLUMNS FROM ".$this->_escape_table($table); |
| 292 | } |
| 293 | |
| 294 | // -------------------------------------------------------------------- |
| 295 | |
| 296 | /** |
| 297 | * Field data query |
| 298 | * |
| 299 | * Generates a platform-specific query so that the column data can be retrieved |
| 300 | * |
| 301 | * @access public |
| 302 | * @param string the table name |
| 303 | * @return object |
| 304 | */ |
| 305 | function _field_data($table) |
| 306 | { |
| 307 | return "SELECT TOP 1 FROM ".$this->_escape_table($table); |
| 308 | } |
| 309 | |
| 310 | // -------------------------------------------------------------------- |
| 311 | |
| 312 | /** |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 313 | * The error message string |
| 314 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 315 | * @access private |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 316 | * @return string |
| 317 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 318 | function _error_message() |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 319 | { |
| 320 | return odbc_errormsg($this->conn_id); |
| 321 | } |
| 322 | |
| 323 | // -------------------------------------------------------------------- |
| 324 | |
| 325 | /** |
| 326 | * The error message number |
| 327 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 328 | * @access private |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 329 | * @return integer |
| 330 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 331 | function _error_number() |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 332 | { |
| 333 | return odbc_error($this->conn_id); |
| 334 | } |
| 335 | |
| 336 | // -------------------------------------------------------------------- |
| 337 | |
| 338 | /** |
| 339 | * Escape Table Name |
| 340 | * |
| 341 | * This function adds backticks if the table name has a period |
| 342 | * in it. Some DBs will get cranky unless periods are escaped |
| 343 | * |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 344 | * @access private |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 345 | * @param string the table name |
| 346 | * @return string |
| 347 | */ |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 348 | function _escape_table($table) |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 349 | { |
| 350 | if (stristr($table, '.')) |
| 351 | { |
| 352 | $table = preg_replace("/\./", "`.`", $table); |
| 353 | } |
| 354 | |
| 355 | return $table; |
| 356 | } |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 357 | |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 358 | // -------------------------------------------------------------------- |
| 359 | |
| 360 | /** |
| 361 | * Insert statement |
| 362 | * |
| 363 | * Generates a platform-specific insert string from the supplied data |
| 364 | * |
| 365 | * @access public |
| 366 | * @param string the table name |
| 367 | * @param array the insert keys |
| 368 | * @param array the insert values |
| 369 | * @return string |
| 370 | */ |
| 371 | function _insert($table, $keys, $values) |
| 372 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 373 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | // -------------------------------------------------------------------- |
| 377 | |
| 378 | /** |
| 379 | * Update statement |
| 380 | * |
| 381 | * Generates a platform-specific update string from the supplied data |
| 382 | * |
| 383 | * @access public |
| 384 | * @param string the table name |
| 385 | * @param array the update data |
| 386 | * @param array the where clause |
| 387 | * @return string |
| 388 | */ |
| 389 | function _update($table, $values, $where) |
| 390 | { |
| 391 | foreach($values as $key => $val) |
| 392 | { |
| 393 | $valstr[] = $key." = ".$val; |
| 394 | } |
| 395 | |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 396 | return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where); |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // -------------------------------------------------------------------- |
| 400 | |
| 401 | /** |
| 402 | * Delete statement |
| 403 | * |
| 404 | * Generates a platform-specific delete string from the supplied data |
| 405 | * |
| 406 | * @access public |
| 407 | * @param string the table name |
| 408 | * @param array the where clause |
| 409 | * @return string |
| 410 | */ |
| 411 | function _delete($table, $where) |
| 412 | { |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 413 | return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where); |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 414 | } |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 415 | |
| 416 | // -------------------------------------------------------------------- |
| 417 | |
| 418 | /** |
| 419 | * Limit string |
| 420 | * |
| 421 | * Generates a platform-specific LIMIT clause |
| 422 | * |
| 423 | * @access public |
| 424 | * @param string the sql query string |
| 425 | * @param integer the number of rows to limit the query to |
| 426 | * @param integer the offset value |
| 427 | * @return string |
| 428 | */ |
| 429 | function _limit($sql, $limit, $offset) |
| 430 | { |
| 431 | // Does ODBC doesn't use the LIMIT clause? |
| 432 | return $sql; |
| 433 | } |
| 434 | |
| 435 | // -------------------------------------------------------------------- |
| 436 | |
| 437 | /** |
| 438 | * Close DB Connection |
| 439 | * |
| 440 | * @access public |
| 441 | * @param resource |
| 442 | * @return void |
| 443 | */ |
| 444 | function _close($conn_id) |
| 445 | { |
admin | befd4c2 | 2006-10-26 01:49:26 +0000 | [diff] [blame] | 446 | @odbc_close($conn_id); |
admin | bb1d439 | 2006-09-24 20:14:38 +0000 | [diff] [blame] | 447 | } |
| 448 | |
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 449 | |
| 450 | } |
| 451 | |
| 452 | |
| 453 | ?> |