Skip to content

Commit

Permalink
Support join on expressions (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalcrazz authored Jul 19, 2021
1 parent 067b2a9 commit 185b3b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Query/JoinClause.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 185b3b2

Please sign in to comment.