var $mLastResult = NULL;
var $mAffectedRows = NULL;
+ var $mPort;
+
function cascadingDeletes() {
return true;
}
$ntAuthPassTest = strtolower( $password );
// Decide which auth scenerio to use
- if( ( $ntAuthPassTest == 'ntauth' && $ntAuthUserTest == 'ntauth' ) ){
+ if( $ntAuthPassTest == 'ntauth' && $ntAuthUserTest == 'ntauth' ){
// Don't add credentials to $connectionInfo
} else {
$connectionInfo['UID'] = $user;
}
// End NT Auth Hack
- $this->mConn = @sqlsrv_connect( $server, $connectionInfo );
+ wfSuppressWarnings();
+ $this->mConn = sqlsrv_connect( $server, $connectionInfo );
+ wfRestoreWarnings();
if ( $this->mConn === false ) {
wfDebug( "DB connection error\n" );
function lastError() {
if ( $this->mConn ) {
return $this->getErrors();
- }
- else {
+ } else {
return "No database connection";
}
}
function lastErrno() {
$err = sqlsrv_errors( SQLSRV_ERR_ALL );
- if ( $err[0] ) return $err[0]['code'];
- else return 0;
+ if ( $err[0] ) {
+ return $err[0]['code'];
+ } else {
+ return 0;
+ }
}
function affectedRows() {
return $rows;
}
-
/**
* Returns information about an index
* If errors are explicitly ignored, returns NULL on failure
$allOk = true;
-
// We know the table we're inserting into, get its identity column
$identity = null;
$tableRaw = preg_replace( '#\[([^\]]*)\]#', '$1', $table ); // strip matching square brackets from table name
$keys = array_keys( $a );
-
// INSERT IGNORE is not supported by SQL Server
// remove IGNORE from options list and set ignore flag to true
$ignoreClause = false;
// example:
// MySQL: INSERT IGNORE INTO user_groups (ug_user,ug_group) VALUES ('1','sysop')
// MSSQL: IF NOT EXISTS (SELECT * FROM user_groups WHERE ug_user = '1') INSERT INTO user_groups (ug_user,ug_group) VALUES ('1','sysop')
- if ( $ignoreClause == true ) {
+ if ( $ignoreClause ) {
$prival = $a[$keys[0]];
$sqlPre .= "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = '$prival')";
}
* srcTable may be an array of tables.
*/
function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'DatabaseMssql::insertSelect',
- $insertOptions = array(), $selectOptions = array() )
- {
+ $insertOptions = array(), $selectOptions = array() ) {
$ret = parent::insertSelect( $destTable, $srcTable, $varMap, $conds, $fname, $insertOptions, $selectOptions );
if ( $ret === false ) {
$res = $this->query( $sql );
$row = $this->fetchRow( $res );
$size = -1;
- if ( strtolower( $row['DATA_TYPE'] ) != 'text' ) $size = $row['CHARACTER_MAXIMUM_LENGTH'];
+ if ( strtolower( $row['DATA_TYPE'] ) != 'text' ) {
+ $size = $row['CHARACTER_MAXIMUM_LENGTH'];
+ }
return $size;
}
return $sql;
}
-
function timestamp( $ts = 0 ) {
return wfTimestamp( TS_ISO_8601, $ts );
}
function getServerVersion() {
$server_info = sqlsrv_server_info( $this->mConn );
$version = 'Error';
- if ( isset( $server_info['SQLServerVersion'] ) ) $version = $server_info['SQLServerVersion'];
+ if ( isset( $server_info['SQLServerVersion'] ) ) {
+ $version = $server_info['SQLServerVersion'];
+ }
return $version;
}
print( "Error in tableExists query: " . $this->getErrors() );
return false;
}
- if ( sqlsrv_fetch( $res ) )
+ if ( sqlsrv_fetch( $res ) ) {
return true;
- else
+ } else {
return false;
+ }
}
/**
print( "Error in fieldExists query: " . $this->getErrors() );
return false;
}
- if ( sqlsrv_fetch( $res ) )
+ if ( sqlsrv_fetch( $res ) ) {
return true;
- else
+ } else {
return false;
+ }
}
function fieldInfo( $table, $field ) {
}
}
- if ( isset( $options['GROUP BY'] ) ) $tailOpts .= " GROUP BY {$options['GROUP BY']}";
- if ( isset( $options['HAVING'] ) ) $tailOpts .= " HAVING {$options['GROUP BY']}";
- if ( isset( $options['ORDER BY'] ) ) $tailOpts .= " ORDER BY {$options['ORDER BY']}";
+ if ( isset( $options['GROUP BY'] ) ) {
+ $tailOpts .= " GROUP BY {$options['GROUP BY']}";
+ }
+ if ( isset( $options['HAVING'] ) ) {
+ $tailOpts .= " HAVING {$options['GROUP BY']}";
+ }
+ if ( isset( $options['ORDER BY'] ) ) {
+ $tailOpts .= " ORDER BY {$options['ORDER BY']}";
+ }
- if ( isset( $noKeyOptions['DISTINCT'] ) && isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT';
+ if ( isset( $noKeyOptions['DISTINCT'] ) && isset( $noKeyOptions['DISTINCTROW'] ) ) {
+ $startOpts .= 'DISTINCT';
+ }
// we want this to be compatible with the output of parent::makeSelectOptions()
return array( $startOpts, '' , $tailOpts, '' );
$this->nullable = ( strtolower( $info['IS_NULLABLE'] ) == 'no' ) ? false:true;
$this->type = $info['DATA_TYPE'];
}
+
function name() {
return $this->name;
}
*/
class MssqlResult {
- public function __construct( $queryresult = false ) {
- $this->mCursor = 0;
- $this->mRows = array();
- $this->mNumFields = sqlsrv_num_fields( $queryresult );
- $this->mFieldMeta = sqlsrv_field_metadata( $queryresult );
- while ( $row = sqlsrv_fetch_array( $queryresult, SQLSRV_FETCH_ASSOC ) ) {
- if ( $row !== null ) {
- foreach ( $row as $k => $v ) {
- if ( is_object( $v ) && method_exists( $v, 'format' ) ) {// DateTime Object
- $row[$k] = $v->format( "Y-m-d\TH:i:s\Z" );
+ public function __construct( $queryresult = false ) {
+ $this->mCursor = 0;
+ $this->mRows = array();
+ $this->mNumFields = sqlsrv_num_fields( $queryresult );
+ $this->mFieldMeta = sqlsrv_field_metadata( $queryresult );
+ while ( $row = sqlsrv_fetch_array( $queryresult, SQLSRV_FETCH_ASSOC ) ) {
+ if ( $row !== null ) {
+ foreach ( $row as $k => $v ) {
+ if ( is_object( $v ) && method_exists( $v, 'format' ) ) {// DateTime Object
+ $row[$k] = $v->format( "Y-m-d\TH:i:s\Z" );
+ }
}
+ $this->mRows[] = $row;// read results into memory, cursors are not supported
}
- $this->mRows[] = $row;// read results into memory, cursors are not supported
}
+ $this->mRowCount = count( $this->mRows );
+ sqlsrv_free_stmt( $queryresult );
}
- $this->mRowCount = count( $this->mRows );
- sqlsrv_free_stmt( $queryresult );
- }
- private function array_to_obj( $array, &$obj ) {
+ private function array_to_obj( $array, &$obj ) {
foreach ( $array as $key => $value ) {
if ( is_array( $value ) ) {
$obj->$key = new stdClass();
}
}
return $obj;
- }
+ }
- public function fetch( $mode = SQLSRV_FETCH_BOTH, $object_class = 'stdClass' ) {
- if ( $this->mCursor >= $this->mRowCount || $this->mRowCount == 0 ) {
- return false;
+ public function fetch( $mode = SQLSRV_FETCH_BOTH, $object_class = 'stdClass' ) {
+ if ( $this->mCursor >= $this->mRowCount || $this->mRowCount == 0 ) {
+ return false;
+ }
+ $arrNum = array();
+ if ( $mode == SQLSRV_FETCH_NUMERIC || $mode == SQLSRV_FETCH_BOTH ) {
+ foreach ( $this->mRows[$this->mCursor] as $value ) {
+ $arrNum[] = $value;
+ }
+ }
+ switch( $mode ) {
+ case SQLSRV_FETCH_ASSOC:
+ $ret = $this->mRows[$this->mCursor];
+ break;
+ case SQLSRV_FETCH_NUMERIC:
+ $ret = $arrNum;
+ break;
+ case 'OBJECT':
+ $o = new $object_class;
+ $ret = $this->array_to_obj( $this->mRows[$this->mCursor], $o );
+ break;
+ case SQLSRV_FETCH_BOTH:
+ default:
+ $ret = $this->mRows[$this->mCursor] + $arrNum;
+ break;
+ }
+
+ $this->mCursor++;
+ return $ret;
+ }
+
+ public function get( $pos, $fld ) {
+ return $this->mRows[$pos][$fld];
}
- $arrNum = array();
- if ( $mode == SQLSRV_FETCH_NUMERIC || $mode == SQLSRV_FETCH_BOTH ) {
- foreach ( $this->mRows[$this->mCursor] as $value ) {
- $arrNum[] = $value;
- }
- }
- switch( $mode ) {
- case SQLSRV_FETCH_ASSOC:
- $ret = $this->mRows[$this->mCursor];
- break;
- case SQLSRV_FETCH_NUMERIC:
- $ret = $arrNum;
- break;
- case 'OBJECT':
- $o = new $object_class;
- $ret = $this->array_to_obj( $this->mRows[$this->mCursor], $o );
- break;
- case SQLSRV_FETCH_BOTH:
- default:
- $ret = $this->mRows[$this->mCursor] + $arrNum;
- break;
- }
-
- $this->mCursor++;
- return $ret;
- }
-
- public function get( $pos, $fld ) {
- return $this->mRows[$pos][$fld];
- }
-
- public function numrows() {
- return $this->mRowCount;
- }
-
- public function seek( $iRow ) {
- $this->mCursor = min( $iRow, $this->mRowCount );
- }
-
- public function numfields() {
- return $this->mNumFields;
- }
-
- public function fieldname( $nr ) {
- $arrKeys = array_keys( $this->mRows[0] );
- return $arrKeys[$nr];
- }
-
- public function fieldtype( $nr ) {
- $i = 0;
- $intType = -1;
- foreach ( $this->mFieldMeta as $meta ) {
- if ( $nr == $i ) {
- $intType = $meta['Type'];
- break;
- }
- $i++;
- }
- // http://msdn.microsoft.com/en-us/library/cc296183.aspx contains type table
- switch( $intType ) {
- case SQLSRV_SQLTYPE_BIGINT: $strType = 'bigint'; break;
- case SQLSRV_SQLTYPE_BINARY: $strType = 'binary'; break;
- case SQLSRV_SQLTYPE_BIT: $strType = 'bit'; break;
- case SQLSRV_SQLTYPE_CHAR: $strType = 'char'; break;
- case SQLSRV_SQLTYPE_DATETIME: $strType = 'datetime'; break;
- case SQLSRV_SQLTYPE_DECIMAL/*($precision, $scale)*/: $strType = 'decimal'; break;
- case SQLSRV_SQLTYPE_FLOAT: $strType = 'float'; break;
- case SQLSRV_SQLTYPE_IMAGE: $strType = 'image'; break;
- case SQLSRV_SQLTYPE_INT: $strType = 'int'; break;
- case SQLSRV_SQLTYPE_MONEY: $strType = 'money'; break;
- case SQLSRV_SQLTYPE_NCHAR/*($charCount)*/: $strType = 'nchar'; break;
- case SQLSRV_SQLTYPE_NUMERIC/*($precision, $scale)*/: $strType = 'numeric'; break;
- case SQLSRV_SQLTYPE_NVARCHAR/*($charCount)*/: $strType = 'nvarchar'; break;
- // case SQLSRV_SQLTYPE_NVARCHAR('max'): $strType = 'nvarchar(MAX)'; break;
- case SQLSRV_SQLTYPE_NTEXT: $strType = 'ntext'; break;
- case SQLSRV_SQLTYPE_REAL: $strType = 'real'; break;
- case SQLSRV_SQLTYPE_SMALLDATETIME: $strType = 'smalldatetime'; break;
- case SQLSRV_SQLTYPE_SMALLINT: $strType = 'smallint'; break;
- case SQLSRV_SQLTYPE_SMALLMONEY: $strType = 'smallmoney'; break;
- case SQLSRV_SQLTYPE_TEXT: $strType = 'text'; break;
- case SQLSRV_SQLTYPE_TIMESTAMP: $strType = 'timestamp'; break;
- case SQLSRV_SQLTYPE_TINYINT: $strType = 'tinyint'; break;
- case SQLSRV_SQLTYPE_UNIQUEIDENTIFIER: $strType = 'uniqueidentifier'; break;
- case SQLSRV_SQLTYPE_UDT: $strType = 'UDT'; break;
- case SQLSRV_SQLTYPE_VARBINARY/*($byteCount)*/: $strType = 'varbinary'; break;
- // case SQLSRV_SQLTYPE_VARBINARY('max'): $strType = 'varbinary(MAX)'; break;
- case SQLSRV_SQLTYPE_VARCHAR/*($charCount)*/: $strType = 'varchar'; break;
- // case SQLSRV_SQLTYPE_VARCHAR('max'): $strType = 'varchar(MAX)'; break;
- case SQLSRV_SQLTYPE_XML: $strType = 'xml'; break;
- default: $strType = $intType;
- }
- return $strType;
- }
-
- public function free() {
- unset( $this->mRows );
- return;
- }
+ public function numrows() {
+ return $this->mRowCount;
+ }
+
+ public function seek( $iRow ) {
+ $this->mCursor = min( $iRow, $this->mRowCount );
+ }
+
+ public function numfields() {
+ return $this->mNumFields;
+ }
+
+ public function fieldname( $nr ) {
+ $arrKeys = array_keys( $this->mRows[0] );
+ return $arrKeys[$nr];
+ }
+
+ public function fieldtype( $nr ) {
+ $i = 0;
+ $intType = -1;
+ foreach ( $this->mFieldMeta as $meta ) {
+ if ( $nr == $i ) {
+ $intType = $meta['Type'];
+ break;
+ }
+ $i++;
+ }
+ // http://msdn.microsoft.com/en-us/library/cc296183.aspx contains type table
+ switch( $intType ) {
+ case SQLSRV_SQLTYPE_BIGINT: $strType = 'bigint'; break;
+ case SQLSRV_SQLTYPE_BINARY: $strType = 'binary'; break;
+ case SQLSRV_SQLTYPE_BIT: $strType = 'bit'; break;
+ case SQLSRV_SQLTYPE_CHAR: $strType = 'char'; break;
+ case SQLSRV_SQLTYPE_DATETIME: $strType = 'datetime'; break;
+ case SQLSRV_SQLTYPE_DECIMAL/*($precision, $scale)*/: $strType = 'decimal'; break;
+ case SQLSRV_SQLTYPE_FLOAT: $strType = 'float'; break;
+ case SQLSRV_SQLTYPE_IMAGE: $strType = 'image'; break;
+ case SQLSRV_SQLTYPE_INT: $strType = 'int'; break;
+ case SQLSRV_SQLTYPE_MONEY: $strType = 'money'; break;
+ case SQLSRV_SQLTYPE_NCHAR/*($charCount)*/: $strType = 'nchar'; break;
+ case SQLSRV_SQLTYPE_NUMERIC/*($precision, $scale)*/: $strType = 'numeric'; break;
+ case SQLSRV_SQLTYPE_NVARCHAR/*($charCount)*/: $strType = 'nvarchar'; break;
+ // case SQLSRV_SQLTYPE_NVARCHAR('max'): $strType = 'nvarchar(MAX)'; break;
+ case SQLSRV_SQLTYPE_NTEXT: $strType = 'ntext'; break;
+ case SQLSRV_SQLTYPE_REAL: $strType = 'real'; break;
+ case SQLSRV_SQLTYPE_SMALLDATETIME: $strType = 'smalldatetime'; break;
+ case SQLSRV_SQLTYPE_SMALLINT: $strType = 'smallint'; break;
+ case SQLSRV_SQLTYPE_SMALLMONEY: $strType = 'smallmoney'; break;
+ case SQLSRV_SQLTYPE_TEXT: $strType = 'text'; break;
+ case SQLSRV_SQLTYPE_TIMESTAMP: $strType = 'timestamp'; break;
+ case SQLSRV_SQLTYPE_TINYINT: $strType = 'tinyint'; break;
+ case SQLSRV_SQLTYPE_UNIQUEIDENTIFIER: $strType = 'uniqueidentifier'; break;
+ case SQLSRV_SQLTYPE_UDT: $strType = 'UDT'; break;
+ case SQLSRV_SQLTYPE_VARBINARY/*($byteCount)*/: $strType = 'varbinary'; break;
+ // case SQLSRV_SQLTYPE_VARBINARY('max'): $strType = 'varbinary(MAX)'; break;
+ case SQLSRV_SQLTYPE_VARCHAR/*($charCount)*/: $strType = 'varchar'; break;
+ // case SQLSRV_SQLTYPE_VARCHAR('max'): $strType = 'varchar(MAX)'; break;
+ case SQLSRV_SQLTYPE_XML: $strType = 'xml'; break;
+ default: $strType = $intType;
+ }
+ return $strType;
+ }
+
+ public function free() {
+ unset( $this->mRows );
+ return;
+ }
}