Skip to content

Commit

Permalink
Merge pull request #205 from cycle/feat/join-type
Browse files Browse the repository at this point in the history
feat: add the ability to specify the full name of the join type
  • Loading branch information
roxblnfk authored Jun 11, 2024
2 parents b3cc5a3 + 90d78e9 commit 6c5a038
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Driver/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ protected function joins(QueryParameters $params, Quoter $q, array $joins): stri
$statement = '';
foreach ($joins as $join) {
$statement .= sprintf(
"\n%s JOIN %s",
\str_contains($join['type'], 'JOIN') ? "\n%s %s" : "\n%s JOIN %s",
$join['type'],
$this->nameWithAlias($params, $q, $join['outer'], $join['alias'], true)
);
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/CompilerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected function hashSelectQuery(QueryParameters $params, array $tokens): stri
$hash .= $this->hashColumns($params, $tokens['columns']);

foreach ($tokens['join'] as $join) {
$hash .= 'j' . $join['alias'] . $join['type'];
$hash .= 'j' . $join['alias'] . \str_replace(['JOIN', ' '], '', $join['type']);

if ($join['outer'] instanceof SelectQuery) {
$hash .= $join['outer']->getPrefix() === null ? '' : 'p_' . $join['outer']->getPrefix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,28 @@ public function testInnerJoin3(): void
);
}

public function testJoinWithFullTypeName(): void
{
$select = $this->database->select()
->from('temperature as t')
->where('t.date', '2022-01-05')
->join(
type: 'LEFT JOIN LATERAL',
outer: $this->database->select()
->from('humidity')
->where('h.date', '<=', 't.date'),
alias: 'h',
on: new Fragment('true')
);

$this->assertSameQuery(
'SELECT * FROM {temperature} AS {t}
LEFT JOIN LATERAL (SELECT * FROM {humidity} WHERE {h}.{date} <= ?) AS {h}
ON true WHERE {t}.{date} = ?',
$select
);
}

//Join with WHERE

public function testJoinOnWhereParameterOrder(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Cycle\Database\Tests\Functional\Driver\Postgres\Query;

// phpcs:ignore
use Cycle\Database\Injection\Fragment;
use Cycle\Database\Tests\Functional\Driver\Common\Query\SelectWithJoinQueryTest as CommonClass;

/**
Expand All @@ -14,4 +15,33 @@
class SelectWithJoinQueryTest extends CommonClass
{
public const DRIVER = 'postgres';

public function testCacheLeftJoinLateral(): void
{
$compiler = $this->database->select()->getDriver()->getQueryCompiler();

$ref = new \ReflectionProperty($compiler, 'cache');
$ref->setAccessible(true);
$ref->setValue($compiler, []);

$select = $this->database->select()
->from('temperature as t')
->where('t.date', '2022-01-05')
->join(
type: 'LEFT JOIN LATERAL',
outer: $this->database->select()
->from('humidity')
->where('h.date', '<=', 't.date'),
alias: 'h',
on: new Fragment('true')
);

$select->sqlStatement();

// Verify that the join name has a correct format in the cache
$this->assertArrayHasKey(
's__temperature as t*,jhLEFTLATERALp_s__humidity*,wANDh.date<=?_1_1onANDtruewANDt.date=?_1_1',
$ref->getValue($compiler)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,31 @@
class SelectWithJoinQueryTest extends CommonClass
{
public const DRIVER = 'sqlite';

public function testCacheJoinWithFullTypeName(): void
{
$compiler = $this->database->select()->getDriver()->getQueryCompiler();

$ref = new \ReflectionProperty($compiler, 'cache');
$ref->setAccessible(true);
$ref->setValue($compiler, []);

$select = $this->database->select()
->from(['users'])
->join('INNER', 'photos')->on('photos.user_id', 'users.id');

$sql1 = $select->sqlStatement();
$cache1 = $ref->getValue($compiler);

$select = $this->database->select()
->from(['users'])
->join('INNER JOIN', 'photos')->on('photos.user_id', 'users.id');

$sql2 = $select->sqlStatement();
$cache2 = $ref->getValue($compiler);

$this->assertCount(1, $ref->getValue($compiler));
$this->assertSame($sql1, $sql2);
$this->assertSame($cache1, $cache2);
}
}

0 comments on commit 6c5a038

Please sign in to comment.