Skip to content

Commit 86c68f9

Browse files
authored
Merge branch 'main' into feature/valid-null-sql-generation
2 parents 717d117 + 24892ac commit 86c68f9

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/Query/Grammar.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public function compile(CaseBuilder $caseBuilder): string
4040

4141
public function wrapColumn($value): string
4242
{
43+
$values = explode('.', $value);
44+
45+
if (count($values) === 2) {
46+
return '`'.str_replace('`', '``', $values[0]).'`.'.'`'.str_replace('`', '``', $values[1]).'`';
47+
}
48+
4349
return '`'.str_replace('`', '``', $value).'`';
4450
}
4551

tests/CaseBuilderTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public function testCanGenerateComplexQuery()
5252
$this->assertCount(7, $caseQuery->getBindings());
5353
$this->assertEquals('case when `payment_status` = 1 then "Paid" when `payment_status` = 2 then "Due" when `payment_status` <= 5 then "Canceled" else "Unknown" end', $caseQuery->toRaw());
5454
}
55-
56-
public function testCanGenerateComplexQueryWithNullishTypes()
55+
56+
public function testCanGenerateComplexQueryWithNullishTypes()
5757
{
5858
/**
5959
* @var QueryCaseBuilder $caseQuery
@@ -77,6 +77,30 @@ public function testCanGenerateComplexQueryWithNullishTypes()
7777
$this->assertEquals('case when `payment_date` > 0 then "Paid" when `payment_date` = 0 then "Due" when `payment_date` IS NULL then "Canceled" else "Unknown" end', $caseQuery->toRaw());
7878
}
7979

80+
public function testCanGenerateComplexQueryWithDotSeparatedColumns()
81+
{
82+
/**
83+
* @var QueryCaseBuilder $caseQuery
84+
*/
85+
$caseQuery = CaseBuilder::when('Invoices.payment_status', 1)
86+
->then('Paid')
87+
->when('Invoices.payment_status', 2)
88+
->then('Due')
89+
->when('Invoices.payment_status', '<=', 5)
90+
->then('Canceled')
91+
->else('Unknown');
92+
93+
$this->assertCount(3, $caseQuery->whens);
94+
$this->assertCount(3, $caseQuery->thens);
95+
$this->assertSameSize($caseQuery->whens, $caseQuery->thens);
96+
$this->assertNotEmpty($caseQuery->else);
97+
98+
$this->assertEquals('case when `Invoices`.`payment_status` = ? then ? when `Invoices`.`payment_status` = ? then ? when `Invoices`.`payment_status` <= ? then ? else ? end', $caseQuery->toSql());
99+
$this->assertEquals([ 1, "Paid", 2, "Due", 5, "Canceled", "Unknown" ], $caseQuery->getBindings());
100+
$this->assertCount(7, $caseQuery->getBindings());
101+
$this->assertEquals('case when `Invoices`.`payment_status` = 1 then "Paid" when `Invoices`.`payment_status` = 2 then "Due" when `Invoices`.`payment_status` <= 5 then "Canceled" else "Unknown" end', $caseQuery->toRaw());
102+
}
103+
80104
public function testCanUseRawQueries()
81105
{
82106
/**

0 commit comments

Comments
 (0)