Skip to content

Commit fc5c8b0

Browse files
committed
address reviews
1 parent 35884ec commit fc5c8b0

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎system/Database/BaseConnection.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $pro
13151315
// In some cases, especially 'from', we end up running through
13161316
// protect_identifiers twice. This algorithm won't work when
13171317
// it contains the escapeChar so strip it out.
1318-
$item = trim($item, $this->escapeChar);
1318+
$item = $this->trimEscapeChar($item);
13191319

13201320
// Is there a table prefix? If not, no need to insert it
13211321
if ($this->DBPrefix !== '') {
@@ -1336,9 +1336,15 @@ public function protectIdentifiers($item, bool $prefixSingle = false, ?bool $pro
13361336
return $item . $alias;
13371337
}
13381338

1339+
private function trimEscapeChar(string $item): string
1340+
{
1341+
return trim($item, is_array($this->escapeChar) ? implode('', $this->escapeChar) : $this->escapeChar);
1342+
}
1343+
13391344
private function protectDotItem(string $item, string $alias, bool $protectIdentifiers, bool $fieldExists): string
13401345
{
1341-
$parts = array_map(fn (string $part): string => trim($part, $this->escapeChar), explode('.', $item));
1346+
$parts = array_map($this->trimEscapeChar(...), explode('.', $item));
1347+
$item = implode('.', $parts);
13421348

13431349
// Does the first segment of the exploded item match
13441350
// one of the aliases previously identified? If so,

‎tests/system/Database/BaseConnectionTest.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,27 @@ public static function provideProtectIdentifiers(): iterable
439439
];
440440
}
441441

442+
public function testProtectIdentifiersWithBracketEscapeChar(): void
443+
{
444+
$db = new MockConnection($this->options);
445+
$db->escapeChar = ['[', ']'];
446+
447+
$this->assertSame('[test_jobs]', $db->protectIdentifiers('jobs', true, true, false));
448+
$this->assertSame('[test_jobs]', $db->protectIdentifiers('[test_jobs]', true, true, false));
449+
$this->assertSame('test_jobs', $db->protectIdentifiers('[test_jobs]', true, false, false));
450+
$this->assertSame('[test_jobs].[id]', $db->protectIdentifiers('jobs.id'));
451+
$this->assertSame('[test_jobs].[id]', $db->protectIdentifiers('[test_jobs].[id]'));
452+
$this->assertSame('test_jobs.id', $db->protectIdentifiers('[test_jobs].[id]', true, false));
453+
}
454+
455+
public function testProtectIdentifiersStripsQuotesFromDottedItemWithoutPrefix(): void
456+
{
457+
$db = new MockConnection([...$this->options, 'DBPrefix' => '']);
458+
459+
$this->assertSame('public.jobs', $db->protectIdentifiers('"public"."jobs"', true, false, false));
460+
$this->assertSame('"public"."jobs"', $db->protectIdentifiers('"public"."jobs"', true, true, false));
461+
}
462+
442463
/**
443464
* These tests are intended to confirm the current behavior.
444465
*/

0 commit comments

Comments
 (0)