|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Platforms; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Schema\Sequence; |
|
23
|
|
|
use const PREG_OFFSET_CAPTURE; |
|
24
|
|
|
use function preg_match; |
|
25
|
|
|
use function preg_match_all; |
|
26
|
|
|
use function substr_count; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Platform to ensure compatibility of Doctrine with Microsoft SQL Server 2012 version. |
|
30
|
|
|
* |
|
31
|
|
|
* Differences to SQL Server 2008 and before are that sequences are introduced, |
|
32
|
|
|
* and support for the new OFFSET... FETCH syntax for result pagination has been added. |
|
33
|
|
|
* |
|
34
|
|
|
* @author Steve Müller <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class SQLServer2012Platform extends SQLServer2008Platform |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
19 |
|
public function getAlterSequenceSQL(Sequence $sequence) |
|
42
|
|
|
{ |
|
43
|
19 |
|
return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) . |
|
44
|
19 |
|
' INCREMENT BY ' . $sequence->getAllocationSize(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
29 |
|
public function getCreateSequenceSQL(Sequence $sequence) |
|
51
|
|
|
{ |
|
52
|
29 |
|
return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) . |
|
53
|
29 |
|
' START WITH ' . $sequence->getInitialValue() . |
|
54
|
29 |
|
' INCREMENT BY ' . $sequence->getAllocationSize() . |
|
55
|
29 |
|
' MINVALUE ' . $sequence->getInitialValue(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
23 |
|
public function getDropSequenceSQL($sequence) |
|
62
|
|
|
{ |
|
63
|
23 |
|
if ($sequence instanceof Sequence) { |
|
64
|
|
|
$sequence = $sequence->getQuotedName($this); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
23 |
|
return 'DROP SEQUENCE ' . $sequence; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
12 |
|
public function getListSequencesSQL($database) |
|
74
|
|
|
{ |
|
75
|
12 |
|
return 'SELECT seq.name, |
|
76
|
|
|
CAST( |
|
77
|
|
|
seq.increment AS VARCHAR(MAX) |
|
78
|
|
|
) AS increment, -- CAST avoids driver error for sql_variant type |
|
79
|
|
|
CAST( |
|
80
|
|
|
seq.start_value AS VARCHAR(MAX) |
|
81
|
|
|
) AS start_value -- CAST avoids driver error for sql_variant type |
|
82
|
|
|
FROM sys.sequences AS seq'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
21 |
|
public function getSequenceNextValSQL($sequenceName) |
|
89
|
|
|
{ |
|
90
|
21 |
|
return 'SELECT NEXT VALUE FOR ' . $sequenceName; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
33 |
|
public function supportsSequences() |
|
97
|
|
|
{ |
|
98
|
33 |
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
* |
|
104
|
|
|
* Returns Microsoft SQL Server 2012 specific keywords class |
|
105
|
|
|
*/ |
|
106
|
1193 |
|
protected function getReservedKeywordsClass() |
|
107
|
|
|
{ |
|
108
|
1193 |
|
return Keywords\SQLServer2012Keywords::class; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
510 |
|
protected function doModifyLimitQuery($query, $limit, $offset = null) |
|
115
|
|
|
{ |
|
116
|
510 |
|
if ($limit === null && $offset <= 0) { |
|
117
|
21 |
|
return $query; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Queries using OFFSET... FETCH MUST have an ORDER BY clause |
|
121
|
|
|
// Find the position of the last instance of ORDER BY and ensure it is not within a parenthetical statement |
|
122
|
|
|
// but can be in a newline |
|
123
|
489 |
|
$matches = []; |
|
124
|
489 |
|
$matchesCount = preg_match_all("/[\\s]+order\\s+by\\s/im", $query, $matches, PREG_OFFSET_CAPTURE); |
|
125
|
489 |
|
$orderByPos = false; |
|
126
|
489 |
|
if ($matchesCount > 0) { |
|
127
|
335 |
|
$orderByPos = $matches[0][($matchesCount - 1)][1]; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
489 |
|
if ($orderByPos === false |
|
131
|
489 |
|
|| substr_count($query, "(", $orderByPos) - substr_count($query, ")", $orderByPos) |
|
132
|
|
|
) { |
|
133
|
173 |
|
if (preg_match('/^SELECT\s+DISTINCT/im', $query)) { |
|
134
|
|
|
// SQL Server won't let us order by a non-selected column in a DISTINCT query, |
|
135
|
|
|
// so we have to do this madness. This says, order by the first column in the |
|
136
|
|
|
// result. SQL Server's docs say that a nonordered query's result order is non- |
|
137
|
|
|
// deterministic anyway, so this won't do anything that a bunch of update and |
|
138
|
|
|
// deletes to the table wouldn't do anyway. |
|
139
|
38 |
|
$query .= " ORDER BY 1"; |
|
140
|
|
|
} else { |
|
141
|
|
|
// In another DBMS, we could do ORDER BY 0, but SQL Server gets angry if you |
|
142
|
|
|
// use constant expressions in the order by list. |
|
143
|
135 |
|
$query .= " ORDER BY (SELECT 0)"; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
489 |
|
if ($offset === null) { |
|
148
|
361 |
|
$offset = 0; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// This looks somewhat like MYSQL, but limit/offset are in inverse positions |
|
152
|
|
|
// Supposedly SQL:2008 core standard. |
|
153
|
|
|
// Per TSQL spec, FETCH NEXT n ROWS ONLY is not valid without OFFSET n ROWS. |
|
154
|
489 |
|
$query .= " OFFSET " . (int) $offset . " ROWS"; |
|
155
|
|
|
|
|
156
|
489 |
|
if ($limit !== null) { |
|
157
|
489 |
|
$query .= " FETCH NEXT " . (int) $limit . " ROWS ONLY"; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
489 |
|
return $query; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|