Skip to content

Commit 7dad7f6

Browse files
committed
fix: alias with dotted names did not work
1 parent 9ee1a38 commit 7dad7f6

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Language/Alias.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use Illuminate\Contracts\Database\Query\Expression;
88
use Illuminate\Database\Grammar;
9+
use Tpetry\QueryExpressions\Concerns\IdentifiesDriver;
910
use Tpetry\QueryExpressions\Concerns\StringizeExpression;
1011

1112
class Alias implements Expression
1213
{
14+
use IdentifiesDriver;
1315
use StringizeExpression;
1416

1517
public function __construct(
@@ -18,10 +20,14 @@ public function __construct(
1820
) {
1921
}
2022

21-
public function getValue(Grammar $grammar)
23+
public function getValue(Grammar $grammar): string
2224
{
2325
$expression = $this->stringize($grammar, $this->expression);
24-
$name = $grammar->wrap($this->name);
26+
$name = match ($this->identify($grammar)) {
27+
'mysql' => '`'.str_replace('`', '``', $this->name).'`',
28+
'pgsql', 'sqlite' => '"'.str_replace('"', '""', $this->name).'"',
29+
'sqlsrv' => '['.str_replace(']', ']]', $this->name).']',
30+
};
2531

2632
return "{$expression} as {$name}";
2733
}

tests/Language/AliasTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@
1515

1616
it('can alias an expression')
1717
->expect(new Alias(new Expression(1), 'num'))
18-
->toBeExecutable(['val int'])
18+
->toBeExecutable()
1919
->toBeMysql('1 as `num`')
2020
->toBePgsql('1 as "num"')
2121
->toBeSqlite('1 as "num"')
2222
->toBeSqlsrv('1 as [num]');
23+
24+
it('can alias dotted names')
25+
->expect(new Alias(new Expression(1), 'dotted.dotted'))
26+
->toBeExecutable()
27+
->toBeMysql('1 as `dotted.dotted`')
28+
->toBePgsql('1 as "dotted.dotted"')
29+
->toBeSqlite('1 as "dotted.dotted"')
30+
->toBeSqlsrv('1 as [dotted.dotted]');

0 commit comments

Comments
 (0)