This repository was archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSQLServerDriver.php
More file actions
179 lines (154 loc) · 4.94 KB
/
SQLServerDriver.php
File metadata and controls
179 lines (154 loc) · 4.94 KB
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
<?php
/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/
declare(strict_types=1);
namespace Spiral\Database\Driver\SQLServer;
use DateTimeInterface;
use PDO;
use PDOStatement;
use Spiral\Database\Driver\Driver;
use Spiral\Database\Exception\DriverException;
use Spiral\Database\Exception\StatementException;
use Spiral\Database\Injection\ParameterInterface;
use Spiral\Database\Query\QueryBuilder;
class SQLServerDriver extends Driver
{
protected const DATETIME = 'Y-m-d\TH:i:s.000';
protected const DEFAULT_PDO_OPTIONS = [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_STRINGIFY_FETCHES => false
];
/**
* {@inheritdoc}
*
* @throws DriverException
*/
public function __construct(array $options)
{
parent::__construct(
$options,
new SQLServerHandler(),
new SQLServerCompiler('[]'),
QueryBuilder::defaultBuilder()
);
if ((int)$this->getPDO()->getAttribute(\PDO::ATTR_SERVER_VERSION) < 11) {
throw new DriverException('SQLServer driver supports only 11+ version of SQLServer');
}
}
/**
* @return string
*/
public function getType(): string
{
return 'SQLServer';
}
/**
* Bind parameters into statement. SQLServer need encoding to be specified for binary parameters.
*
* @param PDOStatement $statement
* @param array $parameters
* @return PDOStatement
*/
protected function bindParameters(
PDOStatement $statement,
iterable $parameters
): PDOStatement {
$index = 0;
foreach ($parameters as $name => $parameter) {
if (is_string($name)) {
$index = $name;
} else {
$index++;
}
$type = PDO::PARAM_STR;
if ($parameter instanceof ParameterInterface) {
$type = $parameter->getType();
$parameter = $parameter->getValue();
}
if ($parameter instanceof DateTimeInterface) {
$parameter = $this->formatDatetime($parameter);
}
if ($type === PDO::PARAM_LOB) {
$statement->bindParam(
$index,
$parameter,
$type,
0,
PDO::SQLSRV_ENCODING_BINARY
);
unset($parameter);
continue;
}
// numeric, @see http://php.net/manual/en/pdostatement.bindparam.php
$statement->bindValue($index, $parameter, $type);
unset($parameter);
}
return $statement;
}
/**
* Create nested transaction save point.
*
* @link http://en.wikipedia.org/wiki/Savepoint
*
* @param int $level Savepoint name/id, must not contain spaces and be valid database
* identifier.
*/
protected function createSavepoint(int $level): void
{
if ($this->logger !== null) {
$this->logger->info("Transaction: new savepoint 'SVP{$level}'");
}
$this->execute('SAVE TRANSACTION ' . $this->identifier("SVP{$level}"));
}
/**
* Commit/release savepoint.
*
* @link http://en.wikipedia.org/wiki/Savepoint
*
* @param int $level Savepoint name/id, must not contain spaces and be valid database identifier.
*/
protected function releaseSavepoint(int $level): void
{
if ($this->logger !== null) {
$this->logger->info("Transaction: release savepoint 'SVP{$level}'");
}
// SQLServer automatically commits nested transactions with parent transaction
}
/**
* Rollback savepoint.
*
* @link http://en.wikipedia.org/wiki/Savepoint
*
* @param int $level Savepoint name/id, must not contain spaces and be valid database identifier.
*/
protected function rollbackSavepoint(int $level): void
{
if ($this->logger !== null) {
$this->logger->info("Transaction: rollback savepoint 'SVP{$level}'");
}
$this->execute('ROLLBACK TRANSACTION ' . $this->identifier("SVP{$level}"));
}
/**
* {@inheritdoc}
*/
protected function mapException(\Throwable $exception, string $query): StatementException
{
$message = strtolower($exception->getMessage());
if (
strpos($message, '0800') !== false
|| strpos($message, '080P') !== false
|| strpos($message, 'connection') !== false
) {
return new StatementException\ConnectionException($exception, $query);
}
if ((int)$exception->getCode() === 23000) {
return new StatementException\ConstrainException($exception, $query);
}
return new StatementException($exception, $query);
}
}