|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Driver\SQLSrv; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
|
7
|
|
|
use Doctrine\DBAL\ParameterType; |
|
8
|
|
|
use const SQLSRV_ERR_ERRORS; |
|
9
|
|
|
use function func_get_args; |
|
10
|
|
|
use function is_float; |
|
11
|
|
|
use function is_int; |
|
12
|
|
|
use function sprintf; |
|
13
|
|
|
use function sqlsrv_begin_transaction; |
|
14
|
|
|
use function sqlsrv_commit; |
|
15
|
|
|
use function sqlsrv_configure; |
|
16
|
|
|
use function sqlsrv_connect; |
|
17
|
|
|
use function sqlsrv_errors; |
|
18
|
|
|
use function sqlsrv_query; |
|
19
|
|
|
use function sqlsrv_rollback; |
|
20
|
|
|
use function sqlsrv_rows_affected; |
|
21
|
|
|
use function sqlsrv_server_info; |
|
22
|
|
|
use function str_replace; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* SQL Server implementation for the Connection interface. |
|
26
|
|
|
*/ |
|
27
|
|
|
class SQLSrvConnection implements Connection, ServerInfoAwareConnection |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var resource */ |
|
30
|
|
|
protected $conn; |
|
31
|
|
|
|
|
32
|
|
|
/** @var LastInsertId */ |
|
33
|
|
|
protected $lastInsertId; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $serverName |
|
37
|
|
|
* @param mixed[] $connectionOptions |
|
38
|
|
|
* |
|
39
|
|
|
* @throws SQLSrvException |
|
40
|
|
|
*/ |
|
41
|
242 |
|
public function __construct($serverName, $connectionOptions) |
|
42
|
|
|
{ |
|
43
|
242 |
|
if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) { |
|
44
|
|
|
throw SQLSrvException::fromSqlSrvErrors(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
242 |
|
$conn = sqlsrv_connect($serverName, $connectionOptions); |
|
48
|
|
|
|
|
49
|
242 |
|
if ($conn === false) { |
|
50
|
221 |
|
throw SQLSrvException::fromSqlSrvErrors(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
242 |
|
$this->conn = $conn; |
|
54
|
242 |
|
$this->lastInsertId = new LastInsertId(); |
|
55
|
242 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
242 |
|
public function getServerVersion() |
|
61
|
|
|
{ |
|
62
|
242 |
|
$serverInfo = sqlsrv_server_info($this->conn); |
|
63
|
|
|
|
|
64
|
242 |
|
return $serverInfo['SQLServerVersion']; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
242 |
|
public function requiresQueryForServerVersion() |
|
71
|
|
|
{ |
|
72
|
242 |
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritDoc} |
|
77
|
|
|
*/ |
|
78
|
241 |
|
public function prepare($sql) |
|
79
|
|
|
{ |
|
80
|
241 |
|
return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritDoc} |
|
85
|
|
|
*/ |
|
86
|
240 |
|
public function query() |
|
87
|
|
|
{ |
|
88
|
240 |
|
$args = func_get_args(); |
|
89
|
240 |
|
$sql = $args[0]; |
|
90
|
240 |
|
$stmt = $this->prepare($sql); |
|
91
|
240 |
|
$stmt->execute(); |
|
92
|
|
|
|
|
93
|
240 |
|
return $stmt; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritDoc} |
|
98
|
|
|
*/ |
|
99
|
224 |
|
public function quote($value, $type = ParameterType::STRING) |
|
100
|
|
|
{ |
|
101
|
224 |
|
if (is_int($value)) { |
|
102
|
215 |
|
return $value; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
224 |
|
if (is_float($value)) { |
|
106
|
|
|
return sprintf('%F', $value); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
224 |
|
return "'" . str_replace("'", "''", $value) . "'"; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritDoc} |
|
114
|
|
|
*/ |
|
115
|
242 |
|
public function exec($statement) |
|
116
|
|
|
{ |
|
117
|
242 |
|
$stmt = sqlsrv_query($this->conn, $statement); |
|
118
|
|
|
|
|
119
|
242 |
|
if ($stmt === false) { |
|
120
|
242 |
|
throw SQLSrvException::fromSqlSrvErrors(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
242 |
|
$rowsAffected = sqlsrv_rows_affected($stmt); |
|
124
|
|
|
|
|
125
|
242 |
|
if ($rowsAffected === false) { |
|
126
|
|
|
throw SQLSrvException::fromSqlSrvErrors(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
242 |
|
return $rowsAffected; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritDoc} |
|
134
|
|
|
*/ |
|
135
|
8 |
|
public function lastInsertId($name = null) |
|
136
|
|
|
{ |
|
137
|
8 |
|
if ($name !== null) { |
|
138
|
7 |
|
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?'); |
|
139
|
7 |
|
$stmt->execute([$name]); |
|
140
|
|
|
} else { |
|
141
|
8 |
|
$stmt = $this->query('SELECT @@IDENTITY'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
8 |
|
return $stmt->fetchColumn(); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritDoc} |
|
149
|
|
|
*/ |
|
150
|
234 |
|
public function beginTransaction() |
|
151
|
|
|
{ |
|
152
|
234 |
|
if (! sqlsrv_begin_transaction($this->conn)) { |
|
153
|
|
|
throw SQLSrvException::fromSqlSrvErrors(); |
|
154
|
|
|
} |
|
155
|
234 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritDoc} |
|
159
|
|
|
*/ |
|
160
|
232 |
|
public function commit() |
|
161
|
|
|
{ |
|
162
|
232 |
|
if (! sqlsrv_commit($this->conn)) { |
|
163
|
|
|
throw SQLSrvException::fromSqlSrvErrors(); |
|
164
|
|
|
} |
|
165
|
232 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* {@inheritDoc} |
|
169
|
|
|
*/ |
|
170
|
234 |
|
public function rollBack() |
|
171
|
|
|
{ |
|
172
|
234 |
|
if (! sqlsrv_rollback($this->conn)) { |
|
173
|
|
|
throw SQLSrvException::fromSqlSrvErrors(); |
|
174
|
|
|
} |
|
175
|
234 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* {@inheritDoc} |
|
179
|
|
|
*/ |
|
180
|
|
|
public function errorCode() |
|
181
|
|
|
{ |
|
182
|
|
|
$errors = sqlsrv_errors(SQLSRV_ERR_ERRORS); |
|
183
|
|
|
if ($errors) { |
|
184
|
|
|
return $errors[0]['code']; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return false; |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* {@inheritDoc} |
|
192
|
|
|
*/ |
|
193
|
|
|
public function errorInfo() |
|
194
|
|
|
{ |
|
195
|
|
|
return (array) sqlsrv_errors(SQLSRV_ERR_ERRORS); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|