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 | */ |
| 79 | function execute($sql) |
| 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 | } |
| 100 | |
| 101 | // -------------------------------------------------------------------- |
| 102 | |
| 103 | /** |
| 104 | * Escape String |
| 105 | * |
| 106 | * @access public |
| 107 | * @param string |
| 108 | * @return string |
| 109 | */ |
| 110 | function escape_str($str) |
| 111 | { |
admin | 1082bdd | 2006-08-27 19:32:02 +0000 | [diff] [blame] | 112 | // Escape single quotes |
| 113 | return str_replace("'", "''", $str); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // -------------------------------------------------------------------- |
| 117 | |
| 118 | /** |
| 119 | * Close DB Connection |
| 120 | * |
| 121 | * @access public |
| 122 | * @param resource |
| 123 | * @return void |
| 124 | */ |
| 125 | function destroy($conn_id) |
| 126 | { |
| 127 | mssql_close($conn_id); |
| 128 | } |
| 129 | |
| 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | /** |
| 133 | * Affected Rows |
| 134 | * |
| 135 | * @access public |
| 136 | * @return integer |
| 137 | */ |
| 138 | function affected_rows() |
| 139 | { |
| 140 | return @mssql_rows_affected($this->conn_id); |
| 141 | } |
| 142 | |
| 143 | // -------------------------------------------------------------------- |
| 144 | |
| 145 | /** |
| 146 | * Insert ID |
| 147 | * |
| 148 | * @access public |
| 149 | * @return integer |
| 150 | */ |
| 151 | function insert_id() |
| 152 | { |
| 153 | // Not supported in MS SQL? |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | // -------------------------------------------------------------------- |
| 158 | |
| 159 | /** |
| 160 | * "Count All" query |
| 161 | * |
| 162 | * Generates a platform-specific query string that counts all records in |
| 163 | * the specified database |
| 164 | * |
| 165 | * @access public |
| 166 | * @param string |
| 167 | * @return string |
| 168 | */ |
| 169 | function count_all($table = '') |
| 170 | { |
| 171 | if ($table == '') |
| 172 | return '0'; |
| 173 | |
| 174 | $query = $this->query("SELECT COUNT(*) AS numrows FROM `".$this->dbprefix.$table."`"); |
| 175 | |
| 176 | if ($query->num_rows() == 0) |
| 177 | return '0'; |
| 178 | |
| 179 | $row = $query->row(); |
| 180 | return $row->numrows; |
| 181 | } |
| 182 | |
| 183 | // -------------------------------------------------------------------- |
| 184 | |
| 185 | /** |
| 186 | * The error message string |
| 187 | * |
| 188 | * @access public |
| 189 | * @return string |
| 190 | */ |
| 191 | function error_message() |
| 192 | { |
| 193 | // Are errros even supported in MS SQL? |
| 194 | return ''; |
| 195 | } |
| 196 | |
| 197 | // -------------------------------------------------------------------- |
| 198 | |
| 199 | /** |
| 200 | * The error message number |
| 201 | * |
| 202 | * @access public |
| 203 | * @return integer |
| 204 | */ |
| 205 | function error_number() |
| 206 | { |
| 207 | // Are error numbers supported? |
| 208 | return ''; |
| 209 | } |
| 210 | |
| 211 | // -------------------------------------------------------------------- |
| 212 | |
| 213 | /** |
| 214 | * Escape Table Name |
| 215 | * |
| 216 | * This function adds backticks if the table name has a period |
| 217 | * in it. Some DBs will get cranky unless periods are escaped |
| 218 | * |
| 219 | * @access public |
| 220 | * @param string the table name |
| 221 | * @return string |
| 222 | */ |
| 223 | function escape_table($table) |
| 224 | { |
| 225 | if (stristr($table, '.')) |
| 226 | { |
| 227 | $table = preg_replace("/\./", "`.`", $table); |
| 228 | } |
| 229 | |
| 230 | return $table; |
| 231 | } |
| 232 | |
| 233 | // -------------------------------------------------------------------- |
| 234 | |
| 235 | /** |
| 236 | * Field data query |
| 237 | * |
| 238 | * Generates a platform-specific query so that the column data can be retrieved |
| 239 | * |
| 240 | * @access public |
| 241 | * @param string the table name |
| 242 | * @return object |
| 243 | */ |
| 244 | function _field_data($table) |
| 245 | { |
| 246 | $sql = "SELECT TOP 1 FROM ".$this->escape_table($table); |
| 247 | $query = $this->query($sql); |
| 248 | return $query->field_data(); |
| 249 | } |
| 250 | |
| 251 | // -------------------------------------------------------------------- |
| 252 | |
| 253 | /** |
| 254 | * Insert statement |
| 255 | * |
| 256 | * Generates a platform-specific insert string from the supplied data |
| 257 | * |
| 258 | * @access public |
| 259 | * @param string the table name |
| 260 | * @param array the insert keys |
| 261 | * @param array the insert values |
| 262 | * @return string |
| 263 | */ |
| 264 | function _insert($table, $keys, $values) |
| 265 | { |
| 266 | return "INSERT INTO ".$this->escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
| 267 | } |
| 268 | |
| 269 | // -------------------------------------------------------------------- |
| 270 | |
| 271 | /** |
| 272 | * Update statement |
| 273 | * |
| 274 | * Generates a platform-specific update string from the supplied data |
| 275 | * |
| 276 | * @access public |
| 277 | * @param string the table name |
| 278 | * @param array the update data |
| 279 | * @param array the where clause |
| 280 | * @return string |
| 281 | */ |
| 282 | function _update($table, $values, $where) |
| 283 | { |
| 284 | foreach($values as $key => $val) |
| 285 | { |
| 286 | $valstr[] = $key." = ".$val; |
| 287 | } |
| 288 | |
| 289 | return "UPDATE ".$this->escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where); |
| 290 | } |
| 291 | |
| 292 | // -------------------------------------------------------------------- |
| 293 | |
| 294 | /** |
| 295 | * Delete statement |
| 296 | * |
| 297 | * Generates a platform-specific delete string from the supplied data |
| 298 | * |
| 299 | * @access public |
| 300 | * @param string the table name |
| 301 | * @param array the where clause |
| 302 | * @return string |
| 303 | */ |
| 304 | function _delete($table, $where) |
| 305 | { |
| 306 | return "DELETE FROM ".$this->escape_table($table)." WHERE ".implode(" ", $where); |
| 307 | } |
| 308 | |
| 309 | // -------------------------------------------------------------------- |
| 310 | |
| 311 | /** |
| 312 | * Version number query string |
| 313 | * |
| 314 | * @access public |
| 315 | * @return string |
| 316 | */ |
| 317 | function _version() |
| 318 | { |
| 319 | return "SELECT version() AS ver"; |
| 320 | } |
| 321 | |
| 322 | // -------------------------------------------------------------------- |
| 323 | |
| 324 | /** |
| 325 | * Show table query |
| 326 | * |
| 327 | * Generates a platform-specific query string so that the table names can be fetched |
| 328 | * |
| 329 | * @access public |
| 330 | * @return string |
| 331 | */ |
| 332 | function _show_tables() |
| 333 | { |
| 334 | return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name"; |
| 335 | } |
| 336 | |
| 337 | // -------------------------------------------------------------------- |
| 338 | |
| 339 | /** |
| 340 | * Show columnn query |
| 341 | * |
| 342 | * Generates a platform-specific query string so that the column names can be fetched |
| 343 | * |
| 344 | * @access public |
| 345 | * @param string the table name |
| 346 | * @return string |
| 347 | */ |
| 348 | function _show_columns($table = '') |
| 349 | { |
| 350 | return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->escape_table($table)."'"; |
| 351 | } |
| 352 | |
| 353 | // -------------------------------------------------------------------- |
| 354 | |
| 355 | /** |
| 356 | * Limit string |
| 357 | * |
| 358 | * Generates a platform-specific LIMIT clause |
| 359 | * |
| 360 | * @access public |
| 361 | * @param string the sql query string |
| 362 | * @param integer the number of rows to limit the query to |
| 363 | * @param integer the offset value |
| 364 | * @return string |
| 365 | */ |
| 366 | function _limit($sql, $limit, $offset) |
| 367 | { |
| 368 | $i = $limit + $offset; |
| 369 | |
| 370 | return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql); |
| 371 | } |
| 372 | |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | /** |
| 378 | * MS SQL Result Class |
| 379 | * |
| 380 | * This class extends the parent result class: CI_DB_result |
| 381 | * |
| 382 | * @category Database |
| 383 | * @author Rick Ellis |
| 384 | * @link http://www.codeigniter.com/user_guide/libraries/database/ |
| 385 | */ |
| 386 | class CI_DB_mssql_result extends CI_DB_result { |
| 387 | |
| 388 | /** |
| 389 | * Number of rows in the result set |
| 390 | * |
| 391 | * @access public |
| 392 | * @return integer |
| 393 | */ |
| 394 | function num_rows() |
| 395 | { |
| 396 | return @mssql_num_rows($this->result_id); |
| 397 | } |
| 398 | |
| 399 | // -------------------------------------------------------------------- |
| 400 | |
| 401 | /** |
| 402 | * Number of fields in the result set |
| 403 | * |
| 404 | * @access public |
| 405 | * @return integer |
| 406 | */ |
| 407 | function num_fields() |
| 408 | { |
| 409 | return @mssql_num_fields($this->result_id); |
| 410 | } |
| 411 | |
| 412 | // -------------------------------------------------------------------- |
| 413 | |
| 414 | /** |
| 415 | * Field data |
| 416 | * |
| 417 | * Generates an array of objects containing field meta-data |
| 418 | * |
| 419 | * @access public |
| 420 | * @return array |
| 421 | */ |
| 422 | function field_data() |
| 423 | { |
| 424 | $retval = array(); |
| 425 | while ($field = mssql_fetch_field($this->result_id)) |
| 426 | { |
| 427 | $F = new CI_DB_field(); |
| 428 | $F->name = $field->name; |
| 429 | $F->type = $field->type; |
| 430 | $F->max_length = $field->max_length; |
| 431 | $F->primary_key = 0; |
| 432 | $F->default = ''; |
| 433 | |
| 434 | $retval[] = $F; |
| 435 | } |
| 436 | |
| 437 | return $retval; |
| 438 | } |
| 439 | |
| 440 | // -------------------------------------------------------------------- |
| 441 | |
| 442 | /** |
| 443 | * Result - associative array |
| 444 | * |
| 445 | * Returns the result set as an array |
| 446 | * |
| 447 | * @access private |
| 448 | * @return array |
| 449 | */ |
| 450 | function _fetch_assoc() |
| 451 | { |
| 452 | return mssql_fetch_assoc($this->result_id); |
| 453 | } |
| 454 | |
| 455 | // -------------------------------------------------------------------- |
| 456 | |
| 457 | /** |
| 458 | * Result - object |
| 459 | * |
| 460 | * Returns the result set as an object |
| 461 | * |
| 462 | * @access private |
| 463 | * @return object |
| 464 | */ |
| 465 | function _fetch_object() |
| 466 | { |
| 467 | return mssql_fetch_object($this->result_id); |
| 468 | } |
| 469 | |
| 470 | } |
| 471 | |
| 472 | ?> |