NEW Add abstraction for sql RIGHT JOIN (#10954)

This commit is contained in:
Guy Sartorelli 2023-09-25 12:32:26 +13:00 committed by GitHub
parent b28749db44
commit bbc0295f86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 52 deletions

View File

@ -832,6 +832,26 @@ class DataQuery
return $this; return $this;
} }
/**
* Add a RIGHT JOIN clause to this query.
*
* @param string $table The unquoted table to join to.
* @param string $onClause The filter for the join (escaped SQL statement).
* @param string $alias An optional alias name (unquoted)
* @param int $order A numerical index to control the order that joins are added to the query; lower order values
* will cause the query to appear first. The default is 20, and joins created automatically by the
* ORM have a value of 10.
* @param array $parameters Any additional parameters if the join is a parameterised subquery
* @return $this
*/
public function rightJoin($table, $onClause, $alias = null, $order = 20, $parameters = [])
{
if ($table) {
$this->query->addRightJoin($table, $onClause, $alias, $order, $parameters);
}
return $this;
}
/** /**
* Prefix of all joined table aliases. E.g. ->filter('Banner.Image.Title)' * Prefix of all joined table aliases. E.g. ->filter('Banner.Image.Title)'
* Will join the Banner, and then Image relations * Will join the Banner, and then Image relations

View File

@ -135,17 +135,25 @@ abstract class SQLConditionalExpression extends SQLExpression
*/ */
public function addLeftJoin($table, $onPredicate, $tableAlias = '', $order = 20, $parameters = []) public function addLeftJoin($table, $onPredicate, $tableAlias = '', $order = 20, $parameters = [])
{ {
if (!$tableAlias) { return $this->addJoin($table, 'LEFT', $onPredicate, $tableAlias, $order, $parameters);
$tableAlias = $table; }
}
$this->from[$tableAlias] = [ /**
'type' => 'LEFT', * Add a RIGHT JOIN criteria to the tables list.
'table' => $table, *
'filter' => [$onPredicate], * @param string $table Unquoted table name
'order' => $order, * @param string $onPredicate The "ON" SQL fragment in a "RIGHT JOIN ... AS ... ON ..." statement, Needs to be valid
'parameters' => $parameters * (quoted) SQL.
]; * @param string $tableAlias Optional alias which makes it easier to identify and replace joins later on
return $this; * @param int $order A numerical index to control the order that joins are added to the query; lower order values
* will cause the query to appear first. The default is 20, and joins created automatically by the
* ORM have a value of 10.
* @param array $parameters Any additional parameters if the join is a parameterized subquery
* @return $this Self reference
*/
public function addRightJoin($table, $onPredicate, $tableAlias = '', $order = 20, $parameters = [])
{
return $this->addJoin($table, 'RIGHT', $onPredicate, $tableAlias, $order, $parameters);
} }
/** /**
@ -162,12 +170,20 @@ abstract class SQLConditionalExpression extends SQLExpression
* @return $this Self reference * @return $this Self reference
*/ */
public function addInnerJoin($table, $onPredicate, $tableAlias = null, $order = 20, $parameters = []) public function addInnerJoin($table, $onPredicate, $tableAlias = null, $order = 20, $parameters = [])
{
return $this->addJoin($table, 'INNER', $onPredicate, $tableAlias, $order, $parameters);
}
/**
* Add a JOIN criteria
*/
private function addJoin($table, $type, $onPredicate, $tableAlias = null, $order = 20, $parameters = []): static
{ {
if (!$tableAlias) { if (!$tableAlias) {
$tableAlias = $table; $tableAlias = $table;
} }
$this->from[$tableAlias] = [ $this->from[$tableAlias] = [
'type' => 'INNER', 'type' => $type,
'table' => $table, 'table' => $table,
'filter' => [$onPredicate], 'filter' => [$onPredicate],
'order' => $order, 'order' => $order,

View File

@ -51,22 +51,33 @@ class DataQueryTest extends SapphireTest
$this->assertEquals('Foo', $result['Title']); $this->assertEquals('Foo', $result['Title']);
} }
public function provideJoins()
{
return [
[
'joinMethod' => 'innerJoin',
'joinType' => 'INNER',
],
[
'joinMethod' => 'leftJoin',
'joinType' => 'LEFT',
],
[
'joinMethod' => 'rightJoin',
'joinType' => 'RIGHT',
],
];
}
/** /**
* Test the leftJoin() and innerJoin method of the DataQuery object * @dataProvider provideJoins
*/ */
public function testJoins() public function testJoins($joinMethod, $joinType)
{ {
$dq = new DataQuery(Member::class); $dq = new DataQuery(Member::class);
$dq->innerJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\""); $dq->$joinMethod("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
$this->assertSQLContains( $this->assertSQLContains(
"INNER JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"", "$joinType JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"",
$dq->sql($parameters)
);
$dq = new DataQuery(Member::class);
$dq->leftJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
$this->assertSQLContains(
"LEFT JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"",
$dq->sql($parameters) $dq->sql($parameters)
); );
} }

View File

@ -496,16 +496,18 @@ class SQLSelectTest extends SapphireTest
); );
} }
public function testInnerJoin() public function testJoinSQL()
{ {
$query = new SQLSelect(); $query = new SQLSelect();
$query->setFrom('MyTable'); $query->setFrom('MyTable');
$query->addInnerJoin('MyOtherTable', 'MyOtherTable.ID = 2'); $query->addInnerJoin('MyOtherTable', 'MyOtherTable.ID = 2');
$query->addRightJoin('MySecondTable', 'MyOtherTable.ID = MySecondTable.ID');
$query->addLeftJoin('MyLastTable', 'MyOtherTable.ID = MyLastTable.ID'); $query->addLeftJoin('MyLastTable', 'MyOtherTable.ID = MyLastTable.ID');
$this->assertSQLEquals( $this->assertSQLEquals(
'SELECT * FROM MyTable ' . 'SELECT * FROM MyTable ' .
'INNER JOIN "MyOtherTable" ON MyOtherTable.ID = 2 ' . 'INNER JOIN "MyOtherTable" ON MyOtherTable.ID = 2 ' .
'RIGHT JOIN "MySecondTable" ON MyOtherTable.ID = MySecondTable.ID ' .
'LEFT JOIN "MyLastTable" ON MyOtherTable.ID = MyLastTable.ID', 'LEFT JOIN "MyLastTable" ON MyOtherTable.ID = MyLastTable.ID',
$query->sql($parameters) $query->sql($parameters)
); );
@ -513,12 +515,14 @@ class SQLSelectTest extends SapphireTest
$query = new SQLSelect(); $query = new SQLSelect();
$query->setFrom('MyTable'); $query->setFrom('MyTable');
$query->addInnerJoin('MyOtherTable', 'MyOtherTable.ID = 2', 'table1'); $query->addInnerJoin('MyOtherTable', 'MyOtherTable.ID = 2', 'table1');
$query->addLeftJoin('MyLastTable', 'MyOtherTable.ID = MyLastTable.ID', 'table2'); $query->addRightJoin('MySecondTable', 'MyOtherTable.ID = MySecondTable.ID', 'table2');
$query->addLeftJoin('MyLastTable', 'MyOtherTable.ID = MyLastTable.ID', 'table3');
$this->assertSQLEquals( $this->assertSQLEquals(
'SELECT * FROM MyTable ' . 'SELECT * FROM MyTable ' .
'INNER JOIN "MyOtherTable" AS "table1" ON MyOtherTable.ID = 2 ' . 'INNER JOIN "MyOtherTable" AS "table1" ON MyOtherTable.ID = 2 ' .
'LEFT JOIN "MyLastTable" AS "table2" ON MyOtherTable.ID = MyLastTable.ID', 'RIGHT JOIN "MySecondTable" AS "table2" ON MyOtherTable.ID = MySecondTable.ID ' .
'LEFT JOIN "MyLastTable" AS "table3" ON MyOtherTable.ID = MyLastTable.ID',
$query->sql($parameters) $query->sql($parameters)
); );
} }
@ -807,38 +811,33 @@ class SQLSelectTest extends SapphireTest
$this->assertEquals(10, $limit['start']); $this->assertEquals(10, $limit['start']);
} }
public function testParameterisedInnerJoins() public function provideParameterisedJoinSQL()
{ {
$query = new SQLSelect(); return [
$query->setSelect(['"SQLSelectTest_DO"."Name"', '"SubSelect"."Count"']); [
$query->setFrom('"SQLSelectTest_DO"'); 'joinMethod' => 'addInnerJoin',
$query->addInnerJoin( 'joinType' => 'INNER',
'(SELECT "Title", COUNT(*) AS "Count" FROM "SQLSelectTestBase" GROUP BY "Title" HAVING "Title" NOT LIKE ?)', ],
'"SQLSelectTest_DO"."Name" = "SubSelect"."Title"', [
'SubSelect', 'joinMethod' => 'addLeftJoin',
20, 'joinType' => 'LEFT',
['%MyName%'] ],
); [
$query->addWhere(['"SQLSelectTest_DO"."Date" > ?' => '2012-08-08 12:00']); 'joinMethod' => 'addRightJoin',
'joinType' => 'RIGHT',
$this->assertSQLEquals( ],
'SELECT "SQLSelectTest_DO"."Name", "SubSelect"."Count" ];
FROM "SQLSelectTest_DO" INNER JOIN (SELECT "Title", COUNT(*) AS "Count" FROM "SQLSelectTestBase"
GROUP BY "Title" HAVING "Title" NOT LIKE ?) AS "SubSelect" ON "SQLSelectTest_DO"."Name" =
"SubSelect"."Title"
WHERE ("SQLSelectTest_DO"."Date" > ?)',
$query->sql($parameters)
);
$this->assertEquals(['%MyName%', '2012-08-08 12:00'], $parameters);
$query->execute();
} }
public function testParameterisedLeftJoins() /**
* @dataProvider provideParameterisedJoinSQL
*/
public function testParameterisedJoinSQL($joinMethod, $joinType)
{ {
$query = new SQLSelect(); $query = new SQLSelect();
$query->setSelect(['"SQLSelectTest_DO"."Name"', '"SubSelect"."Count"']); $query->setSelect(['"SQLSelectTest_DO"."Name"', '"SubSelect"."Count"']);
$query->setFrom('"SQLSelectTest_DO"'); $query->setFrom('"SQLSelectTest_DO"');
$query->addLeftJoin( $query->$joinMethod(
'(SELECT "Title", COUNT(*) AS "Count" FROM "SQLSelectTestBase" GROUP BY "Title" HAVING "Title" NOT LIKE ?)', '(SELECT "Title", COUNT(*) AS "Count" FROM "SQLSelectTestBase" GROUP BY "Title" HAVING "Title" NOT LIKE ?)',
'"SQLSelectTest_DO"."Name" = "SubSelect"."Title"', '"SQLSelectTest_DO"."Name" = "SubSelect"."Title"',
'SubSelect', 'SubSelect',
@ -849,7 +848,7 @@ class SQLSelectTest extends SapphireTest
$this->assertSQLEquals( $this->assertSQLEquals(
'SELECT "SQLSelectTest_DO"."Name", "SubSelect"."Count" 'SELECT "SQLSelectTest_DO"."Name", "SubSelect"."Count"
FROM "SQLSelectTest_DO" LEFT JOIN (SELECT "Title", COUNT(*) AS "Count" FROM "SQLSelectTestBase" FROM "SQLSelectTest_DO" ' . $joinType . ' JOIN (SELECT "Title", COUNT(*) AS "Count" FROM "SQLSelectTestBase"
GROUP BY "Title" HAVING "Title" NOT LIKE ?) AS "SubSelect" ON "SQLSelectTest_DO"."Name" = GROUP BY "Title" HAVING "Title" NOT LIKE ?) AS "SubSelect" ON "SQLSelectTest_DO"."Name" =
"SubSelect"."Title" "SubSelect"."Title"
WHERE ("SQLSelectTest_DO"."Date" > ?)', WHERE ("SQLSelectTest_DO"."Date" > ?)',