1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Driver\PDOSqlsrv; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\Connection; |
6
|
|
|
use Doctrine\DBAL\Driver\PDOConnection; |
7
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
8
|
|
|
use Doctrine\DBAL\ParameterType; |
9
|
|
|
use function func_get_args; |
10
|
|
|
use function strpos; |
11
|
|
|
use function substr; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Sqlsrv Connection implementation. |
15
|
|
|
*/ |
16
|
|
|
class PDOSqlsrvConnection implements Connection, ServerInfoAwareConnection |
17
|
|
|
{ |
18
|
|
|
/** @var PDOConnection */ |
19
|
|
|
protected $conn; |
20
|
|
|
|
21
|
|
|
/** @var LastInsertId */ |
22
|
|
|
protected $lastInsertId; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function __construct($dsn, $user = null, $password = null, ?array $options = null) |
28
|
|
|
{ |
29
|
|
|
$this->conn = new PDOConnection($dsn, $user, $password, $options); |
30
|
|
|
$this->lastInsertId = new LastInsertId(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
36
|
|
|
public function prepare($sql) |
37
|
|
|
{ |
38
|
|
|
$this->lastInsertId = new LastInsertId(); |
39
|
|
|
return new PDOSqlsrvStatement($this->conn, $sql, $this->lastInsertId); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
public function query() |
46
|
|
|
{ |
47
|
|
|
$args = func_get_args(); |
48
|
|
|
$sql = $args[0]; |
49
|
|
|
$stmt = $this->prepare($sql); |
50
|
|
|
$stmt->execute(); |
51
|
|
|
|
52
|
|
|
return $stmt; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
58
|
|
|
public function exec($statement) |
59
|
|
|
{ |
60
|
|
|
return $this->conn->exec($statement); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function quote($value, $type = ParameterType::STRING) |
67
|
|
|
{ |
68
|
|
|
$val = $this->conn->quote($value, $type); |
69
|
|
|
|
70
|
|
|
// Fix for a driver version terminating all values with null byte |
71
|
|
|
if (strpos($val, "\0") !== false) { |
72
|
|
|
$val = substr($val, 0, -1); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $val; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function lastInsertId($name = null) |
82
|
|
|
{ |
83
|
|
|
if ($this->lastInsertId->getId()) { |
84
|
|
|
return $this->lastInsertId->getId(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($name === null) { |
88
|
|
|
return $this->conn->lastInsertId(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?'); |
92
|
|
|
$stmt->execute([$name]); |
93
|
|
|
|
94
|
|
|
return $stmt->fetchColumn(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
|
|
public function beginTransaction() |
101
|
|
|
{ |
102
|
|
|
return $this->conn->beginTransaction(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
*/ |
108
|
|
|
public function commit() |
109
|
|
|
{ |
110
|
|
|
return $this->conn->commit(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritDoc} |
115
|
|
|
*/ |
116
|
|
|
public function rollBack() |
117
|
|
|
{ |
118
|
|
|
return $this->conn->rollBack(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
*/ |
124
|
|
|
public function errorCode() |
125
|
|
|
{ |
126
|
|
|
return $this->conn->errorCode(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
|
|
public function errorInfo() |
133
|
|
|
{ |
134
|
|
|
return $this->conn->errorInfo(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function getServerVersion() |
141
|
|
|
{ |
142
|
|
|
return $this->conn->getServerVersion(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function requiresQueryForServerVersion() |
149
|
|
|
{ |
150
|
|
|
return $this->conn->requiresQueryForServerVersion(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param int $attribute |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
public function getAttribute($attribute) |
159
|
|
|
{ |
160
|
|
|
return $this->conn->getAttribute($attribute); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|