Skip to content

Commit 5fb657d

Browse files
committed
Engine::delimite() -> delimit() (BC break)
1 parent a53cfc0 commit 5fb657d

14 files changed

+57
-57
lines changed

src/Database/Drivers/Engine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function convertToPhp(mixed $value, array $meta, TypeConverter $converter): mixe
4040
/********************* SQL utilities ****************d*g**/
4141

4242
/** Adds delimiters around database identifier. */
43-
function delimite(string $name): string;
43+
function delimit(string $name): string;
4444

4545
/** Formats a date-time value for use in an SQL statement. */
4646
function formatDateTime(\DateTimeInterface $value): string;

src/Database/Drivers/Engines/MSSQLEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function classifyException(Nette\Database\DriverException $e): ?string
4141
/********************* SQL ****************d*g**/
4242

4343

44-
public function delimite(string $name): string
44+
public function delimit(string $name): string
4545
{
4646
// @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
4747
return '[' . str_replace(['[', ']'], ['[[', ']]'], $name) . ']';

src/Database/Drivers/Engines/MySQLEngine.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function classifyException(Nette\Database\DriverException $e): ?string
5252
/********************* SQL ****************d*g**/
5353

5454

55-
public function delimite(string $name): string
55+
public function delimit(string $name): string
5656
{
5757
// @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
5858
return '`' . str_replace('`', '``', $name) . '`';
@@ -114,7 +114,7 @@ public function getTables(): array
114114
public function getColumns(string $table): array
115115
{
116116
$columns = [];
117-
$rows = $this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table));
117+
$rows = $this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimit($table));
118118
while ($row = $rows->fetch()) {
119119
$row = array_change_key_case($row);
120120
$typeInfo = Nette\Database\Helpers::parseColumnType($row['type']);
@@ -140,7 +140,7 @@ public function getColumns(string $table): array
140140
public function getIndexes(string $table): array
141141
{
142142
$indexes = [];
143-
$rows = $this->connection->query('SHOW INDEX FROM ' . $this->delimite($table));
143+
$rows = $this->connection->query('SHOW INDEX FROM ' . $this->delimit($table));
144144
while ($row = $rows->fetch()) {
145145
$id = $row['Key_name'];
146146
$indexes[$id]['name'] = $id;

src/Database/Drivers/Engines/ODBCEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function classifyException(Nette\Database\DriverException $e): ?string
3434
/********************* SQL ****************d*g**/
3535

3636

37-
public function delimite(string $name): string
37+
public function delimit(string $name): string
3838
{
3939
return '[' . str_replace(['[', ']'], ['[[', ']]'], $name) . ']';
4040
}

src/Database/Drivers/Engines/OracleEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function classifyException(Nette\Database\DriverException $e): ?string
5050
/********************* SQL ****************d*g**/
5151

5252

53-
public function delimite(string $name): string
53+
public function delimit(string $name): string
5454
{
5555
// @see http://download.oracle.com/docs/cd/B10500_01/server.920/a96540/sql_elements9a.htm
5656
return '"' . str_replace('"', '""', $name) . '"';

src/Database/Drivers/Engines/PostgreSQLEngine.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function classifyException(Nette\Database\DriverException $e): ?string
4848
/********************* SQL ****************d*g**/
4949

5050

51-
public function delimite(string $name): string
51+
public function delimit(string $name): string
5252
{
5353
// @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
5454
return '"' . str_replace('"', '""', $name) . '"';
@@ -155,7 +155,7 @@ public function getColumns(string $table): array
155155
AND NOT a.attisdropped
156156
ORDER BY
157157
a.attnum
158-
X, [$this->delimiteFQN($table)]);
158+
X, [$this->delimitFQN($table)]);
159159

160160
while ($row = $rows->fetch()) {
161161
$column = $row;
@@ -186,7 +186,7 @@ public function getIndexes(string $table): array
186186
WHERE
187187
c1.relkind IN ('r', 'p')
188188
AND c1.oid = ?::regclass
189-
X, [$this->delimiteFQN($table)]);
189+
X, [$this->delimitFQN($table)]);
190190

191191
while ($row = $rows->fetch()) {
192192
$id = $row['name'];
@@ -221,7 +221,7 @@ public function getForeignKeys(string $table): array
221221
co.contype = 'f'
222222
AND cl.oid = ?::regclass
223223
AND nf.nspname = ANY (pg_catalog.current_schemas(FALSE))
224-
X, [$this->delimiteFQN($table)]);
224+
X, [$this->delimitFQN($table)]);
225225

226226
while ($row = $rows->fetch()) {
227227
$id = $row['name'];
@@ -246,8 +246,8 @@ public function convertToPhp(mixed $value, array $meta, TypeConverter $converter
246246
/**
247247
* Converts: schema.name => "schema"."name"
248248
*/
249-
private function delimiteFQN(string $name): string
249+
private function delimitFQN(string $name): string
250250
{
251-
return implode('.', array_map([$this, 'delimite'], explode('.', $name)));
251+
return implode('.', array_map([$this, 'delimit'], explode('.', $name)));
252252
}
253253
}

src/Database/Drivers/Engines/SQLServerEngine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function classifyException(Nette\Database\DriverException $e): ?string
4141
/********************* SQL ****************d*g**/
4242

4343

44-
public function delimite(string $name): string
44+
public function delimit(string $name): string
4545
{
4646
/** @see https://msdn.microsoft.com/en-us/library/ms176027.aspx */
4747
return '[' . str_replace(']', ']]', $name) . ']';

src/Database/Drivers/Engines/SQLiteEngine.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function classifyException(Nette\Database\DriverException $e): ?string
7070
/********************* SQL ****************d*g**/
7171

7272

73-
public function delimite(string $name): string
73+
public function delimit(string $name): string
7474
{
7575
return '[' . strtr($name, '[]', ' ') . ']';
7676
}
@@ -144,7 +144,7 @@ public function getColumns(string $table): array
144144
X, [$table, $table])->fetch();
145145

146146
$columns = [];
147-
$rows = $this->connection->query("PRAGMA table_info({$this->delimite($table)})");
147+
$rows = $this->connection->query("PRAGMA table_info({$this->delimit($table)})");
148148
while ($row = $rows->fetch()) {
149149
$column = $row['name'];
150150
$pattern = "/(\"$column\"|`$column`|\\[$column\\]|$column)\\s+[^,]+\\s+PRIMARY\\s+KEY\\s+AUTOINCREMENT/Ui";
@@ -171,7 +171,7 @@ public function getColumns(string $table): array
171171
public function getIndexes(string $table): array
172172
{
173173
$indexes = [];
174-
$rows = $this->connection->query("PRAGMA index_list({$this->delimite($table)})");
174+
$rows = $this->connection->query("PRAGMA index_list({$this->delimit($table)})");
175175
while ($row = $rows->fetch()) {
176176
$id = $row['name'];
177177
$indexes[$id]['name'] = $id;
@@ -180,7 +180,7 @@ public function getIndexes(string $table): array
180180
}
181181

182182
foreach ($indexes as $index => $values) {
183-
$res = $this->connection->query("PRAGMA index_info({$this->delimite($index)})");
183+
$res = $this->connection->query("PRAGMA index_info({$this->delimit($index)})");
184184
while ($row = $res->fetch()) {
185185
$indexes[$index]['columns'][] = $row['name'];
186186
}
@@ -218,7 +218,7 @@ public function getIndexes(string $table): array
218218
public function getForeignKeys(string $table): array
219219
{
220220
$keys = [];
221-
$rows = $this->connection->query("PRAGMA foreign_key_list({$this->delimite($table)})");
221+
$rows = $this->connection->query("PRAGMA foreign_key_list({$this->delimit($table)})");
222222
while ($row = $rows->fetch()) {
223223
$id = $row['id'];
224224
$keys[$id]['name'] = $id;

src/Database/SqlPreprocessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,6 @@ private function formatLiteral(SqlLiteral $value): string
351351
*/
352352
private function delimit(string $name): string
353353
{
354-
return implode('.', array_map($this->engine->delimite(...), explode('.', $name)));
354+
return implode('.', array_map($this->engine->delimit(...), explode('.', $name)));
355355
}
356356
}

src/Database/Table/Selection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ public function insert(iterable $data): ActiveRow|array|int|bool
810810

811811
// First check sequence
812812
if (!empty($primarySequenceName) && $primaryAutoincrementKey) {
813-
$primaryKey[$primaryAutoincrementKey] = $this->explorer->getInsertId($this->explorer->getDatabaseEngine()->delimite($primarySequenceName));
813+
$primaryKey[$primaryAutoincrementKey] = $this->explorer->getInsertId($this->explorer->getDatabaseEngine()->delimit($primarySequenceName));
814814

815815
// Autoincrement primary without sequence
816816
} elseif ($primaryAutoincrementKey) {

src/Database/Table/SqlBuilder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function __construct(string $tableName, Explorer $explorer)
5959
$this->conventions = $explorer->getConventions();
6060
$this->structure = $explorer->getStructure();
6161
$tableNameParts = explode('.', $tableName);
62-
$this->delimitedTable = implode('.', array_map($this->engine->delimite(...), $tableNameParts));
62+
$this->delimitedTable = implode('.', array_map($this->engine->delimit(...), $tableNameParts));
6363
$this->checkUniqueTableName(end($tableNameParts), $tableName);
6464
}
6565

@@ -78,7 +78,7 @@ public function buildInsertQuery(): string
7878

7979
public function buildUpdateQuery(): string
8080
{
81-
$query = "UPDATE {$this->delimitedTable} SET ?set" . $this->tryDelimite($this->buildConditions());
81+
$query = "UPDATE {$this->delimitedTable} SET ?set" . $this->tryDelimit($this->buildConditions());
8282

8383
if ($this->order !== []) {
8484
$query .= ' ORDER BY ' . implode(', ', $this->order);
@@ -94,7 +94,7 @@ public function buildUpdateQuery(): string
9494

9595
public function buildDeleteQuery(): string
9696
{
97-
$query = "DELETE FROM {$this->delimitedTable}" . $this->tryDelimite($this->buildConditions());
97+
$query = "DELETE FROM {$this->delimitedTable}" . $this->tryDelimit($this->buildConditions());
9898
if ($this->limit !== null || $this->offset) {
9999
$query = $this->engine->applyLimit($query, $this->limit, $this->offset);
100100
}
@@ -185,7 +185,7 @@ public function buildSelectQuery(?array $columns = null): string
185185

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

188-
return $this->tryDelimite($query);
188+
return $this->tryDelimit($query);
189189
}
190190

191191

@@ -814,13 +814,13 @@ protected function buildQueryEnd(): string
814814
}
815815

816816

817-
protected function tryDelimite(string $s): string
817+
protected function tryDelimit(string $s): string
818818
{
819819
return preg_replace_callback(
820820
'#(?<=[^\w`"\[?:]|^)[a-z_][a-z0-9_]*(?=[^\w`"(\]]|$)#Di',
821821
fn(array $m): string => strtoupper($m[0]) === $m[0]
822822
? $m[0]
823-
: $this->engine->delimite($m[0]),
823+
: $this->engine->delimit($m[0]),
824824
$s,
825825
);
826826
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Database\Table: tryDelimit.
5+
* @dataProvider? ../databases.ini
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/../../bootstrap.php';
13+
14+
$explorer = connectToDB();
15+
16+
$sqlBuilder = new Nette\Database\Table\SqlBuilder('book', $explorer);
17+
$tryDelimit = (new ReflectionClass($sqlBuilder))->getMethod('tryDelimit');
18+
$tryDelimit->setAccessible(true);
19+
20+
Assert::same(reformat('[hello]'), $tryDelimit->invoke($sqlBuilder, 'hello'));
21+
Assert::same(reformat(' [hello] '), $tryDelimit->invoke($sqlBuilder, ' hello '));
22+
Assert::same(reformat('HELLO'), $tryDelimit->invoke($sqlBuilder, 'HELLO'));
23+
Assert::same(reformat('[HellO]'), $tryDelimit->invoke($sqlBuilder, 'HellO'));
24+
Assert::same(reformat('[hello].[world]'), $tryDelimit->invoke($sqlBuilder, 'hello.world'));
25+
Assert::same(reformat('[hello] [world]'), $tryDelimit->invoke($sqlBuilder, 'hello world'));
26+
Assert::same(reformat('HELLO([world])'), $tryDelimit->invoke($sqlBuilder, 'HELLO(world)'));
27+
Assert::same(reformat('hello([world])'), $tryDelimit->invoke($sqlBuilder, 'hello(world)'));
28+
Assert::same('[hello]', $tryDelimit->invoke($sqlBuilder, '[hello]'));
29+
Assert::same(reformat('::int'), $tryDelimit->invoke($sqlBuilder, '::int'));

tests/Database/Explorer/SqlBuilder.tryDelimite().phpt

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/Database/Row.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require __DIR__ . '/../bootstrap.php';
1414
$connection = connectToDB();
1515

1616
test('numeric field', function () use ($connection) {
17-
$row = $connection->fetch("SELECT 123 AS {$connection->getDatabaseEngine()->delimite('123')}, NULL as nullcol");
17+
$row = $connection->fetch("SELECT 123 AS {$connection->getDatabaseEngine()->delimit('123')}, NULL as nullcol");
1818
Assert::same(123, $row->{123});
1919
Assert::same(123, $row->{'123'});
2020
Assert::true(isset($row->{123}));

0 commit comments

Comments
 (0)