00001 <?php
00013 class DatabaseMssql extends DatabaseBase {
00014
00015 var $mAffectedRows;
00016 var $mLastResult;
00017 var $mLastError;
00018 var $mLastErrorNo;
00019 var $mDatabaseFile;
00020
00024 function __construct($server = false, $user = false, $password = false, $dbName = false,
00025 $failFunction = false, $flags = 0, $tablePrefix = 'get from global') {
00026
00027 global $wgOut, $wgDBprefix, $wgCommandLineMode;
00028 if (!isset($wgOut)) $wgOut = null; # Can't get a reference if it hasn't been set yet
00029 $this->mOut =& $wgOut;
00030 $this->mFailFunction = $failFunction;
00031 $this->mFlags = $flags;
00032
00033 if ( $this->mFlags & DBO_DEFAULT ) {
00034 if ( $wgCommandLineMode ) {
00035 $this->mFlags &= ~DBO_TRX;
00036 } else {
00037 $this->mFlags |= DBO_TRX;
00038 }
00039 }
00040
00042 $this->mTablePrefix = $tablePrefix == 'get from global' ? $wgDBprefix : $tablePrefix;
00043
00044 if ($server) $this->open($server, $user, $password, $dbName);
00045
00046 }
00047
00048 function getType() {
00049 return 'mssql';
00050 }
00051
00055 function implicitGroupby() { return false; }
00056 function implicitOrderby() { return false; }
00057
00058 static function newFromParams($server, $user, $password, $dbName, $failFunction = false, $flags = 0) {
00059 return new DatabaseMssql($server, $user, $password, $dbName, $failFunction, $flags);
00060 }
00061
00065 function open($server,$user,$password,$dbName) {
00066 wfProfileIn(__METHOD__);
00067
00068 # Test for missing mysql.so
00069 # First try to load it
00070 if (!@extension_loaded('mssql')) {
00071 @dl('mssql.so');
00072 }
00073
00074 # Fail now
00075 # Otherwise we get a suppressed fatal error, which is very hard to track down
00076 if (!function_exists( 'mssql_connect')) {
00077 throw new DBConnectionError( $this, "MSSQL functions missing, have you compiled PHP with the --with-mssql option?\n" );
00078 }
00079
00080 $this->close();
00081 $this->mServer = $server;
00082 $this->mUser = $user;
00083 $this->mPassword = $password;
00084 $this->mDBname = $dbName;
00085
00086 wfProfileIn("dbconnect-$server");
00087
00088 # Try to connect up to three times
00089 # The kernel's default SYN retransmission period is far too slow for us,
00090 # so we use a short timeout plus a manual retry.
00091 $this->mConn = false;
00092 $max = 3;
00093 for ( $i = 0; $i < $max && !$this->mConn; $i++ ) {
00094 if ( $i > 1 ) {
00095 usleep( 1000 );
00096 }
00097 if ($this->mFlags & DBO_PERSISTENT) {
00098 @$this->mConn = mssql_pconnect($server, $user, $password);
00099 } else {
00100 # Create a new connection...
00101 @$this->mConn = mssql_connect($server, $user, $password, true);
00102 }
00103 }
00104
00105 wfProfileOut("dbconnect-$server");
00106
00107 if ($dbName != '') {
00108 if ($this->mConn !== false) {
00109 $success = @mssql_select_db($dbName, $this->mConn);
00110 if (!$success) {
00111 $error = "Error selecting database $dbName on server {$this->mServer} " .
00112 "from client host " . wfHostname() . "\n";
00113 wfLogDBError(" Error selecting database $dbName on server {$this->mServer} \n");
00114 wfDebug( $error );
00115 }
00116 } else {
00117 wfDebug("DB connection error\n");
00118 wfDebug("Server: $server, User: $user, Password: ".substr($password, 0, 3)."...\n");
00119 $success = false;
00120 }
00121 } else {
00122 # Delay USE query
00123 $success = (bool)$this->mConn;
00124 }
00125
00126 if (!$success) $this->reportConnectionError();
00127 $this->mOpened = $success;
00128 wfProfileOut(__METHOD__);
00129 return $success;
00130 }
00131
00135 function close() {
00136 $this->mOpened = false;
00137 if ($this->mConn) {
00138 if ($this->trxLevel()) $this->commit();
00139 return mssql_close($this->mConn);
00140 } else return true;
00141 }
00142
00148 function doQuery($sql) {
00149 if ($sql == 'BEGIN' || $sql == 'COMMIT' || $sql == 'ROLLBACK') return true; # $sql .= ' TRANSACTION';
00150 $sql = preg_replace('|[^\x07-\x7e]|','?',$sql); # TODO: need to fix unicode - just removing it here while testing
00151 $ret = mssql_query($sql, $this->mConn);
00152 if ($ret === false) {
00153 $err = mssql_get_last_message();
00154 if ($err) $this->mlastError = $err;
00155 $row = mssql_fetch_row(mssql_query('select @@ERROR'));
00156 if ($row[0]) $this->mlastErrorNo = $row[0];
00157 } else $this->mlastErrorNo = false;
00158 return $ret;
00159 }
00160
00164 function freeResult( $res ) {
00165 if ( $res instanceof ResultWrapper ) {
00166 $res = $res->result;
00167 }
00168 if ( !@mssql_free_result( $res ) ) {
00169 throw new DBUnexpectedError( $this, "Unable to free MSSQL result" );
00170 }
00171 }
00172
00182 function fetchObject( $res ) {
00183 if ( $res instanceof ResultWrapper ) {
00184 $res = $res->result;
00185 }
00186 @$row = mssql_fetch_object( $res );
00187 if ( $this->lastErrno() ) {
00188 throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) );
00189 }
00190 return $row;
00191 }
00192
00201 function fetchRow( $res ) {
00202 if ( $res instanceof ResultWrapper ) {
00203 $res = $res->result;
00204 }
00205 @$row = mssql_fetch_array( $res );
00206 if ( $this->lastErrno() ) {
00207 throw new DBUnexpectedError( $this, 'Error in fetchRow(): ' . htmlspecialchars( $this->lastError() ) );
00208 }
00209 return $row;
00210 }
00211
00215 function numRows( $res ) {
00216 if ( $res instanceof ResultWrapper ) {
00217 $res = $res->result;
00218 }
00219 @$n = mssql_num_rows( $res );
00220 if ( $this->lastErrno() ) {
00221 throw new DBUnexpectedError( $this, 'Error in numRows(): ' . htmlspecialchars( $this->lastError() ) );
00222 }
00223 return $n;
00224 }
00225
00231 function numFields( $res ) {
00232 if ( $res instanceof ResultWrapper ) {
00233 $res = $res->result;
00234 }
00235 return mssql_num_fields( $res );
00236 }
00237
00245 function fieldName( $res, $n ) {
00246 if ( $res instanceof ResultWrapper ) {
00247 $res = $res->result;
00248 }
00249 return mssql_field_name( $res, $n );
00250 }
00251
00262 function insertId() {
00263 $row = mssql_fetch_row(mssql_query('select @@IDENTITY'));
00264 return $row[0];
00265 }
00266
00273 function dataSeek( $res, $row ) {
00274 if ( $res instanceof ResultWrapper ) {
00275 $res = $res->result;
00276 }
00277 return mssql_data_seek( $res, $row );
00278 }
00279
00283 function lastErrno() {
00284 return $this->mlastErrorNo;
00285 }
00286
00290 function lastError() {
00291 return $this->mlastError;
00292 }
00293
00297 function affectedRows() {
00298 return mssql_rows_affected( $this->mConn );
00299 }
00300
00309 function set( $table, $var, $value, $cond, $fname = 'Database::set' )
00310 {
00311 if ($value == "NULL") $value = "''"; # see comments in makeListWithoutNulls()
00312 $table = $this->tableName( $table );
00313 $sql = "UPDATE $table SET $var = '" .
00314 $this->strencode( $value ) . "' WHERE ($cond)";
00315 return (bool)$this->query( $sql, $fname );
00316 }
00317
00323 function selectField( $table, $var, $cond='', $fname = 'Database::selectField', $options = array() ) {
00324 if ( !is_array( $options ) ) {
00325 $options = array( $options );
00326 }
00327 $options['LIMIT'] = 1;
00328
00329 $res = $this->select( $table, $var, $cond, $fname, $options );
00330 if ( $res === false || !$this->numRows( $res ) ) {
00331 return false;
00332 }
00333 $row = $this->fetchRow( $res );
00334 if ( $row !== false ) {
00335 $this->freeResult( $res );
00336 return $row[0];
00337 } else {
00338 return false;
00339 }
00340 }
00341
00352 function makeSelectOptions( $options ) {
00353 $preLimitTail = $postLimitTail = '';
00354 $startOpts = '';
00355
00356 $noKeyOptions = array();
00357 foreach ( $options as $key => $option ) {
00358 if ( is_numeric( $key ) ) {
00359 $noKeyOptions[$option] = true;
00360 }
00361 }
00362
00363 if ( isset( $options['GROUP BY'] ) ) $preLimitTail .= " GROUP BY {$options['GROUP BY']}";
00364 if ( isset( $options['HAVING'] ) ) $preLimitTail .= " HAVING {$options['HAVING']}";
00365 if ( isset( $options['ORDER BY'] ) ) $preLimitTail .= " ORDER BY {$options['ORDER BY']}";
00366
00367
00368
00369
00370
00371
00372
00373 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $postLimitTail .= ' FOR UPDATE';
00374 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $postLimitTail .= ' LOCK IN SHARE MODE';
00375 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT';
00376
00377 # Various MySQL extensions
00378 if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) $startOpts .= ' /*! STRAIGHT_JOIN */';
00379 if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) $startOpts .= ' HIGH_PRIORITY';
00380 if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) $startOpts .= ' SQL_BIG_RESULT';
00381 if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) $startOpts .= ' SQL_BUFFER_RESULT';
00382 if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) $startOpts .= ' SQL_SMALL_RESULT';
00383 if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) $startOpts .= ' SQL_CALC_FOUND_ROWS';
00384 if ( isset( $noKeyOptions['SQL_CACHE'] ) ) $startOpts .= ' SQL_CACHE';
00385 if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) $startOpts .= ' SQL_NO_CACHE';
00386
00387 if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) {
00388 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
00389 } else {
00390 $useIndex = '';
00391 }
00392
00393 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
00394 }
00395
00407 function select( $table, $vars, $conds='', $fname = 'Database::select', $options = array() )
00408 {
00409 if( is_array( $vars ) ) {
00410 $vars = implode( ',', $vars );
00411 }
00412 if( !is_array( $options ) ) {
00413 $options = array( $options );
00414 }
00415 if( is_array( $table ) ) {
00416 if ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
00417 $from = ' FROM ' . $this->tableNamesWithUseIndex( $table, $options['USE INDEX'] );
00418 else
00419 $from = ' FROM ' . implode( ',', array_map( array( &$this, 'tableName' ), $table ) );
00420 } elseif ($table!='') {
00421 if ($table{0}==' ') {
00422 $from = ' FROM ' . $table;
00423 } else {
00424 $from = ' FROM ' . $this->tableName( $table );
00425 }
00426 } else {
00427 $from = '';
00428 }
00429
00430 list( $startOpts, $useIndex, $preLimitTail, $postLimitTail ) = $this->makeSelectOptions( $options );
00431
00432 if( !empty( $conds ) ) {
00433 if ( is_array( $conds ) ) {
00434 $conds = $this->makeList( $conds, LIST_AND );
00435 }
00436 $sql = "SELECT $startOpts $vars $from $useIndex WHERE $conds $preLimitTail";
00437 } else {
00438 $sql = "SELECT $startOpts $vars $from $useIndex $preLimitTail";
00439 }
00440
00441 if (isset($options['LIMIT']))
00442 $sql = $this->limitResult($sql, $options['LIMIT'],
00443 isset($options['OFFSET']) ? $options['OFFSET'] : false);
00444 $sql = "$sql $postLimitTail";
00445
00446 if (isset($options['EXPLAIN'])) {
00447 $sql = 'EXPLAIN ' . $sql;
00448 }
00449 return $this->query( $sql, $fname );
00450 }
00451
00457 function fieldExists( $table, $field, $fname = 'Database::fieldExists' ) {
00458 $table = $this->tableName( $table );
00459 $sql = "SELECT TOP 1 * FROM $table";
00460 $res = $this->query( $sql, 'Database::fieldExists' );
00461
00462 $found = false;
00463 while ( $row = $this->fetchArray( $res ) ) {
00464 if ( isset($row[$field]) ) {
00465 $found = true;
00466 break;
00467 }
00468 }
00469
00470 $this->freeResult( $res );
00471 return $found;
00472 }
00473
00478 function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
00479
00480 throw new DBUnexpectedError( $this, 'Database::indexInfo called which is not supported yet' );
00481 return null;
00482
00483 $table = $this->tableName( $table );
00484 $sql = 'SHOW INDEX FROM '.$table;
00485 $res = $this->query( $sql, $fname );
00486 if ( !$res ) {
00487 return null;
00488 }
00489
00490 $result = array();
00491 while ( $row = $this->fetchObject( $res ) ) {
00492 if ( $row->Key_name == $index ) {
00493 $result[] = $row;
00494 }
00495 }
00496 $this->freeResult($res);
00497
00498 return empty($result) ? false : $result;
00499 }
00500
00504 function tableExists( $table ) {
00505 $table = $this->tableName( $table );
00506 $res = $this->query( "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'" );
00507 $exist = ($res->numRows() > 0);
00508 $this->freeResult($res);
00509 return $exist;
00510 }
00511
00519 function fieldInfo( $table, $field ) {
00520 $table = $this->tableName( $table );
00521 $res = $this->query( "SELECT TOP 1 * FROM $table" );
00522 $n = mssql_num_fields( $res->result );
00523 for( $i = 0; $i < $n; $i++ ) {
00524 $meta = mssql_fetch_field( $res->result, $i );
00525 if( $field == $meta->name ) {
00526 return new MSSQLField($meta);
00527 }
00528 }
00529 return false;
00530 }
00531
00535 function fieldType( $res, $index ) {
00536 if ( $res instanceof ResultWrapper ) {
00537 $res = $res->result;
00538 }
00539 return mssql_field_type( $res, $index );
00540 }
00541
00554 function insert( $table, $a, $fname = 'Database::insert', $options = array() ) {
00555 # No rows to insert, easy just return now
00556 if ( !count( $a ) ) {
00557 return true;
00558 }
00559 $table = $this->tableName( $table );
00560 if ( !is_array( $options ) ) {
00561 $options = array( $options );
00562 }
00563
00564 # todo: need to record primary keys at table create time, and remove NULL assignments to them
00565 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
00566 $multi = true;
00567 $keys = array_keys( $a[0] );
00568 # if (ereg('_id$',$keys[0])) {
00569 foreach ($a as $i) {
00570 if (is_null($i[$keys[0]])) unset($i[$keys[0]]); # remove primary-key column from multiple insert lists if empty value
00571 }
00572 # }
00573 $keys = array_keys( $a[0] );
00574 } else {
00575 $multi = false;
00576 $keys = array_keys( $a );
00577 # if (ereg('_id$',$keys[0]) && empty($a[$keys[0]])) unset($a[$keys[0]]); # remove primary-key column from insert list if empty value
00578 if (is_null($a[$keys[0]])) unset($a[$keys[0]]); # remove primary-key column from insert list if empty value
00579 $keys = array_keys( $a );
00580 }
00581
00582 # handle IGNORE option
00583 # example:
00584 # MySQL: INSERT IGNORE INTO user_groups (ug_user,ug_group) VALUES ('1','sysop')
00585 # MSSQL: IF NOT EXISTS (SELECT * FROM user_groups WHERE ug_user = '1') INSERT INTO user_groups (ug_user,ug_group) VALUES ('1','sysop')
00586 $ignore = in_array('IGNORE',$options);
00587
00588 # remove IGNORE from options list
00589 if ($ignore) {
00590 $oldoptions = $options;
00591 $options = array();
00592 foreach ($oldoptions as $o) if ($o != 'IGNORE') $options[] = $o;
00593 }
00594
00595 $keylist = implode(',', $keys);
00596 $sql = 'INSERT '.implode(' ', $options)." INTO $table (".implode(',', $keys).') VALUES ';
00597 if ($multi) {
00598 if ($ignore) {
00599 # If multiple and ignore, then do each row as a separate conditional insert
00600 foreach ($a as $row) {
00601 $prival = $row[$keys[0]];
00602 $sql = "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = '$prival') $sql";
00603 if (!$this->query("$sql (".$this->makeListWithoutNulls($row).')', $fname)) return false;
00604 }
00605 return true;
00606 } else {
00607 $first = true;
00608 foreach ($a as $row) {
00609 if ($first) $first = false; else $sql .= ',';
00610 $sql .= '('.$this->makeListWithoutNulls($row).')';
00611 }
00612 }
00613 } else {
00614 if ($ignore) {
00615 $prival = $a[$keys[0]];
00616 $sql = "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = '$prival') $sql";
00617 }
00618 $sql .= '('.$this->makeListWithoutNulls($a).')';
00619 }
00620 return (bool)$this->query( $sql, $fname );
00621 }
00622
00629 function makeListWithoutNulls($a, $mode = LIST_COMMA) {
00630 return str_replace("NULL","''",$this->makeList($a,$mode));
00631 }
00632
00645 function update( $table, $values, $conds, $fname = 'Database::update', $options = array() ) {
00646 $table = $this->tableName( $table );
00647 $opts = $this->makeUpdateOptions( $options );
00648 $sql = "UPDATE $opts $table SET " . $this->makeListWithoutNulls( $values, LIST_SET );
00649 if ( $conds != '*' ) {
00650 $sql .= " WHERE " . $this->makeList( $conds, LIST_AND );
00651 }
00652 return $this->query( $sql, $fname );
00653 }
00654
00662 function makeUpdateOptions( $options ) {
00663 if( !is_array( $options ) ) {
00664 $options = array( $options );
00665 }
00666 $opts = array();
00667 if ( in_array( 'LOW_PRIORITY', $options ) )
00668 $opts[] = $this->lowPriorityOption();
00669 if ( in_array( 'IGNORE', $options ) )
00670 $opts[] = 'IGNORE';
00671 return implode(' ', $opts);
00672 }
00673
00677 function selectDB( $db ) {
00678 $this->mDBname = $db;
00679 return mssql_select_db( $db, $this->mConn );
00680 }
00681
00685 function tableName($name) {
00686 return strpos($name, $this->mTablePrefix) === 0 ? $name : "{$this->mTablePrefix}$name";
00687 }
00688
00694 function strencode($s) {
00695 return str_replace("'","''",$s);
00696 }
00697
00711 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
00712 $table = $this->tableName( $table );
00713
00714 # Single row case
00715 if ( !is_array( reset( $rows ) ) ) {
00716 $rows = array( $rows );
00717 }
00718
00719 $sql = "REPLACE INTO $table (" . implode( ',', array_keys( $rows[0] ) ) .') VALUES ';
00720 $first = true;
00721 foreach ( $rows as $row ) {
00722 if ( $first ) {
00723 $first = false;
00724 } else {
00725 $sql .= ',';
00726 }
00727 $sql .= '(' . $this->makeList( $row ) . ')';
00728 }
00729 return $this->query( $sql, $fname );
00730 }
00731
00748 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = 'Database::deleteJoin' ) {
00749 if ( !$conds ) {
00750 throw new DBUnexpectedError( $this, 'Database::deleteJoin() called with empty $conds' );
00751 }
00752
00753 $delTable = $this->tableName( $delTable );
00754 $joinTable = $this->tableName( $joinTable );
00755 $sql = "DELETE $delTable FROM $delTable, $joinTable WHERE $delVar=$joinVar ";
00756 if ( $conds != '*' ) {
00757 $sql .= ' AND ' . $this->makeList( $conds, LIST_AND );
00758 }
00759
00760 return $this->query( $sql, $fname );
00761 }
00762
00766 function textFieldSize( $table, $field ) {
00767 $table = $this->tableName( $table );
00768 $sql = "SELECT TOP 1 * FROM $table;";
00769 $res = $this->query( $sql, 'Database::textFieldSize' );
00770 $row = $this->fetchObject( $res );
00771 $this->freeResult( $res );
00772
00773 $m = array();
00774 if ( preg_match( '/\((.*)\)/', $row->Type, $m ) ) {
00775 $size = $m[1];
00776 } else {
00777 $size = -1;
00778 }
00779 return $size;
00780 }
00781
00785 function lowPriorityOption() {
00786 return 'LOW_PRIORITY';
00787 }
00788
00796 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'Database::insertSelect',
00797 $insertOptions = array(), $selectOptions = array() )
00798 {
00799 $destTable = $this->tableName( $destTable );
00800 if ( is_array( $insertOptions ) ) {
00801 $insertOptions = implode( ' ', $insertOptions );
00802 }
00803 if( !is_array( $selectOptions ) ) {
00804 $selectOptions = array( $selectOptions );
00805 }
00806 list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
00807 if( is_array( $srcTable ) ) {
00808 $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
00809 } else {
00810 $srcTable = $this->tableName( $srcTable );
00811 }
00812 $sql = "INSERT $insertOptions INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
00813 " SELECT $startOpts " . implode( ',', $varMap ) .
00814 " FROM $srcTable $useIndex ";
00815 if ( $conds != '*' ) {
00816 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
00817 }
00818 $sql .= " $tailOpts";
00819 return $this->query( $sql, $fname );
00820 }
00821
00829 function limitResult($sql, $limit, $offset=false) {
00830 if( !is_numeric($limit) ) {
00831 throw new DBUnexpectedError( $this, "Invalid non-numeric limit passed to limitResult()\n" );
00832 }
00833 if ($offset) {
00834 throw new DBUnexpectedError( $this, 'Database::limitResult called with non-zero offset which is not supported yet' );
00835 } else {
00836 $sql = ereg_replace("^SELECT", "SELECT TOP $limit", $sql);
00837 }
00838 return $sql;
00839 }
00840
00845 function wasDeadlock() {
00846 return $this->lastErrno() == 1205;
00847 }
00848
00852 function timestamp( $ts=0 ) {
00853 return wfTimestamp(TS_MW,$ts);
00854 }
00855
00859 function timestampOrNull( $ts = null ) {
00860 if( is_null( $ts ) ) {
00861 return null;
00862 } else {
00863 return $this->timestamp( $ts );
00864 }
00865 }
00866
00870 function getSoftwareLink() {
00871 return "[http://www.microsoft.com/sql/default.mspx Microsoft SQL Server 2005 Home]";
00872 }
00873
00877 function getServerVersion() {
00878 $row = mssql_fetch_row(mssql_query('select @@VERSION'));
00879 return ereg("^(.+[0-9]+\\.[0-9]+\\.[0-9]+) ",$row[0],$m) ? $m[1] : $row[0];
00880 }
00881
00882 function limitResultForUpdate($sql, $num) {
00883 return $sql;
00884 }
00885
00889 public function getLag() {
00890 return 0;
00891 }
00892
00897 public function setup_database() {
00898 global $IP,$wgDBTableOptions;
00899 $wgDBTableOptions = '';
00900 $mysql_tmpl = "$IP/maintenance/tables.sql";
00901 $mysql_iw = "$IP/maintenance/interwiki.sql";
00902 $mssql_tmpl = "$IP/maintenance/mssql/tables.sql";
00903
00904 # Make an MSSQL template file if it doesn't exist (based on the same one MySQL uses to create a new wiki db)
00905 if (!file_exists($mssql_tmpl)) { # todo: make this conditional again
00906 $sql = file_get_contents($mysql_tmpl);
00907 $sql = preg_replace('/^\s*--.*?$/m','',$sql); # strip comments
00908 $sql = preg_replace('/^\s*(UNIQUE )?(INDEX|KEY|FULLTEXT).+?$/m', '', $sql); # These indexes should be created with a CREATE INDEX query
00909 $sql = preg_replace('/(\sKEY) [^\(]+\(/is', '$1 (', $sql); # "KEY foo (foo)" should just be "KEY (foo)"
00910 $sql = preg_replace('/(varchar\([0-9]+\))\s+binary/i', '$1', $sql); # "varchar(n) binary" cannot be followed by "binary"
00911 $sql = preg_replace('/(var)?binary\(([0-9]+)\)/ie', '"varchar(".strlen(pow(2,$2)).")"', $sql); # use varchar(chars) not binary(bits)
00912 $sql = preg_replace('/ (var)?binary/i', ' varchar', $sql); # use varchar not binary
00913 $sql = preg_replace('/(varchar\([0-9]+\)(?! N))/', '$1 NULL', $sql); # MSSQL complains if NULL is put into a varchar
00914 #$sql = preg_replace('/ binary/i',' varchar',$sql); # MSSQL binary's can't be assigned with strings, so use varchar's instead
00915 #$sql = preg_replace('/(binary\([0-9]+\) (NOT NULL )?default) [\'"].*?[\'"]/i','$1 0',$sql); # binary default cannot be string
00916 $sql = preg_replace('/[a-z]*(blob|text)([ ,])/i', 'text$2', $sql); # no BLOB types in MSSQL
00917 $sql = preg_replace('/\).+?;/',');', $sql); # remove all table options
00918 $sql = preg_replace('/ (un)?signed/i', '', $sql);
00919 $sql = preg_replace('/ENUM\(.+?\)/','TEXT',$sql); # Make ENUM's into TEXT's
00920 $sql = str_replace(' bool ', ' bit ', $sql);
00921 $sql = str_replace('auto_increment', 'IDENTITY(1,1)', $sql);
00922 #$sql = preg_replace('/NOT NULL(?! IDENTITY)/', 'NULL', $sql); # Allow NULL's for non IDENTITY columns
00923
00924 # Tidy up and write file
00925 $sql = preg_replace('/,\s*\)/s', "\n)", $sql); # Remove spurious commas left after INDEX removals
00926 $sql = preg_replace('/^\s*^/m', '', $sql); # Remove empty lines
00927 $sql = preg_replace('/;$/m', ";\n", $sql); # Separate each statement with an empty line
00928 file_put_contents($mssql_tmpl, $sql);
00929 }
00930
00931 # Parse the MSSQL template replacing inline variables such as
00932 $err = $this->sourceFile($mssql_tmpl);
00933 if ($err !== true) $this->reportQueryError($err,0,$sql,__FUNCTION__);
00934
00935 # Use DatabasePostgres's code to populate interwiki from MySQL template
00936 $f = fopen($mysql_iw,'r');
00937 if ($f == false) dieout("<li>Could not find the interwiki.sql file");
00938 $sql = "INSERT INTO {$this->mTablePrefix}interwiki(iw_prefix,iw_url,iw_local) VALUES ";
00939 while (!feof($f)) {
00940 $line = fgets($f,1024);
00941 $matches = array();
00942 if (!preg_match('/^\s*(\(.+?),(\d)\)/', $line, $matches)) continue;
00943 $this->query("$sql $matches[1],$matches[2])");
00944 }
00945 }
00946
00947 public function getSearchEngine() {
00948 return "SearchEngineDummy";
00949 }
00950 }
00951
00955 class MSSQLField extends MySQLField {
00956
00957 function __construct() {
00958 }
00959
00960 static function fromText($db, $table, $field) {
00961 $n = new MSSQLField;
00962 $n->name = $field;
00963 $n->tablename = $table;
00964 return $n;
00965 }
00966
00967 }
00968