1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * MS SQL Database Adapter Class
\r
21 * Note: _DB is an extender class that the app controller
\r
22 * creates dynamically based on whether the active record
\r
23 * class is being used or not.
\r
25 * @package CodeIgniter
\r
26 * @subpackage Drivers
\r
27 * @category Database
\r
28 * @author ExpressionEngine Dev Team
\r
29 * @link http://codeigniter.com/user_guide/database/
\r
31 class CI_DB_mssql_driver extends CI_DB {
\r
33 var $dbdriver = 'mssql';
\r
35 // The character used for escaping
\r
36 var $_escape_char = '';
\r
38 * The syntax to count rows is slightly different across different
\r
39 * database engines, so this string appears in each driver and is
\r
40 * used for the count_all() and count_all_results() functions.
\r
42 var $_count_string = "SELECT COUNT(*) AS ";
\r
43 var $_random_keyword = ' ASC'; // not currently supported
\r
46 * Non-persistent database connection
\r
48 * @access private called by the base class
\r
51 function db_connect()
\r
53 if ($this->port != '')
\r
55 $this->hostname .= ','.$this->port;
\r
58 return @mssql_connect($this->hostname, $this->username, $this->password);
\r
61 // --------------------------------------------------------------------
\r
64 * Persistent database connection
\r
66 * @access private called by the base class
\r
69 function db_pconnect()
\r
71 if ($this->port != '')
\r
73 $this->hostname .= ','.$this->port;
\r
76 return @mssql_pconnect($this->hostname, $this->username, $this->password);
\r
79 // --------------------------------------------------------------------
\r
82 * Select the database
\r
84 * @access private called by the base class
\r
87 function db_select()
\r
89 // Note: The brackets are required in the event that the DB name
\r
90 // contains reserved characters
\r
91 return @mssql_select_db('['.$this->database.']', $this->conn_id);
\r
94 // --------------------------------------------------------------------
\r
97 * Set client character set
\r
104 function db_set_charset($charset, $collation)
\r
106 // @todo - add support if needed
\r
110 // --------------------------------------------------------------------
\r
113 * Execute the query
\r
115 * @access private called by the base class
\r
116 * @param string an SQL query
\r
119 function _execute($sql)
\r
121 $sql = $this->_prep_query($sql);
\r
122 return @mssql_query($sql, $this->conn_id);
\r
125 // --------------------------------------------------------------------
\r
130 * If needed, each database adapter can prep the query string
\r
132 * @access private called by execute()
\r
133 * @param string an SQL query
\r
136 function _prep_query($sql)
\r
141 // --------------------------------------------------------------------
\r
144 * Begin Transaction
\r
149 function trans_begin($test_mode = FALSE)
\r
151 if ( ! $this->trans_enabled)
\r
156 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
157 if ($this->_trans_depth > 0)
\r
162 // Reset the transaction failure flag.
\r
163 // If the $test_mode flag is set to TRUE transactions will be rolled back
\r
164 // even if the queries produce a successful result.
\r
165 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
\r
167 $this->simple_query('BEGIN TRAN');
\r
171 // --------------------------------------------------------------------
\r
174 * Commit Transaction
\r
179 function trans_commit()
\r
181 if ( ! $this->trans_enabled)
\r
186 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
187 if ($this->_trans_depth > 0)
\r
192 $this->simple_query('COMMIT TRAN');
\r
196 // --------------------------------------------------------------------
\r
199 * Rollback Transaction
\r
204 function trans_rollback()
\r
206 if ( ! $this->trans_enabled)
\r
211 // When transactions are nested we only begin/commit/rollback the outermost ones
\r
212 if ($this->_trans_depth > 0)
\r
217 $this->simple_query('ROLLBACK TRAN');
\r
221 // --------------------------------------------------------------------
\r
230 function escape_str($str)
\r
232 // Access the CI object
\r
233 $CI =& get_instance();
\r
235 // Escape single quotes
\r
236 return str_replace("'", "''", $CI->input->_remove_invisible_characters($str));
\r
239 // --------------------------------------------------------------------
\r
247 function affected_rows()
\r
249 return @mssql_rows_affected($this->conn_id);
\r
252 // --------------------------------------------------------------------
\r
257 * Returns the last id created in the Identity column.
\r
262 function insert_id()
\r
264 $ver = self::_parse_major_version($this->version());
\r
265 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
\r
266 $query = $this->query($sql);
\r
267 $row = $query->row();
\r
268 return $row->last_id;
\r
271 // --------------------------------------------------------------------
\r
274 * Parse major version
\r
276 * Grabs the major version number from the
\r
277 * database server version string passed in.
\r
280 * @param string $version
\r
281 * @return int16 major version number
\r
283 function _parse_major_version($version)
\r
285 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
\r
286 return $ver_info[1]; // return the major version b/c that's all we're interested in.
\r
289 // --------------------------------------------------------------------
\r
292 * Version number query string
\r
297 function _version()
\r
299 return "SELECT @@VERSION AS ver";
\r
302 // --------------------------------------------------------------------
\r
305 * "Count All" query
\r
307 * Generates a platform-specific query string that counts all records in
\r
308 * the specified database
\r
314 function count_all($table = '')
\r
319 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
\r
321 if ($query->num_rows() == 0)
\r
324 $row = $query->row();
\r
325 return $row->numrows;
\r
328 // --------------------------------------------------------------------
\r
333 * Generates a platform-specific query string so that the table names can be fetched
\r
339 function _list_tables($prefix_limit = FALSE)
\r
341 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
\r
343 // for future compatibility
\r
344 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
\r
346 //$sql .= " LIKE '".$this->dbprefix."%'";
\r
347 return FALSE; // not currently supported
\r
353 // --------------------------------------------------------------------
\r
356 * List column query
\r
358 * Generates a platform-specific query string so that the column names can be fetched
\r
361 * @param string the table name
\r
364 function _list_columns($table = '')
\r
366 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table."'";
\r
369 // --------------------------------------------------------------------
\r
374 * Generates a platform-specific query so that the column data can be retrieved
\r
377 * @param string the table name
\r
380 function _field_data($table)
\r
382 return "SELECT TOP 1 * FROM ".$table;
\r
385 // --------------------------------------------------------------------
\r
388 * The error message string
\r
393 function _error_message()
\r
395 // Are errros even supported in MS SQL?
\r
399 // --------------------------------------------------------------------
\r
402 * The error message number
\r
407 function _error_number()
\r
409 // Are error numbers supported?
\r
413 // --------------------------------------------------------------------
\r
416 * Escape the SQL Identifiers
\r
418 * This function escapes column and table names
\r
424 function _escape_identifiers($item)
\r
426 if ($this->_escape_char == '')
\r
431 if (strpos($item, '.') !== FALSE)
\r
433 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
\r
437 $str = $this->_escape_char.$item.$this->_escape_char;
\r
440 // remove duplicates if the user already included the escape
\r
441 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
\r
444 // --------------------------------------------------------------------
\r
449 * This function implicitly groups FROM tables so there is no confusion
\r
450 * about operator precedence in harmony with SQL standards
\r
456 function _from_tables($tables)
\r
458 if ( ! is_array($tables))
\r
460 $tables = array($tables);
\r
463 return implode(', ', $tables);
\r
466 // --------------------------------------------------------------------
\r
471 * Generates a platform-specific insert string from the supplied data
\r
474 * @param string the table name
\r
475 * @param array the insert keys
\r
476 * @param array the insert values
\r
479 function _insert($table, $keys, $values)
\r
481 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
\r
484 // --------------------------------------------------------------------
\r
489 * Generates a platform-specific update string from the supplied data
\r
492 * @param string the table name
\r
493 * @param array the update data
\r
494 * @param array the where clause
\r
495 * @param array the orderby clause
\r
496 * @param array the limit clause
\r
499 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
\r
501 foreach($values as $key => $val)
\r
503 $valstr[] = $key." = ".$val;
\r
506 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
\r
508 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
\r
510 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
\r
512 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
\r
514 $sql .= $orderby.$limit;
\r
520 // --------------------------------------------------------------------
\r
523 * Truncate statement
\r
525 * Generates a platform-specific truncate string from the supplied data
\r
526 * If the database does not support the truncate() command
\r
527 * This function maps to "DELETE FROM table"
\r
530 * @param string the table name
\r
533 function _truncate($table)
\r
535 return "TRUNCATE ".$table;
\r
538 // --------------------------------------------------------------------
\r
543 * Generates a platform-specific delete string from the supplied data
\r
546 * @param string the table name
\r
547 * @param array the where clause
\r
548 * @param string the limit clause
\r
551 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
\r
555 if (count($where) > 0 OR count($like) > 0)
\r
557 $conditions = "\nWHERE ";
\r
558 $conditions .= implode("\n", $this->ar_where);
\r
560 if (count($where) > 0 && count($like) > 0)
\r
562 $conditions .= " AND ";
\r
564 $conditions .= implode("\n", $like);
\r
567 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
\r
569 return "DELETE FROM ".$table.$conditions.$limit;
\r
572 // --------------------------------------------------------------------
\r
577 * Generates a platform-specific LIMIT clause
\r
580 * @param string the sql query string
\r
581 * @param integer the number of rows to limit the query to
\r
582 * @param integer the offset value
\r
585 function _limit($sql, $limit, $offset)
\r
587 $i = $limit + $offset;
\r
589 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
\r
592 // --------------------------------------------------------------------
\r
595 * Close DB Connection
\r
601 function _close($conn_id)
\r
603 @mssql_close($conn_id);
\r
610 /* End of file mssql_driver.php */
\r
611 /* Location: ./system/database/drivers/mssql/mssql_driver.php */