admin | b0dd10f | 2006-08-25 17:25:49 +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. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * MS SQL 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/libraries/database/ |
| 30 | */ |
| 31 | class CI_DB_mssql 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 | { |
| 41 | return mssql_connect($this->hostname, $this->username, $this->password); |
| 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 | { |
| 54 | return mssql_pconnect($this->hostname, $this->username, $this->password); |
| 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 | return @mssql_select_db($this->database, $this->conn_id); |
| 68 | } |
| 69 | |
| 70 | // -------------------------------------------------------------------- |
| 71 | |
| 72 | /** |
| 73 | * Execute the query |
| 74 | * |
| 75 | * @access private called by the base class |
| 76 | * @param string an SQL query |
| 77 | * @return resource |
| 78 | */ |
admin | e885d78 | 2006-09-23 20:25:05 +0000 | [diff] [blame^] | 79 | function _execute($sql) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 80 | { |
| 81 | $sql = $this->_prep_query($sql); |
| 82 | return @mssql_query($sql, $this->conn_id); |
| 83 | } |
| 84 | |
| 85 | // -------------------------------------------------------------------- |
| 86 | |
| 87 | /** |
| 88 | * Prep the query |
| 89 | * |
| 90 | * If needed, each database adapter can prep the query string |
| 91 | * |
| 92 | * @access private called by execute() |
| 93 | * @param string an SQL query |
| 94 | * @return string |
| 95 | */ |
admin | b071bb5 | 2006-08-26 19:28:37 +0000 | [diff] [blame] | 96 | function _prep_query($sql) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 97 | { |
| 98 | return $sql; |
| 99 | } |
admin | e885d78 | 2006-09-23 20:25:05 +0000 | [diff] [blame^] | 100 | |
| 101 | // -------------------------------------------------------------------- |
| 102 | |
| 103 | /** |
| 104 | * Begin Transaction |
| 105 | * |
| 106 | * @access public |
| 107 | * @return bool |
| 108 | */ |
| 109 | function trans_begin() |
| 110 | { |
| 111 | if ( ! $this->trans_enabled) |
| 112 | { |
| 113 | return TRUE; |
| 114 | } |
| 115 | |
| 116 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 117 | if ($this->_trans_depth > 0) |
| 118 | { |
| 119 | return TRUE; |
| 120 | } |
| 121 | |
| 122 | $this->simple_query('BEGIN TRAN'); |
| 123 | return TRUE; |
| 124 | } |
| 125 | |
| 126 | // -------------------------------------------------------------------- |
| 127 | |
| 128 | /** |
| 129 | * Commit Transaction |
| 130 | * |
| 131 | * @access public |
| 132 | * @return bool |
| 133 | */ |
| 134 | function trans_commit() |
| 135 | { |
| 136 | if ( ! $this->trans_enabled) |
| 137 | { |
| 138 | return TRUE; |
| 139 | } |
| 140 | |
| 141 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 142 | if ($this->_trans_depth > 0) |
| 143 | { |
| 144 | return TRUE; |
| 145 | } |
| 146 | |
| 147 | $this->simple_query('COMMIT TRAN'); |
| 148 | return TRUE; |
| 149 | } |
| 150 | |
| 151 | // -------------------------------------------------------------------- |
| 152 | |
| 153 | /** |
| 154 | * Rollback Transaction |
| 155 | * |
| 156 | * @access public |
| 157 | * @return bool |
| 158 | */ |
| 159 | function trans_rollback() |
| 160 | { |
| 161 | if ( ! $this->trans_enabled) |
| 162 | { |
| 163 | return TRUE; |
| 164 | } |
| 165 | |
| 166 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 167 | if ($this->_trans_depth > 0) |
| 168 | { |
| 169 | return TRUE; |
| 170 | } |
| 171 | |
| 172 | $this->simple_query('ROLLBACK TRAN'); |
| 173 | return TRUE; |
| 174 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 175 | |
| 176 | // -------------------------------------------------------------------- |
| 177 | |
| 178 | /** |
| 179 | * Escape String |
| 180 | * |
| 181 | * @access public |
| 182 | * @param string |
| 183 | * @return string |
| 184 | */ |
| 185 | function escape_str($str) |
| 186 | { |
admin | 1082bdd | 2006-08-27 19:32:02 +0000 | [diff] [blame] | 187 | // Escape single quotes |
| 188 | return str_replace("'", "''", $str); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // -------------------------------------------------------------------- |
| 192 | |
| 193 | /** |
| 194 | * Close DB Connection |
| 195 | * |
| 196 | * @access public |
| 197 | * @param resource |
| 198 | * @return void |
| 199 | */ |
| 200 | function destroy($conn_id) |
| 201 | { |
| 202 | mssql_close($conn_id); |
| 203 | } |
| 204 | |
| 205 | // -------------------------------------------------------------------- |
| 206 | |
| 207 | /** |
| 208 | * Affected Rows |
| 209 | * |
| 210 | * @access public |
| 211 | * @return integer |
| 212 | */ |
| 213 | function affected_rows() |
| 214 | { |
| 215 | return @mssql_rows_affected($this->conn_id); |
| 216 | } |
| 217 | |
| 218 | // -------------------------------------------------------------------- |
| 219 | |
| 220 | /** |
| 221 | * Insert ID |
| 222 | * |
| 223 | * @access public |
| 224 | * @return integer |
| 225 | */ |
| 226 | function insert_id() |
| 227 | { |
| 228 | // Not supported in MS SQL? |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | // -------------------------------------------------------------------- |
| 233 | |
| 234 | /** |
| 235 | * "Count All" query |
| 236 | * |
| 237 | * Generates a platform-specific query string that counts all records in |
| 238 | * the specified database |
| 239 | * |
| 240 | * @access public |
| 241 | * @param string |
| 242 | * @return string |
| 243 | */ |
| 244 | function count_all($table = '') |
| 245 | { |
| 246 | if ($table == '') |
| 247 | return '0'; |
| 248 | |
| 249 | $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`"); |
| 250 | |
| 251 | if ($query->num_rows() == 0) |
| 252 | return '0'; |
| 253 | |
| 254 | $row = $query->row(); |
| 255 | return $row->numrows; |
| 256 | } |
| 257 | |
| 258 | // -------------------------------------------------------------------- |
| 259 | |
| 260 | /** |
| 261 | * The error message string |
| 262 | * |
| 263 | * @access public |
| 264 | * @return string |
| 265 | */ |
| 266 | function error_message() |
| 267 | { |
| 268 | // Are errros even supported in MS SQL? |
| 269 | return ''; |
| 270 | } |
| 271 | |
| 272 | // -------------------------------------------------------------------- |
| 273 | |
| 274 | /** |
| 275 | * The error message number |
| 276 | * |
| 277 | * @access public |
| 278 | * @return integer |
| 279 | */ |
| 280 | function error_number() |
| 281 | { |
| 282 | // Are error numbers supported? |
| 283 | return ''; |
| 284 | } |
| 285 | |
| 286 | // -------------------------------------------------------------------- |
| 287 | |
| 288 | /** |
| 289 | * Escape Table Name |
| 290 | * |
| 291 | * This function adds backticks if the table name has a period |
| 292 | * in it. Some DBs will get cranky unless periods are escaped |
| 293 | * |
| 294 | * @access public |
| 295 | * @param string the table name |
| 296 | * @return string |
| 297 | */ |
| 298 | function escape_table($table) |
| 299 | { |
| 300 | if (stristr($table, '.')) |
| 301 | { |
| 302 | $table = preg_replace("/\./", "`.`", $table); |
| 303 | } |
| 304 | |
| 305 | return $table; |
| 306 | } |
| 307 | |
| 308 | // -------------------------------------------------------------------- |
| 309 | |
| 310 | /** |
| 311 | * Field data query |
| 312 | * |
| 313 | * Generates a platform-specific query so that the column data can be retrieved |
| 314 | * |
| 315 | * @access public |
| 316 | * @param string the table name |
| 317 | * @return object |
| 318 | */ |
| 319 | function _field_data($table) |
| 320 | { |
| 321 | $sql = "SELECT TOP 1 FROM ".$this->escape_table($table); |
| 322 | $query = $this->query($sql); |
| 323 | return $query->field_data(); |
| 324 | } |
| 325 | |
| 326 | // -------------------------------------------------------------------- |
| 327 | |
| 328 | /** |
| 329 | * Insert statement |
| 330 | * |
| 331 | * Generates a platform-specific insert string from the supplied data |
| 332 | * |
| 333 | * @access public |
| 334 | * @param string the table name |
| 335 | * @param array the insert keys |
| 336 | * @param array the insert values |
| 337 | * @return string |
| 338 | */ |
| 339 | function _insert($table, $keys, $values) |
| 340 | { |
| 341 | return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 342 | } |
| 343 | |
| 344 | // -------------------------------------------------------------------- |
| 345 | |
| 346 | /** |
| 347 | * Update statement |
| 348 | * |
| 349 | * Generates a platform-specific update string from the supplied data |
| 350 | * |
| 351 | * @access public |
| 352 | * @param string the table name |
| 353 | * @param array the update data |
| 354 | * @param array the where clause |
| 355 | * @return string |
| 356 | */ |
| 357 | function _update($table, $values, $where) |
| 358 | { |
| 359 | foreach($values as $key => $val) |
| 360 | { |
| 361 | $valstr[] = $key." = ".$val; |
| 362 | } |
| 363 | |
| 364 | return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where); |
| 365 | } |
| 366 | |
| 367 | // -------------------------------------------------------------------- |
| 368 | |
| 369 | /** |
| 370 | * Delete statement |
| 371 | * |
| 372 | * Generates a platform-specific delete string from the supplied data |
| 373 | * |
| 374 | * @access public |
| 375 | * @param string the table name |
| 376 | * @param array the where clause |
| 377 | * @return string |
| 378 | */ |
| 379 | function _delete($table, $where) |
| 380 | { |
| 381 | return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where); |
| 382 | } |
| 383 | |
| 384 | // -------------------------------------------------------------------- |
| 385 | |
| 386 | /** |
| 387 | * Version number query string |
| 388 | * |
| 389 | * @access public |
| 390 | * @return string |
| 391 | */ |
| 392 | function _version() |
| 393 | { |
| 394 | return "SELECT version() AS ver"; |
| 395 | } |
| 396 | |
| 397 | // -------------------------------------------------------------------- |
| 398 | |
| 399 | /** |
| 400 | * Show table query |
| 401 | * |
| 402 | * Generates a platform-specific query string so that the table names can be fetched |
| 403 | * |
| 404 | * @access public |
| 405 | * @return string |
| 406 | */ |
| 407 | function _show_tables() |
| 408 | { |
| 409 | return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
| 410 | } |
| 411 | |
| 412 | // -------------------------------------------------------------------- |
| 413 | |
| 414 | /** |
| 415 | * Show columnn query |
| 416 | * |
| 417 | * Generates a platform-specific query string so that the column names can be fetched |
| 418 | * |
| 419 | * @access public |
| 420 | * @param string the table name |
| 421 | * @return string |
| 422 | */ |
| 423 | function _show_columns($table = '') |
| 424 | { |
| 425 | return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->escape_table($table)."'"; |
| 426 | } |
| 427 | |
| 428 | // -------------------------------------------------------------------- |
| 429 | |
| 430 | /** |
| 431 | * Limit string |
| 432 | * |
| 433 | * Generates a platform-specific LIMIT clause |
| 434 | * |
| 435 | * @access public |
| 436 | * @param string the sql query string |
| 437 | * @param integer the number of rows to limit the query to |
| 438 | * @param integer the offset value |
| 439 | * @return string |
| 440 | */ |
| 441 | function _limit($sql, $limit, $offset) |
| 442 | { |
| 443 | $i = $limit + $offset; |
| 444 | |
| 445 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql); |
| 446 | } |
| 447 | |
| 448 | } |
| 449 | |
| 450 | |
| 451 | |
| 452 | /** |
| 453 | * MS SQL Result Class |
| 454 | * |
| 455 | * This class extends the parent result class: CI_DB_result |
| 456 | * |
| 457 | * @category Database |
| 458 | * @author Rick Ellis |
| 459 | * @link http://www.codeigniter.com/user_guide/libraries/database/ |
| 460 | */ |
| 461 | class CI_DB_mssql_result extends CI_DB_result { |
| 462 | |
| 463 | /** |
| 464 | * Number of rows in the result set |
| 465 | * |
| 466 | * @access public |
| 467 | * @return integer |
| 468 | */ |
| 469 | function num_rows() |
| 470 | { |
| 471 | return @mssql_num_rows($this->result_id); |
| 472 | } |
| 473 | |
| 474 | // -------------------------------------------------------------------- |
| 475 | |
| 476 | /** |
| 477 | * Number of fields in the result set |
| 478 | * |
| 479 | * @access public |
| 480 | * @return integer |
| 481 | */ |
| 482 | function num_fields() |
| 483 | { |
| 484 | return @mssql_num_fields($this->result_id); |
| 485 | } |
| 486 | |
| 487 | // -------------------------------------------------------------------- |
| 488 | |
| 489 | /** |
| 490 | * Field data |
| 491 | * |
| 492 | * Generates an array of objects containing field meta-data |
| 493 | * |
| 494 | * @access public |
| 495 | * @return array |
| 496 | */ |
| 497 | function field_data() |
| 498 | { |
| 499 | $retval = array(); |
| 500 | while ($field = mssql_fetch_field($this->result_id)) |
| 501 | { |
admin | e348efb | 2006-09-20 21:13:26 +0000 | [diff] [blame] | 502 | $F = new stdClass(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 503 | $F->name = $field->name; |
| 504 | $F->type = $field->type; |
| 505 | $F->max_length = $field->max_length; |
| 506 | $F->primary_key = 0; |
| 507 | $F->default = ''; |
| 508 | |
| 509 | $retval[] = $F; |
| 510 | } |
| 511 | |
| 512 | return $retval; |
| 513 | } |
| 514 | |
| 515 | // -------------------------------------------------------------------- |
| 516 | |
| 517 | /** |
| 518 | * Result - associative array |
| 519 | * |
| 520 | * Returns the result set as an array |
| 521 | * |
| 522 | * @access private |
| 523 | * @return array |
| 524 | */ |
| 525 | function _fetch_assoc() |
| 526 | { |
| 527 | return mssql_fetch_assoc($this->result_id); |
| 528 | } |
| 529 | |
| 530 | // -------------------------------------------------------------------- |
| 531 | |
| 532 | /** |
| 533 | * Result - object |
| 534 | * |
| 535 | * Returns the result set as an object |
| 536 | * |
| 537 | * @access private |
| 538 | * @return object |
| 539 | */ |
| 540 | function _fetch_object() |
| 541 | { |
| 542 | return mssql_fetch_object($this->result_id); |
| 543 | } |
| 544 | |
| 545 | } |
| 546 | |
| 547 | ?> |