|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver\PDOSqlsrv; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Driver\PDOConnection; |
|
8
|
|
|
use Doctrine\DBAL\Driver\PDOStatement; |
|
9
|
|
|
use Doctrine\DBAL\Driver\ResultStatement; |
|
10
|
|
|
use Doctrine\DBAL\Driver\Statement as DBALStatement; |
|
11
|
|
|
use function stripos; |
|
12
|
|
|
use function strpos; |
|
13
|
|
|
use function substr; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Sqlsrv Connection implementation. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Connection extends PDOConnection |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var LastInsertId|null */ |
|
21
|
|
|
protected $lastInsertId; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Append to any INSERT query to retrieve the last insert id. |
|
25
|
|
|
*/ |
|
26
|
|
|
protected const LAST_INSERT_ID_SQL = ';SELECT SCOPE_IDENTITY() AS LastInsertId;'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function lastInsertId($name = null) |
|
32
|
|
|
{ |
|
33
|
|
|
if ($this->lastInsertId && $this->lastInsertId->getId()) { |
|
34
|
|
|
return $this->lastInsertId->getId(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if ($name === null) { |
|
38
|
|
|
return parent::lastInsertId(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?'); |
|
42
|
|
|
$stmt->execute([$name]); |
|
43
|
|
|
|
|
44
|
|
|
return (string) $stmt->fetchColumn(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function quote(string $input) : string |
|
51
|
|
|
{ |
|
52
|
|
|
$val = parent::quote($input); |
|
53
|
|
|
|
|
54
|
|
|
// Fix for a driver version terminating all values with null byte |
|
55
|
|
|
if (strpos($val, "\0") !== false) { |
|
56
|
|
|
$val = substr($val, 0, -1); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $val; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function prepare(string $sql) : DBALStatement |
|
66
|
|
|
{ |
|
67
|
|
|
return parent::prepare($this->prepareSql($sql)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function query(string $sql) : ResultStatement |
|
74
|
|
|
{ |
|
75
|
|
|
return parent::query($this->prepareSql($sql)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritDoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function createStatement(\PDOStatement $stmt, string $sql) : PDOStatement |
|
82
|
|
|
{ |
|
83
|
|
|
$this->lastInsertId = static::isInsertStatement($sql) ? new LastInsertId() : null; |
|
84
|
|
|
|
|
85
|
|
|
return new Statement($stmt, $this->lastInsertId); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function prepareSql(string $sql) : string |
|
89
|
|
|
{ |
|
90
|
|
|
if (static::isInsertStatement($sql)) { |
|
91
|
|
|
$sql .= self::LAST_INSERT_ID_SQL; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $sql; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected static function isInsertStatement(string $sql) : bool |
|
98
|
|
|
{ |
|
99
|
|
|
return stripos($sql, 'INSERT INTO ') === 0; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|