From 185b3b2305a3f61f11ad92b11643c8ee39dc75e7 Mon Sep 17 00:00:00 2001 From: Vitalii Date: Tue, 20 Jul 2021 01:51:45 +0300 Subject: [PATCH] Support join on expressions (#47) --- src/Query/JoinClause.php | 14 +++++++------- tests/BuilderTest.php | 6 ++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Query/JoinClause.php b/src/Query/JoinClause.php index 95c040a..d34c409 100644 --- a/src/Query/JoinClause.php +++ b/src/Query/JoinClause.php @@ -127,19 +127,19 @@ public function using(...$columns): self /** * Set "on" clause for join. * - * @param string $first - * @param string $operator - * @param string $second - * @param string $concatOperator + * @param string|Expression $first + * @param string $operator + * @param string|Expression $second + * @param string $concatOperator * * @return JoinClause */ - public function on(string $first, string $operator, string $second, string $concatOperator = Operator::AND): self + public function on($first, string $operator, $second, string $concatOperator = Operator::AND): self { $expression = (new TwoElementsLogicExpression($this->query)) - ->firstElement(new Identifier($first)) + ->firstElement(is_string($first) ? new Identifier($first) : $first) ->operator($operator) - ->secondElement(new Identifier($second)) + ->secondElement(is_string($second) ? new Identifier($second) : $second) ->concatOperator($concatOperator); $this->onClauses[] = $expression; diff --git a/tests/BuilderTest.php b/tests/BuilderTest.php index 63332e9..9458b99 100644 --- a/tests/BuilderTest.php +++ b/tests/BuilderTest.php @@ -1175,6 +1175,12 @@ public function testJoinWithOnClause() $join->table('table2')->on('column_from_table_1', '=', 'column_from_table_2'); }); $this->assertEquals('SELECT * FROM `table1` ANY LEFT JOIN `table2` ON `column_from_table_1` = `column_from_table_2`', $builder->toSql()); + + $builder = $this->getBuilder(); + $builder->from('table1')->anyLeftJoin(function (JoinClause $join) { + $join->table('table2')->on('column_from_table_1', '=', raw('toUInt32(`column_from_table_2`)')); + }); + $this->assertEquals('SELECT * FROM `table1` ANY LEFT JOIN `table2` ON `column_from_table_1` = toUInt32(`column_from_table_2`)', $builder->toSql()); } public function testMultipleJoins()