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