00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 if (PHP_OS == "WINNT" || PHP_OS == "WIN32") {
00073 $portDelimiter = ",";
00074 } else {
00075 $portDelimiter = ":";
00076 }
00077
00078 if(!empty($dsninfo['port'])) {
00079 $dbhost .= $portDelimiter.$dsninfo['port'];
00080 } else {
00081 $dbhost .= $portDelimiter.'1433';
00082 }
00083
00084 $connect_function = $persistent ? 'mssql_pconnect' : 'mssql_connect';
00085
00086 if ($dbhost && $user && $pw) {
00087 $conn = @$connect_function($dbhost, $user, $pw);
00088 } elseif ($dbhost && $user) {
00089 $conn = @$connect_function($dbhost, $user);
00090 } else {
00091 $conn = @$connect_function($dbhost);
00092 }
00093 if (!$conn) {
00094 throw new SQLException('connect failed', mssql_get_last_message());
00095 }
00096
00097 if ($dsninfo['database']) {
00098 if (!@mssql_select_db($dsninfo['database'], $conn)) {
00099 throw new SQLException('No database selected');
00100 }
00101
00102 $this->database = $dsninfo['database'];
00103 }
00104
00105 $this->dblink = $conn;
00106 }
00107
00111 public function getDatabaseInfo()
00112 {
00113 require_once 'creole/drivers/mssql/metadata/MSSQLDatabaseInfo.php';
00114 return new MSSQLDatabaseInfo($this);
00115 }
00116
00120 public function getIdGenerator()
00121 {
00122 require_once 'creole/drivers/mssql/MSSQLIdGenerator.php';
00123 return new MSSQLIdGenerator($this);
00124 }
00125
00129 public function prepareStatement($sql)
00130 {
00131 require_once 'creole/drivers/mssql/MSSQLPreparedStatement.php';
00132 return new MSSQLPreparedStatement($this, $sql);
00133 }
00134
00138 public function createStatement()
00139 {
00140 require_once 'creole/drivers/mssql/MSSQLStatement.php';
00141 return new MSSQLStatement($this);
00142 }
00143
00147 public function applyLimit(&$sql, $offset, $limit)
00148 {
00149 return false;
00150 }
00151
00155 function close()
00156 {
00157 $ret = @mssql_close($this->dblink);
00158 $this->dblink = null;
00159 return $ret;
00160 }
00161
00165 function executeQuery($sql, $fetchmode = null)
00166 {
00167 $this->lastQuery = $sql;
00168 if (!@mssql_select_db($this->database, $this->dblink)) {
00169 throw new SQLException('No database selected');
00170 }
00171 $result = @mssql_query($sql, $this->dblink);
00172 if (!$result) {
00173 throw new SQLException('Could not execute query', mssql_get_last_message());
00174 }
00175 return new MSSQLResultSet($this, $result, $fetchmode);
00176 }
00177
00181 function executeUpdate($sql)
00182 {
00183
00184 $this->lastQuery = $sql;
00185 if (!mssql_select_db($this->database, $this->dblink)) {
00186 throw new SQLException('No database selected');
00187 }
00188
00189 $result = @mssql_query($sql, $this->dblink);
00190 if (!$result) {
00191 throw new SQLException('Could not execute update', mssql_get_last_message(), $sql);
00192 }
00193
00194 return $this->getUpdateCount();
00195 }
00196
00202 protected function beginTrans()
00203 {
00204 $result = @mssql_query('BEGIN TRAN', $this->dblink);
00205 if (!$result) {
00206 throw new SQLException('Could not begin transaction', mssql_get_last_message());
00207 }
00208 }
00209
00215 protected function commitTrans()
00216 {
00217 if (!@mssql_select_db($this->database, $this->dblink)) {
00218 throw new SQLException('No database selected');
00219 }
00220 $result = @mssql_query('COMMIT TRAN', $this->dblink);
00221 if (!$result) {
00222 throw new SQLException('Could not commit transaction', mssql_get_last_message());
00223 }
00224 }
00225
00231 protected function rollbackTrans()
00232 {
00233 if (!@mssql_select_db($this->database, $this->dblink)) {
00234 throw new SQLException('no database selected');
00235 }
00236 $result = @mssql_query('ROLLBACK TRAN', $this->dblink);
00237 if (!$result) {
00238 throw new SQLException('Could not rollback transaction', mssql_get_last_message());
00239 }
00240 }
00241
00249 function getUpdateCount()
00250 {
00251 $res = @mssql_query('select @@rowcount', $this->dblink);
00252 if (!$res) {
00253 throw new SQLException('Unable to get affected row count', mssql_get_last_message());
00254 }
00255 $ar = @mssql_fetch_row($res);
00256 if (!$ar) {
00257 $result = 0;
00258 } else {
00259 @mssql_free_result($res);
00260 $result = $ar[0];
00261 }
00262
00263 return $result;
00264 }
00265
00266
00274 function prepareCall($sql)
00275 {
00276 require_once 'creole/drivers/mssql/MSSQLCallableStatement.php';
00277 $stmt = mssql_init($sql);
00278 if (!$stmt) {
00279 throw new SQLException('Unable to prepare statement', mssql_get_last_message(), $sql);
00280 }
00281 return new MSSQLCallableStatement($this, $stmt);
00282 }
00283 }