MSSQLConnection.php

Go to the documentation of this file.
00001 <?php 00002 00003 /* 00004 * $Id: MSSQLConnection.php,v 1.1 2004/05/02 21:59:24 micha Exp $ 00005 * 00006 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00007 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00008 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00009 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00010 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00011 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00012 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00013 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00014 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00015 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00016 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * This software consists of voluntary contributions made by many individuals 00019 * and is licensed under the LGPL. For more information please see 00020 * <http://creole.phpdb.org>. 00021 */ 00022 00023 00024 require_once 'creole/Connection.php'; 00025 require_once 'creole/common/ConnectionCommon.php'; 00026 include_once 'creole/drivers/mssql/MSSQLResultSet.php'; 00027 00051 class MSSQLConnection extends ConnectionCommon 00052 { 00054 var $database; 00055 00059 function connect($dsninfo, $flags = 0) 00060 { 00061 if (!extension_loaded('mssql') && !extension_loaded('sybase') && !extension_loaded('sybase_ct')) { 00062 return new SQLException(CREOLE_ERROR_EXTENSION_NOT_FOUND, 'mssql extension not loaded'); 00063 } 00064 00065 $this->dsn = $dsninfo; 00066 $this->flags = $flags; 00067 00068 $persistent = ($flags & Creole::PERSISTENT() === Creole::PERSISTENT()); 00069 00070 $user = $dsninfo['username']; 00071 $pw = $dsninfo['password']; 00072 $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; 00073 00074 $connect_function = $persistent ? 'mssql_pconnect' : 'mssql_connect'; 00075 00076 if ($dbhost && $user && $pw) { 00077 $conn = @$connect_function($dbhost, $user, $pw); 00078 } elseif ($dbhost && $user) { 00079 $conn = @$connect_function($dbhost, $user); 00080 } else { 00081 $conn = @$connect_function($dbhost); 00082 } 00083 if (!$conn) { 00084 return new SQLException(CREOLE_ERROR_CONNECT_FAILED, 'connect failed', mssql_get_last_message()); 00085 } 00086 00087 if ($dsninfo['database']) { 00088 if (!@mssql_select_db($dsninfo['database'], $conn)) { 00089 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00090 } 00091 00092 $this->database = $dsninfo['database']; 00093 } 00094 00095 $this->dblink = $conn; 00096 } 00097 00101 function & getDatabaseInfo() 00102 { 00103 require_once 'creole/drivers/mssql/metadata/MSSQLDatabaseInfo.php'; 00104 return new MSSQLDatabaseInfo($this); 00105 } 00106 00110 function & getIdGenerator() 00111 { 00112 require_once 'creole/drivers/mssql/MSSQLIdGenerator.php'; 00113 return new MSSQLIdGenerator($this); 00114 } 00115 00119 function & prepareStatement($sql) 00120 { 00121 require_once 'creole/drivers/mssql/MSSQLPreparedStatement.php'; 00122 return new MSSQLPreparedStatement($this, $sql); 00123 } 00124 00128 function & createStatement() 00129 { 00130 require_once 'creole/drivers/mssql/MSSQLStatement.php'; 00131 return new MSSQLStatement($this); 00132 } 00133 00137 function applyLimit(&$sql, $offset, $limit) 00138 { 00139 return false; 00140 } 00141 00145 function close() 00146 { 00147 @mssql_close($this->dblink); 00148 } 00149 00153 function & executeQuery($sql, $fetchmode = null) 00154 { 00155 $this->lastQuery = $sql; 00156 if (!@mssql_select_db($this->database, $this->dblink)) { 00157 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00158 } 00159 $result = @mssql_query($sql, $this->dblink); 00160 if (!$result) { 00161 return new SQLException(CREOLE_ERROR, 'Could not execute query', mssql_get_last_message()); 00162 } 00163 return new MSSQLResultSet($this, $result, $fetchmode); 00164 } 00165 00169 function executeUpdate($sql) 00170 { 00171 $this->lastQuery = $sql; 00172 if (!mssql_select_db($this->database, $this->dblink)) { 00173 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00174 } 00175 00176 if (!$this->autocommit) { 00177 if ($this->transactionOpcount === 0) { 00178 $result = @mssql_query('BEGIN TRAN', $this->dblink); 00179 if (!$result) { 00180 return new SQLException(CREOLE_ERROR, 'Could not begin transaction', mssql_get_last_message()); 00181 } 00182 } 00183 $this->transactionOpcount++; 00184 } 00185 00186 $result = @mssql_query($sql, $this->dblink); 00187 if (!$result) { 00188 return new SQLException(CREOLE_ERROR, 'Could not execute update', mssql_get_last_message(), $sql); 00189 } 00190 00191 return $this->getUpdateCount(); 00192 } 00193 00197 function commit() 00198 { 00199 if ($this->transactionOpcount > 0) { 00200 if (!@mssql_select_db($this->database, $this->dblink)) { 00201 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00202 } 00203 $result = @mssql_query('COMMIT TRAN', $this->dblink); 00204 $this->transactionOpcount = 0; 00205 if (!$result) { 00206 return new SQLException(CREOLE_ERROR, 'Could not commit transaction', mssql_get_last_message()); 00207 } 00208 } 00209 } 00210 00216 function rollback() 00217 { 00218 if ($this->transactionOpcount > 0) { 00219 if (!@mssql_select_db($this->database, $this->dblink)) { 00220 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'no database selected'); 00221 } 00222 $result = @mssql_query('ROLLBACK TRAN', $this->dblink); 00223 $this->transactionOpcount = 0; 00224 if (!$result) { 00225 return new SQLException(CREOLE_ERROR, 'Could not rollback transaction', mssql_get_last_message()); 00226 } 00227 } 00228 } 00229 00237 function getUpdateCount() 00238 { 00239 $res = @mssql_query('select @@rowcount', $this->dblink); 00240 if (!$res) { 00241 return new SQLException(CREOLE_ERROR, 'Unable to get affected row count', mssql_get_last_message()); 00242 } 00243 $ar = @mssql_fetch_row($res); 00244 if (!$ar) { 00245 $result = 0; 00246 } else { 00247 @mssql_free_result($res); 00248 $result = $ar[0]; 00249 } 00250 00251 return $result; 00252 } 00253 00254 00262 function & prepareCall($sql) 00263 { 00264 require_once 'creole/drivers/mssql/MSSQLCallableStatement.php'; 00265 $stmt = mssql_init($sql); 00266 if (!$stmt) { 00267 return new SQLException(CREOLE_ERROR, 'Unable to prepare statement', mssql_get_last_message(), $sql); 00268 } 00269 return new MSSQLCallableStatement($this, $stmt); 00270 } 00271 00272 }

This file is part of the Creole[php4] library.


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS