* - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
* - Author: [http://www.organicdesign.co.nz/nad User:Nad]
*
- * {{php}}{{category:Extensions|DatabaseMssql.php}}
+ * See maintenance/mssql/README for development notes and other specific information
*/
/**
var $mAffectedRows;
var $mLastResult;
+ var $mLastError;
+ var $mLastErrorNo;
var $mDatabaseFile;
/**
}
/**
- * MSSQL doesn't seem to do buffered results
+ * - MSSQL doesn't seem to do buffered results
+ * - the trasnaction syntax is modified here to avoid having to replicate
+ * Database::query which uses BEGIN, COMMIT, ROLLBACK
*/
function doQuery($sql) {
- print "<pre>\n$sql\n</pre>";
+ if ($sql == 'BEGIN' || $sql == 'COMMIT' || $sql == 'ROLLBACK') return true; # $sql .= ' TRANSACTION';
+ $sql = preg_replace('|[^\x07-\x7e]|','?',$sql); # TODO: need to fix unicode - just removing it here while testing
$ret = mssql_query($sql, $this->mConn);
+ if ($ret === false) {
+ $err = mssql_get_last_message();
+ if ($err) $this->mlastError = $err;
+ $row = mssql_fetch_row(mssql_query('select @@ERROR'));
+ if ($row[0]) $this->mlastErrorNo = $row[0];
+ } else $this->mlastErrorNo = false;
return $ret;
}
$res = $res->result;
}
@/**/$row = mssql_fetch_object( $res );
- if( $this->lastErrno() ) {
+ if ( $this->lastErrno() ) {
throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) );
}
return $row;
$res = $res->result;
}
@/**/$n = mssql_num_rows( $res );
- if( $this->lastErrno() ) {
+ if ( $this->lastErrno() ) {
throw new DBUnexpectedError( $this, 'Error in numRows(): ' . htmlspecialchars( $this->lastError() ) );
}
return $n;
* Get the last error number
*/
function lastErrno() {
- $row = mssql_fetch_row(mssql_query('select @@ERROR'));
- return $row[0];
+ return $this->mlastErrorNo;
}
/**
* Get a description of the last error
*/
function lastError() {
- return mssql_get_last_message();
- return $error;
+ return $this->mlastError;
}
/**
# If multiple and ignore, then do each row as a separate conditional insert
foreach ($a as $row) {
$prival = $row[$keys[0]];
- $sql = "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = $prival) $sql";
+ $sql = "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = '$prival') $sql";
if (!$this->query("$sql (".$this->makeListWithoutNulls($row).')', $fname)) return false;
}
return true;
} else {
if ($ignore) {
$prival = $a[$keys[0]];
- $sql = "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = $prival) $sql";
+ $sql = "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = '$prival') $sql";
}
$sql .= '('.$this->makeListWithoutNulls($a).')';
}
}
/**
- * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks
+ * MSSQL has a problem with the backtick quoting, so all this does is ensure the prefix is added exactly once
*/
function tableName($name) {
- return str_replace('`','',parent::tableName($name));
+ return strpos($name, $this->mTablePrefix) === 0 ? $name : "{$this->mTablePrefix}$name";
}
/**
return false;
}
- /**
- * Begin a transaction, committing any previously open transaction
- */
- function begin( $fname = 'Database::begin' ) {
- $this->query( 'BEGIN TRANSACTION', $fname );
- $this->mTrxLevel = 1;
- }
-
- /**
- * End a transaction
- */
- function commit( $fname = 'Database::commit' ) {
- $this->query( 'COMMIT TRANSACTION', $fname );
- $this->mTrxLevel = 0;
- }
-
- /**
- * Rollback a transaction.
- * No-op on non-transactional databases.
- */
- function rollback( $fname = 'Database::rollback' ) {
- $this->query( 'ROLLBACK TRANSACTION', $fname, true );
- $this->mTrxLevel = 0;
- }
-
/**
* Begin a transaction, committing any previously open transaction
* @deprecated use begin()
$mssql_tmpl = "$IP/maintenance/mssql/tables.sql";
# Make an MSSQL template file if it doesn't exist (based on the same one MySQL uses to create a new wiki db)
- if (1 || !file_exists($mssql_tmpl)) { # todo: make this conditional again
+ if (!file_exists($mssql_tmpl)) { # todo: make this conditional again
$sql = file_get_contents($mysql_tmpl);
$sql = preg_replace('/^\s*--.*?$/m','',$sql); # strip comments
$sql = preg_replace('/^\s*(UNIQUE )?(INDEX|KEY|FULLTEXT).+?$/m', '', $sql); # These indexes should be created with a CREATE INDEX query