Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
| 8 | * @author Rick Ellis
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Allard | 6838f00 | 2007-10-04 19:29:59 +0000 | [diff] [blame] | 10 | * @license http://www.codeigniter.com/user_guide/license.html
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 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 | */
|
| 31 | class CI_DB_odbc_driver extends CI_DB {
|
| 32 |
|
| 33 | /**
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 34 | * The syntax to count rows is slightly different across different
|
| 35 | * database engines, so this string appears in each driver and is
|
| 36 | * used for the count_all() and count_all_results() functions.
|
| 37 | */
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 38 | var $_count_string = "SELECT COUNT(*) AS numrows ";
|
| 39 | var $_random_keyword = ' RND('.time().')'; // database specific random keyword
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 40 |
|
| 41 | /**
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 42 | * Non-persistent database connection
|
| 43 | *
|
| 44 | * @access private called by the base class
|
| 45 | * @return resource
|
| 46 | */
|
| 47 | function db_connect()
|
| 48 | {
|
| 49 | return @odbc_connect($this->hostname, $this->username, $this->password);
|
| 50 | }
|
| 51 |
|
| 52 | // --------------------------------------------------------------------
|
| 53 |
|
| 54 | /**
|
| 55 | * Persistent database connection
|
| 56 | *
|
| 57 | * @access private called by the base class
|
| 58 | * @return resource
|
| 59 | */
|
| 60 | function db_pconnect()
|
| 61 | {
|
| 62 | return @odbc_pconnect($this->hostname, $this->username, $this->password);
|
| 63 | }
|
| 64 |
|
| 65 | // --------------------------------------------------------------------
|
| 66 |
|
| 67 | /**
|
| 68 | * Select the database
|
| 69 | *
|
| 70 | * @access private called by the base class
|
| 71 | * @return resource
|
| 72 | */
|
| 73 | function db_select()
|
| 74 | {
|
| 75 | // Not needed for ODBC
|
| 76 | return TRUE;
|
| 77 | }
|
| 78 |
|
| 79 | // --------------------------------------------------------------------
|
| 80 |
|
| 81 | /**
|
| 82 | * Version number query string
|
| 83 | *
|
| 84 | * @access public
|
| 85 | * @return string
|
| 86 | */
|
| 87 | function _version()
|
| 88 | {
|
| 89 | return "SELECT version() AS ver";
|
| 90 | }
|
| 91 |
|
| 92 | // --------------------------------------------------------------------
|
| 93 |
|
| 94 | /**
|
| 95 | * Execute the query
|
| 96 | *
|
| 97 | * @access private called by the base class
|
| 98 | * @param string an SQL query
|
| 99 | * @return resource
|
| 100 | */
|
| 101 | function _execute($sql)
|
| 102 | {
|
| 103 | $sql = $this->_prep_query($sql);
|
| 104 | return @odbc_exec($this->conn_id, $sql);
|
| 105 | }
|
| 106 |
|
| 107 | // --------------------------------------------------------------------
|
| 108 |
|
| 109 | /**
|
| 110 | * Prep the query
|
| 111 | *
|
| 112 | * If needed, each database adapter can prep the query string
|
| 113 | *
|
| 114 | * @access private called by execute()
|
| 115 | * @param string an SQL query
|
| 116 | * @return string
|
| 117 | */
|
| 118 | function _prep_query($sql)
|
| 119 | {
|
| 120 | return $sql;
|
| 121 | }
|
| 122 |
|
| 123 | // --------------------------------------------------------------------
|
| 124 |
|
| 125 | /**
|
| 126 | * Begin Transaction
|
| 127 | *
|
| 128 | * @access public
|
| 129 | * @return bool
|
| 130 | */
|
| 131 | function trans_begin($test_mode = FALSE)
|
| 132 | {
|
| 133 | if ( ! $this->trans_enabled)
|
| 134 | {
|
| 135 | return TRUE;
|
| 136 | }
|
| 137 |
|
| 138 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 139 | if ($this->_trans_depth > 0)
|
| 140 | {
|
| 141 | return TRUE;
|
| 142 | }
|
| 143 |
|
| 144 | // Reset the transaction failure flag.
|
| 145 | // If the $test_mode flag is set to TRUE transactions will be rolled back
|
| 146 | // even if the queries produce a successful result.
|
| 147 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
|
| 148 |
|
| 149 | return odbc_autocommit($this->conn_id, FALSE);
|
| 150 | }
|
| 151 |
|
| 152 | // --------------------------------------------------------------------
|
| 153 |
|
| 154 | /**
|
| 155 | * Commit Transaction
|
| 156 | *
|
| 157 | * @access public
|
| 158 | * @return bool
|
| 159 | */
|
| 160 | function trans_commit()
|
| 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_commit($this->conn_id);
|
| 174 | odbc_autocommit($this->conn_id, TRUE);
|
| 175 | return $ret;
|
| 176 | }
|
| 177 |
|
| 178 | // --------------------------------------------------------------------
|
| 179 |
|
| 180 | /**
|
| 181 | * Rollback Transaction
|
| 182 | *
|
| 183 | * @access public
|
| 184 | * @return bool
|
| 185 | */
|
| 186 | function trans_rollback()
|
| 187 | {
|
| 188 | if ( ! $this->trans_enabled)
|
| 189 | {
|
| 190 | return TRUE;
|
| 191 | }
|
| 192 |
|
| 193 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 194 | if ($this->_trans_depth > 0)
|
| 195 | {
|
| 196 | return TRUE;
|
| 197 | }
|
| 198 |
|
| 199 | $ret = odbc_rollback($this->conn_id);
|
| 200 | odbc_autocommit($this->conn_id, TRUE);
|
| 201 | return $ret;
|
| 202 | }
|
| 203 |
|
| 204 | // --------------------------------------------------------------------
|
| 205 |
|
| 206 | /**
|
| 207 | * Escape String
|
| 208 | *
|
| 209 | * @access public
|
| 210 | * @param string
|
| 211 | * @return string
|
| 212 | */
|
| 213 | function escape_str($str)
|
| 214 | {
|
| 215 | // ODBC doesn't require escaping
|
| 216 | return $str;
|
| 217 | }
|
| 218 |
|
| 219 | // --------------------------------------------------------------------
|
| 220 |
|
| 221 | /**
|
| 222 | * Affected Rows
|
| 223 | *
|
| 224 | * @access public
|
| 225 | * @return integer
|
| 226 | */
|
| 227 | function affected_rows()
|
| 228 | {
|
| 229 | return @odbc_num_rows($this->conn_id);
|
| 230 | }
|
| 231 |
|
| 232 | // --------------------------------------------------------------------
|
| 233 |
|
| 234 | /**
|
| 235 | * Insert ID
|
| 236 | *
|
| 237 | * @access public
|
| 238 | * @return integer
|
| 239 | */
|
| 240 | function insert_id()
|
| 241 | {
|
| 242 | return @odbc_insert_id($this->conn_id);
|
| 243 | }
|
| 244 |
|
| 245 | // --------------------------------------------------------------------
|
| 246 |
|
| 247 | /**
|
| 248 | * "Count All" query
|
| 249 | *
|
| 250 | * Generates a platform-specific query string that counts all records in
|
| 251 | * the specified database
|
| 252 | *
|
| 253 | * @access public
|
| 254 | * @param string
|
| 255 | * @return string
|
| 256 | */
|
| 257 | function count_all($table = '')
|
| 258 | {
|
| 259 | if ($table == '')
|
| 260 | return '0';
|
| 261 |
|
Derek Allard | 6ddb5a1 | 2007-12-18 17:22:50 +0000 | [diff] [blame] | 262 | $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 263 |
|
| 264 | if ($query->num_rows() == 0)
|
| 265 | return '0';
|
| 266 |
|
| 267 | $row = $query->row();
|
| 268 | return $row->numrows;
|
| 269 | }
|
| 270 |
|
| 271 | // --------------------------------------------------------------------
|
| 272 |
|
| 273 | /**
|
| 274 | * Show table query
|
| 275 | *
|
| 276 | * Generates a platform-specific query string so that the table names can be fetched
|
| 277 | *
|
| 278 | * @access private
|
| 279 | * @return string
|
| 280 | */
|
| 281 | function _list_tables()
|
| 282 | {
|
| 283 | return "SHOW TABLES FROM `".$this->database."`";
|
| 284 | }
|
| 285 |
|
| 286 | // --------------------------------------------------------------------
|
| 287 |
|
| 288 | /**
|
| 289 | * Show column query
|
| 290 | *
|
| 291 | * Generates a platform-specific query string so that the column names can be fetched
|
| 292 | *
|
| 293 | * @access public
|
| 294 | * @param string the table name
|
| 295 | * @return string
|
| 296 | */
|
| 297 | function _list_columns($table = '')
|
| 298 | {
|
| 299 | return "SHOW COLUMNS FROM ".$this->_escape_table($table);
|
| 300 | }
|
| 301 |
|
| 302 | // --------------------------------------------------------------------
|
| 303 |
|
| 304 | /**
|
| 305 | * Field data query
|
| 306 | *
|
| 307 | * Generates a platform-specific query so that the column data can be retrieved
|
| 308 | *
|
| 309 | * @access public
|
| 310 | * @param string the table name
|
| 311 | * @return object
|
| 312 | */
|
| 313 | function _field_data($table)
|
| 314 | {
|
| 315 | return "SELECT TOP 1 FROM ".$this->_escape_table($table);
|
| 316 | }
|
| 317 |
|
| 318 | // --------------------------------------------------------------------
|
| 319 |
|
| 320 | /**
|
| 321 | * The error message string
|
| 322 | *
|
| 323 | * @access private
|
| 324 | * @return string
|
| 325 | */
|
| 326 | function _error_message()
|
| 327 | {
|
| 328 | return odbc_errormsg($this->conn_id);
|
| 329 | }
|
| 330 |
|
| 331 | // --------------------------------------------------------------------
|
| 332 |
|
| 333 | /**
|
| 334 | * The error message number
|
| 335 | *
|
| 336 | * @access private
|
| 337 | * @return integer
|
| 338 | */
|
| 339 | function _error_number()
|
| 340 | {
|
| 341 | return odbc_error($this->conn_id);
|
| 342 | }
|
| 343 |
|
| 344 | // --------------------------------------------------------------------
|
| 345 |
|
| 346 | /**
|
| 347 | * Escape Table Name
|
| 348 | *
|
| 349 | * This function adds backticks if the table name has a period
|
| 350 | * in it. Some DBs will get cranky unless periods are escaped
|
| 351 | *
|
| 352 | * @access private
|
| 353 | * @param string the table name
|
| 354 | * @return string
|
| 355 | */
|
| 356 | function _escape_table($table)
|
| 357 | {
|
| 358 | if (stristr($table, '.'))
|
| 359 | {
|
| 360 | $table = preg_replace("/\./", "`.`", $table);
|
| 361 | }
|
| 362 |
|
| 363 | return $table;
|
| 364 | }
|
| 365 |
|
| 366 | // --------------------------------------------------------------------
|
| 367 |
|
| 368 | /**
|
| 369 | * Insert statement
|
| 370 | *
|
| 371 | * Generates a platform-specific insert string from the supplied data
|
| 372 | *
|
| 373 | * @access public
|
| 374 | * @param string the table name
|
| 375 | * @param array the insert keys
|
| 376 | * @param array the insert values
|
| 377 | * @return string
|
| 378 | */
|
| 379 | function _insert($table, $keys, $values)
|
| 380 | {
|
| 381 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
|
| 382 | }
|
| 383 |
|
| 384 | // --------------------------------------------------------------------
|
| 385 |
|
| 386 | /**
|
| 387 | * Update statement
|
| 388 | *
|
| 389 | * Generates a platform-specific update string from the supplied data
|
| 390 | *
|
| 391 | * @access public
|
| 392 | * @param string the table name
|
| 393 | * @param array the update data
|
| 394 | * @param array the where clause
|
| 395 | * @return string
|
| 396 | */
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame^] | 397 | function _update($table, $values, $where, $limit = FALSE)
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 398 | {
|
| 399 | foreach($values as $key => $val)
|
| 400 | {
|
| 401 | $valstr[] = $key." = ".$val;
|
| 402 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame^] | 403 |
|
| 404 | $limit = (!$limit) ? '' : ' LIMIT '.$limit;
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 405 |
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame^] | 406 | return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where).$limit;
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 407 | }
|
| 408 |
|
| 409 | // --------------------------------------------------------------------
|
| 410 |
|
| 411 | /**
|
| 412 | * Delete statement
|
| 413 | *
|
| 414 | * Generates a platform-specific delete string from the supplied data
|
| 415 | *
|
| 416 | * @access public
|
| 417 | * @param string the table name
|
| 418 | * @param array the where clause
|
| 419 | * @return string
|
| 420 | */
|
| 421 | function _delete($table, $where)
|
| 422 | {
|
| 423 | return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
|
| 424 | }
|
| 425 |
|
| 426 | // --------------------------------------------------------------------
|
| 427 |
|
| 428 | /**
|
| 429 | * Limit string
|
| 430 | *
|
| 431 | * Generates a platform-specific LIMIT clause
|
| 432 | *
|
| 433 | * @access public
|
| 434 | * @param string the sql query string
|
| 435 | * @param integer the number of rows to limit the query to
|
| 436 | * @param integer the offset value
|
| 437 | * @return string
|
| 438 | */
|
| 439 | function _limit($sql, $limit, $offset)
|
| 440 | {
|
| 441 | // Does ODBC doesn't use the LIMIT clause?
|
| 442 | return $sql;
|
| 443 | }
|
| 444 |
|
| 445 | // --------------------------------------------------------------------
|
| 446 |
|
| 447 | /**
|
| 448 | * Close DB Connection
|
| 449 | *
|
| 450 | * @access public
|
| 451 | * @param resource
|
| 452 | * @return void
|
| 453 | */
|
| 454 | function _close($conn_id)
|
| 455 | {
|
| 456 | @odbc_close($conn_id);
|
| 457 | }
|
| 458 |
|
| 459 |
|
| 460 | }
|
| 461 |
|
| 462 |
|
admin | 0d01169 | 2006-09-24 18:12:58 +0000 | [diff] [blame] | 463 | ?> |