3 * This is the MS SQL Server Native database abstraction layer.
7 * @author Joel Penner <a-joelpe at microsoft dot com>
8 * @author Chris Pucci <a-cpucci at microsoft dot com>
9 * @author Ryan Biesemeyer <v-ryanbi at microsoft dot com>
15 class DatabaseMssql
extends DatabaseBase
{
16 var $mInsertId = NULL;
17 var $mLastResult = NULL;
18 var $mAffectedRows = NULL;
22 function cascadingDeletes() {
25 function cleanupTriggers() {
28 function strictIPs() {
31 function realTimestamps() {
34 function implicitGroupby() {
37 function implicitOrderby() {
40 function functionalIndexes() {
43 function unionSupportsOrderAndLimit() {
48 * Usually aborts on failure
50 function open( $server, $user, $password, $dbName ) {
51 # Test for driver support, to avoid suppressed fatal error
52 if ( !function_exists( 'sqlsrv_connect' ) ) {
53 throw new DBConnectionError( $this, "MS Sql Server Native (sqlsrv) functions missing. You can download the driver from: http://go.microsoft.com/fwlink/?LinkId=123470\n" );
58 if ( !strlen( $user ) ) { # e.g. the class is being loaded
63 $this->mServer
= $server;
64 $this->mPort
= $wgDBport;
66 $this->mPassword
= $password;
67 $this->mDBname
= $dbName;
69 $connectionInfo = array();
72 $connectionInfo['Database'] = $dbName;
76 // Quick and dirty work around to provide NT Auth designation support.
77 // Current solution requires installer to know to input 'ntauth' for both username and password
78 // to trigger connection via NT Auth. - ugly, ugly, ugly
79 // TO-DO: Make this better and add NT Auth choice to MW installer when SQL Server option is chosen.
80 $ntAuthUserTest = strtolower( $user );
81 $ntAuthPassTest = strtolower( $password );
83 // Decide which auth scenerio to use
84 if( $ntAuthPassTest == 'ntauth' && $ntAuthUserTest == 'ntauth' ){
85 // Don't add credentials to $connectionInfo
87 $connectionInfo['UID'] = $user;
88 $connectionInfo['PWD'] = $password;
93 $this->mConn
= sqlsrv_connect( $server, $connectionInfo );
96 if ( $this->mConn
=== false ) {
97 wfDebug( "DB connection error\n" );
98 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
99 wfDebug( $this->lastError() . "\n" );
103 $this->mOpened
= true;
108 * Closes a database connection, if it is open
109 * Returns success, true if already closed
112 $this->mOpened
= false;
113 if ( $this->mConn
) {
114 return sqlsrv_close( $this->mConn
);
120 protected function doQuery( $sql ) {
121 wfDebug( "SQL: [$sql]\n" );
124 // several extensions seem to think that all databases support limits via LIMIT N after the WHERE clause
125 // well, MSSQL uses SELECT TOP N, so to catch any of those extensions we'll do a quick check for a LIMIT
126 // clause and pass $sql through $this->LimitToTopN() which parses the limit clause and passes the result to
127 // $this->limitResult();
128 if ( preg_match( '/\bLIMIT\s*/i', $sql ) ) {
129 // massage LIMIT -> TopN
130 $sql = $this->LimitToTopN( $sql ) ;
133 // MSSQL doesn't have EXTRACT(epoch FROM XXX)
134 if ( preg_match('#\bEXTRACT\s*?\(\s*?EPOCH\s+FROM\b#i', $sql, $matches ) ) {
135 // This is same as UNIX_TIMESTAMP, we need to calc # of seconds from 1970
136 $sql = str_replace( $matches[0], "DATEDIFF(s,CONVERT(datetime,'1/1/1970'),", $sql );
140 $stmt = sqlsrv_query( $this->mConn
, $sql );
141 if ( $stmt == false ) {
142 $message = "A database error has occurred. Did you forget to run maintenance/update.php after upgrading? See: http://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
143 "Query: " . htmlentities( $sql ) . "\n" .
144 "Function: " . __METHOD__
. "\n";
145 // process each error (our driver will give us an array of errors unlike other providers)
146 foreach ( sqlsrv_errors() as $error ) {
147 $message .= $message . "ERROR[" . $error['code'] . "] " . $error['message'] . "\n";
150 throw new DBUnexpectedError( $this, $message );
152 // remember number of rows affected
153 $this->mAffectedRows
= sqlsrv_rows_affected( $stmt );
155 // if it is a SELECT statement, or an insert with a request to output something we want to return a row.
156 if ( ( preg_match( '#\bSELECT\s#i', $sql ) ) ||
157 ( preg_match( '#\bINSERT\s#i', $sql ) && preg_match( '#\bOUTPUT\s+INSERTED\b#i', $sql ) ) ) {
158 // this is essentially a rowset, but Mediawiki calls these 'result'
159 // the rowset owns freeing the statement
160 $res = new MssqlResult( $stmt );
162 // otherwise we simply return it was successful, failure throws an exception
168 function freeResult( $res ) {
169 if ( $res instanceof ResultWrapper
) {
175 function fetchObject( $res ) {
176 if ( $res instanceof ResultWrapper
) {
179 $row = $res->fetch( 'OBJECT' );
183 function getErrors() {
185 $retErrors = sqlsrv_errors( SQLSRV_ERR_ALL
);
186 if ( $retErrors != null ) {
187 foreach ( $retErrors as $arrError ) {
188 $strRet .= "SQLState: " . $arrError[ 'SQLSTATE'] . "\n";
189 $strRet .= "Error Code: " . $arrError[ 'code'] . "\n";
190 $strRet .= "Message: " . $arrError[ 'message'] . "\n";
193 $strRet = "No errors found";
198 function fetchRow( $res ) {
199 if ( $res instanceof ResultWrapper
) {
202 $row = $res->fetch( SQLSRV_FETCH_BOTH
);
206 function numRows( $res ) {
207 if ( $res instanceof ResultWrapper
) {
210 return ( $res ) ?
$res->numrows() : 0;
213 function numFields( $res ) {
214 if ( $res instanceof ResultWrapper
) {
217 return ( $res ) ?
$res->numfields() : 0;
220 function fieldName( $res, $n ) {
221 if ( $res instanceof ResultWrapper
) {
224 return ( $res ) ?
$res->fieldname( $n ) : 0;
228 * This must be called after nextSequenceVal
230 function insertId() {
231 return $this->mInsertId
;
234 function dataSeek( $res, $row ) {
235 if ( $res instanceof ResultWrapper
) {
238 return ( $res ) ?
$res->seek( $row ) : false;
241 function lastError() {
242 if ( $this->mConn
) {
243 return $this->getErrors();
245 return "No database connection";
249 function lastErrno() {
250 $err = sqlsrv_errors( SQLSRV_ERR_ALL
);
252 return $err[0]['code'];
258 function affectedRows() {
259 return $this->mAffectedRows
;
265 * @param $table Mixed: array or string, table name(s) (prefix auto-added)
266 * @param $vars Mixed: array or string, field name(s) to be retrieved
267 * @param $conds Mixed: array or string, condition(s) for WHERE
268 * @param $fname String: calling function name (use __METHOD__) for logs/profiling
269 * @param $options Array: associative array of options (e.g. array('GROUP BY' => 'page_title')),
270 * see Database::makeSelectOptions code for list of supported stuff
271 * @param $join_conds Array: Associative array of table join conditions (optional)
272 * (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') )
273 * @return Mixed: database result resource (feed to Database::fetchObject or whatever), or false on failure
275 function select( $table, $vars, $conds = '', $fname = 'DatabaseMssql::select', $options = array(), $join_conds = array() )
277 $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
278 if ( isset( $options['EXPLAIN'] ) ) {
279 sqlsrv_query( $this->mConn
, "SET SHOWPLAN_ALL ON;" );
280 $ret = $this->query( $sql, $fname );
281 sqlsrv_query( $this->mConn
, "SET SHOWPLAN_ALL OFF;" );
284 return $this->query( $sql, $fname );
290 * @param $table Mixed: Array or string, table name(s) (prefix auto-added)
291 * @param $vars Mixed: Array or string, field name(s) to be retrieved
292 * @param $conds Mixed: Array or string, condition(s) for WHERE
293 * @param $fname String: Calling function name (use __METHOD__) for logs/profiling
294 * @param $options Array: Associative array of options (e.g. array('GROUP BY' => 'page_title')),
295 * see Database::makeSelectOptions code for list of supported stuff
296 * @param $join_conds Array: Associative array of table join conditions (optional)
297 * (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') )
298 * @return string, the SQL text
300 function selectSQLText( $table, $vars, $conds = '', $fname = 'DatabaseMssql::select', $options = array(), $join_conds = array() ) {
301 if ( isset( $options['EXPLAIN'] ) ) {
302 unset( $options['EXPLAIN'] );
304 return parent
::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
308 * Estimate rows in dataset
309 * Returns estimated count, based on SHOWPLAN_ALL output
310 * This is not necessarily an accurate estimate, so use sparingly
311 * Returns -1 if count cannot be found
312 * Takes same arguments as Database::select()
314 function estimateRowCount( $table, $vars = '*', $conds = '', $fname = 'DatabaseMssql::estimateRowCount', $options = array() ) {
315 $options['EXPLAIN'] = true;// http://msdn2.microsoft.com/en-us/library/aa259203.aspx
316 $res = $this->select( $table, $vars, $conds, $fname, $options );
320 $row = $this->fetchRow( $res );
321 if ( isset( $row['EstimateRows'] ) ) $rows = $row['EstimateRows'];
327 * Returns information about an index
328 * If errors are explicitly ignored, returns NULL on failure
330 function indexInfo( $table, $index, $fname = 'DatabaseMssql::indexExists' ) {
331 # This does not return the same info as MYSQL would, but that's OK because MediaWiki never uses the
332 # returned value except to check for the existance of indexes.
333 $sql = "sp_helpindex '" . $table . "'";
334 $res = $this->query( $sql, $fname );
340 foreach ( $res as $row ) {
341 if ( $row->index_name
== $index ) {
342 $row->Non_unique
= !stristr( $row->index_description
, "unique" );
343 $cols = explode( ", ", $row->index_keys
);
344 foreach ( $cols as $col ) {
345 $row->Column_name
= trim( $col );
346 $result[] = clone $row;
348 } elseif ( $index == 'PRIMARY' && stristr( $row->index_description
, 'PRIMARY' ) ) {
349 $row->Non_unique
= 0;
350 $cols = explode( ", ", $row->index_keys
);
351 foreach ( $cols as $col ) {
352 $row->Column_name
= trim( $col );
353 $result[] = clone $row;
357 return empty( $result ) ?
false : $result;
361 * INSERT wrapper, inserts an array into a table
363 * $arrToInsert may be a single associative array, or an array of these with numeric keys, for
366 * Usually aborts on failure
367 * If errors are explicitly ignored, returns success
369 function insert( $table, $arrToInsert, $fname = 'DatabaseMssql::insert', $options = array() ) {
370 # No rows to insert, easy just return now
371 if ( !count( $arrToInsert ) ) {
375 if ( !is_array( $options ) ) {
376 $options = array( $options );
379 $table = $this->tableName( $table );
381 if ( !( isset( $arrToInsert[0] ) && is_array( $arrToInsert[0] ) ) ) {// Not multi row
382 $arrToInsert = array( 0 => $arrToInsert );// make everything multi row compatible
387 // We know the table we're inserting into, get its identity column
389 $tableRaw = preg_replace( '#\[([^\]]*)\]#', '$1', $table ); // strip matching square brackets from table name
390 $res = $this->doQuery( "SELECT NAME AS idColumn FROM SYS.IDENTITY_COLUMNS WHERE OBJECT_NAME(OBJECT_ID)='{$tableRaw}'" );
391 if( $res && $res->numrows() ){
392 // There is an identity for this table.
393 $identity = array_pop( $res->fetch( SQLSRV_FETCH_ASSOC
) );
397 foreach ( $arrToInsert as $a ) {
398 // start out with empty identity column, this is so we can return it as a result of the insert logic
401 $identityClause = '';
403 // if we have an identity column
406 foreach ($a as $k => $v ) {
407 if ( $k == $identity ) {
409 // there is a value being passed to us, we need to turn on and off inserted identity
410 $sqlPre = "SET IDENTITY_INSERT $table ON;" ;
411 $sqlPost = ";SET IDENTITY_INSERT $table OFF;";
414 // we can't insert NULL into an identity column, so remove the column from the insert.
419 $identityClause = "OUTPUT INSERTED.$identity "; // we want to output an identity column as result
422 $keys = array_keys( $a );
424 // INSERT IGNORE is not supported by SQL Server
425 // remove IGNORE from options list and set ignore flag to true
426 $ignoreClause = false;
427 foreach ( $options as $k => $v ) {
428 if ( strtoupper( $v ) == "IGNORE" ) {
429 unset( $options[$k] );
430 $ignoreClause = true;
434 // translate MySQL INSERT IGNORE to something SQL Server can use
436 // MySQL: INSERT IGNORE INTO user_groups (ug_user,ug_group) VALUES ('1','sysop')
437 // MSSQL: IF NOT EXISTS (SELECT * FROM user_groups WHERE ug_user = '1') INSERT INTO user_groups (ug_user,ug_group) VALUES ('1','sysop')
438 if ( $ignoreClause ) {
439 $prival = $a[$keys[0]];
440 $sqlPre .= "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = '$prival')";
443 // Build the actual query
444 $sql = $sqlPre . 'INSERT ' . implode( ' ', $options ) .
445 " INTO $table (" . implode( ',', $keys ) . ") $identityClause VALUES (";
448 foreach ( $a as $value ) {
454 if ( is_string( $value ) ) {
455 $sql .= $this->addQuotes( $value );
456 } elseif ( is_null( $value ) ) {
458 } elseif ( is_array( $value ) ||
is_object( $value ) ) {
459 if ( is_object( $value ) && strtolower( get_class( $value ) ) == 'blob' ) {
460 $sql .= $this->addQuotes( $value );
462 $sql .= $this->addQuotes( serialize( $value ) );
468 $sql .= ')' . $sqlPost;
471 $ret = sqlsrv_query( $this->mConn
, $sql );
473 if ( $ret === false ) {
474 throw new DBQueryError( $this, $this->getErrors(), $this->lastErrno(), $sql, $fname );
475 } elseif ( $ret != NULL ) {
476 // remember number of rows affected
477 $this->mAffectedRows
= sqlsrv_rows_affected( $ret );
478 if ( !is_null($identity) ) {
479 // then we want to get the identity column value we were assigned and save it off
480 $row = sqlsrv_fetch_object( $ret );
481 $this->mInsertId
= $row->$identity;
483 sqlsrv_free_stmt( $ret );
492 * INSERT SELECT wrapper
493 * $varMap must be an associative array of the form array( 'dest1' => 'source1', ...)
494 * Source items may be literals rather than field names, but strings should be quoted with Database::addQuotes()
495 * $conds may be "*" to copy the whole table
496 * srcTable may be an array of tables.
498 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'DatabaseMssql::insertSelect',
499 $insertOptions = array(), $selectOptions = array() ) {
500 $ret = parent
::insertSelect( $destTable, $srcTable, $varMap, $conds, $fname, $insertOptions, $selectOptions );
502 if ( $ret === false ) {
503 throw new DBQueryError( $this, $this->getErrors(), $this->lastErrno(), /*$sql*/ '', $fname );
504 } elseif ( $ret != NULL ) {
505 // remember number of rows affected
506 $this->mAffectedRows
= sqlsrv_rows_affected( $ret );
513 * Return the next in a sequence, save the value for retrieval via insertId()
515 function nextSequenceValue( $seqName ) {
516 if ( !$this->tableExists( 'sequence_' . $seqName ) ) {
517 sqlsrv_query( $this->mConn
, "CREATE TABLE [sequence_$seqName] (id INT NOT NULL IDENTITY PRIMARY KEY, junk varchar(10) NULL)" );
519 sqlsrv_query( $this->mConn
, "INSERT INTO [sequence_$seqName] (junk) VALUES ('')" );
520 $ret = sqlsrv_query( $this->mConn
, "SELECT TOP 1 id FROM [sequence_$seqName] ORDER BY id DESC" );
521 $row = sqlsrv_fetch_array( $ret, SQLSRV_FETCH_ASSOC
);// KEEP ASSOC THERE, weird weird bug dealing with the return value if you don't
523 sqlsrv_free_stmt( $ret );
524 $this->mInsertId
= $row['id'];
529 * Return the current value of a sequence. Assumes it has ben nextval'ed in this session.
531 function currentSequenceValue( $seqName ) {
532 $ret = sqlsrv_query( $this->mConn
, "SELECT TOP 1 id FROM [sequence_$seqName] ORDER BY id DESC" );
533 if ( $ret !== false ) {
534 $row = sqlsrv_fetch_array( $ret );
535 sqlsrv_free_stmt( $ret );
538 return $this->nextSequenceValue( $seqName );
542 # Returns the size of a text field, or -1 for "unlimited"
543 function textFieldSize( $table, $field ) {
544 $table = $this->tableName( $table );
545 $sql = "SELECT CHARACTER_MAXIMUM_LENGTH,DATA_TYPE FROM INFORMATION_SCHEMA.Columns
546 WHERE TABLE_NAME = '$table' AND COLUMN_NAME = '$field'";
547 $res = $this->query( $sql );
548 $row = $this->fetchRow( $res );
550 if ( strtolower( $row['DATA_TYPE'] ) != 'text' ) {
551 $size = $row['CHARACTER_MAXIMUM_LENGTH'];
557 * Construct a LIMIT query with optional offset
558 * This is used for query pages
559 * $sql string SQL query we will append the limit too
560 * $limit integer the SQL limit
561 * $offset integer the SQL offset (default false)
563 function limitResult( $sql, $limit, $offset = false ) {
564 if ( $offset === false ||
$offset == 0 ) {
565 if ( strpos( $sql, "SELECT" ) === false ) {
566 return "TOP {$limit} " . $sql;
568 return preg_replace( '/\bSELECT(\s*DISTINCT)?\b/Dsi', 'SELECT$1 TOP ' . $limit, $sql, 1 );
573 SELECT sub2.*, ROW_NUMBER() OVER(ORDER BY sub2.line2) AS line3 FROM (
574 SELECT 1 AS line2, sub1.* FROM (' . $sql . ') AS sub1
577 WHERE line3 BETWEEN ' . ( $offset +
1 ) . ' AND ' . ( $offset +
$limit );
582 // If there is a limit clause, parse it, strip it, and pass the remaining sql through limitResult()
583 // with the appropriate parameters. Not the prettiest solution, but better than building a whole new parser.
584 // This exists becase there are still too many extensions that don't use dynamic sql generation.
585 function LimitToTopN( $sql ) {
586 // Matches: LIMIT {[offset,] row_count | row_count OFFSET offset}
587 $pattern = '/\bLIMIT\s+((([0-9]+)\s*,\s*)?([0-9]+)(\s+OFFSET\s+([0-9]+))?)/i';
588 if ( preg_match( $pattern, $sql, $matches ) ) {
589 // row_count = $matches[4]
590 $row_count = $matches[4];
591 // offset = $matches[3] OR $matches[6]
592 $offset = $matches[3] or
593 $offset = $matches[6] or
596 // strip the matching LIMIT clause out
597 $sql = str_replace( $matches[0], '', $sql );
598 return $this->limitResult( $sql, $row_count, $offset );
603 // MSSQL does support this, but documentation is too thin to make a generalized
604 // function for this. Apparently UPDATE TOP (N) works, but the sort order
605 // may not be what we're expecting so the top n results may be a random selection.
606 // TODO: Implement properly.
607 function limitResultForUpdate( $sql, $num ) {
611 function timestamp( $ts = 0 ) {
612 return wfTimestamp( TS_ISO_8601
, $ts );
616 * @return string wikitext of a link to the server software's web site
618 public static function getSoftwareLink() {
619 return "[http://www.microsoft.com/sql/ MS SQL Server]";
623 * @return string Version information from the database
625 function getServerVersion() {
626 $server_info = sqlsrv_server_info( $this->mConn
);
628 if ( isset( $server_info['SQLServerVersion'] ) ) {
629 $version = $server_info['SQLServerVersion'];
634 function tableExists ( $table, $schema = false ) {
635 $res = sqlsrv_query( $this->mConn
, "SELECT * FROM information_schema.tables
636 WHERE table_type='BASE TABLE' AND table_name = '$table'" );
637 if ( $res === false ) {
638 print( "Error in tableExists query: " . $this->getErrors() );
641 if ( sqlsrv_fetch( $res ) ) {
649 * Query whether a given column exists in the mediawiki schema
651 function fieldExists( $table, $field, $fname = 'DatabaseMssql::fieldExists' ) {
652 $table = $this->tableName( $table );
653 $res = sqlsrv_query( $this->mConn
, "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.Columns
654 WHERE TABLE_NAME = '$table' AND COLUMN_NAME = '$field'" );
655 if ( $res === false ) {
656 print( "Error in fieldExists query: " . $this->getErrors() );
659 if ( sqlsrv_fetch( $res ) ) {
666 function fieldInfo( $table, $field ) {
667 $table = $this->tableName( $table );
668 $res = sqlsrv_query( $this->mConn
, "SELECT * FROM INFORMATION_SCHEMA.Columns
669 WHERE TABLE_NAME = '$table' AND COLUMN_NAME = '$field'" );
670 if ( $res === false ) {
671 print( "Error in fieldInfo query: " . $this->getErrors() );
674 $meta = $this->fetchRow( $res );
676 return new MssqlField( $meta );
682 * Begin a transaction, committing any previously open transaction
684 function begin( $fname = 'DatabaseMssql::begin' ) {
685 sqlsrv_begin_transaction( $this->mConn
);
686 $this->mTrxLevel
= 1;
692 function commit( $fname = 'DatabaseMssql::commit' ) {
693 sqlsrv_commit( $this->mConn
);
694 $this->mTrxLevel
= 0;
698 * Rollback a transaction.
699 * No-op on non-transactional databases.
701 function rollback( $fname = 'DatabaseMssql::rollback' ) {
702 sqlsrv_rollback( $this->mConn
);
703 $this->mTrxLevel
= 0;
707 * Escapes a identifier for use inm SQL.
708 * Throws an exception if it is invalid.
709 * Reference: http://msdn.microsoft.com/en-us/library/aa224033%28v=SQL.80%29.aspx
711 private function escapeIdentifier( $identifier ) {
712 if ( strlen( $identifier ) == 0 ) {
713 throw new MWException( "An identifier must not be empty" );
715 if ( strlen( $identifier ) > 128 ) {
716 throw new MWException( "The identifier '$identifier' is too long (max. 128)" );
718 if ( ( strpos( $identifier, '[' ) !== false ) ||
( strpos( $identifier, ']' ) !== false ) ) {
719 // It may be allowed if you quoted with double quotation marks, but that would break if QUOTED_IDENTIFIER is OFF
720 throw new MWException( "You can't use square brackers in the identifier '$identifier'" );
722 return "[$identifier]";
727 * Precondition: This object is connected as the superuser.
728 * Creates the database, schema, user and login.
730 function initial_setup( $dbName, $newUser, $loginPassword ) {
731 $dbName = $this->escapeIdentifier( $dbName );
733 // It is not clear what can be used as a login,
734 // From http://msdn.microsoft.com/en-us/library/ms173463.aspx
735 // a sysname may be the same as an identifier.
736 $newUser = $this->escapeIdentifier( $newUser );
737 $loginPassword = $this->addQuotes( $loginPassword );
739 $this->doQuery("CREATE DATABASE $dbName;");
740 $this->doQuery("USE $dbName;");
741 $this->doQuery("CREATE SCHEMA $dbName;");
746 PASSWORD=$loginPassword
755 DEFAULT_SCHEMA=$dbName
768 CREATE FULLTEXT CATALOG
786 function encodeBlob( $b ) {
787 // we can't have zero's and such, this is a simple encoding to make sure we don't barf
788 return base64_encode( $b );
791 function decodeBlob( $b ) {
792 // we can't have zero's and such, this is a simple encoding to make sure we don't barf
793 return base64_decode( $b );
799 function tableNamesWithUseIndexOrJOIN( $tables, $use_index = array(), $join_conds = array() ) {
802 $use_index_safe = is_array( $use_index ) ?
$use_index : array();
803 $join_conds_safe = is_array( $join_conds ) ?
$join_conds : array();
804 foreach ( $tables as $table ) {
805 // Is there a JOIN and INDEX clause for this table?
806 if ( isset( $join_conds_safe[$table] ) && isset( $use_index_safe[$table] ) ) {
807 $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table );
808 $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) );
809 $tableClause .= ' ON (' . $this->makeList( (array)$join_conds_safe[$table][1], LIST_AND
) . ')';
810 $retJOIN[] = $tableClause;
811 // Is there an INDEX clause?
812 } elseif ( isset( $use_index_safe[$table] ) ) {
813 $tableClause = $this->tableName( $table );
814 $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) );
815 $ret[] = $tableClause;
816 // Is there a JOIN clause?
817 } elseif ( isset( $join_conds_safe[$table] ) ) {
818 $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table );
819 $tableClause .= ' ON (' . $this->makeList( (array)$join_conds_safe[$table][1], LIST_AND
) . ')';
820 $retJOIN[] = $tableClause;
822 $tableClause = $this->tableName( $table );
823 $ret[] = $tableClause;
826 // We can't separate explicit JOIN clauses with ',', use ' ' for those
827 $straightJoins = !empty( $ret ) ?
implode( ',', $ret ) : "";
828 $otherJoins = !empty( $retJOIN ) ?
implode( ' ', $retJOIN ) : "";
829 // Compile our final table clause
830 return implode( ' ', array( $straightJoins, $otherJoins ) );
833 function strencode( $s ) { # Should not be called by us
834 return str_replace( "'", "''", $s );
837 function addQuotes( $s ) {
838 if ( $s instanceof Blob
) {
839 return "'" . $s->fetch( $s ) . "'";
841 return parent
::addQuotes( $s );
845 public function addIdentifierQuotes( $s ) {
846 // http://msdn.microsoft.com/en-us/library/aa223962.aspx
847 return '[' . $s . ']';
850 public function isQuotedIdentifier( $name ) {
851 return $name[0] == '[' && substr( $name, -1, 1 ) == ']';
854 function selectDB( $db ) {
855 return ( $this->query( "SET DATABASE $db" ) !== false );
861 * @param $options Array: an associative array of options to be turned into
862 * an SQL query, valid keys are listed in the function.
865 function makeSelectOptions( $options ) {
869 $noKeyOptions = array();
870 foreach ( $options as $key => $option ) {
871 if ( is_numeric( $key ) ) {
872 $noKeyOptions[$option] = true;
876 if ( isset( $options['GROUP BY'] ) ) {
877 $tailOpts .= " GROUP BY {$options['GROUP BY']}";
879 if ( isset( $options['HAVING'] ) ) {
880 $tailOpts .= " HAVING {$options['GROUP BY']}";
882 if ( isset( $options['ORDER BY'] ) ) {
883 $tailOpts .= " ORDER BY {$options['ORDER BY']}";
886 if ( isset( $noKeyOptions['DISTINCT'] ) && isset( $noKeyOptions['DISTINCTROW'] ) ) {
887 $startOpts .= 'DISTINCT';
890 // we want this to be compatible with the output of parent::makeSelectOptions()
891 return array( $startOpts, '' , $tailOpts, '' );
895 * Get the type of the DBMS, as it appears in $wgDBtype.
901 function buildConcat( $stringList ) {
902 return implode( ' + ', $stringList );
905 public function getSearchEngine() {
906 return "SearchMssql";
910 * Since MSSQL doesn't recognize the infinity keyword, set date manually.
911 * @todo Remove magic date
913 public function getInfinity() {
914 return '3000-01-31 00:00:00.000';
917 } // end DatabaseMssql class
924 class MssqlField
implements Field
{
925 private $name, $tablename, $default, $max_length, $nullable, $type;
926 function __construct ( $info ) {
927 $this->name
= $info['COLUMN_NAME'];
928 $this->tablename
= $info['TABLE_NAME'];
929 $this->default = $info['COLUMN_DEFAULT'];
930 $this->max_length
= $info['CHARACTER_MAXIMUM_LENGTH'];
931 $this->nullable
= !( strtolower( $info['IS_NULLABLE'] ) == 'no' );
932 $this->type
= $info['DATA_TYPE'];
939 function tableName() {
940 return $this->tableName
;
943 function defaultValue() {
944 return $this->default;
947 function maxLength() {
948 return $this->max_length
;
951 function isNullable() {
952 return $this->nullable
;
961 * The MSSQL PHP driver doesn't support sqlsrv_num_rows, so we recall all rows into an array and maintain our
962 * own cursor index into that array...This is similar to the way the Oracle driver handles this same issue
968 public function __construct( $queryresult = false ) {
970 $this->mRows
= array();
971 $this->mNumFields
= sqlsrv_num_fields( $queryresult );
972 $this->mFieldMeta
= sqlsrv_field_metadata( $queryresult );
974 $rows = sqlsrv_fetch_array( $queryresult, SQLSRV_FETCH_ASSOC
);
976 foreach( $rows as $row ) {
977 if ( $row !== null ) {
978 foreach ( $row as $k => $v ) {
979 if ( is_object( $v ) && method_exists( $v, 'format' ) ) {// DateTime Object
980 $row[$k] = $v->format( "Y-m-d\TH:i:s\Z" );
983 $this->mRows
[] = $row;// read results into memory, cursors are not supported
986 $this->mRowCount
= count( $this->mRows
);
987 sqlsrv_free_stmt( $queryresult );
990 private function array_to_obj( $array, &$obj ) {
991 foreach ( $array as $key => $value ) {
992 if ( is_array( $value ) ) {
993 $obj->$key = new stdClass();
994 $this->array_to_obj( $value, $obj->$key );
996 if ( !empty( $key ) ) {
1004 public function fetch( $mode = SQLSRV_FETCH_BOTH
, $object_class = 'stdClass' ) {
1005 if ( $this->mCursor
>= $this->mRowCount ||
$this->mRowCount
== 0 ) {
1009 if ( $mode == SQLSRV_FETCH_NUMERIC ||
$mode == SQLSRV_FETCH_BOTH
) {
1010 foreach ( $this->mRows
[$this->mCursor
] as $value ) {
1015 case SQLSRV_FETCH_ASSOC
:
1016 $ret = $this->mRows
[$this->mCursor
];
1018 case SQLSRV_FETCH_NUMERIC
:
1022 $o = new $object_class;
1023 $ret = $this->array_to_obj( $this->mRows
[$this->mCursor
], $o );
1025 case SQLSRV_FETCH_BOTH
:
1027 $ret = $this->mRows
[$this->mCursor
] +
$arrNum;
1035 public function get( $pos, $fld ) {
1036 return $this->mRows
[$pos][$fld];
1039 public function numrows() {
1040 return $this->mRowCount
;
1043 public function seek( $iRow ) {
1044 $this->mCursor
= min( $iRow, $this->mRowCount
);
1047 public function numfields() {
1048 return $this->mNumFields
;
1051 public function fieldname( $nr ) {
1052 $arrKeys = array_keys( $this->mRows
[0] );
1053 return $arrKeys[$nr];
1056 public function fieldtype( $nr ) {
1059 foreach ( $this->mFieldMeta
as $meta ) {
1061 $intType = $meta['Type'];
1066 // http://msdn.microsoft.com/en-us/library/cc296183.aspx contains type table
1067 switch( $intType ) {
1068 case SQLSRV_SQLTYPE_BIGINT
: $strType = 'bigint'; break;
1069 case SQLSRV_SQLTYPE_BINARY
: $strType = 'binary'; break;
1070 case SQLSRV_SQLTYPE_BIT
: $strType = 'bit'; break;
1071 case SQLSRV_SQLTYPE_CHAR
: $strType = 'char'; break;
1072 case SQLSRV_SQLTYPE_DATETIME
: $strType = 'datetime'; break;
1073 case SQLSRV_SQLTYPE_DECIMAL
/*($precision, $scale)*/: $strType = 'decimal'; break;
1074 case SQLSRV_SQLTYPE_FLOAT
: $strType = 'float'; break;
1075 case SQLSRV_SQLTYPE_IMAGE
: $strType = 'image'; break;
1076 case SQLSRV_SQLTYPE_INT
: $strType = 'int'; break;
1077 case SQLSRV_SQLTYPE_MONEY
: $strType = 'money'; break;
1078 case SQLSRV_SQLTYPE_NCHAR
/*($charCount)*/: $strType = 'nchar'; break;
1079 case SQLSRV_SQLTYPE_NUMERIC
/*($precision, $scale)*/: $strType = 'numeric'; break;
1080 case SQLSRV_SQLTYPE_NVARCHAR
/*($charCount)*/: $strType = 'nvarchar'; break;
1081 // case SQLSRV_SQLTYPE_NVARCHAR('max'): $strType = 'nvarchar(MAX)'; break;
1082 case SQLSRV_SQLTYPE_NTEXT
: $strType = 'ntext'; break;
1083 case SQLSRV_SQLTYPE_REAL
: $strType = 'real'; break;
1084 case SQLSRV_SQLTYPE_SMALLDATETIME
: $strType = 'smalldatetime'; break;
1085 case SQLSRV_SQLTYPE_SMALLINT
: $strType = 'smallint'; break;
1086 case SQLSRV_SQLTYPE_SMALLMONEY
: $strType = 'smallmoney'; break;
1087 case SQLSRV_SQLTYPE_TEXT
: $strType = 'text'; break;
1088 case SQLSRV_SQLTYPE_TIMESTAMP
: $strType = 'timestamp'; break;
1089 case SQLSRV_SQLTYPE_TINYINT
: $strType = 'tinyint'; break;
1090 case SQLSRV_SQLTYPE_UNIQUEIDENTIFIER
: $strType = 'uniqueidentifier'; break;
1091 case SQLSRV_SQLTYPE_UDT
: $strType = 'UDT'; break;
1092 case SQLSRV_SQLTYPE_VARBINARY
/*($byteCount)*/: $strType = 'varbinary'; break;
1093 // case SQLSRV_SQLTYPE_VARBINARY('max'): $strType = 'varbinary(MAX)'; break;
1094 case SQLSRV_SQLTYPE_VARCHAR
/*($charCount)*/: $strType = 'varchar'; break;
1095 // case SQLSRV_SQLTYPE_VARCHAR('max'): $strType = 'varchar(MAX)'; break;
1096 case SQLSRV_SQLTYPE_XML
: $strType = 'xml'; break;
1097 default: $strType = $intType;
1102 public function free() {
1103 unset( $this->mRows
);