MDL-76902 reportbuilder: entity method for defining table join alias.

This commit is contained in:
Paul Holden
2023-08-22 15:16:31 +01:00
parent e998f14061
commit 3ca41c77e4
3 changed files with 51 additions and 1 deletions
+29 -1
View File
@@ -38,9 +38,12 @@ abstract class base {
/** @var lang_string $entitytitle Used as a title for the entity in reports */
private $entitytitle = null;
/** @var array $tablealiases Database tables that this entity uses and their default aliases */
/** @var array $tablealiases Database tables that this entity uses and their aliases */
private $tablealiases = [];
/** @var array $tablejoinaliases Database tables that have already been joined to the report and their aliases */
private $tablejoinaliases = [];
/** @var string[] $joins List of SQL joins for the entity */
private $joins = [];
@@ -182,6 +185,31 @@ abstract class base {
return $this->tablealiases[$tablename] ?? $defaulttablealiases[$tablename];
}
/**
* Set the alias for given database table that has already been added to the report. Enables entities to avoid additional
* joins on the same table by allowing re-use of existing table aliases in their own queries, {@see has_table_join_alias}
*
* @param string $tablename
* @param string $alias
* @return self
*/
final public function set_table_join_alias(string $tablename, string $alias): self {
$this->tablejoinaliases[$tablename] = $alias;
// Internally set the same table alias for the entity.
return $this->set_table_alias($tablename, $alias);
}
/**
* Determine whether defined table join alias was specified. Call {@see get_table_alias} to retrieve said value
*
* @param string $tablename
* @return bool
*/
final public function has_table_join_alias(string $tablename): bool {
return array_key_exists($tablename, $this->tablejoinaliases);
}
/**
* Add join clause required for this entity to join to existing tables/entities
*
@@ -114,6 +114,27 @@ class base_test extends advanced_testcase {
]);
}
/**
* Test setting table join alias
*/
public function test_set_table_join_alias(): void {
$entity = new base_test_entity();
$entity->set_table_join_alias('mytable', 'newalias');
$this->assertTrue($entity->has_table_join_alias('mytable'));
$this->assertEquals('newalias', $entity->get_table_alias('mytable'));
}
/**
* Test that entity doesn't have table join alias by default
*
* {@see test_set_table_join_alias} for assertion where it does
*/
public function test_has_table_join_alias(): void {
$entity = new base_test_entity();
$this->assertFalse($entity->has_table_join_alias('mytable'));
}
/**
* Test entity name
*/
+1
View File
@@ -7,6 +7,7 @@ Information provided here is intended especially for developers.
- `core_reportbuilder_can_view_system_report`
- `core_reportbuilder_retrieve_system_report`
* New `get_tag_joins_for_entity` helper in base entity class, for returning SQL joins necessary for retrieving tags
* New methods `[set|has]_table_join_alias` in the base entity class, to allow entities to reduce joins on the same table
* New `set_is_deprecated` method in base `local\report\[column|filter]` classes to deprecate report entity columns and filters
* The following report entity columns have been deprecated, with replacements as follows:
- `comment:context` => `context:name`