3 * This is the MS SQL Server Native database abstraction layer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 * @author Joel Penner <a-joelpe at microsoft dot com>
23 * @author Chris Pucci <a-cpucci at microsoft dot com>
24 * @author Ryan Biesemeyer <v-ryanbi at microsoft dot com>
30 class DatabaseMssql
extends DatabaseBase
{
32 protected $mLastResult = null;
34 /** @var int The number of rows affected as an integer */
35 protected $mAffectedRows = null;
38 * @var int Post number for database
39 * @todo Unused and can be removed?
44 private $mInsertId = null;
46 function cascadingDeletes() {
50 function cleanupTriggers() {
54 function strictIPs() {
58 function realTimestamps() {
62 function implicitGroupby() {
66 function implicitOrderby() {
70 function functionalIndexes() {
74 function unionSupportsOrderAndLimit() {
79 * Usually aborts on failure
80 * @param string $server
82 * @param string $password
83 * @param string $dbName
84 * @throws DBConnectionError
85 * @return bool|DatabaseBase|null
87 function open( $server, $user, $password, $dbName ) {
88 # Test for driver support, to avoid suppressed fatal error
89 if ( !function_exists( 'sqlsrv_connect' ) ) {
90 throw new DBConnectionError(
92 "MS Sql Server Native (sqlsrv) functions missing. You can download " .
93 "the driver from: http://go.microsoft.com/fwlink/?LinkId=123470\n" );
98 # e.g. the class is being loaded
99 if ( !strlen( $user ) ) {
104 $this->mServer
= $server;
105 $this->mPort
= $wgDBport;
106 $this->mUser
= $user;
107 $this->mPassword
= $password;
108 $this->mDBname
= $dbName;
110 $connectionInfo = array();
113 $connectionInfo['Database'] = $dbName;
116 // Start NT Auth Hack
117 // Quick and dirty work around to provide NT Auth designation support.
118 // Current solution requires installer to know to input 'ntauth' for
119 // both username and password to trigger connection via NT Auth. Ugly,
121 // @todo Make this better and add NT Auth choice to MW installer when
122 // SQL Server option is chosen.
123 $ntAuthUserTest = strtolower( $user );
124 $ntAuthPassTest = strtolower( $password );
126 // Decide which auth scenerio to use
127 if ( $ntAuthPassTest == 'ntauth' && $ntAuthUserTest == 'ntauth' ) {
128 // Don't add credentials to $connectionInfo
130 $connectionInfo['UID'] = $user;
131 $connectionInfo['PWD'] = $password;
135 wfSuppressWarnings();
136 $this->mConn
= sqlsrv_connect( $server, $connectionInfo );
139 if ( $this->mConn
=== false ) {
140 wfDebug( "DB connection error\n" );
141 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " .
142 substr( $password, 0, 3 ) . "...\n" );
143 wfDebug( $this->lastError() . "\n" );
148 $this->mOpened
= true;
154 * Closes a database connection, if it is open
155 * Returns success, true if already closed
158 protected function closeConnection() {
159 return sqlsrv_close( $this->mConn
);
164 * @return bool|MssqlResult
165 * @throws DBUnexpectedError
167 protected function doQuery( $sql ) {
168 wfDebug( "SQL: [$sql]\n" );
170 // several extensions seem to think that all databases support limits
171 // via LIMIT N after the WHERE clause well, MSSQL uses SELECT TOP N,
172 // so to catch any of those extensions we'll do a quick check for a
173 // LIMIT clause and pass $sql through $this->LimitToTopN() which parses
174 // the limit clause and passes the result to $this->limitResult();
175 if ( preg_match( '/\bLIMIT\s*/i', $sql ) ) {
176 // massage LIMIT -> TopN
177 $sql = $this->LimitToTopN( $sql );
180 // MSSQL doesn't have EXTRACT(epoch FROM XXX)
181 if ( preg_match( '#\bEXTRACT\s*?\(\s*?EPOCH\s+FROM\b#i', $sql, $matches ) ) {
182 // This is same as UNIX_TIMESTAMP, we need to calc # of seconds from 1970
183 $sql = str_replace( $matches[0], "DATEDIFF(s,CONVERT(datetime,'1/1/1970'),", $sql );
187 $stmt = sqlsrv_query( $this->mConn
, $sql );
188 if ( $stmt == false ) {
189 $message = "A database error has occurred. Did you forget " .
190 "to run maintenance/update.php after upgrading? See: " .
191 "http://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
192 "Query: " . htmlentities( $sql ) . "\n" .
193 "Function: " . __METHOD__
. "\n";
194 // process each error (our driver will give us an array of errors unlike other providers)
195 foreach ( sqlsrv_errors() as $error ) {
196 $message .= $message . "ERROR[" . $error['code'] . "] " . $error['message'] . "\n";
199 throw new DBUnexpectedError( $this, $message );
201 // remember number of rows affected
202 $this->mAffectedRows
= sqlsrv_rows_affected( $stmt );
204 // if it is a SELECT statement, or an insert with a request to output
205 // something we want to return a row.
206 if ( ( preg_match( '#\bSELECT\s#i', $sql ) ) ||
207 ( preg_match( '#\bINSERT\s#i', $sql ) && preg_match( '#\bOUTPUT\s+INSERTED\b#i', $sql ) )
209 // this is essentially a rowset, but Mediawiki calls these 'result'
210 // the rowset owns freeing the statement
211 $res = new MssqlResult( $stmt );
213 // otherwise we simply return it was successful, failure throws an exception
221 * @param mixed|ResultWrapper $res
223 function freeResult( $res ) {
224 if ( $res instanceof ResultWrapper
) {
231 * @param ResultWrapper|stdClass $res
234 function fetchObject( $res ) {
235 if ( $res instanceof ResultWrapper
) {
238 $row = $res->fetch( 'OBJECT' );
246 function getErrors() {
248 $retErrors = sqlsrv_errors( SQLSRV_ERR_ALL
);
249 if ( $retErrors != null ) {
250 foreach ( $retErrors as $arrError ) {
251 $strRet .= "SQLState: " . $arrError['SQLSTATE'] . "\n";
252 $strRet .= "Error Code: " . $arrError['code'] . "\n";
253 $strRet .= "Message: " . $arrError['message'] . "\n";
256 $strRet = "No errors found";
263 * @param resource $res
266 function fetchRow( $res ) {
267 if ( $res instanceof ResultWrapper
) {
270 $row = $res->fetch( SQLSRV_FETCH_BOTH
);
279 function numRows( $res ) {
280 if ( $res instanceof ResultWrapper
) {
284 return ( $res ) ?
$res->numrows() : 0;
287 function numFields( $res ) {
288 if ( $res instanceof ResultWrapper
) {
292 return ( $res ) ?
$res->numfields() : 0;
295 function fieldName( $res, $n ) {
296 if ( $res instanceof ResultWrapper
) {
300 return ( $res ) ?
$res->fieldname( $n ) : 0;
304 * This must be called after nextSequenceVal
307 function insertId() {
308 return $this->mInsertId
;
316 function dataSeek( $res, $row ) {
317 if ( $res instanceof ResultWrapper
) {
321 return ( $res ) ?
$res->seek( $row ) : false;
324 function lastError() {
325 if ( $this->mConn
) {
326 return $this->getErrors();
328 return "No database connection";
332 function lastErrno() {
333 $err = sqlsrv_errors( SQLSRV_ERR_ALL
);
335 return $err[0]['code'];
341 function affectedRows() {
342 return $this->mAffectedRows
;
348 * @param mixed $table Array or string, table name(s) (prefix auto-added)
349 * @param mixed $vars Array or string, field name(s) to be retrieved
350 * @param mixed $conds Array or string, condition(s) for WHERE
351 * @param string $fname Calling function name (use __METHOD__) for logs/profiling
352 * @param array $options Associative array of options (e.g.
353 * array('GROUP BY' => 'page_title')), see Database::makeSelectOptions
354 * code for list of supported stuff
355 * @param array $join_conds Associative array of table join conditions
356 * (optional) (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') )
357 * @return mixed Database result resource (feed to Database::fetchObject
358 * or whatever), or false on failure
360 function select( $table, $vars, $conds = '', $fname = __METHOD__
,
361 $options = array(), $join_conds = array()
363 $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
364 if ( isset( $options['EXPLAIN'] ) ) {
365 sqlsrv_query( $this->mConn
, "SET SHOWPLAN_ALL ON;" );
366 $ret = $this->query( $sql, $fname );
367 sqlsrv_query( $this->mConn
, "SET SHOWPLAN_ALL OFF;" );
372 return $this->query( $sql, $fname );
378 * @param mixed $table Array or string, table name(s) (prefix auto-added)
379 * @param mixed $vars Array or string, field name(s) to be retrieved
380 * @param mixed $conds Array or string, condition(s) for WHERE
381 * @param string $fname Calling function name (use __METHOD__) for logs/profiling
382 * @param array $options Associative array of options (e.g. array('GROUP BY' => 'page_title')),
383 * see Database::makeSelectOptions code for list of supported stuff
384 * @param array $join_conds Associative array of table join conditions (optional)
385 * (e.g. array( 'page' => array('LEFT JOIN','page_latest=rev_id') )
386 * @return string The SQL text
388 function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__
,
389 $options = array(), $join_conds = array()
391 if ( isset( $options['EXPLAIN'] ) ) {
392 unset( $options['EXPLAIN'] );
395 return parent
::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
399 * Estimate rows in dataset
400 * Returns estimated count, based on SHOWPLAN_ALL output
401 * This is not necessarily an accurate estimate, so use sparingly
402 * Returns -1 if count cannot be found
403 * Takes same arguments as Database::select()
404 * @param string $table
405 * @param string $vars
406 * @param string $conds
407 * @param string $fname
408 * @param array $options
411 function estimateRowCount( $table, $vars = '*', $conds = '',
412 $fname = __METHOD__
, $options = array()
414 // http://msdn2.microsoft.com/en-us/library/aa259203.aspx
415 $options['EXPLAIN'] = true;
416 $res = $this->select( $table, $vars, $conds, $fname, $options );
420 $row = $this->fetchRow( $res );
421 if ( isset( $row['EstimateRows'] ) ) {
422 $rows = $row['EstimateRows'];
430 * Returns information about an index
431 * If errors are explicitly ignored, returns NULL on failure
432 * @param string $table
433 * @param string $index
434 * @param string $fname
435 * @return array|bool|null
437 function indexInfo( $table, $index, $fname = __METHOD__
) {
438 # This does not return the same info as MYSQL would, but that's OK
439 # because MediaWiki never uses the returned value except to check for
440 # the existance of indexes.
441 $sql = "sp_helpindex '" . $table . "'";
442 $res = $this->query( $sql, $fname );
448 foreach ( $res as $row ) {
449 if ( $row->index_name
== $index ) {
450 $row->Non_unique
= !stristr( $row->index_description
, "unique" );
451 $cols = explode( ", ", $row->index_keys
);
452 foreach ( $cols as $col ) {
453 $row->Column_name
= trim( $col );
454 $result[] = clone $row;
456 } elseif ( $index == 'PRIMARY' && stristr( $row->index_description
, 'PRIMARY' ) ) {
457 $row->Non_unique
= 0;
458 $cols = explode( ", ", $row->index_keys
);
459 foreach ( $cols as $col ) {
460 $row->Column_name
= trim( $col );
461 $result[] = clone $row;
466 return empty( $result ) ?
false : $result;
470 * INSERT wrapper, inserts an array into a table
472 * $arrToInsert may be a single associative array, or an array of these with numeric keys, for
475 * Usually aborts on failure
476 * If errors are explicitly ignored, returns success
477 * @param string $table
478 * @param array $arrToInsert
479 * @param string $fname
480 * @param array $options
481 * @throws DBQueryError
484 function insert( $table, $arrToInsert, $fname = __METHOD__
, $options = array() ) {
485 # No rows to insert, easy just return now
486 if ( !count( $arrToInsert ) ) {
490 if ( !is_array( $options ) ) {
491 $options = array( $options );
494 $table = $this->tableName( $table );
496 if ( !( isset( $arrToInsert[0] ) && is_array( $arrToInsert[0] ) ) ) { // Not multi row
497 $arrToInsert = array( 0 => $arrToInsert ); // make everything multi row compatible
502 // We know the table we're inserting into, get its identity column
504 // strip matching square brackets from table name
505 $tableRaw = preg_replace( '#\[([^\]]*)\]#', '$1', $table );
506 $res = $this->doQuery(
507 "SELECT NAME AS idColumn FROM SYS.IDENTITY_COLUMNS " .
508 "WHERE OBJECT_NAME(OBJECT_ID)='{$tableRaw}'"
510 if ( $res && $res->numrows() ) {
511 // There is an identity for this table.
512 $identity = array_pop( $res->fetch( SQLSRV_FETCH_ASSOC
) );
516 foreach ( $arrToInsert as $a ) {
517 // start out with empty identity column, this is so we can return
518 // it as a result of the insert logic
521 $identityClause = '';
523 // if we have an identity column
526 foreach ( $a as $k => $v ) {
527 if ( $k == $identity ) {
528 if ( !is_null( $v ) ) {
529 // there is a value being passed to us, we need to turn on and off inserted identity
530 $sqlPre = "SET IDENTITY_INSERT $table ON;";
531 $sqlPost = ";SET IDENTITY_INSERT $table OFF;";
533 // we can't insert NULL into an identity column, so remove the column from the insert.
539 // we want to output an identity column as result
540 $identityClause = "OUTPUT INSERTED.$identity ";
543 $keys = array_keys( $a );
545 // INSERT IGNORE is not supported by SQL Server
546 // remove IGNORE from options list and set ignore flag to true
547 $ignoreClause = false;
548 foreach ( $options as $k => $v ) {
549 if ( strtoupper( $v ) == "IGNORE" ) {
550 unset( $options[$k] );
551 $ignoreClause = true;
555 // translate MySQL INSERT IGNORE to something SQL Server can use
557 // MySQL: INSERT IGNORE INTO user_groups (ug_user,ug_group) VALUES ('1','sysop')
558 // MSSQL: IF NOT EXISTS (SELECT * FROM user_groups WHERE ug_user = '1')
559 // INSERT INTO user_groups (ug_user,ug_group) VALUES ('1','sysop')
560 if ( $ignoreClause ) {
561 $prival = $a[$keys[0]];
562 $sqlPre .= "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = '$prival')";
565 // Build the actual query
566 $sql = $sqlPre . 'INSERT ' . implode( ' ', $options ) .
567 " INTO $table (" . implode( ',', $keys ) . ") $identityClause VALUES (";
570 foreach ( $a as $value ) {
576 if ( is_string( $value ) ) {
577 $sql .= $this->addQuotes( $value );
578 } elseif ( is_null( $value ) ) {
580 } elseif ( is_array( $value ) ||
is_object( $value ) ) {
581 if ( is_object( $value ) && strtolower( get_class( $value ) ) == 'blob' ) {
582 $sql .= $this->addQuotes( $value );
584 $sql .= $this->addQuotes( serialize( $value ) );
590 $sql .= ')' . $sqlPost;
593 $ret = sqlsrv_query( $this->mConn
, $sql );
595 if ( $ret === false ) {
596 throw new DBQueryError( $this, $this->getErrors(), $this->lastErrno(), $sql, $fname );
597 } elseif ( $ret != null ) {
598 // remember number of rows affected
599 $this->mAffectedRows
= sqlsrv_rows_affected( $ret );
600 if ( !is_null( $identity ) ) {
601 // then we want to get the identity column value we were assigned and save it off
602 $row = sqlsrv_fetch_object( $ret );
603 $this->mInsertId
= $row->$identity;
605 sqlsrv_free_stmt( $ret );
615 * INSERT SELECT wrapper
616 * $varMap must be an associative array of the form array( 'dest1' => 'source1', ...)
617 * Source items may be literals rather than field names, but strings should
618 * be quoted with Database::addQuotes().
619 * @param string $destTable
620 * @param array|string $srcTable May be an array of tables.
621 * @param array $varMap
622 * @param array $conds May be "*" to copy the whole table.
623 * @param string $fname
624 * @param array $insertOptions
625 * @param array $selectOptions
626 * @throws DBQueryError
627 * @return null|ResultWrapper
629 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__
,
630 $insertOptions = array(), $selectOptions = array() ) {
631 $ret = parent
::insertSelect(
641 if ( $ret === false ) {
642 throw new DBQueryError( $this, $this->getErrors(), $this->lastErrno(), /*$sql*/ '', $fname );
643 } elseif ( $ret != null ) {
644 // remember number of rows affected
645 // @todo FIXME: $ret type does not appear to be a resource
646 $this->mAffectedRows
= sqlsrv_rows_affected( $ret );
655 * Return the next in a sequence, save the value for retrieval via insertId()
656 * @param string $seqName
659 function nextSequenceValue( $seqName ) {
660 if ( !$this->tableExists( 'sequence_' . $seqName ) ) {
663 "CREATE TABLE [sequence_$seqName] (id INT NOT NULL IDENTITY PRIMARY KEY, junk varchar(10) NULL)"
666 sqlsrv_query( $this->mConn
, "INSERT INTO [sequence_$seqName] (junk) VALUES ('')" );
667 $ret = sqlsrv_query( $this->mConn
, "SELECT TOP 1 id FROM [sequence_$seqName] ORDER BY id DESC" );
668 // KEEP ASSOC THERE, weird weird bug dealing with the return value if you don't
669 $row = sqlsrv_fetch_array( $ret, SQLSRV_FETCH_ASSOC
);
671 sqlsrv_free_stmt( $ret );
672 $this->mInsertId
= $row['id'];
678 * Return the current value of a sequence. Assumes it has ben nextval'ed in this session.
679 * @param string $seqName
682 function currentSequenceValue( $seqName ) {
683 $ret = sqlsrv_query( $this->mConn
, "SELECT TOP 1 id FROM [sequence_$seqName] ORDER BY id DESC" );
684 if ( $ret !== false ) {
685 $row = sqlsrv_fetch_array( $ret );
686 sqlsrv_free_stmt( $ret );
690 return $this->nextSequenceValue( $seqName );
695 * @param string $table
696 * @param string $field
697 * @return int Returns the size of a text field, or -1 for "unlimited"
699 function textFieldSize( $table, $field ) {
700 $table = $this->tableName( $table );
701 $sql = "SELECT CHARACTER_MAXIMUM_LENGTH,DATA_TYPE FROM INFORMATION_SCHEMA.Columns
702 WHERE TABLE_NAME = '$table' AND COLUMN_NAME = '$field'";
703 $res = $this->query( $sql );
704 $row = $this->fetchRow( $res );
706 if ( strtolower( $row['DATA_TYPE'] ) != 'text' ) {
707 $size = $row['CHARACTER_MAXIMUM_LENGTH'];
714 * Construct a LIMIT query with optional offset
715 * This is used for query pages
717 * @param string $sql SQL query we will append the limit too
718 * @param int $limit The SQL limit
719 * @param bool|int $offset The SQL offset (default false)
720 * @return array|string
722 function limitResult( $sql, $limit, $offset = false ) {
723 if ( $offset === false ||
$offset == 0 ) {
724 if ( strpos( $sql, "SELECT" ) === false ) {
725 return "TOP {$limit} " . $sql;
727 return preg_replace( '/\bSELECT(\s*DISTINCT)?\b/Dsi', 'SELECT$1 TOP ' . $limit, $sql, 1 );
732 SELECT sub2.*, ROW_NUMBER() OVER(ORDER BY sub2.line2) AS line3 FROM (
733 SELECT 1 AS line2, sub1.* FROM (' . $sql . ') AS sub1
736 WHERE line3 BETWEEN ' . ( $offset +
1 ) . ' AND ' . ( $offset +
$limit );
743 * If there is a limit clause, parse it, strip it, and pass the remaining
744 * SQL through limitResult() with the appropriate parameters. Not the
745 * prettiest solution, but better than building a whole new parser. This
746 * exists becase there are still too many extensions that don't use dynamic
750 * @return array|mixed|string
752 function LimitToTopN( $sql ) {
753 // Matches: LIMIT {[offset,] row_count | row_count OFFSET offset}
754 $pattern = '/\bLIMIT\s+((([0-9]+)\s*,\s*)?([0-9]+)(\s+OFFSET\s+([0-9]+))?)/i';
755 if ( preg_match( $pattern, $sql, $matches ) ) {
756 // row_count = $matches[4]
757 $row_count = $matches[4];
758 // offset = $matches[3] OR $matches[6]
759 $offset = $matches[3] or
760 $offset = $matches[6] or
763 // strip the matching LIMIT clause out
764 $sql = str_replace( $matches[0], '', $sql );
766 return $this->limitResult( $sql, $row_count, $offset );
773 * Timestamp in ISO 8601 format with no timezone: 1986-02-09T20:00:00Z
775 * @param int $ts Timestamp
776 * @return bool|string The same date in ISO 8601 format with no timezone or false
778 function timestamp( $ts = 0 ) {
779 return wfTimestamp( TS_ISO_8601
, $ts );
783 * @return string Wikitext of a link to the server software's web site
785 public function getSoftwareLink() {
786 return "[http://www.microsoft.com/sql/ MS SQL Server]";
790 * @return string Version information from the database
792 function getServerVersion() {
793 $server_info = sqlsrv_server_info( $this->mConn
);
795 if ( isset( $server_info['SQLServerVersion'] ) ) {
796 $version = $server_info['SQLServerVersion'];
803 * @param string $table
804 * @param string $fname
805 * @param bool $schema
808 function tableExists( $table, $fname = __METHOD__
, $schema = false ) {
809 $res = sqlsrv_query( $this->mConn
, "SELECT * FROM information_schema.tables
810 WHERE table_type='BASE TABLE' AND table_name = '$table'" );
811 if ( $res === false ) {
812 print "Error in tableExists query: " . $this->getErrors();
816 if ( sqlsrv_fetch( $res ) ) {
824 * Query whether a given column exists in the mediawiki schema
825 * @param string $table
826 * @param string $field
827 * @param string $fname
830 function fieldExists( $table, $field, $fname = __METHOD__
) {
831 $table = $this->tableName( $table );
832 $res = sqlsrv_query( $this->mConn
, "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.Columns
833 WHERE TABLE_NAME = '$table' AND COLUMN_NAME = '$field'" );
834 if ( $res === false ) {
835 print "Error in fieldExists query: " . $this->getErrors();
839 if ( sqlsrv_fetch( $res ) ) {
846 function fieldInfo( $table, $field ) {
847 $table = $this->tableName( $table );
848 $res = sqlsrv_query( $this->mConn
, "SELECT * FROM INFORMATION_SCHEMA.Columns
849 WHERE TABLE_NAME = '$table' AND COLUMN_NAME = '$field'" );
850 if ( $res === false ) {
851 print "Error in fieldInfo query: " . $this->getErrors();
855 $meta = $this->fetchRow( $res );
857 return new MssqlField( $meta );
864 * Begin a transaction, committing any previously open transaction
866 protected function doBegin( $fname = __METHOD__
) {
867 sqlsrv_begin_transaction( $this->mConn
);
868 $this->mTrxLevel
= 1;
874 protected function doCommit( $fname = __METHOD__
) {
875 sqlsrv_commit( $this->mConn
);
876 $this->mTrxLevel
= 0;
880 * Rollback a transaction.
881 * No-op on non-transactional databases.
883 protected function doRollback( $fname = __METHOD__
) {
884 sqlsrv_rollback( $this->mConn
);
885 $this->mTrxLevel
= 0;
889 * Escapes a identifier for use inm SQL.
890 * Throws an exception if it is invalid.
891 * Reference: http://msdn.microsoft.com/en-us/library/aa224033%28v=SQL.80%29.aspx
892 * @param string $identifier
893 * @throws MWException
896 private function escapeIdentifier( $identifier ) {
897 if ( strlen( $identifier ) == 0 ) {
898 throw new MWException( "An identifier must not be empty" );
900 if ( strlen( $identifier ) > 128 ) {
901 throw new MWException( "The identifier '$identifier' is too long (max. 128)" );
903 if ( ( strpos( $identifier, '[' ) !== false ) ||
( strpos( $identifier, ']' ) !== false ) ) {
904 // It may be allowed if you quoted with double quotation marks, but
905 // that would break if QUOTED_IDENTIFIER is OFF
906 throw new MWException( "You can't use square brackers in the identifier '$identifier'" );
909 return "[$identifier]";
914 * Precondition: This object is connected as the superuser.
915 * Creates the database, schema, user and login.
917 function initial_setup( $dbName, $newUser, $loginPassword ) {
918 $dbName = $this->escapeIdentifier( $dbName );
920 // It is not clear what can be used as a login,
921 // From http://msdn.microsoft.com/en-us/library/ms173463.aspx
922 // a sysname may be the same as an identifier.
923 $newUser = $this->escapeIdentifier( $newUser );
924 $loginPassword = $this->addQuotes( $loginPassword );
926 $this->doQuery( "CREATE DATABASE $dbName;" );
927 $this->doQuery( "USE $dbName;" );
928 $this->doQuery( "CREATE SCHEMA $dbName;" );
933 PASSWORD=$loginPassword
942 DEFAULT_SCHEMA=$dbName
955 CREATE FULLTEXT CATALOG
971 function encodeBlob( $b ) {
972 // we can't have zero's and such, this is a simple encoding to make sure we don't barf
973 return base64_encode( $b );
976 function decodeBlob( $b ) {
977 // we can't have zero's and such, this is a simple encoding to make sure we don't barf
978 return base64_decode( $b );
982 * @param array $tables
983 * @param array $use_index
984 * @param array $join_conds
987 protected function tableNamesWithUseIndexOrJOIN( $tables, $use_index = array(), $join_conds = array() ) {
990 $use_index_safe = is_array( $use_index ) ?
$use_index : array();
991 $join_conds_safe = is_array( $join_conds ) ?
$join_conds : array();
992 foreach ( $tables as $table ) {
993 // Is there a JOIN and INDEX clause for this table?
994 if ( isset( $join_conds_safe[$table] ) && isset( $use_index_safe[$table] ) ) {
995 $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table );
996 $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) );
997 $tableClause .= ' ON (' . $this->makeList( (array)$join_conds_safe[$table][1], LIST_AND
) . ')';
998 $retJOIN[] = $tableClause;
999 // Is there an INDEX clause?
1000 } elseif ( isset( $use_index_safe[$table] ) ) {
1001 $tableClause = $this->tableName( $table );
1002 $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) );
1003 $ret[] = $tableClause;
1004 // Is there a JOIN clause?
1005 } elseif ( isset( $join_conds_safe[$table] ) ) {
1006 $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table );
1007 $tableClause .= ' ON (' . $this->makeList( (array)$join_conds_safe[$table][1], LIST_AND
) . ')';
1008 $retJOIN[] = $tableClause;
1010 $tableClause = $this->tableName( $table );
1011 $ret[] = $tableClause;
1014 // We can't separate explicit JOIN clauses with ',', use ' ' for those
1015 $straightJoins = !empty( $ret ) ?
implode( ',', $ret ) : "";
1016 $otherJoins = !empty( $retJOIN ) ?
implode( ' ', $retJOIN ) : "";
1018 // Compile our final table clause
1019 return implode( ' ', array( $straightJoins, $otherJoins ) );
1022 function strencode( $s ) { # Should not be called by us
1023 return str_replace( "'", "''", $s );
1026 function addQuotes( $s ) {
1027 if ( $s instanceof Blob
) {
1028 return "'" . $s->fetch( $s ) . "'";
1030 return parent
::addQuotes( $s );
1034 public function addIdentifierQuotes( $s ) {
1035 // http://msdn.microsoft.com/en-us/library/aa223962.aspx
1036 return '[' . $s . ']';
1039 public function isQuotedIdentifier( $name ) {
1040 return $name[0] == '[' && substr( $name, -1, 1 ) == ']';
1043 function selectDB( $db ) {
1044 return ( $this->query( "SET DATABASE $db" ) !== false );
1048 * @param array $options an associative array of options to be turned into
1049 * an SQL query, valid keys are listed in the function.
1052 function makeSelectOptions( $options ) {
1056 $noKeyOptions = array();
1057 foreach ( $options as $key => $option ) {
1058 if ( is_numeric( $key ) ) {
1059 $noKeyOptions[$option] = true;
1063 $tailOpts .= $this->makeGroupByWithHaving( $options );
1065 $tailOpts .= $this->makeOrderBy( $options );
1067 if ( isset( $noKeyOptions['DISTINCT'] ) && isset( $noKeyOptions['DISTINCTROW'] ) ) {
1068 $startOpts .= 'DISTINCT';
1071 // we want this to be compatible with the output of parent::makeSelectOptions()
1072 return array( $startOpts, '', $tailOpts, '' );
1076 * Get the type of the DBMS, as it appears in $wgDBtype.
1079 function getType() {
1083 function buildConcat( $stringList ) {
1084 return implode( ' + ', $stringList );
1087 public function getSearchEngine() {
1088 return "SearchMssql";
1092 * Since MSSQL doesn't recognize the infinity keyword, set date manually.
1093 * @todo Remove magic date
1096 public function getInfinity() {
1097 return '3000-01-31 00:00:00.000';
1099 } // end DatabaseMssql class
1106 class MssqlField
implements Field
{
1107 private $name, $tableName, $default, $max_length, $nullable, $type;
1109 function __construct( $info ) {
1110 $this->name
= $info['COLUMN_NAME'];
1111 $this->tableName
= $info['TABLE_NAME'];
1112 $this->default = $info['COLUMN_DEFAULT'];
1113 $this->max_length
= $info['CHARACTER_MAXIMUM_LENGTH'];
1114 $this->nullable
= !( strtolower( $info['IS_NULLABLE'] ) == 'no' );
1115 $this->type
= $info['DATA_TYPE'];
1122 function tableName() {
1123 return $this->tableName
;
1126 function defaultValue() {
1127 return $this->default;
1130 function maxLength() {
1131 return $this->max_length
;
1134 function isNullable() {
1135 return $this->nullable
;
1144 * The MSSQL PHP driver doesn't support sqlsrv_num_rows, so we recall all rows
1145 * into an array and maintain our own cursor index into that array... This is
1146 * similar to the way the Oracle driver handles this same issue
1157 /** @var bool|int */
1158 private $mNumFields;
1160 /** @var array|bool */
1161 private $mFieldMeta;
1164 * @param bool|resource $queryresult
1166 public function __construct( $queryresult = false ) {
1168 $this->mRows
= array();
1169 $this->mNumFields
= sqlsrv_num_fields( $queryresult );
1170 $this->mFieldMeta
= sqlsrv_field_metadata( $queryresult );
1172 $rows = sqlsrv_fetch_array( $queryresult, SQLSRV_FETCH_ASSOC
);
1174 foreach ( $rows as $row ) {
1175 if ( $row !== null ) {
1176 foreach ( $row as $k => $v ) {
1177 if ( is_object( $v ) && method_exists( $v, 'format' ) ) { // DateTime Object
1178 $row[$k] = $v->format( "Y-m-d\TH:i:s\Z" );
1181 $this->mRows
[] = $row; // read results into memory, cursors are not supported
1184 $this->mRowCount
= count( $this->mRows
);
1185 sqlsrv_free_stmt( $queryresult );
1189 * @param array $array
1190 * @param stdClass $obj
1193 private function array_to_obj( $array, &$obj ) {
1194 foreach ( $array as $key => $value ) {
1195 if ( is_array( $value ) ) {
1196 $obj->$key = new stdClass();
1197 $this->array_to_obj( $value, $obj->$key );
1199 if ( !empty( $key ) ) {
1200 $obj->$key = $value;
1208 public function fetch( $mode = SQLSRV_FETCH_BOTH
, $object_class = 'stdClass' ) {
1209 if ( $this->mCursor
>= $this->mRowCount ||
$this->mRowCount
== 0 ) {
1213 if ( $mode == SQLSRV_FETCH_NUMERIC ||
$mode == SQLSRV_FETCH_BOTH
) {
1214 foreach ( $this->mRows
[$this->mCursor
] as $value ) {
1219 case SQLSRV_FETCH_ASSOC
:
1220 $ret = $this->mRows
[$this->mCursor
];
1222 case SQLSRV_FETCH_NUMERIC
:
1226 $o = new $object_class;
1227 $ret = $this->array_to_obj( $this->mRows
[$this->mCursor
], $o );
1229 case SQLSRV_FETCH_BOTH
:
1231 $ret = $this->mRows
[$this->mCursor
] +
$arrNum;
1240 public function get( $pos, $fld ) {
1241 return $this->mRows
[$pos][$fld];
1244 public function numrows() {
1245 return $this->mRowCount
;
1248 public function seek( $iRow ) {
1249 $this->mCursor
= min( $iRow, $this->mRowCount
);
1252 public function numfields() {
1253 return $this->mNumFields
;
1256 public function fieldname( $nr ) {
1257 $arrKeys = array_keys( $this->mRows
[0] );
1259 return $arrKeys[$nr];
1262 public function fieldtype( $nr ) {
1265 foreach ( $this->mFieldMeta
as $meta ) {
1267 $intType = $meta['Type'];
1272 // http://msdn.microsoft.com/en-us/library/cc296183.aspx contains type table
1273 switch ( $intType ) {
1274 case SQLSRV_SQLTYPE_BIGINT
:
1275 $strType = 'bigint';
1277 case SQLSRV_SQLTYPE_BINARY
:
1278 $strType = 'binary';
1280 case SQLSRV_SQLTYPE_BIT
:
1283 case SQLSRV_SQLTYPE_CHAR
:
1286 case SQLSRV_SQLTYPE_DATETIME
:
1287 $strType = 'datetime';
1289 case SQLSRV_SQLTYPE_DECIMAL
: // ($precision, $scale)
1290 $strType = 'decimal';
1292 case SQLSRV_SQLTYPE_FLOAT
:
1295 case SQLSRV_SQLTYPE_IMAGE
:
1298 case SQLSRV_SQLTYPE_INT
:
1301 case SQLSRV_SQLTYPE_MONEY
:
1304 case SQLSRV_SQLTYPE_NCHAR
: // ($charCount):
1307 case SQLSRV_SQLTYPE_NUMERIC
: // ($precision, $scale):
1308 $strType = 'numeric';
1310 case SQLSRV_SQLTYPE_NVARCHAR
: // ($charCount)
1311 $strType = 'nvarchar';
1313 // case SQLSRV_SQLTYPE_NVARCHAR('max'):
1314 // $strType = 'nvarchar(MAX)';
1316 case SQLSRV_SQLTYPE_NTEXT
:
1319 case SQLSRV_SQLTYPE_REAL
:
1322 case SQLSRV_SQLTYPE_SMALLDATETIME
:
1323 $strType = 'smalldatetime';
1325 case SQLSRV_SQLTYPE_SMALLINT
:
1326 $strType = 'smallint';
1328 case SQLSRV_SQLTYPE_SMALLMONEY
:
1329 $strType = 'smallmoney';
1331 case SQLSRV_SQLTYPE_TEXT
:
1334 case SQLSRV_SQLTYPE_TIMESTAMP
:
1335 $strType = 'timestamp';
1337 case SQLSRV_SQLTYPE_TINYINT
:
1338 $strType = 'tinyint';
1340 case SQLSRV_SQLTYPE_UNIQUEIDENTIFIER
:
1341 $strType = 'uniqueidentifier';
1343 case SQLSRV_SQLTYPE_UDT
:
1346 case SQLSRV_SQLTYPE_VARBINARY
: // ($byteCount)
1347 $strType = 'varbinary';
1349 // case SQLSRV_SQLTYPE_VARBINARY('max'):
1350 // $strType = 'varbinary(MAX)';
1352 case SQLSRV_SQLTYPE_VARCHAR
: // ($charCount)
1353 $strType = 'varchar';
1355 // case SQLSRV_SQLTYPE_VARCHAR('max'):
1356 // $strType = 'varchar(MAX)';
1358 case SQLSRV_SQLTYPE_XML
:
1362 $strType = $intType;
1368 public function free() {
1369 unset( $this->mRows
);