-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathSQLSrvConnection.php
220 lines (188 loc) · 5.07 KB
/
SQLSrvConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
namespace Doctrine\DBAL\Driver\SQLSrv;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use const SQLSRV_ERR_ERRORS;
use function sqlsrv_begin_transaction;
use function sqlsrv_commit;
use function sqlsrv_configure;
use function sqlsrv_connect;
use function sqlsrv_errors;
use function sqlsrv_query;
use function sqlsrv_rollback;
use function sqlsrv_rows_affected;
use function sqlsrv_server_info;
use function str_replace;
/**
* SQL Server implementation for the Connection interface.
*/
class SQLSrvConnection implements Connection, ServerInfoAwareConnection
{
/** @var resource */
protected $conn;
/** @var LastInsertId */
protected $lastInsertId;
/**
* @param string $serverName
* @param mixed[] $connectionOptions
*
* @throws DriverException
*/
public function __construct($serverName, $connectionOptions)
{
if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) {
throw self::exceptionFromSqlSrvErrors();
}
$this->conn = sqlsrv_connect($serverName, $connectionOptions);
if (! $this->conn) {
throw self::exceptionFromSqlSrvErrors();
}
$this->lastInsertId = new LastInsertId();
}
/**
* {@inheritdoc}
*/
public function getServerVersion()
{
$serverInfo = sqlsrv_server_info($this->conn);
return $serverInfo['SQLServerVersion'];
}
/**
* {@inheritdoc}
*/
public function requiresQueryForServerVersion()
{
return false;
}
/**
* {@inheritDoc}
*/
public function prepare(string $sql) : DriverStatement
{
return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId);
}
/**
* {@inheritDoc}
*/
public function query(string $sql) : ResultStatement
{
$stmt = $this->prepare($sql);
$stmt->execute();
return $stmt;
}
/**
* {@inheritDoc}
*/
public function quote(string $value) : string
{
return "'" . str_replace("'", "''", $value) . "'";
}
/**
* {@inheritDoc}
*/
public function exec(string $statement) : int
{
$stmt = sqlsrv_query($this->conn, $statement);
if ($stmt === false) {
throw self::exceptionFromSqlSrvErrors();
}
return sqlsrv_rows_affected($stmt);
}
/**
* {@inheritDoc}
*/
public function lastInsertId(?string $name = null) : string
{
if ($name !== null) {
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
$stmt->execute([$name]);
} else {
$stmt = $this->query('SELECT @@IDENTITY');
}
$result = $stmt->fetchColumn();
if ($name !== null) {
if ($result === false) {
throw DriverException::noSuchSequence($name);
}
} else {
if ($result === null) {
throw DriverException::noInsertId();
}
}
return (string) $result;
}
/**
* {@inheritDoc}
*/
public function beginTransaction() : void
{
if (! sqlsrv_begin_transaction($this->conn)) {
throw self::exceptionFromSqlSrvErrors();
}
}
/**
* {@inheritDoc}
*/
public function commit() : void
{
if (! sqlsrv_commit($this->conn)) {
throw self::exceptionFromSqlSrvErrors();
}
}
/**
* {@inheritDoc}
*/
public function rollBack() : void
{
if (! sqlsrv_rollback($this->conn)) {
throw self::exceptionFromSqlSrvErrors();
}
}
/**
* {@inheritDoc}
*/
public function errorCode()
{
$errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
if ($errors) {
return $errors[0]['code'];
}
return false;
}
/**
* {@inheritDoc}
*/
public function errorInfo()
{
return sqlsrv_errors(SQLSRV_ERR_ERRORS);
}
/**
* Helper method to turn sql server errors into exception.
*
* @return DriverException
*/
public static function exceptionFromSqlSrvErrors() : DriverException
{
$errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
$message = '';
$sqlState = null;
$errorCode = null;
foreach ($errors as $error) {
$message .= 'SQLSTATE [' . $error['SQLSTATE'] . ', ' . $error['code'] . ']: ' . $error['message'] . "\n";
if ($sqlState === null) {
$sqlState = $error['SQLSTATE'];
}
if ($errorCode !== null) {
continue;
}
$errorCode = $error['code'];
}
if (! $message) {
$message = 'SQL Server error occurred but no error message was retrieved from driver.';
}
return new DriverException(rtrim($message), $sqlState, $errorCode);
}
}