diff --git a/tripal_chado/src/Services/ChadoCustomTableManager.php b/tripal_chado/src/Services/ChadoCustomTableManager.php index 44822b063..6a6be8a86 100644 --- a/tripal_chado/src/Services/ChadoCustomTableManager.php +++ b/tripal_chado/src/Services/ChadoCustomTableManager.php @@ -2,17 +2,34 @@ namespace Drupal\tripal_chado\Services; -use Drupal\Core\Database\Database; +use Drupal\Core\Database\Connection; +use Drupal\tripal_chado\Database\ChadoConnection; use Drupal\tripal_chado\ChadoCustomTables\ChadoCustomTable; class ChadoCustomTableManager { + /** + * The Drupal database connection. + */ + public Connection $connection; + + /** + * The chado connection used to query chado. + */ + public ChadoConnection $chado_connection; + /** * Instantiates a new ChadoCustomTableManager object. + * + * @param \Drupal\Core\Database\Connection + * The database connection object. + * @param Drupal\tripal_chado\Database\ChadoConnection + * The chado connection used to query chado. */ - public function __construct() { - + public function __construct(Connection $connection, ChadoConnection $chado_connection) { + $this->connection = $connection; + $this->chado_connection = $chado_connection; } /** @@ -31,6 +48,8 @@ public function __construct() { * @return \Drupal\tripal_chado\ChadoCustomTables\ChadoCustomTable */ public function create(string $table_name, string $chado_schema = NULL) { + // If the schema is not specified, get the default one. + $chado_schema = $this->chado_connection->schema()->getDefault(); $custom_table = new ChadoCustomTable($table_name, $chado_schema); return $custom_table; } @@ -45,9 +64,7 @@ public function create(string $table_name, string $chado_schema = NULL) { * A ChadoCustomTable object or NULL if not found. */ public function loadById(int $id) { - $public = \Drupal::database(); - - $query = $public->select('tripal_custom_tables','tct'); + $query = $this->connection->select('tripal_custom_tables','tct'); $query->fields('tct', ['table_name', 'chado']); $query->condition('tct.table_id', $id); $record = $query->execute()->fetchAssoc(); @@ -93,8 +110,11 @@ public function loadbyName(string $table_name, string $chado_schema = NULL) { */ public function findByName(string $table_name, string $chado_schema = NULL) { - $public = \Drupal::database(); - $query = $public->select('tripal_custom_tables','tct'); + // Retrieve the default name of the Chado schema if it's not provided. + if ($chado_schema === NULL) { + $chado_schema = $this->chado_connection->schema()->getDefault(); + } + $query = $this->connection->select('tripal_custom_tables','tct'); $query->fields('tct', ['table_id']); $query->condition('tct.chado', $chado_schema); $query->condition('tct.table_name', $table_name); @@ -115,8 +135,11 @@ public function findByName(string $table_name, string $chado_schema = NULL) { public function getTables(string $chado_schema = NULL) { $tables = []; - $public = \Drupal::database(); - $query = $public->select('tripal_custom_tables','tct'); + // Retrieve the default name of the Chado schema if it's not provided. + if ($chado_schema === NULL) { + $chado_schema = $this->chado_connection->schema()->getDefault(); + } + $query = $this->connection->select('tripal_custom_tables','tct'); $query->fields('tct', ['table_id', 'table_name']); $query->condition('tct.chado', $chado_schema); $query->orderBy('table_name'); diff --git a/tripal_chado/src/Services/ChadoMviewsManager.php b/tripal_chado/src/Services/ChadoMviewsManager.php index 154ff4fee..ff94345bd 100644 --- a/tripal_chado/src/Services/ChadoMviewsManager.php +++ b/tripal_chado/src/Services/ChadoMviewsManager.php @@ -3,6 +3,8 @@ namespace Drupal\tripal_chado\Services; use Drupal\Core\Database\Database; +use Drupal\Core\Database\Connection; +use Drupal\tripal_chado\Database\ChadoConnection; use Drupal\tripal_chado\ChadoCustomTables\ChadoMview; class ChadoMviewsManager extends ChadoCustomTableManager { @@ -10,9 +12,15 @@ class ChadoMviewsManager extends ChadoCustomTableManager { /** * Instantiates a new ChadoCustomTableManager object. + * + * @param \Drupal\Core\Database\Connection + * The database connection object. + * @param Drupal\tripal_chado\Database\ChadoConnection + * The chado connection used to query chado. */ - public function __construct() { - parent::__construct(); + public function __construct(Connection $connection, ChadoConnection $chado_connection) { + $this->connection = $connection; + $this->chado_connection = $chado_connection; } /** diff --git a/tripal_chado/tests/src/Functional/Services/ChadoCustomTableManagerTest.php b/tripal_chado/tests/src/Functional/Services/ChadoCustomTableManagerTest.php new file mode 100644 index 000000000..fa181b82b --- /dev/null +++ b/tripal_chado/tests/src/Functional/Services/ChadoCustomTableManagerTest.php @@ -0,0 +1,70 @@ +createTestSchema(ChadoTestBrowserBase::INIT_CHADO_EMPTY); + $chado = \Drupal::service('tripal_chado.database'); + $default_chado_schema = $chado->schema()->getDefault(); + + // Get an instance of the manager. + $ct_service = \Drupal::service('tripal_chado.custom_tables'); + + // Test that the manager was created successfully. + $this->assertInstanceOf(ChadoCustomTableManager::class, $ct_service, 'The Chado Custom Table Manager could not be created.'); + + // Test creating a table. The create() function returns a ChadoCustomTable object + $custom_table = $ct_service->create('test_custom_table', $default_chado_schema); + + $this->assertInstanceOf(ChadoCustomTable::class, $custom_table, 'The test_custom_table could not be created'); + + // We created a table, let's load it by name. + $test_table_by_name = $ct_service->loadByName('test_custom_table', $default_chado_schema); + + $this->assertInstanceOf(ChadoCustomTable::class, $test_table_by_name, 'The test_custom_table could not be created'); + + // Test loadById() with the id from the $test_table we just loaded. Make sure the returned tables are the same table object. + $test_table_by_id = $ct_service->loadById($test_table_by_name->getTableId()); + + $this->assertEquals($test_table_by_name, $test_table_by_id, 'Could not load the table by ID'); + + // Test the findByName() function. The returned int should match the ID found above by the table's getTableID() function. + $test_table_find = $ct_service->findByName('test_custom_table', $default_chado_schema); + $this->assertEquals($test_table_find, $test_table_by_name->getTableId(), 'Could not find the correct table'); + + // Test the getTables() function. + $expected_tables = [ + '1' => 'test_custom_table', + ]; + $tables = $ct_service->getTables($default_chado_schema); + $this->assertEquals($tables, $expected_tables, 'Could not get a list of tables from the ' . $default_chado_schema . ' schema.'); + // Add another table + $custom_table = $ct_service->create('test_custom_table2', $default_chado_schema); + $expected_tables = [ + '1' => 'test_custom_table', + '2' => 'test_custom_table2', + ]; + $tables = $ct_service->getTables($default_chado_schema); + $this->assertEquals($tables, $expected_tables, 'Could not get a list of tables from the ' . $default_chado_schema . ' schema.'); + + } +} \ No newline at end of file diff --git a/tripal_chado/tripal_chado.services.yml b/tripal_chado/tripal_chado.services.yml index d9abc45d3..33ca564c1 100644 --- a/tripal_chado/tripal_chado.services.yml +++ b/tripal_chado/tripal_chado.services.yml @@ -8,8 +8,10 @@ services: arguments: ['@tripal_chado.database', '@tripal.logger'] tripal_chado.custom_tables: class: Drupal\tripal_chado\Services\ChadoCustomTableManager + arguments: ['@database', '@tripal_chado.database'] tripal_chado.materialized_views: class: Drupal\tripal_chado\Services\ChadoMviewsManager + arguments: ['@database', '@tripal_chado.database'] tripal_chado.terms_init: class: Drupal\tripal_chado\Services\ChadoTermsInit tripal_chado.cloner: