MSSQLConnection.php

Go to the documentation of this file.
00001 <?php 00002 00003 /* 00004 * $Id: MSSQLConnection.php,v 1.22 2004/05/02 21:52:37 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 00049 class MSSQLConnection extends ConnectionCommon implements Connection { 00050 00052 private $database; 00053 00057 function connect($dsninfo, $flags = 0) 00058 { 00059 if (!extension_loaded('mssql') && !extension_loaded('sybase') && !extension_loaded('sybase_ct')) { 00060 throw new SQLException('mssql extension not loaded'); 00061 } 00062 00063 $this->dsn = $dsninfo; 00064 $this->flags = $flags; 00065 00066 $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT); 00067 00068 $user = $dsninfo['username']; 00069 $pw = $dsninfo['password']; 00070 $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; 00071 00072 $connect_function = $persistent ? 'mssql_pconnect' : 'mssql_connect'; 00073 00074 if ($dbhost && $user && $pw) { 00075 $conn = @$connect_function($dbhost, $user, $pw); 00076 } elseif ($dbhost && $user) { 00077 $conn = @$connect_function($dbhost, $user); 00078 } else { 00079 $conn = @$connect_function($dbhost); 00080 } 00081 if (!$conn) { 00082 throw new SQLException('connect failed', mssql_get_last_message()); 00083 } 00084 00085 if ($dsninfo['database']) { 00086 if (!@mssql_select_db($dsninfo['database'], $conn)) { 00087 throw new SQLException('No database selected'); 00088 } 00089 00090 $this->database = $dsninfo['database']; 00091 } 00092 00093 $this->dblink = $conn; 00094 } 00095 00099 public function getDatabaseInfo() 00100 { 00101 require_once 'creole/drivers/mssql/metadata/MSSQLDatabaseInfo.php'; 00102 return new MSSQLDatabaseInfo($this); 00103 } 00104 00108 public function getIdGenerator() 00109 { 00110 require_once 'creole/drivers/mssql/MSSQLIdGenerator.php'; 00111 return new MSSQLIdGenerator($this); 00112 } 00113 00117 public function prepareStatement($sql) 00118 { 00119 require_once 'creole/drivers/mssql/MSSQLPreparedStatement.php'; 00120 return new MSSQLPreparedStatement($this, $sql); 00121 } 00122 00126 public function createStatement() 00127 { 00128 require_once 'creole/drivers/mssql/MSSQLStatement.php'; 00129 return new MSSQLStatement($this); 00130 } 00131 00135 public function applyLimit(&$sql, $offset, $limit) 00136 { 00137 return false; 00138 } 00139 00143 function close() 00144 { 00145 @mssql_close($this->dblink); 00146 } 00147 00151 function executeQuery($sql, $fetchmode = null) 00152 { 00153 $this->lastQuery = $sql; 00154 if (!@mssql_select_db($this->database, $this->dblink)) { 00155 throw new SQLException('No database selected'); 00156 } 00157 $result = @mssql_query($sql, $this->dblink); 00158 if (!$result) { 00159 throw new SQLException('Could not execute query', mssql_get_last_message()); 00160 } 00161 return new MSSQLResultSet($this, $result, $fetchmode); 00162 } 00163 00167 function executeUpdate($sql) 00168 { 00169 00170 $this->lastQuery = $sql; 00171 if (!mssql_select_db($this->database, $this->dblink)) { 00172 throw new SQLException('No database selected'); 00173 } 00174 00175 if (!$this->autocommit) { 00176 if ($this->transactionOpcount === 0) { 00177 $result = @mssql_query('BEGIN TRAN', $this->dblink); 00178 if (!$result) { 00179 throw new SQLException('Could not begin transaction', mssql_get_last_message()); 00180 } 00181 } 00182 $this->transactionOpcount++; 00183 } 00184 00185 $result = @mssql_query($sql, $this->dblink); 00186 if (!$result) { 00187 throw new SQLException('Could not execute update', mssql_get_last_message(), $sql); 00188 } 00189 00190 return $this->getUpdateCount(); 00191 } 00192 00196 function commit() 00197 { 00198 if ($this->transactionOpcount > 0) { 00199 if (!@mssql_select_db($this->database, $this->dblink)) { 00200 throw new SQLException('No database selected'); 00201 } 00202 $result = @mssql_query('COMMIT TRAN', $this->dblink); 00203 $this->transactionOpcount = 0; 00204 if (!$result) { 00205 throw new SQLException('Could not commit transaction', mssql_get_last_message()); 00206 } 00207 } 00208 } 00209 00215 function rollback() 00216 { 00217 if ($this->transactionOpcount > 0) { 00218 if (!@mssql_select_db($this->database, $this->dblink)) { 00219 throw new SQLException('no database selected'); 00220 } 00221 $result = @mssql_query('ROLLBACK TRAN', $this->dblink); 00222 $this->transactionOpcount = 0; 00223 if (!$result) { 00224 throw new SQLException('Could not rollback transaction', mssql_get_last_message()); 00225 } 00226 } 00227 } 00228 00236 function getUpdateCount() 00237 { 00238 $res = @mssql_query('select @@rowcount', $this->dblink); 00239 if (!$res) { 00240 throw new SQLException('Unable to get affected row count', mssql_get_last_message()); 00241 } 00242 $ar = @mssql_fetch_row($res); 00243 if (!$ar) { 00244 $result = 0; 00245 } else { 00246 @mssql_free_result($res); 00247 $result = $ar[0]; 00248 } 00249 00250 return $result; 00251 } 00252 00253 00261 function prepareCall($sql) 00262 { 00263 require_once 'creole/drivers/mssql/MSSQLCallableStatement.php'; 00264 $stmt = mssql_init($sql); 00265 if (!$stmt) { 00266 throw new SQLException('Unable to prepare statement', mssql_get_last_message(), $sql); 00267 } 00268 return new MSSQLCallableStatement($this, $stmt); 00269 } 00270 }

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


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS