Skip to content

Commit

Permalink
Merge pull request tripal#1771 from tripal/tv4g2-issue1758-chado_cust…
Browse files Browse the repository at this point in the history
…om_table_functionality

Tv4g2 issue1758 chado custom table functionality
  • Loading branch information
laceysanderson authored Feb 12, 2024
2 parents 08689e4 + e850e76 commit 1eb2f1a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 12 deletions.
43 changes: 33 additions & 10 deletions tripal_chado/src/Services/ChadoCustomTableManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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');
Expand Down
12 changes: 10 additions & 2 deletions tripal_chado/src/Services/ChadoMviewsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
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 {


/**
* 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Drupal\Tests\tripal_chado\Functional;

use Drupal\Core\Database\Database;
use Drupal\Core\Test\FunctionalTestSetupTrait;
use Drupal\tripal_chado\ChadoCustomTables\ChadoCustomTable;
use Drupal\tripal_chado\Services\ChadoCustomTableManager;

/**
* Tests the functions in the ChadoCustomTableManager services class.
*
* @group Tripal
* @group Tripal Chado
*/
class ChadoCustomTableManagerTest extends ChadoTestBrowserBase {

/**
* Tests that we can create, list, and get custom table objects
*
* @group chado
*/
public function testCustomTableManager() {
// Create and then get the existing test chado schema name.
$this->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.');

}
}
2 changes: 2 additions & 0 deletions tripal_chado/tripal_chado.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1eb2f1a

Please sign in to comment.