Source for file mssql.php
Documentation is available at mssql.php
// vim: set et ts=4 sw=4 fdm=marker:
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith, Frank M. Kromann |
// | All rights reserved. |
// +----------------------------------------------------------------------+
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
// | API as well as database abstraction for PHP applications. |
// | This LICENSE is in the BSD license style. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
// | Lukas Smith nor the names of his contributors may be used to endorse |
// | or promote products derived from this software without specific prior|
// | written permission. |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+
// | Author: Frank M. Kromann <frank@kromann.info> |
// +----------------------------------------------------------------------+
// $Id: mssql.php,v 1.12 2004/04/25 10:04:59 lsmith Exp $
* MDB2 MSSQL Server driver
* @author Frank M. Kromann <frank@kromann.info>
$this->phptype = 'mssql';
$this->dbsyntax = 'mssql';
$this->supported['summary_functions'] = true;
$db->options ['database_device'] = false;
$db->options ['database_size'] = false;
* This method is used to collect information about an error
$result = @mssql_query ('select @@ERROR as ErrorCode', $this->connection);
$native_code = @mssql_result ($result);
@mssql_free_result ($result);
$native_msg = @mssql_get_last_message ();
if (isset ($ecode_map[$native_code])) {
$error = $ecode_map[$native_code];
return array ($error, $native_code, $native_msg);
* Quote a string so it can be safely used as a table / column name
* Quoting style depends on which database driver is being used.
* @param string $str identifier name to be quoted
* @return string quoted identifier string
* Define whether database changes done on the database be automatically
* committed. This function may also implicitly start or end a transaction.
* @param boolean $auto_commit flag that indicates whether the database
* changes should be committed right after
* executing every query statement. If this
* argument is 0 a transaction implicitly
* started. Otherwise, if a transaction is
* in progress it is ended by committing any
* database changes that were pending.
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug(($auto_commit ? 'On' : 'Off'), 'autoCommit');
$result = $this->query('COMMIT TRANSACTION');
$result = $this->query('BEGIN TRANSACTION');
$this->in_transaction = !$auto_commit;
* Commit the database changes done during a transaction that is in
* progress. This function may only be called when auto-committing is
* disabled, otherwise it will fail. Therefore, a new transaction is
* implicitly started after committing the pending changes.
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug('commit transaction', 'commit');
'commit: transaction changes are being auto commited');
$result = $this->query('COMMIT TRANSACTION');
return $this->query('BEGIN TRANSACTION');
* Cancel any database changes done during a transaction that is in
* progress. This function may only be called when auto-committing is
* disabled, otherwise it will fail. Therefore, a new transaction is
* implicitly started after canceling the pending changes.
* @return mixed MDB2_OK on success, a MDB2 error on failure
$this->debug('rolling back transaction', 'rollback');
'rollback: transactions can not be rolled back when changes are auto commited');
$result = $this->query('ROLLBACK TRANSACTION');
return $this->query('BEGIN TRANSACTION');
function _doQuery ($query)
$this->current_row = $this->affected_rows = -1;
return @mssql_query ($query, $this->connection);
* Connect to the database
* @return true on success, MDB2 Error Object on failure
if ($this->connection != 0 ) {
&& $this->opened_persistent == $this->options['persistent'])
@mssql_close ($this->connection);
$this->affected_rows = -1;
if (!PEAR ::loadExtension ($this->phptype)) {
'connect: extension '. $this->phptype. ' is not compiled into PHP');
$function = ($this->options['persistent'] ? 'mssql_pconnect' : 'mssql_connect');
$user = $dsninfo['username'];
$pw = $dsninfo['password'];
$dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
$port = $dsninfo['port'] ? ':' . $dsninfo['port'] : '';
if ($dbhost && $user && $pw) {
$connection = @$function($dbhost, $user, $pw);
} elseif ($dbhost && $user) {
$connection = @$function($dbhost, $user);
$connection = @$function($dbhost);
$this->connection = $connection;
$this->connected_dsn = $this->dsn;
$this->connected_database_name = '';
$this->opened_persistent = $this->options['persistent'];
@mssql_close ($this->connection);
$this->affected_rows = -1;
return $this->raiseError('connect: Could not begin the initial transaction');
* all the RDBMS specific things needed close a DB connection
if ($this->connection != 0 ) {
$result = $this->_doQuery ('ROLLBACK TRANSACTION');
@mssql_close ($this->connection);
$this->affected_rows = -1;
unset ($GLOBALS['_MDB2_databases'][$this->db_index]);
* @param string $query the SQL query
* @return mixed MDB2_OK on success, a MDB2 error on failure
if (!PEAR ::loadExtension ($this->phptype)) {
'standaloneQuery: extension '. $this->phptype. ' is not compiled into PHP');
$connection = @mssql_connect ($this->dsn['hostspec'],$this->dsn['username'],$this->dsn['password']);
return $this->raiseError('standaloneQuery: Could not connect to the Microsoft SQL server');
$result = @mssql_query ($query, $connection);
return $this->raiseError('standaloneQuery: Could not query a Microsoft SQL server');
@mssql_close ($connection);
* Send a query to the database and return any results
* @param string $query the SQL query
* @param mixed $types array that contains the types of the columns in
* @param mixed $result_class string which specifies which result class to use
* @param mixed $result_wrap_class string which specifies which class to wrap results in
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
function &query($query, $types = null , $result_class = false , $result_wrap_class = false )
$offset = $this->row_offset;
$limit = $this->row_limit;
$this->row_offset = $this->row_limit = 0;
$fetch = $offset + $limit;
$query = str_replace('SELECT', " SELECT TOP $fetch" , $query);
$this->debug($query, 'query');
if ($this->database_name) {
if (!@mssql_select_db ($this->database_name, $this->connection)) {
$this->connected_database_name = $this->database_name;
if ($result = $this->_doQuery ($query)) {
$result_class = $this->options['result_buffering']
? $this->options['buffered_result_class'] : $this->options['result_class'];
$class_name = sprintf($result_class, $this->phptype);
$result = & new $class_name($this, $result, $offset, $limit);
$err = $result->setResultTypes ($types);
if (!$result_wrap_class) {
$result_wrap_class = $this->options['result_wrap_class'];
if ($result_wrap_class) {
$result = & new $result_wrap_class($result);
* returns the affected rows of a query
* @return mixed MDB2 Error Object or number of rows
$affected_rows = @mssql_affected_rows ($this->connection);
if ($affected_rows === false ) {
* returns the next free id of a sequence
* @param string $seq_name name of the sequence
* @param boolean $ondemand when true the seqence is
* automatic created, if it
* @return mixed MDB2 Error Object or id
function nextID($seq_name, $ondemand = true )
$result = $this->query(" INSERT INTO $sequence_name DEFAULT VALUES" );
// Since we are creating the sequence on demand
// we know the first id = 1 so initialize the
$result = $this->manager->createSequence ($seq_name, 2 );
'nextID: on demand sequence could not be created');
// First ID of a newly created sequence is 1
$value = $this->queryOne(" SELECT @@IDENTITY FROM $sequence_name" , 'integer');
$this->warnings[] = 'nextID: could not delete previous sequence table values';
class MDB2_Result_mssql extends MDB2_Result_Common
function MDB2_Result_mssql (&$mdb, &$result, $offset, $limit)
parent ::MDB2_Result_Common ($mdb, $result);
// {{{ _skipLimitOffset()
* Skip the first row of a result set.
* @param resource $result
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
function _skipLimitOffset ()
if (isset ($this->limits) && is_array($this->limits)) {
if ($this->rownum >= $this->limits['limit']) {
while ($this->limits['count'] < $this->limits['offset']) {
$this->limits['count']++;
$this->limits['count'] = $this->limits['offset'];
* fetch value from a result set
* @param int $rownum number of the row where the data can be found
* @param int $colnum field number where the data can be found
* @return mixed string on success, a MDB2 error on failure
function fetch ($rownum = 0 , $colnum = 0 )
$value = @mssql_result ($this->result, $rownum, $colnum);
'fetch: resultset has already been freed');
if (isset ($this->types[$colnum])) {
$value = $this->mdb->datatype ->convertResult ($value, $this->types[$colnum]);
* Fetch a row and insert the data into an existing array.
* @param int $fetchmode how the array data should be indexed
* @return int data array on success, a MDB2 error on failure
function fetchRow ($fetchmode = MDB2_FETCHMODE_DEFAULT )
$fetchmode = $this->mdb->fetchmode;
if (!$this->_skipLimitOffset ()) {
$row = @mssql_fetch_assoc ($this->result);
$row = @mssql_fetch_row ($this->result);
'fetchRow: resultset has already been freed');
if (isset ($this->types)) {
$row = $this->mdb->datatype ->convertResultRow ($this->types, $row);
$this->mdb->_convertEmptyArrayValuesToNull ($row);
* Retrieve the names of columns returned by the DBMS in a query result.
* @param resource $result result identifier
* @return mixed associative array variable
* that holds the names of columns. The indexes of the array are
* the column names mapped to lower case and the values are the
* respective numbers of the columns starting from 0. Some DBMS may
* not return any columns when the result set does not contain any
function getColumnNames ()
$numcols = $this->numCols ();
for ($column = 0; $column < $numcols; $column++ ) {
$column_name = @mssql_field_name ($this->result, $column);
$columns[$column_name] = $column;
* Count the number of columns returned by the DBMS in a query result.
* @return mixed integer value with the number of columns, a MDB2 error
$cols = @mssql_num_fields ($this->result);
'numCols: resultset has already been freed');
return $this->mdb->raiseError ();
* Move the internal result pointer to the next available result
* Currently not supported
* @return true if a result is available otherwise return false
'nextResult: resultset has already been freed');
return @mssql_next_result ($this->result);
* Free the internal resources associated with $result.
* @return boolean true on success, false if $result is invalid
$free = @mssql_free_result ($this->result);
return $this->mdb->raiseError ();
class MDB2_BufferedResult_mssql extends MDB2_Result_mssql
function MDB2_BufferedResult_mssql (&$mdb, &$result, $offset, $limit)
parent ::MDB2_Result_mssql ($mdb, $result, $offset, $limit);
* check if the end of the result set has been reached
* @return mixed true or false on sucess, a MDB2 error on failure
$numrows = $this->numRows ();
return $this->rownum < ($numrows - 1 );
* returns the number of rows in a result object
* @return mixed MDB2 Error Object or the number of rows
$rows = @mssql_num_rows ($this->result);
'numRows: resultset has already been freed');
return $this->raiseError ();
if (isset ($this->limits)) {
$rows -= $this->limits[0 ];
Documentation generated on Mon, 11 Mar 2019 10:15:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|