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