NEW Add rightJoin method to DataList (#10961)

This commit is contained in:
Guy Sartorelli 2023-09-27 15:24:32 +13:00 committed by GitHub
parent 55e42683f8
commit 7d5c62ed5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 67 deletions

View File

@ -806,6 +806,25 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
}); });
} }
/**
* Return a new DataList instance with a right join clause added to this list's query.
*
* @param string $table Table name (unquoted and as escaped SQL)
* @param string $onClause Escaped SQL statement, e.g. '"Table1"."ID" = "Table2"."ID"'
* @param string $alias - if you want this table to be aliased under another name
* @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 static
*/
public function rightJoin($table, $onClause, $alias = null, $order = 20, $parameters = [])
{
return $this->alterDataQuery(function (DataQuery $query) use ($table, $onClause, $alias, $order, $parameters) {
$query->rightJoin($table, $onClause, $alias, $order, $parameters);
});
}
/** /**
* Return an array of the actual items that this DataList contains at this stage. * Return an array of the actual items that this DataList contains at this stage.
* This is when the query is actually executed. * This is when the query is actually executed.

View File

@ -304,12 +304,33 @@ class DataListTest extends SapphireTest
$this->assertSQLEquals($expected, $list->sql($parameters)); $this->assertSQLEquals($expected, $list->sql($parameters));
} }
public function testInnerJoin() public function provideJoin()
{
return [
[
'joinMethod' => 'innerJoin',
'joinType' => 'INNER JOIN',
],
[
'joinMethod' => 'leftJoin',
'joinType' => 'LEFT JOIN',
],
[
'joinMethod' => 'rightJoin',
'joinType' => 'RIGHT JOIN',
],
];
}
/**
* @dataProvider provideJoin
*/
public function testJoin(string $joinMethod, string $joinType)
{ {
$db = DB::get_conn(); $db = DB::get_conn();
$list = TeamComment::get(); $list = TeamComment::get();
$list = $list->innerJoin( $list = $list->$joinMethod(
'DataObjectTest_Team', 'DataObjectTest_Team',
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"', '"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"',
'Team' 'Team'
@ -322,22 +343,24 @@ class DataListTest extends SapphireTest
. 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL' . 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL'
. ' THEN "DataObjectTest_TeamComment"."ClassName" ELSE ' . ' THEN "DataObjectTest_TeamComment"."ClassName" ELSE '
. $db->quoteString(DataObjectTest\TeamComment::class) . $db->quoteString(DataObjectTest\TeamComment::class)
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" INNER JOIN ' . ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" ' . $joinType
. '"DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = ' . ' "DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = '
. '"DataObjectTest_TeamComment"."TeamID"' . '"DataObjectTest_TeamComment"."TeamID"'
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC'; . ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
$this->assertSQLEquals($expected, $list->sql($parameters)); $this->assertSQLEquals($expected, $list->sql($parameters));
$this->assertEmpty($parameters); $this->assertEmpty($parameters);
} }
public function testInnerJoinParameterised() /**
* @dataProvider provideJoin
*/
public function testJoinParameterised(string $joinMethod, string $joinType)
{ {
$db = DB::get_conn(); $db = DB::get_conn();
$list = TeamComment::get(); $list = TeamComment::get();
$list = $list->innerJoin( $list = $list->$joinMethod(
'DataObjectTest_Team', 'DataObjectTest_Team',
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID" ' '"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID" '
. 'AND "DataObjectTest_Team"."Title" LIKE ?', . 'AND "DataObjectTest_Team"."Title" LIKE ?',
@ -353,66 +376,8 @@ class DataListTest extends SapphireTest
. 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL' . 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL'
. ' THEN "DataObjectTest_TeamComment"."ClassName" ELSE ' . ' THEN "DataObjectTest_TeamComment"."ClassName" ELSE '
. $db->quoteString(DataObjectTest\TeamComment::class) . $db->quoteString(DataObjectTest\TeamComment::class)
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" INNER JOIN ' . ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" ' . $joinType
. '"DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = ' . ' "DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = '
. '"DataObjectTest_TeamComment"."TeamID" '
. 'AND "DataObjectTest_Team"."Title" LIKE ?'
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
$this->assertSQLEquals($expected, $list->sql($parameters));
$this->assertEquals(['Team%'], $parameters);
}
public function testLeftJoin()
{
$db = DB::get_conn();
$list = TeamComment::get();
$list = $list->leftJoin(
'DataObjectTest_Team',
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"',
'Team'
);
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", '
. '"DataObjectTest_TeamComment"."LastEdited", "DataObjectTest_TeamComment"."Created", '
. '"DataObjectTest_TeamComment"."Name", "DataObjectTest_TeamComment"."Comment", '
. '"DataObjectTest_TeamComment"."TeamID", "DataObjectTest_TeamComment"."ID", '
. 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL '
. 'THEN "DataObjectTest_TeamComment"."ClassName" ELSE '
. $db->quoteString(DataObjectTest\TeamComment::class)
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" LEFT JOIN "DataObjectTest_Team" '
. 'AS "Team" ON "DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID"'
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';
$this->assertSQLEquals($expected, $list->sql($parameters));
$this->assertEmpty($parameters);
}
public function testLeftJoinParameterised()
{
$db = DB::get_conn();
$list = TeamComment::get();
$list = $list->leftJoin(
'DataObjectTest_Team',
'"DataObjectTest_Team"."ID" = "DataObjectTest_TeamComment"."TeamID" '
. 'AND "DataObjectTest_Team"."Title" LIKE ?',
'Team',
20,
['Team%']
);
$expected = 'SELECT DISTINCT "DataObjectTest_TeamComment"."ClassName", '
. '"DataObjectTest_TeamComment"."LastEdited", "DataObjectTest_TeamComment"."Created", '
. '"DataObjectTest_TeamComment"."Name", "DataObjectTest_TeamComment"."Comment", '
. '"DataObjectTest_TeamComment"."TeamID", "DataObjectTest_TeamComment"."ID", '
. 'CASE WHEN "DataObjectTest_TeamComment"."ClassName" IS NOT NULL'
. ' THEN "DataObjectTest_TeamComment"."ClassName" ELSE '
. $db->quoteString(DataObjectTest\TeamComment::class)
. ' END AS "RecordClassName" FROM "DataObjectTest_TeamComment" LEFT JOIN '
. '"DataObjectTest_Team" AS "Team" ON "DataObjectTest_Team"."ID" = '
. '"DataObjectTest_TeamComment"."TeamID" ' . '"DataObjectTest_TeamComment"."TeamID" '
. 'AND "DataObjectTest_Team"."Title" LIKE ?' . 'AND "DataObjectTest_Team"."Title" LIKE ?'
. ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC'; . ' ORDER BY "DataObjectTest_TeamComment"."Name" ASC';