| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | require_once 'creole/Connection.php'; |
|---|
| 25 | require_once 'creole/common/ConnectionCommon.php'; |
|---|
| 26 | include_once 'creole/drivers/mssql/MSSQLResultSet.php'; |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | class MSSQLConnection extends ConnectionCommon implements Connection { |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | private $database; |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | function connect($dsninfo, $flags = 0) |
|---|
| 58 | { |
|---|
| 59 | if (!extension_loaded('mssql') && !extension_loaded('sybase') && !extension_loaded('sybase_ct')) { |
|---|
| 60 | throw new SQLException('mssql extension not loaded'); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | $this->dsn = $dsninfo; |
|---|
| 64 | $this->flags = $flags; |
|---|
| 65 | |
|---|
| 66 | $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT); |
|---|
| 67 | |
|---|
| 68 | $user = $dsninfo['username']; |
|---|
| 69 | $pw = $dsninfo['password']; |
|---|
| 70 | $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; |
|---|
| 71 | |
|---|
| 72 | if (PHP_OS == "WINNT" || PHP_OS == "WIN32") { |
|---|
| 73 | $portDelimiter = ","; |
|---|
| 74 | } else { |
|---|
| 75 | $portDelimiter = ":"; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | if(!empty($dsninfo['port'])) { |
|---|
| 79 | $dbhost .= $portDelimiter.$dsninfo['port']; |
|---|
| 80 | } else { |
|---|
| 81 | $dbhost .= $portDelimiter.'1433'; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | $connect_function = $persistent ? 'mssql_pconnect' : 'mssql_connect'; |
|---|
| 85 | |
|---|
| 86 | if ($dbhost && $user && $pw) { |
|---|
| 87 | $conn = @$connect_function($dbhost, $user, $pw); |
|---|
| 88 | } elseif ($dbhost && $user) { |
|---|
| 89 | $conn = @$connect_function($dbhost, $user); |
|---|
| 90 | } else { |
|---|
| 91 | $conn = @$connect_function($dbhost); |
|---|
| 92 | } |
|---|
| 93 | if (!$conn) { |
|---|
| 94 | throw new SQLException('connect failed', mssql_get_last_message()); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | if ($dsninfo['database']) { |
|---|
| 98 | if (!@mssql_select_db($dsninfo['database'], $conn)) { |
|---|
| 99 | throw new SQLException('No database selected'); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | $this->database = $dsninfo['database']; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | $this->dblink = $conn; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | public function getDatabaseInfo() |
|---|
| 112 | { |
|---|
| 113 | require_once 'creole/drivers/mssql/metadata/MSSQLDatabaseInfo.php'; |
|---|
| 114 | return new MSSQLDatabaseInfo($this); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | public function getIdGenerator() |
|---|
| 121 | { |
|---|
| 122 | require_once 'creole/drivers/mssql/MSSQLIdGenerator.php'; |
|---|
| 123 | return new MSSQLIdGenerator($this); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | public function prepareStatement($sql) |
|---|
| 130 | { |
|---|
| 131 | require_once 'creole/drivers/mssql/MSSQLPreparedStatement.php'; |
|---|
| 132 | return new MSSQLPreparedStatement($this, $sql); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | public function createStatement() |
|---|
| 139 | { |
|---|
| 140 | require_once 'creole/drivers/mssql/MSSQLStatement.php'; |
|---|
| 141 | return new MSSQLStatement($this); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | public function applyLimit(&$sql, $offset, $limit) |
|---|
| 148 | { |
|---|
| 149 | return false; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | function close() |
|---|
| 156 | { |
|---|
| 157 | $ret = @mssql_close($this->dblink); |
|---|
| 158 | $this->dblink = null; |
|---|
| 159 | return $ret; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | function executeQuery($sql, $fetchmode = null) |
|---|
| 166 | { |
|---|
| 167 | $this->lastQuery = $sql; |
|---|
| 168 | if (!@mssql_select_db($this->database, $this->dblink)) { |
|---|
| 169 | throw new SQLException('No database selected'); |
|---|
| 170 | } |
|---|
| 171 | $result = @mssql_query($sql, $this->dblink); |
|---|
| 172 | if (!$result) { |
|---|
| 173 | throw new SQLException('Could not execute query', mssql_get_last_message()); |
|---|
| 174 | } |
|---|
| 175 | return new MSSQLResultSet($this, $result, $fetchmode); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | function executeUpdate($sql) |
|---|
| 182 | { |
|---|
| 183 | |
|---|
| 184 | $this->lastQuery = $sql; |
|---|
| 185 | if (!mssql_select_db($this->database, $this->dblink)) { |
|---|
| 186 | throw new SQLException('No database selected'); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | $result = @mssql_query($sql, $this->dblink); |
|---|
| 190 | if (!$result) { |
|---|
| 191 | throw new SQLException('Could not execute update', mssql_get_last_message(), $sql); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | return $this->getUpdateCount(); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | protected function beginTrans() |
|---|
| 203 | { |
|---|
| 204 | $result = @mssql_query('BEGIN TRAN', $this->dblink); |
|---|
| 205 | if (!$result) { |
|---|
| 206 | throw new SQLException('Could not begin transaction', mssql_get_last_message()); |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | protected function commitTrans() |
|---|
| 216 | { |
|---|
| 217 | if (!@mssql_select_db($this->database, $this->dblink)) { |
|---|
| 218 | throw new SQLException('No database selected'); |
|---|
| 219 | } |
|---|
| 220 | $result = @mssql_query('COMMIT TRAN', $this->dblink); |
|---|
| 221 | if (!$result) { |
|---|
| 222 | throw new SQLException('Could not commit transaction', mssql_get_last_message()); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | protected function rollbackTrans() |
|---|
| 232 | { |
|---|
| 233 | if (!@mssql_select_db($this->database, $this->dblink)) { |
|---|
| 234 | throw new SQLException('no database selected'); |
|---|
| 235 | } |
|---|
| 236 | $result = @mssql_query('ROLLBACK TRAN', $this->dblink); |
|---|
| 237 | if (!$result) { |
|---|
| 238 | throw new SQLException('Could not rollback transaction', mssql_get_last_message()); |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | function getUpdateCount() |
|---|
| 250 | { |
|---|
| 251 | $res = @mssql_query('select @@rowcount', $this->dblink); |
|---|
| 252 | if (!$res) { |
|---|
| 253 | throw new SQLException('Unable to get affected row count', mssql_get_last_message()); |
|---|
| 254 | } |
|---|
| 255 | $ar = @mssql_fetch_row($res); |
|---|
| 256 | if (!$ar) { |
|---|
| 257 | $result = 0; |
|---|
| 258 | } else { |
|---|
| 259 | @mssql_free_result($res); |
|---|
| 260 | $result = $ar[0]; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | return $result; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | function prepareCall($sql) |
|---|
| 275 | { |
|---|
| 276 | require_once 'creole/drivers/mssql/MSSQLCallableStatement.php'; |
|---|
| 277 | $stmt = mssql_init($sql); |
|---|
| 278 | if (!$stmt) { |
|---|
| 279 | throw new SQLException('Unable to prepare statement', mssql_get_last_message(), $sql); |
|---|
| 280 | } |
|---|
| 281 | return new MSSQLCallableStatement($this, $stmt); |
|---|
| 282 | } |
|---|
| 283 | } |
|---|