Tripal
Public Member Functions | Protected Attributes | List of all members
Drupal\tripal\TripalDBX\TripalDbxSchema Class Reference
Inheritance diagram for Drupal\tripal\TripalDBX\TripalDbxSchema:
Inheritance graph
[legend]
Collaboration diagram for Drupal\tripal\TripalDBX\TripalDbxSchema:
Collaboration graph
[legend]

Public Member Functions

 getSchemaDef (array $parameters)
 
 __construct (\Drupal\tripal\TripalDBX\TripalDbxConnection $connection)
 
 initialize ()
 
 getSchemaName ()
 
 findTables ($table_expression)
 
 queryTableInformation ($table)
 
 indexExists ($table, $index_name, bool $exact_name=FALSE)
 
 getSchemaSize ()
 
 schemaExists ()
 
 functionExists (string $function_name, array $function_parameters)
 
 sequenceExists (?string $table_name=NULL, ?string $column_name=NULL, ?string &$sequence_name=NULL)
 
 constraintExists ( $table, $constraint_name, ?string $type=NULL)
 
 primaryKeyExists (string $table, ?string $column=NULL)
 
 foreignKeyConstraintExists (string $base_table, string $base_column)
 
 getTables (array $include=[])
 
 getTableDef (string $table, array $parameters)
 
 getTableDdl (string $table_name, bool $clear_cache=FALSE)
 
 getReferencingTables (string $table_name, bool $clear_cache=FALSE)
 
 createSchema ()
 
 cloneSchema (string $source_schema)
 
 renameSchema (string $new_schema_name)
 
 dropSchema ()
 
 tableExists ($table)
 

Protected Attributes

 $defaultSchema = ''
 
 $quotedDefaultSchema = ''
 
 $initialized = FALSE
 
 $tripalDbxApi = NULL
 

Detailed Description

Tripal DBX API Schema class.

NOTE: This class should not be instantiated directly but rather it should be accessed through a TripalDbxConnection object using the schema() method.

This class provides a Tripal-specific implementation of the Drupal Schema abstract class. The Drupal PostgreSQL (and other database driver) implementations of the base Drupal Schema class follow the assumption that there is a single schema. As such the core Drupal implementations focus on managing tables within a single schema.

This implementation extends that table-management functionality to also include Schema-focused management including creation, cloning, renaming, dropping and definition export. Additionally, it removes the assumption of a single schema by allowing the default schema to be set based on a Tripal DBX connection.

Here are some useful functions to know that are inherited from Drupal Schema classes (core + PgSchema implementation):

See also
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Database!Driver!pgsql!Schema.php/class/Schema/9.0.x
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Schema.php/class/Schema/9.0.x

Constructor & Destructor Documentation

◆ __construct()

Drupal\tripal\TripalDBX\TripalDbxSchema::__construct ( \Drupal\tripal\TripalDBX\TripalDbxConnection  $connection)

Constructor.

OVERRIDES \Drupal\Core\Database\Schema:__construct().

Overrides default constructor to manage the Tripal DBX managed schema name.

The TripalDbxSchema object should be instantiated by the TripalDbxConnection::schema() method in order to avoid issues when the default Tripal DBX managed schema name is changed in the TripalDbxConnection object which could lead to issues.

If you choose to instantiate a TripalDbxSchema object yourself, you are responsible to not change the Tripal DBX managed schema name of the connection object used to instantiate this TripalDbxSchema.

Parameters
\Drupal\tripal\TripalDBX\TripalDbxConnection$connectionA Tripal DBX connection object.
Exceptions

Member Function Documentation

◆ cloneSchema()

Drupal\tripal\TripalDBX\TripalDbxSchema::cloneSchema ( string  $source_schema)

Clones a schema into new (unexisting) one.

The target schema must not exist. If $target_schema is omitted, current schema will be used. For instance, if you want to clone "chado" schema into a "chado_copy" schema, you would do something like this:

// We assume 'chado_copy' schema does not exist.
$new_schema = new ChadoConnection('chado_copy');
$new_schema->clone('chado');
Parameters
string$source_schemaSource schema to clone.
?string$target_schema Destination schema that will be created and filled with a copy of $source_schema. If not set, current schema will be the target.
Returns
int The new schema size in bytes or 0 if the operation failed or the schema to clone was empty.

◆ constraintExists()

Drupal\tripal\TripalDBX\TripalDbxSchema::constraintExists (   $table,
  $constraint_name,
?string  $type = NULL 
)

Check that the constraint exists.

OVERRIDES \Drupal\Core\Database\Schema:constraintExists().

Our version of this method adds an optional parameter $type to support type-specific searching and exact name matches since many biological data-focused databases will not follow the Drupal naming patterns. For example, Chado does not follow these patterns.

Parameters
string$tableThe table the constraint applies to.
string$constraint_nameThe name of the constraint you want to check.
?string$type The type of constraint. Should be one of "PRIMARY KEY", "UNIQUE", or "FOREIGN KEY".
Returns
TRUE if the constraint exists and FALSE otherwise.

◆ createSchema()

Drupal\tripal\TripalDBX\TripalDbxSchema::createSchema ( )

Creates the given schema.

The schema to create must not exist. If an error occurs, an exception is thrown.

Exceptions

◆ dropSchema()

Drupal\tripal\TripalDBX\TripalDbxSchema::dropSchema ( )

Removes the given schema.

The schema to remove must exist. If an error occurs, an exception is thrown.

Exceptions

◆ findTables()

Drupal\tripal\TripalDBX\TripalDbxSchema::findTables (   $table_expression)

Finds all tables that are like the specified base table name.

OVERRIDES \Drupal\Core\Database\Schema:findTables().

NOTE: In Drupal 10 individually prefixed tables will no longer be supported. At this point we should re-evaluate if this override is still needed.

Overrides the PostgreSQL implementation for two reasons:

  1. Switch back to the generic Drupal implementation to prepare for other database driver support in the future.
  2. To override where tablePrefix() is called to include the optional parameter indicating to use the Tripal DBX API.
Parameters
string$table_expressionAn SQL expression, for example "cache_%" (without the quotes).
Returns
array Both the keys and the values are the matching tables.

◆ foreignKeyConstraintExists()

Drupal\tripal\TripalDBX\TripalDbxSchema::foreignKeyConstraintExists ( string  $base_table,
string  $base_column 
)

Check the foreign key constrain specified exists.

Parameters
string$base_tableThe name of the table the foreign key resides in. E.g. 'feature' for the feature.type_id => cvterm.cvterm_id foreign key.
string$base_columnThe name of the column that is a foreign key in. E.g. 'type_id' for the feature.type_id => cvterm.cvterm_id foreign key.
Returns
TRUE if the constraint exists and FALSE otherwise.

◆ functionExists()

Drupal\tripal\TripalDBX\TripalDbxSchema::functionExists ( string  $function_name,
array  $function_parameters 
)

Check that the given function exists.

Example:

$exists = $tripaldbx_schema->functionExists('dummyfunc', ['bigint', 'varchar']);
Parameters
string$function_nameThe name of the function.
array$func_parametersAn ordered array of input parameter types that are part of the function signature.
Returns
bool TRUE if the function exists in the schema and FALSE otherwise.
Exceptions

◆ getReferencingTables()

Drupal\tripal\TripalDBX\TripalDbxSchema::getReferencingTables ( string  $table_name,
bool  $clear_cache = FALSE 
)

Retrieves tables referencing a given one.

Parameters
string$tableThe name of the table used as foreign table by other tables.
bool$clear_cacheIf TRUE, cache is cleared.
Returns
array First level are referencing table names and values are local table column names => referencing table column name. If no referencing tables are found, returns an empty array.

◆ getSchemaDef()

Drupal\tripal\TripalDBX\TripalDbxSchema::getSchemaDef ( array  $parameters)
abstract

Retrieve schema details from selected source in the requested format.

Parameters
array$parametersAn array of key-value parameters:
  • 'source': either 'database' to extract data from database or 'file' to get the data from a static YAML file. Default: 'file'.
  • 'version': version of the Tripal DBX managed schema to fetch from a file. Ignored for 'database' source. Default: implementation specific.
  • 'format': return format, either 'SQL' for an array of SQL string, 'Drupal' for Drupal schema API, 'none' to return nothing or anything else to provide a data structure as returned by TripalDbx::parseTableDdl. If the selected source is 'file', the format parameter will be ignored and 'Drupal' format will be used. Default: TripalDbx::parseTableDdl data structure structure.
  • 'clear': if not empty, cache will be cleared.
Returns
An array with details for the current schema version as defined by $parameters values.
Exceptions

Reimplemented in Drupal\tripal_chado\Database\ChadoSchema, and Drupal\Tests\tripal\Kernel\TripalDBX\Subclass\TripalDbxSchemaFake.

◆ getSchemaName()

Drupal\tripal\TripalDBX\TripalDbxSchema::getSchemaName ( )

Returns current schema name.

Returns
string Current schema name.

◆ getSchemaSize()

Drupal\tripal\TripalDBX\TripalDbxSchema::getSchemaSize ( )

Returns the size in bytes of a PostgreSQL schema.

Returns
integer The size in bytes of the schema or 0 if the size is not available.
Exceptions

◆ getTableDdl()

Drupal\tripal\TripalDBX\TripalDbxSchema::getTableDdl ( string  $table_name,
bool  $clear_cache = FALSE 
)

Retrieves the table DDL (table data definition language).

Parameters
string$tableThe name of the table to retrieve.
bool$clear_cacheIf TRUE, cache is cleared.
Returns
string A set of SQL queries used to create the table including its constraints or an empty string if the table was not found.

◆ getTableDef()

Drupal\tripal\TripalDBX\TripalDbxSchema::getTableDef ( string  $table,
array  $parameters 
)

Returns the specified table structure details.

Parameters
string$tableThe name of the table.
array$parametersAn array of key-value parameters:
  • 'source': either 'database' to extract data from database or 'file' to get the data from a static YAML file or 'tripal' to get the data from Tripal records. Default: 'file'.
  • 'version': version of the Tripal DBX managed schema to fetch from a file. Ignored fot 'database' source. Default: implementation specific.
  • 'format': return format, either 'sql' for an array of SQL string, 'drupal' for Drupal schema API, 'none' to return nothing or anything else (like 'default') to provide a data structure as returned by TripalDbx::parseTableDdl plus a 'referenced_by' key containing all the referencing tables returned by ::getReferencingTables. If the selected source is 'file' or 'tripal', the format parameter will be ignored and 'Drupal' format will be used. Default: TripalDbx::parseTableDdl data structure structure.
  • 'clear': if not empty, cache will be cleared.
Returns
An array with details from the specified source for the specified table using the specified format or an empty array if table not found.

◆ getTables()

Drupal\tripal\TripalDBX\TripalDbxSchema::getTables ( array  $include = [])

Retrieves the list of tables in the given schema.

Note: only peristant tables (ie. no unlogged or temporary tables) visible by current DB user are returned.

Parameters
array$includeAn associative array to select other element type to include. Supported keys are: 'table': include all tables; 'base': include only base tables (as defined in the original Tripal Dbx schema definition); 'custom': include only custom tables (not part of the original Tripal Dbx schema definition); 'view': include views; 'partition': include partitions; 'materialized view': include PostgreSQL materialized views (not to be confused with Tripal materialized views which currently are regular tables); If both 'base' and 'custom' are specified, all tables are returned. Default: tables only (empty array).
Returns
array An associative array where the keys are the table names and values are array of properties such as: -'name': table name -'type': one of 'table', 'view', 'partition' and 'materialized view' for PostgreSQL materialized views. -'status': either 'base' for a base table, or 'custom' for a custom table or a tripal materialized view, or 'other' for other elements.

◆ indexExists()

Drupal\tripal\TripalDBX\TripalDbxSchema::indexExists (   $table,
  $index_name,
bool  $exact_name = FALSE 
)

Checks if an index exists in the given table.

OVERRIDES \Drupal\Core\Database\Schema:indexExists().

Our version of this method adds an optional parameter $exact_name to support exact name matches since many biological data-focused databases will not follow the Drupal index naming pattern. For example, Chado does not follow this pattern.

Parameters
$tableThe name of the table in Tripal DBX managed schema.
$nameThe full name of the index (including the '_idx' part for instance).
bool$exact_nameIf FALSE, Drupal will append to the given name the '__idx' suffix (added when ::addIndex is used) and will adjust the name if needed (length). If TRUE, the function assumes the given index name is complete.
Returns
TRUE if the given index exists, otherwise FALSE.

◆ primaryKeyExists()

Drupal\tripal\TripalDBX\TripalDbxSchema::primaryKeyExists ( string  $table,
?string  $column = NULL 
)

Check that the primary key exists, has a sequence and a constraint.

Parameters
string$tableThe table you want to check the primary key for.
?string$column (optional) The name of the primary key column.
Returns
TRUE if the primary key meets all the requirements and FALSE otherwise.

◆ queryTableInformation()

Drupal\tripal\TripalDBX\TripalDbxSchema::queryTableInformation (   $table)

Fetch the list of blobs and sequences used on a table.

OVERRIDES \Drupal\Core\Database\Schema:queryTableInformation().

Override is required for proper table prefixing. While the exact mechanism isn't clear, if this function is removed then tests will fail due to incorrect table prefixing.

Parameters
string$table_nameThe non-prefixed name of the table.
Returns
object An object with two member variables:
  • 'blob_fields' that lists all the blob fields in the table.
  • 'sequences' that lists the sequences used in that table.

◆ renameSchema()

Drupal\tripal\TripalDBX\TripalDbxSchema::renameSchema ( string  $new_schema_name)

Renames a schema.

The new schema name must not be used by an existing schema. If an error occurs, an exception is thrown.

Parameters
string$new_schema_nameNew name to use.
Exceptions

◆ schemaExists()

Drupal\tripal\TripalDBX\TripalDbxSchema::schemaExists ( )

Check that the given schema exists.

Returns
bool TRUE/FALSE depending upon whether or not the schema exists.

◆ sequenceExists()

Drupal\tripal\TripalDBX\TripalDbxSchema::sequenceExists ( ?string  $table_name = NULL,
?string  $column_name = NULL,
?string &  $sequence_name = NULL 
)

Check that any given sequence in a table exists.

When the sequence name is not known, a table name and a column name can be specified instead. Then, the sequence name will be guessed from those and returned into the given $sequence_name variable if provided. If both the sequence name and the table and column names are specified, only the sequence name will be taken into account.

This information can also be extracted from ::queryTableInformation()['sequences'].

Parameters
string$table_nameThe name of the table the sequence is used in.
string$column_nameThe name of the column the sequence is used to populate.
string&$sequence_nameThe name of the sequence is to check if known. Otherwise, the function will set it.
Returns
bool TRUE if the seqeuence exists and FALSE if it does not.
Exceptions

◆ tableExists()

Drupal\tripal\TripalDBX\TripalDbxSchema::tableExists (   $table)

Overrides Drupal\Core\Database\Schema->tableExists().

We needed to override it because core Drupal makes some assumptions when building the where condition that do not match our multi-schema setup.


The documentation for this class was generated from the following file: