From 3ca41c77e4373ae8e9653db9d0d12b520c8eb02c Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 17 Jan 2023 11:02:41 +0000 Subject: [PATCH] MDL-76902 reportbuilder: entity method for defining table join alias. --- reportbuilder/classes/local/entities/base.php | 30 ++++++++++++++++++- .../tests/local/entities/base_test.php | 21 +++++++++++++ reportbuilder/upgrade.txt | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/reportbuilder/classes/local/entities/base.php b/reportbuilder/classes/local/entities/base.php index 1dfcacefe94..29a56665547 100644 --- a/reportbuilder/classes/local/entities/base.php +++ b/reportbuilder/classes/local/entities/base.php @@ -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 * diff --git a/reportbuilder/tests/local/entities/base_test.php b/reportbuilder/tests/local/entities/base_test.php index a4110934997..38e9ff784a8 100644 --- a/reportbuilder/tests/local/entities/base_test.php +++ b/reportbuilder/tests/local/entities/base_test.php @@ -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 */ diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt index 410ebc3d168..66acedccacf 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -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`