Skip to content

Commit 7056a19

Browse files
committed
Engine::applyLimit() returns string (BC break)
1 parent 8a7c1b9 commit 7056a19

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
@@ -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 ($offset) {
6767
throw new Nette\NotSupportedException('Offset is not supported by this database.');
@@ -75,6 +75,8 @@ public function applyLimit(string &$sql, ?int $limit, ?int $offset): void
7575
throw new Nette\InvalidArgumentException('SQL query must begin with SELECT, UPDATE or DELETE command.');
7676
}
7777
}
78+
79+
return $sql;
7880
}
7981

8082

src/Database/Drivers/Engines/MySQLEngine.php

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

7474

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

8789

src/Database/Drivers/Engines/ODBCEngine.php

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

5555

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

7274

src/Database/Drivers/Engines/OracleEngine.php

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

7272

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

8991

src/Database/Drivers/Engines/PostgreSQLEngine.php

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

7070

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

8688

src/Database/Drivers/Engines/SQLServerEngine.php

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

6464

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

7779

src/Database/Drivers/Engines/SQLiteEngine.php

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

9191

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

103105

src/Database/Table/SqlBuilder.php

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

8989
if ($this->limit !== null || $this->offset) {
90-
$this->engine->applyLimit($query, $this->limit, $this->offset);
90+
$query = $this->engine->applyLimit($query, $this->limit, $this->offset);
9191
}
9292

9393
return $query;
@@ -98,7 +98,7 @@ public function buildDeleteQuery(): string
9898
{
9999
$query = "DELETE FROM {$this->delimitedTable}" . $this->tryDelimite($this->buildConditions());
100100
if ($this->limit !== null || $this->offset) {
101-
$this->engine->applyLimit($query, $this->limit, $this->offset);
101+
$query = $this->engine->applyLimit($query, $this->limit, $this->offset);
102102
}
103103

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

188-
$this->engine->applyLimit($query, $this->limit, $this->offset);
188+
$query = $this->engine->applyLimit($query, $this->limit, $this->offset);
189189

190190
return $this->tryDelimite($query);
191191
}

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)