Skip to content

Commit 371299e

Browse files
committed
feat: add escapeIdentifier() method
1 parent 39ada81 commit 371299e

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎system/Database/BaseConnection.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,24 @@ private function protectDotItem(string $item, string $alias, bool $protectIdenti
11881188
return $item . $alias;
11891189
}
11901190

1191+
/**
1192+
* Escape the SQL Identifier
1193+
*
1194+
* This function escapes single identifier.
1195+
*
1196+
* @param non-empty-string $item
1197+
*/
1198+
public function escapeIdentifier(string $item): string
1199+
{
1200+
return $this->escapeChar
1201+
. str_replace(
1202+
$this->escapeChar,
1203+
$this->escapeChar . $this->escapeChar,
1204+
$item
1205+
)
1206+
. $this->escapeChar;
1207+
}
1208+
11911209
/**
11921210
* Escape the SQL Identifiers
11931211
*

‎tests/system/Database/BaseConnectionTest.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,25 @@ public static function provideEscapeIdentifiers(): iterable
306306
'with dots' => ['com.sitedb.web', '"com"."sitedb"."web"'],
307307
];
308308
}
309+
310+
/**
311+
* @dataProvider provideEscapeIdentifier
312+
*/
313+
public function testEscapeIdentifier(string $item, string $expected): void
314+
{
315+
$db = new MockConnection($this->options);
316+
317+
$return = $db->escapeIdentifier($item);
318+
319+
$this->assertSame($expected, $return);
320+
}
321+
322+
public static function provideEscapeIdentifier(): iterable
323+
{
324+
yield from [
325+
// $item, $expected
326+
'simple' => ['test', '"test"'],
327+
'with dots' => ['com.sitedb.web', '"com.sitedb.web"'],
328+
];
329+
}
309330
}

0 commit comments

Comments
 (0)