Skip to content

Commit d5ef809

Browse files
committed
Engine::applyLimit() returns string (BC break)
1 parent b25f1af commit d5ef809

16 files changed

+325
-254
lines changed

src/Database/Drivers/Engine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function formatDateTime(\DateTimeInterface $value): string;
4949
function formatDateInterval(\DateInterval $value): string;
5050

5151
/** Applies LIMIT and OFFSET clauses to an SQL query. */
52-
function applyLimit(string &$sql, ?int $limit, ?int $offset): void;
52+
function applyLimit(string $sql, ?int $limit, ?int $offset): string;
5353

5454
/********************* reflection ****************d*g**/
5555

src/Database/Drivers/Engines/MSSQLEngine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function formatDateInterval(\DateInterval $value): string
6060
}
6161

6262

63-
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
63+
public function applyLimit(string $sql, ?int $limit, ?int $offset): string
6464
{
6565
if ($offset) {
6666
throw new Nette\NotSupportedException('Offset is not supported by this database.');
@@ -74,6 +74,8 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
7474
throw new Nette\InvalidArgumentException('SQL query must begin with SELECT, UPDATE or DELETE command.');
7575
}
7676
}
77+
78+
return $sql;
7779
}
7880

7981

src/Database/Drivers/Engines/MySQLEngine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function formatDateInterval(\DateInterval $value): string
7171
}
7272

7373

74-
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
74+
public function applyLimit(string $sql, ?int $limit, ?int $offset): string
7575
{
7676
if ($limit < 0 || $offset < 0) {
7777
throw new Nette\InvalidArgumentException('Negative offset or limit.');
@@ -81,6 +81,8 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
8181
$sql .= ' LIMIT ' . ($limit ?? '18446744073709551615')
8282
. ($offset ? ' OFFSET ' . $offset : '');
8383
}
84+
85+
return $sql;
8486
}
8587

8688

src/Database/Drivers/Engines/ODBCEngine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function formatDateInterval(\DateInterval $value): string
5252
}
5353

5454

55-
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
55+
public function applyLimit(string $sql, ?int $limit, ?int $offset): string
5656
{
5757
if ($offset) {
5858
throw new Nette\NotSupportedException('Offset is not supported by this database.');
@@ -66,6 +66,8 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
6666
throw new Nette\InvalidArgumentException('SQL query must begin with SELECT, UPDATE or DELETE command.');
6767
}
6868
}
69+
70+
return $sql;
6971
}
7072

7173

src/Database/Drivers/Engines/OracleEngine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function formatDateInterval(\DateInterval $value): string
6969
}
7070

7171

72-
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
72+
public function applyLimit(string $sql, ?int $limit, ?int $offset): string
7373
{
7474
if ($limit < 0 || $offset < 0) {
7575
throw new Nette\InvalidArgumentException('Negative offset or limit.');
@@ -83,6 +83,8 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
8383
} elseif ($limit !== null) {
8484
$sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . $limit;
8585
}
86+
87+
return $sql;
8688
}
8789

8890

src/Database/Drivers/Engines/PostgreSQLEngine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function formatDateInterval(\DateInterval $value): string
6767
}
6868

6969

70-
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
70+
public function applyLimit(string $sql, ?int $limit, ?int $offset): string
7171
{
7272
if ($limit < 0 || $offset < 0) {
7373
throw new Nette\InvalidArgumentException('Negative offset or limit.');
@@ -80,6 +80,8 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
8080
if ($offset) {
8181
$sql .= ' OFFSET ' . $offset;
8282
}
83+
84+
return $sql;
8385
}
8486

8587

src/Database/Drivers/Engines/SQLServerEngine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function formatDateInterval(\DateInterval $value): string
6161
}
6262

6363

64-
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
64+
public function applyLimit(string $sql, ?int $limit, ?int $offset): string
6565
{
6666
if ($limit < 0 || $offset < 0) {
6767
throw new Nette\InvalidArgumentException('Negative offset or limit.');
@@ -71,6 +71,8 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
7171
$sql .= ' OFFSET ' . (int) $offset . ' ROWS '
7272
. 'FETCH NEXT ' . (int) $limit . ' ROWS ONLY';
7373
}
74+
75+
return $sql;
7476
}
7577

7678

src/Database/Drivers/Engines/SQLiteEngine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function formatDateInterval(\DateInterval $value): string
8888
}
8989

9090

91-
public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
91+
public function applyLimit(string $sql, ?int $limit, ?int $offset): string
9292
{
9393
if ($limit < 0 || $offset < 0) {
9494
throw new Nette\InvalidArgumentException('Negative offset or limit.');
@@ -97,6 +97,8 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
9797
$sql .= ' LIMIT ' . ($limit ?? '-1')
9898
. ($offset ? ' OFFSET ' . $offset : '');
9999
}
100+
101+
return $sql;
100102
}
101103

102104

src/Database/Table/SqlBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function buildUpdateQuery(): string
8585
}
8686

8787
if ($this->limit !== null || $this->offset) {
88-
$this->engine->applyLimit($query, $this->limit, $this->offset);
88+
$query = $this->engine->applyLimit($query, $this->limit, $this->offset);
8989
}
9090

9191
return $query;
@@ -96,7 +96,7 @@ public function buildDeleteQuery(): string
9696
{
9797
$query = "DELETE FROM {$this->delimitedTable}" . $this->tryDelimite($this->buildConditions());
9898
if ($this->limit !== null || $this->offset) {
99-
$this->engine->applyLimit($query, $this->limit, $this->offset);
99+
$query = $this->engine->applyLimit($query, $this->limit, $this->offset);
100100
}
101101

102102
return $query;
@@ -183,7 +183,7 @@ public function buildSelectQuery(?array $columns = null): string
183183
$queryJoins = $this->buildQueryJoins($joins, $finalJoinConditions);
184184
$query = "{$querySelect} FROM {$this->delimitedTable}{$queryJoins}{$queryCondition}{$queryEnd}";
185185

186-
$this->engine->applyLimit($query, $this->limit, $this->offset);
186+
$query = $this->engine->applyLimit($query, $this->limit, $this->offset);
187187

188188
return $this->tryDelimite($query);
189189
}

tests/Database/Engines/MSSQLEngine.applyLimit.phpt

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,52 +9,63 @@ require __DIR__ . '/../../bootstrap.php';
99
$connection = Mockery::mock(Nette\Database\Drivers\Connection::class);
1010
$engine = new Nette\Database\Drivers\Engines\MSSQLEngine($connection);
1111

12-
Assert::exception(function () use ($engine) {
13-
$query = 'SELECT 1 FROM t';
14-
$engine->applyLimit($query, 10, 20);
15-
}, Nette\NotSupportedException::class, 'Offset is not supported by this database.');
16-
17-
Assert::exception(function () use ($engine) {
18-
$query = 'SELECT 1 FROM t';
19-
$engine->applyLimit($query, 0, 20);
20-
}, Nette\NotSupportedException::class, 'Offset is not supported by this database.');
21-
22-
$query = 'SELECT 1 FROM t';
23-
$engine->applyLimit($query, 10, 0);
24-
Assert::same('SELECT TOP 10 1 FROM t', $query);
25-
26-
Assert::exception(function () use ($engine) {
27-
$query = 'SELECT 1 FROM t';
28-
$engine->applyLimit($query, null, 20);
29-
}, Nette\NotSupportedException::class, 'Offset is not supported by this database.');
30-
31-
$query = 'SELECT 1 FROM t';
32-
$engine->applyLimit($query, 10, null);
33-
Assert::same('SELECT TOP 10 1 FROM t', $query);
34-
35-
$query = ' select distinct 1 FROM t';
36-
$engine->applyLimit($query, 10, null);
37-
Assert::same(' select distinct TOP 10 1 FROM t', $query);
38-
39-
$query = 'UPDATE t SET';
40-
$engine->applyLimit($query, 10, null);
41-
Assert::same('UPDATE TOP 10 t SET', $query);
42-
43-
$query = 'DELETE FROM t SET';
44-
$engine->applyLimit($query, 10, null);
45-
Assert::same('DELETE TOP 10 FROM t SET', $query);
46-
47-
Assert::exception(function () use ($engine) {
48-
$query = 'SET FROM t';
49-
$engine->applyLimit($query, 10, null);
50-
}, Nette\InvalidArgumentException::class, 'SQL query must begin with SELECT, UPDATE or DELETE command.');
51-
52-
Assert::exception(function () use ($engine) {
53-
$query = 'SELECT 1 FROM t';
54-
$engine->applyLimit($query, -1, null);
55-
}, Nette\InvalidArgumentException::class, 'Negative offset or limit.');
56-
57-
Assert::exception(function () use ($engine) {
58-
$query = 'SELECT 1 FROM t';
59-
$engine->applyLimit($query, null, -1);
60-
}, Nette\NotSupportedException::class, 'Offset is not supported by this database.');
12+
Assert::exception(
13+
fn() => $engine->applyLimit('SELECT 1 FROM t', 10, 20),
14+
Nette\NotSupportedException::class,
15+
'Offset is not supported by this database.',
16+
);
17+
18+
Assert::exception(
19+
fn() => $engine->applyLimit('SELECT 1 FROM t', 0, 20),
20+
Nette\NotSupportedException::class,
21+
'Offset is not supported by this database.',
22+
);
23+
24+
Assert::same(
25+
$engine->applyLimit('SELECT 1 FROM t', 10, 0),
26+
'SELECT TOP 10 1 FROM t',
27+
);
28+
29+
Assert::exception(
30+
fn() => $engine->applyLimit('SELECT 1 FROM t', null, 20),
31+
Nette\NotSupportedException::class,
32+
'Offset is not supported by this database.',
33+
);
34+
35+
Assert::same(
36+
$engine->applyLimit('SELECT 1 FROM t', 10, null),
37+
'SELECT TOP 10 1 FROM t',
38+
);
39+
40+
Assert::same(
41+
$engine->applyLimit(' select distinct 1 FROM t', 10, null),
42+
' select distinct TOP 10 1 FROM t',
43+
);
44+
45+
Assert::same(
46+
$engine->applyLimit('UPDATE t SET', 10, null),
47+
'UPDATE TOP 10 t SET',
48+
);
49+
50+
Assert::same(
51+
$engine->applyLimit('DELETE FROM t SET', 10, null),
52+
'DELETE TOP 10 FROM t SET',
53+
);
54+
55+
Assert::exception(
56+
fn() => $engine->applyLimit('SET FROM t', 10, null),
57+
Nette\InvalidArgumentException::class,
58+
'SQL query must begin with SELECT, UPDATE or DELETE command.',
59+
);
60+
61+
Assert::exception(
62+
fn() => $engine->applyLimit('SELECT 1 FROM t', -1, null),
63+
Nette\InvalidArgumentException::class,
64+
'Negative offset or limit.',
65+
);
66+
67+
Assert::exception(
68+
fn() => $engine->applyLimit('SELECT 1 FROM t', null, -1),
69+
Nette\NotSupportedException::class,
70+
'Offset is not supported by this database.',
71+
);

0 commit comments

Comments
 (0)