1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver\SQLSrv; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Driver\Connection; |
8
|
|
|
use Doctrine\DBAL\Driver\ResultStatement; |
9
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
10
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
11
|
|
|
use function sqlsrv_begin_transaction; |
12
|
|
|
use function sqlsrv_commit; |
13
|
|
|
use function sqlsrv_configure; |
14
|
|
|
use function sqlsrv_connect; |
15
|
|
|
use function sqlsrv_query; |
16
|
|
|
use function sqlsrv_rollback; |
17
|
|
|
use function sqlsrv_rows_affected; |
18
|
|
|
use function sqlsrv_server_info; |
19
|
|
|
use function str_replace; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SQL Server implementation for the Connection interface. |
23
|
|
|
*/ |
24
|
|
|
class SQLSrvConnection implements Connection, ServerInfoAwareConnection |
25
|
|
|
{ |
26
|
|
|
/** @var resource */ |
27
|
|
|
protected $conn; |
28
|
|
|
|
29
|
|
|
/** @var LastInsertId */ |
30
|
|
|
protected $lastInsertId; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $serverName |
34
|
|
|
* @param mixed[] $connectionOptions |
35
|
|
|
* |
36
|
|
|
* @throws SQLSrvException |
37
|
|
|
*/ |
38
|
271 |
|
public function __construct($serverName, $connectionOptions) |
39
|
|
|
{ |
40
|
271 |
|
if (! sqlsrv_configure('WarningsReturnAsErrors', 0)) { |
41
|
|
|
throw SQLSrvException::fromSqlSrvErrors(); |
42
|
|
|
} |
43
|
|
|
|
44
|
271 |
|
$conn = sqlsrv_connect($serverName, $connectionOptions); |
45
|
|
|
|
46
|
271 |
|
if ($conn === false) { |
47
|
251 |
|
throw SQLSrvException::fromSqlSrvErrors(); |
48
|
|
|
} |
49
|
|
|
|
50
|
271 |
|
$this->conn = $conn; |
51
|
271 |
|
$this->lastInsertId = new LastInsertId(); |
52
|
271 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
271 |
|
public function getServerVersion() |
58
|
|
|
{ |
59
|
271 |
|
$serverInfo = sqlsrv_server_info($this->conn); |
60
|
|
|
|
61
|
271 |
|
return $serverInfo['SQLServerVersion']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
271 |
|
public function requiresQueryForServerVersion() |
68
|
|
|
{ |
69
|
271 |
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
270 |
|
public function prepare(string $sql) : DriverStatement |
76
|
|
|
{ |
77
|
270 |
|
return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
269 |
|
public function query(string $sql) : ResultStatement |
84
|
|
|
{ |
85
|
269 |
|
$stmt = $this->prepare($sql); |
86
|
269 |
|
$stmt->execute(); |
87
|
|
|
|
88
|
269 |
|
return $stmt; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritDoc} |
93
|
|
|
*/ |
94
|
254 |
|
public function quote(string $input) : string |
95
|
|
|
{ |
96
|
254 |
|
return "'" . str_replace("'", "''", $input) . "'"; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritDoc} |
101
|
|
|
*/ |
102
|
271 |
|
public function exec(string $statement) : int |
103
|
|
|
{ |
104
|
271 |
|
$stmt = sqlsrv_query($this->conn, $statement); |
105
|
|
|
|
106
|
271 |
|
if ($stmt === false) { |
107
|
271 |
|
throw SQLSrvException::fromSqlSrvErrors(); |
108
|
|
|
} |
109
|
|
|
|
110
|
271 |
|
$rowsAffected = sqlsrv_rows_affected($stmt); |
111
|
|
|
|
112
|
271 |
|
if ($rowsAffected === false) { |
113
|
|
|
throw SQLSrvException::fromSqlSrvErrors(); |
114
|
|
|
} |
115
|
|
|
|
116
|
271 |
|
return $rowsAffected; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
*/ |
122
|
8 |
|
public function lastInsertId($name = null) |
123
|
|
|
{ |
124
|
8 |
|
if ($name !== null) { |
125
|
7 |
|
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?'); |
126
|
7 |
|
$stmt->execute([$name]); |
127
|
|
|
} else { |
128
|
8 |
|
$stmt = $this->query('SELECT @@IDENTITY'); |
129
|
|
|
} |
130
|
|
|
|
131
|
8 |
|
return $stmt->fetchColumn(); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
*/ |
137
|
263 |
|
public function beginTransaction() : void |
138
|
|
|
{ |
139
|
263 |
|
if (! sqlsrv_begin_transaction($this->conn)) { |
140
|
|
|
throw SQLSrvException::fromSqlSrvErrors(); |
141
|
|
|
} |
142
|
263 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritDoc} |
146
|
|
|
*/ |
147
|
261 |
|
public function commit() : void |
148
|
|
|
{ |
149
|
261 |
|
if (! sqlsrv_commit($this->conn)) { |
150
|
|
|
throw SQLSrvException::fromSqlSrvErrors(); |
151
|
|
|
} |
152
|
261 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritDoc} |
156
|
|
|
*/ |
157
|
263 |
|
public function rollBack() : void |
158
|
|
|
{ |
159
|
263 |
|
if (! sqlsrv_rollback($this->conn)) { |
160
|
|
|
throw SQLSrvException::fromSqlSrvErrors(); |
161
|
|
|
} |
162
|
263 |
|
} |
163
|
|
|
} |
164
|
|
|
|