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