]>
jfr.im git - z_archive/KronOS.git/blob - video/system/database/drivers/sqlsrv/sqlsrv_driver.php
400fd31c6add8567b40af033151738fa879d0103
1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * SQLSRV 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_sqlsrv_driver
extends CI_DB
{
33 var $dbdriver = 'sqlsrv';
35 // The character used for escaping
36 var $_escape_char = '';
38 // clause and character used for LIKE escape sequences
39 var $_like_escape_str = " ESCAPE '%s' ";
40 var $_like_escape_chr = '!';
43 * The syntax to count rows is slightly different across different
44 * database engines, so this string appears in each driver and is
45 * used for the count_all() and count_all_results() functions.
47 var $_count_string = "SELECT COUNT(*) AS ";
48 var $_random_keyword = ' ASC'; // not currently supported
51 * Non-persistent database connection
53 * @access private called by the base class
56 function db_connect($pooling = false)
58 // Check for a UTF-8 charset being passed as CI's default 'utf8'.
59 $character_set = (0 === strcasecmp('utf8', $this->char_set
)) ? 'UTF-8' : $this->char_set
;
62 'UID' => empty($this->username
) ? '' : $this->username
,
63 'PWD' => empty($this->password
) ? '' : $this->password
,
64 'Database' => $this->database
,
65 'ConnectionPooling' => $pooling ? 1 : 0,
66 'CharacterSet' => $character_set,
67 'ReturnDatesAsStrings' => 1
70 // If the username and password are both empty, assume this is a
71 // 'Windows Authentication Mode' connection.
72 if(empty($connection['UID']) && empty($connection['PWD'])) {
73 unset($connection['UID'], $connection['PWD']);
76 return sqlsrv_connect($this->hostname
, $connection);
79 // --------------------------------------------------------------------
82 * Persistent database connection
84 * @access private called by the base class
87 function db_pconnect()
89 $this->db_connect(TRUE);
92 // --------------------------------------------------------------------
97 * Keep / reestablish the db connection if no queries have been
98 * sent for a length of time exceeding the server's idle timeout
105 // not implemented in MSSQL
108 // --------------------------------------------------------------------
111 * Select the database
113 * @access private called by the base class
118 return $this->_execute('USE ' . $this->database
);
121 // --------------------------------------------------------------------
124 * Set client character set
131 function db_set_charset($charset, $collation)
133 // @todo - add support if needed
137 // --------------------------------------------------------------------
142 * @access private called by the base class
143 * @param string an SQL query
146 function _execute($sql)
148 $sql = $this->_prep_query($sql);
149 return sqlsrv_query($this->conn_id
, $sql, null, array(
150 'Scrollable' => SQLSRV_CURSOR_STATIC
,
151 'SendStreamParamsAtExec' => true
155 // --------------------------------------------------------------------
160 * If needed, each database adapter can prep the query string
162 * @access private called by execute()
163 * @param string an SQL query
166 function _prep_query($sql)
171 // --------------------------------------------------------------------
179 function trans_begin($test_mode = FALSE)
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 // Reset the transaction failure flag.
193 // If the $test_mode flag is set to TRUE transactions will be rolled back
194 // even if the queries produce a successful result.
195 $this->_trans_failure
= ($test_mode === TRUE) ? TRUE : FALSE;
197 return sqlsrv_begin_transaction($this->conn_id
);
200 // --------------------------------------------------------------------
208 function trans_commit()
210 if ( ! $this->trans_enabled
)
215 // When transactions are nested we only begin/commit/rollback the outermost ones
216 if ($this->_trans_depth
> 0)
221 return sqlsrv_commit($this->conn_id
);
224 // --------------------------------------------------------------------
227 * Rollback Transaction
232 function trans_rollback()
234 if ( ! $this->trans_enabled
)
239 // When transactions are nested we only begin/commit/rollback the outermost ones
240 if ($this->_trans_depth
> 0)
245 return sqlsrv_rollback($this->conn_id
);
248 // --------------------------------------------------------------------
255 * @param bool whether or not the string will be used in a LIKE condition
258 function escape_str($str, $like = FALSE)
260 // Escape single quotes
261 return str_replace("'", "''", $str);
264 // --------------------------------------------------------------------
272 function affected_rows()
274 return @sqlrv_rows_affected($this->conn_id
);
277 // --------------------------------------------------------------------
282 * Returns the last id created in the Identity column.
289 return $this->query('select @@IDENTITY as insert_id')->row('insert_id');
292 // --------------------------------------------------------------------
295 * Parse major version
297 * Grabs the major version number from the
298 * database server version string passed in.
301 * @param string $version
302 * @return int16 major version number
304 function _parse_major_version($version)
306 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
307 return $ver_info[1]; // return the major version b/c that's all we're interested in.
310 // --------------------------------------------------------------------
313 * Version number query string
320 $info = sqlsrv_server_info($this->conn_id
);
321 return sprintf("select '%s' as ver", $info['SQLServerVersion']);
324 // --------------------------------------------------------------------
329 * Generates a platform-specific query string that counts all records in
330 * the specified database
336 function count_all($table = '')
341 $query = $this->query("SELECT COUNT(*) AS numrows FROM " . $this->dbprefix
. $table);
343 if ($query->num_rows() == 0)
346 $row = $query->row();
347 $this->_reset_select();
348 return $row->numrows
;
351 // --------------------------------------------------------------------
356 * Generates a platform-specific query string so that the table names can be fetched
362 function _list_tables($prefix_limit = FALSE)
364 return "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
367 // --------------------------------------------------------------------
372 * Generates a platform-specific query string so that the column names can be fetched
375 * @param string the table name
378 function _list_columns($table = '')
380 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";
383 // --------------------------------------------------------------------
388 * Generates a platform-specific query so that the column data can be retrieved
391 * @param string the table name
394 function _field_data($table)
396 return "SELECT TOP 1 * FROM " . $this->_escape_table($table);
399 // --------------------------------------------------------------------
402 * The error message string
407 function _error_message()
409 $error = array_shift(sqlsrv_errors());
410 return !empty($error['message']) ? $error['message'] : null;
413 // --------------------------------------------------------------------
416 * The error message number
421 function _error_number()
423 $error = array_shift(sqlsrv_errors());
424 return isset($error['SQLSTATE']) ? $error['SQLSTATE'] : null;
427 // --------------------------------------------------------------------
432 * This function adds backticks if the table name has a period
433 * in it. Some DBs will get cranky unless periods are escaped
436 * @param string the table name
439 function _escape_table($table)
446 * Escape the SQL Identifiers
448 * This function escapes column and table names
454 function _escape_identifiers($item)
459 // --------------------------------------------------------------------
464 * This function implicitly groups FROM tables so there is no confusion
465 * about operator precedence in harmony with SQL standards
471 function _from_tables($tables)
473 if ( ! is_array($tables))
475 $tables = array($tables);
478 return implode(', ', $tables);
481 // --------------------------------------------------------------------
486 * Generates a platform-specific insert string from the supplied data
489 * @param string the table name
490 * @param array the insert keys
491 * @param array the insert values
494 function _insert($table, $keys, $values)
496 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
499 // --------------------------------------------------------------------
504 * Generates a platform-specific update string from the supplied data
507 * @param string the table name
508 * @param array the update data
509 * @param array the where clause
510 * @param array the orderby clause
511 * @param array the limit clause
514 function _update($table, $values, $where)
516 foreach($values as $key => $val)
518 $valstr[] = $key." = ".$val;
521 return "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr)." WHERE ".implode(" ", $where);
524 // --------------------------------------------------------------------
529 * Generates a platform-specific truncate string from the supplied data
530 * If the database does not support the truncate() command
531 * This function maps to "DELETE FROM table"
534 * @param string the table name
537 function _truncate($table)
539 return "TRUNCATE ".$table;
542 // --------------------------------------------------------------------
547 * Generates a platform-specific delete string from the supplied data
550 * @param string the table name
551 * @param array the where clause
552 * @param string the limit clause
555 function _delete($table, $where)
557 return "DELETE FROM ".$this->_escape_table($table)." WHERE ".implode(" ", $where);
560 // --------------------------------------------------------------------
565 * Generates a platform-specific LIMIT clause
568 * @param string the sql query string
569 * @param integer the number of rows to limit the query to
570 * @param integer the offset value
573 function _limit($sql, $limit, $offset)
575 $i = $limit +
$offset;
577 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);
580 // --------------------------------------------------------------------
583 * Close DB Connection
589 function _close($conn_id)
591 @sqlsrv_close($conn_id);
598 /* End of file mssql_driver.php */
599 /* Location: ./system/database/drivers/mssql/mssql_driver.php */