Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Rick Ellis | 9ba2bf2 | 2008-09-12 23:34:39 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 28 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 29 | * @link http://codeigniter.com/user_guide/database/
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 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 | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 38 | var $_count_string = "SELECT COUNT(*) AS ";
|
| 39 | var $_random_keyword;
|
| 40 |
|
| 41 |
|
Rick Ellis | ceb6f0b | 2008-10-07 00:48:19 +0000 | [diff] [blame^] | 42 | function CI_DB_odbc_driver($params)
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 43 | {
|
Rick Ellis | ceb6f0b | 2008-10-07 00:48:19 +0000 | [diff] [blame^] | 44 | if (is_array($params))
|
| 45 | {
|
| 46 | foreach ($params as $key => $val)
|
| 47 | {
|
| 48 | $this->$key = $val;
|
| 49 | }
|
| 50 | }
|
| 51 |
|
Derek Allard | b52bdc4 | 2008-01-27 14:58:24 +0000 | [diff] [blame] | 52 | $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 53 | }
|
Derek Allard | 694b5b8 | 2007-12-18 15:58:03 +0000 | [diff] [blame] | 54 |
|
| 55 | /**
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 56 | * Non-persistent database connection
|
| 57 | *
|
| 58 | * @access private called by the base class
|
| 59 | * @return resource
|
| 60 | */
|
| 61 | function db_connect()
|
| 62 | {
|
| 63 | return @odbc_connect($this->hostname, $this->username, $this->password);
|
| 64 | }
|
| 65 |
|
| 66 | // --------------------------------------------------------------------
|
| 67 |
|
| 68 | /**
|
| 69 | * Persistent database connection
|
| 70 | *
|
| 71 | * @access private called by the base class
|
| 72 | * @return resource
|
| 73 | */
|
| 74 | function db_pconnect()
|
| 75 | {
|
| 76 | return @odbc_pconnect($this->hostname, $this->username, $this->password);
|
| 77 | }
|
| 78 |
|
| 79 | // --------------------------------------------------------------------
|
| 80 |
|
| 81 | /**
|
| 82 | * Select the database
|
| 83 | *
|
| 84 | * @access private called by the base class
|
| 85 | * @return resource
|
| 86 | */
|
| 87 | function db_select()
|
| 88 | {
|
| 89 | // Not needed for ODBC
|
| 90 | return TRUE;
|
| 91 | }
|
| 92 |
|
| 93 | // --------------------------------------------------------------------
|
| 94 |
|
| 95 | /**
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 96 | * Set client character set
|
| 97 | *
|
| 98 | * @access public
|
| 99 | * @param string
|
| 100 | * @param string
|
| 101 | * @return resource
|
| 102 | */
|
| 103 | function db_set_charset($charset, $collation)
|
| 104 | {
|
Rick Ellis | ff73401 | 2008-09-30 20:38:12 +0000 | [diff] [blame] | 105 | // @todo - add support if needed
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 106 | return TRUE;
|
| 107 | }
|
| 108 |
|
| 109 | // --------------------------------------------------------------------
|
| 110 |
|
| 111 | /**
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 112 | * Version number query string
|
| 113 | *
|
| 114 | * @access public
|
| 115 | * @return string
|
| 116 | */
|
| 117 | function _version()
|
| 118 | {
|
| 119 | return "SELECT version() AS ver";
|
| 120 | }
|
| 121 |
|
| 122 | // --------------------------------------------------------------------
|
| 123 |
|
| 124 | /**
|
| 125 | * Execute the query
|
| 126 | *
|
| 127 | * @access private called by the base class
|
| 128 | * @param string an SQL query
|
| 129 | * @return resource
|
| 130 | */
|
| 131 | function _execute($sql)
|
| 132 | {
|
| 133 | $sql = $this->_prep_query($sql);
|
| 134 | return @odbc_exec($this->conn_id, $sql);
|
| 135 | }
|
| 136 |
|
| 137 | // --------------------------------------------------------------------
|
| 138 |
|
| 139 | /**
|
| 140 | * Prep the query
|
| 141 | *
|
| 142 | * If needed, each database adapter can prep the query string
|
| 143 | *
|
| 144 | * @access private called by execute()
|
| 145 | * @param string an SQL query
|
| 146 | * @return string
|
| 147 | */
|
| 148 | function _prep_query($sql)
|
| 149 | {
|
| 150 | return $sql;
|
| 151 | }
|
| 152 |
|
| 153 | // --------------------------------------------------------------------
|
| 154 |
|
| 155 | /**
|
| 156 | * Begin Transaction
|
| 157 | *
|
| 158 | * @access public
|
| 159 | * @return bool
|
| 160 | */
|
| 161 | function trans_begin($test_mode = FALSE)
|
| 162 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 163 | if ( ! $this->trans_enabled)
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 164 | {
|
| 165 | return TRUE;
|
| 166 | }
|
| 167 |
|
| 168 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 169 | if ($this->_trans_depth > 0)
|
| 170 | {
|
| 171 | return TRUE;
|
| 172 | }
|
| 173 |
|
| 174 | // Reset the transaction failure flag.
|
| 175 | // If the $test_mode flag is set to TRUE transactions will be rolled back
|
| 176 | // even if the queries produce a successful result.
|
| 177 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
|
| 178 |
|
| 179 | return odbc_autocommit($this->conn_id, FALSE);
|
| 180 | }
|
| 181 |
|
| 182 | // --------------------------------------------------------------------
|
| 183 |
|
| 184 | /**
|
| 185 | * Commit Transaction
|
| 186 | *
|
| 187 | * @access public
|
| 188 | * @return bool
|
| 189 | */
|
| 190 | function trans_commit()
|
| 191 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 192 | if ( ! $this->trans_enabled)
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 193 | {
|
| 194 | return TRUE;
|
| 195 | }
|
| 196 |
|
| 197 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 198 | if ($this->_trans_depth > 0)
|
| 199 | {
|
| 200 | return TRUE;
|
| 201 | }
|
| 202 |
|
| 203 | $ret = odbc_commit($this->conn_id);
|
| 204 | odbc_autocommit($this->conn_id, TRUE);
|
| 205 | return $ret;
|
| 206 | }
|
| 207 |
|
| 208 | // --------------------------------------------------------------------
|
| 209 |
|
| 210 | /**
|
| 211 | * Rollback Transaction
|
| 212 | *
|
| 213 | * @access public
|
| 214 | * @return bool
|
| 215 | */
|
| 216 | function trans_rollback()
|
| 217 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 218 | if ( ! $this->trans_enabled)
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 219 | {
|
| 220 | return TRUE;
|
| 221 | }
|
| 222 |
|
| 223 | // When transactions are nested we only begin/commit/rollback the outermost ones
|
| 224 | if ($this->_trans_depth > 0)
|
| 225 | {
|
| 226 | return TRUE;
|
| 227 | }
|
| 228 |
|
| 229 | $ret = odbc_rollback($this->conn_id);
|
| 230 | odbc_autocommit($this->conn_id, TRUE);
|
| 231 | return $ret;
|
| 232 | }
|
| 233 |
|
| 234 | // --------------------------------------------------------------------
|
| 235 |
|
| 236 | /**
|
| 237 | * Escape String
|
| 238 | *
|
| 239 | * @access public
|
| 240 | * @param string
|
| 241 | * @return string
|
| 242 | */
|
| 243 | function escape_str($str)
|
| 244 | {
|
| 245 | // ODBC doesn't require escaping
|
Rick Ellis | 2cad6e9 | 2008-10-07 00:19:34 +0000 | [diff] [blame] | 246 | return $this->input->CI->_remove_invisible_characters($str);
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 247 | }
|
| 248 |
|
| 249 | // --------------------------------------------------------------------
|
| 250 |
|
| 251 | /**
|
| 252 | * Affected Rows
|
| 253 | *
|
| 254 | * @access public
|
| 255 | * @return integer
|
| 256 | */
|
| 257 | function affected_rows()
|
| 258 | {
|
| 259 | return @odbc_num_rows($this->conn_id);
|
| 260 | }
|
| 261 |
|
| 262 | // --------------------------------------------------------------------
|
| 263 |
|
| 264 | /**
|
| 265 | * Insert ID
|
| 266 | *
|
| 267 | * @access public
|
| 268 | * @return integer
|
| 269 | */
|
| 270 | function insert_id()
|
| 271 | {
|
| 272 | return @odbc_insert_id($this->conn_id);
|
| 273 | }
|
| 274 |
|
| 275 | // --------------------------------------------------------------------
|
| 276 |
|
| 277 | /**
|
| 278 | * "Count All" query
|
| 279 | *
|
| 280 | * Generates a platform-specific query string that counts all records in
|
| 281 | * the specified database
|
| 282 | *
|
| 283 | * @access public
|
| 284 | * @param string
|
| 285 | * @return string
|
| 286 | */
|
| 287 | function count_all($table = '')
|
| 288 | {
|
| 289 | if ($table == '')
|
| 290 | return '0';
|
| 291 |
|
Derek Allard | f6cd45c | 2008-01-18 14:31:51 +0000 | [diff] [blame] | 292 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
|
| 293 |
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 294 | if ($query->num_rows() == 0)
|
| 295 | return '0';
|
| 296 |
|
| 297 | $row = $query->row();
|
| 298 | return $row->numrows;
|
| 299 | }
|
| 300 |
|
| 301 | // --------------------------------------------------------------------
|
| 302 |
|
| 303 | /**
|
| 304 | * Show table query
|
| 305 | *
|
| 306 | * Generates a platform-specific query string so that the table names can be fetched
|
| 307 | *
|
| 308 | * @access private
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 309 | * @param boolean
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 310 | * @return string
|
| 311 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 312 | function _list_tables($prefix_limit = FALSE)
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 313 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 314 | $sql = "SHOW TABLES FROM `".$this->database."`";
|
| 315 |
|
| 316 | if ($prefix_limit !== FALSE AND $this->dbprefix != '')
|
| 317 | {
|
| 318 | //$sql .= " LIKE '".$this->dbprefix."%'";
|
| 319 | return FALSE; // not currently supported
|
| 320 | }
|
| 321 |
|
| 322 | return $sql;
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 323 | }
|
| 324 |
|
| 325 | // --------------------------------------------------------------------
|
| 326 |
|
| 327 | /**
|
| 328 | * Show column query
|
| 329 | *
|
| 330 | * Generates a platform-specific query string so that the column names can be fetched
|
| 331 | *
|
| 332 | * @access public
|
| 333 | * @param string the table name
|
| 334 | * @return string
|
| 335 | */
|
| 336 | function _list_columns($table = '')
|
| 337 | {
|
| 338 | return "SHOW COLUMNS FROM ".$this->_escape_table($table);
|
| 339 | }
|
| 340 |
|
| 341 | // --------------------------------------------------------------------
|
| 342 |
|
| 343 | /**
|
| 344 | * Field data query
|
| 345 | *
|
| 346 | * Generates a platform-specific query so that the column data can be retrieved
|
| 347 | *
|
| 348 | * @access public
|
| 349 | * @param string the table name
|
| 350 | * @return object
|
| 351 | */
|
| 352 | function _field_data($table)
|
| 353 | {
|
| 354 | return "SELECT TOP 1 FROM ".$this->_escape_table($table);
|
| 355 | }
|
| 356 |
|
| 357 | // --------------------------------------------------------------------
|
| 358 |
|
| 359 | /**
|
| 360 | * The error message string
|
| 361 | *
|
| 362 | * @access private
|
| 363 | * @return string
|
| 364 | */
|
| 365 | function _error_message()
|
| 366 | {
|
| 367 | return odbc_errormsg($this->conn_id);
|
| 368 | }
|
| 369 |
|
| 370 | // --------------------------------------------------------------------
|
| 371 |
|
| 372 | /**
|
| 373 | * The error message number
|
| 374 | *
|
| 375 | * @access private
|
| 376 | * @return integer
|
| 377 | */
|
| 378 | function _error_number()
|
| 379 | {
|
| 380 | return odbc_error($this->conn_id);
|
| 381 | }
|
Rick Ellis | 52dc8ca | 2008-09-30 19:53:52 +0000 | [diff] [blame] | 382 | // --------------------------------------------------------------------
|
| 383 |
|
| 384 | /**
|
| 385 | * Escape Column Name
|
| 386 | *
|
| 387 | * This function adds backticks around supplied column name
|
| 388 | *
|
| 389 | * @access private
|
| 390 | * @param string the column name
|
| 391 | * @return string
|
| 392 | */
|
| 393 | function _escape_column($column)
|
| 394 | {
|
| 395 | // Not necessary with ODBC so we simply return the value
|
| 396 | return $column;
|
| 397 | }
|
| 398 |
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 399 | // --------------------------------------------------------------------
|
| 400 |
|
| 401 | /**
|
| 402 | * Escape Table Name
|
| 403 | *
|
| 404 | * This function adds backticks if the table name has a period
|
| 405 | * in it. Some DBs will get cranky unless periods are escaped
|
| 406 | *
|
| 407 | * @access private
|
| 408 | * @param string the table name
|
| 409 | * @return string
|
| 410 | */
|
| 411 | function _escape_table($table)
|
| 412 | {
|
Rick Ellis | 52dc8ca | 2008-09-30 19:53:52 +0000 | [diff] [blame] | 413 | // Not necessary with ODBC so we simply return the value
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 414 | return $table;
|
Rick Ellis | 52dc8ca | 2008-09-30 19:53:52 +0000 | [diff] [blame] | 415 | }
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 416 |
|
| 417 | // --------------------------------------------------------------------
|
| 418 |
|
| 419 | /**
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 420 | * Protect Identifiers
|
| 421 | *
|
| 422 | * This function adds backticks if appropriate based on db type
|
| 423 | *
|
| 424 | * @access private
|
| 425 | * @param mixed the item to escape
|
| 426 | * @param boolean only affect the first word
|
| 427 | * @return mixed the item with backticks
|
| 428 | */
|
| 429 | function _protect_identifiers($item, $first_word_only = FALSE)
|
| 430 | {
|
| 431 | if (is_array($item))
|
| 432 | {
|
| 433 | $escaped_array = array();
|
| 434 |
|
| 435 | foreach($item as $k=>$v)
|
| 436 | {
|
| 437 | $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
|
| 438 | }
|
| 439 |
|
| 440 | return $escaped_array;
|
| 441 | }
|
| 442 |
|
| 443 | // This function may get "item1 item2" as a string, and so
|
| 444 | // we may need "`item1` `item2`" and not "`item1 item2`"
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 445 | if (ctype_alnum($item) === FALSE)
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 446 | {
|
Derek Allard | 9b3e7b5 | 2008-02-04 23:20:34 +0000 | [diff] [blame] | 447 | if (strpos($item, '.') !== FALSE)
|
| 448 | {
|
| 449 | $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
|
| 450 | $table_name = substr($item, 0, strpos($item, '.')+1);
|
| 451 | $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
|
| 452 | }
|
| 453 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 454 | // This function may get "field >= 1", and need it to return "`field` >= 1"
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 455 | $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 456 |
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 457 | $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
|
| 458 | }
|
| 459 | else
|
| 460 | {
|
Derek Allard | 903cc98 | 2008-02-11 06:06:59 +0000 | [diff] [blame] | 461 | return "{$item}";
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 462 | }
|
| 463 |
|
Derek Allard | 9a4d1da | 2008-02-25 14:18:38 +0000 | [diff] [blame] | 464 | $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 465 |
|
| 466 | foreach ($exceptions as $exception)
|
| 467 | {
|
Derek Allard | 6157938 | 2008-01-16 22:22:42 +0000 | [diff] [blame] | 468 |
|
Derek Allard | 903cc98 | 2008-02-11 06:06:59 +0000 | [diff] [blame] | 469 | if (stristr($item, " {$exception} ") !== FALSE)
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 470 | {
|
Derek Allard | 903cc98 | 2008-02-11 06:06:59 +0000 | [diff] [blame] | 471 | $item = preg_replace('/ ('.preg_quote($exception).') /i', ' $1 ', $item);
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 472 | }
|
| 473 | }
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 474 | return $item;
|
| 475 | }
|
| 476 |
|
| 477 | // --------------------------------------------------------------------
|
| 478 |
|
| 479 | /**
|
Derek Jones | c6ad023 | 2008-01-29 18:44:54 +0000 | [diff] [blame] | 480 | * From Tables
|
| 481 | *
|
| 482 | * This function implicitly groups FROM tables so there is no confusion
|
| 483 | * about operator precedence in harmony with SQL standards
|
| 484 | *
|
| 485 | * @access public
|
| 486 | * @param type
|
| 487 | * @return type
|
| 488 | */
|
| 489 | function _from_tables($tables)
|
| 490 | {
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 491 | if ( ! is_array($tables))
|
Derek Jones | c6ad023 | 2008-01-29 18:44:54 +0000 | [diff] [blame] | 492 | {
|
| 493 | $tables = array($tables);
|
| 494 | }
|
| 495 |
|
| 496 | return '('.implode(', ', $tables).')';
|
| 497 | }
|
| 498 |
|
| 499 | // --------------------------------------------------------------------
|
| 500 |
|
| 501 | /**
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 502 | * Insert statement
|
| 503 | *
|
| 504 | * Generates a platform-specific insert string from the supplied data
|
| 505 | *
|
| 506 | * @access public
|
| 507 | * @param string the table name
|
| 508 | * @param array the insert keys
|
| 509 | * @param array the insert values
|
| 510 | * @return string
|
| 511 | */
|
| 512 | function _insert($table, $keys, $values)
|
| 513 | {
|
| 514 | return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
|
| 515 | }
|
| 516 |
|
| 517 | // --------------------------------------------------------------------
|
| 518 |
|
| 519 | /**
|
| 520 | * Update statement
|
| 521 | *
|
| 522 | * Generates a platform-specific update string from the supplied data
|
| 523 | *
|
| 524 | * @access public
|
| 525 | * @param string the table name
|
| 526 | * @param array the update data
|
| 527 | * @param array the where clause
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 528 | * @param array the orderby clause
|
| 529 | * @param array the limit clause
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 530 | * @return string
|
| 531 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 532 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 533 | {
|
| 534 | foreach($values as $key => $val)
|
| 535 | {
|
| 536 | $valstr[] = $key." = ".$val;
|
| 537 | }
|
Derek Allard | da6d240 | 2007-12-19 14:49:29 +0000 | [diff] [blame] | 538 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 539 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 540 |
|
| 541 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 542 |
|
Derek Allard | 32cf7eb | 2008-02-05 16:03:50 +0000 | [diff] [blame] | 543 | $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
|
| 544 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
|
| 545 | $sql .= $orderby.$limit;
|
| 546 |
|
| 547 | return $sql;
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 548 | }
|
| 549 |
|
| 550 |
|
| 551 | // --------------------------------------------------------------------
|
| 552 |
|
| 553 | /**
|
| 554 | * Truncate statement
|
| 555 | *
|
| 556 | * Generates a platform-specific truncate string from the supplied data
|
| 557 | * If the database does not support the truncate() command
|
| 558 | * This function maps to "DELETE FROM table"
|
| 559 | *
|
| 560 | * @access public
|
| 561 | * @param string the table name
|
| 562 | * @return string
|
| 563 | */
|
| 564 | function _truncate($table)
|
| 565 | {
|
| 566 | return $this->_delete($table);
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 567 | }
|
| 568 |
|
| 569 | // --------------------------------------------------------------------
|
| 570 |
|
| 571 | /**
|
| 572 | * Delete statement
|
| 573 | *
|
| 574 | * Generates a platform-specific delete string from the supplied data
|
| 575 | *
|
| 576 | * @access public
|
| 577 | * @param string the table name
|
| 578 | * @param array the where clause
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 579 | * @param string the limit clause
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 580 | * @return string
|
| 581 | */
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 582 | function _delete($table, $where = array(), $like = array(), $limit = FALSE)
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 583 | {
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 584 | $conditions = '';
|
| 585 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 586 | if (count($where) > 0 OR count($like) > 0)
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 587 | {
|
| 588 | $conditions = "\nWHERE ";
|
| 589 | $conditions .= implode("\n", $this->ar_where);
|
| 590 |
|
| 591 | if (count($where) > 0 && count($like) > 0)
|
| 592 | {
|
| 593 | $conditions .= " AND ";
|
| 594 | }
|
| 595 | $conditions .= implode("\n", $like);
|
| 596 | }
|
| 597 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 598 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
|
Derek Allard | e77d77c | 2007-12-19 15:01:55 +0000 | [diff] [blame] | 599 |
|
Derek Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 600 | return "DELETE FROM ".$table.$conditions.$limit;
|
Derek Allard | 5c3905b | 2007-03-24 11:08:55 +0000 | [diff] [blame] | 601 | }
|
| 602 |
|
| 603 | // --------------------------------------------------------------------
|
| 604 |
|
| 605 | /**
|
| 606 | * Limit string
|
| 607 | *
|
| 608 | * Generates a platform-specific LIMIT clause
|
| 609 | *
|
| 610 | * @access public
|
| 611 | * @param string the sql query string
|
| 612 | * @param integer the number of rows to limit the query to
|
| 613 | * @param integer the offset value
|
| 614 | * @return string
|
| 615 | */
|
| 616 | function _limit($sql, $limit, $offset)
|
| 617 | {
|
| 618 | // Does ODBC doesn't use the LIMIT clause?
|
| 619 | return $sql;
|
| 620 | }
|
| 621 |
|
| 622 | // --------------------------------------------------------------------
|
| 623 |
|
| 624 | /**
|
| 625 | * Close DB Connection
|
| 626 | *
|
| 627 | * @access public
|
| 628 | * @param resource
|
| 629 | * @return void
|
| 630 | */
|
| 631 | function _close($conn_id)
|
| 632 | {
|
| 633 | @odbc_close($conn_id);
|
| 634 | }
|
| 635 |
|
| 636 |
|
| 637 | }
|
| 638 |
|
| 639 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 640 |
|
| 641 | /* End of file odbc_driver.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 642 | /* Location: ./system/database/drivers/odbc/odbc_driver.php */ |