123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- <?php
- class ChadoSchema {
-
- protected $version = '';
-
- protected $schema_name = 'chado';
-
- public function __construct($version = NULL, $schema_name = NULL) {
-
- if ($version === NULL) {
- $this->version = chado_get_version(TRUE);
- }
- else {
- $this->version = $version;
- }
-
- if ($schema_name === NULL) {
- $this->schema_name = chado_get_schema_name('chado');
- }
- else {
- $this->schema_name = $schema_name;
- }
-
-
- if (!chado_is_local()) {
- tripal_report_error(
- 'ChadoSchema',
- TRIPAL_NOTICE,
- 'The ChadoSchema class requires chado be installed within the drupal database
- in a separate schema for any compliance checking functionality.'
- );
- }
- if (!chado_is_installed()) {
- tripal_report_error(
- 'ChadoSchema',
- TRIPAL_NOTICE,
- 'The ChadoSchema class requires chado be installed
- for any compliance checking functionality.'
- );
- }
- }
-
- public function getVersion() {
- return $this->version;
- }
-
- public function getSchemaName() {
- return $this->schema_name;
- }
-
- public function getTableNames($include_custom = FALSE) {
- $tables = array();
- if ($this->version == '1.3') {
- $tables_v1_3 = tripal_chado_chado_get_v1_3_tables();
- foreach ($tables_v1_3 as $table) {
- $tables[$table] = $table;
- }
- }
- if ($this->version == '1.2') {
- $tables_v1_2 = tripal_chado_chado_get_v1_2_tables();
- foreach ($tables_v1_2 as $table) {
- $tables[$table] = $table;
- }
- }
- if ($this->version == '1.11' or $this->version == '1.11 or older') {
- $tables_v1_11 = tripal_chado_chado_get_v1_11_tables();
- foreach ($tables_v1_11 as $table) {
- $tables[$table] = $table;
- }
- }
-
- if ($include_custom) {
- $sql = "SELECT table FROM {tripal_custom_tables}";
- $resource = db_query($sql);
- foreach ($resource as $r) {
- $tables[$r->table] = $r->table;
- }
- }
- asort($tables);
- return $tables;
- }
-
- public function getTableSchema($table) {
-
- $v = $this->version;
-
- $v = preg_replace("/\./", "_", $v);
-
- $hook_name = "chado_schema_v" . $v . "_" . $table;
- $table_arr = module_invoke_all($hook_name);
-
-
-
- if ((!$table_arr or !is_array($table_arr)) and
- function_exists('tripal_chado_' . $hook_name)) {
- $api_hook = "tripal_chado_" . $hook_name;
- $table_arr = $api_hook();
- }
-
- if (!is_array($table_arr) or count($table_arr) == 0) {
- $table_arr = $this->getCustomTableSchema($table);
- }
- return $table_arr;
- }
-
- public function getCustomTableSchema($table) {
- $sql = "SELECT schema FROM {tripal_custom_tables} WHERE table_name = :table_name";
- $results = db_query($sql, array(':table_name' => $table));
- $custom = $results->fetchObject();
- if (!$custom) {
- return FALSE;
- }
- else {
- return unserialize($custom->schema);
- }
- }
-
- function getBaseTables() {
-
-
- $base_tables = array('organism', 'project', 'analysis', 'biomaterial',
- 'eimage', 'assay');
-
-
-
- $schema = $this->getTableSchema('cvterm');
- $referring = $schema['referring_tables'];
- foreach ($referring as $tablename) {
-
- if ($tablename == 'cvterm_dbxref' || $tablename == 'cvterm_relationship' ||
- $tablename == 'cvtermpath' || $tablename == 'cvtermprop' || $tablename == 'chadoprop' ||
- $tablename == 'cvtermsynonym' || preg_match('/_relationship$/', $tablename) ||
- preg_match('/_cvterm$/', $tablename) ||
-
- preg_match('/prop$/', $tablename) || preg_match('/prop_.+$/', $tablename) ||
-
- preg_match('/^nd_/', $tablename)) {
- continue;
- }
- else {
- array_push($base_tables, $tablename);
- }
- }
-
-
- $final_list = array();
- foreach ($base_tables as $i => $tablename) {
-
-
- if ($tablename == 'biomaterial' or $tablename == 'assay' or $tablename == 'arraydesign') {
- $final_list[] = $tablename;
- continue;
- }
-
-
- if ($tablename == 'phenotype') {
- continue;
- }
- $num_links = 0;
- $schema = $this->getTableSchema($tablename);
- $fkeys = $schema['foreign keys'];
- foreach ($fkeys as $fkid => $details) {
- $fktable = $details['table'];
- if (in_array($fktable, $base_tables)) {
- $num_links++;
- }
- }
- if ($num_links < 2) {
- $final_list[] = $tablename;
- }
- }
-
- $final_list[] = 'cvterm';
-
- sort($final_list);
- return $final_list;
- }
-
- public function getCvtermMapping($params) {
- return chado_get_cvterm_mapping($params);
- }
-
- public function checkTableExists($table) {
- return chado_table_exists($table);
- }
-
- public function checkColumnExists($table, $column) {
- return chado_column_exists($table, $column);
- }
-
- public function checkColumnType($table, $column, $expected_type = NULL) {
-
- if (!$this->checkColumnExists($table, $column)) {
- tripal_report_error(
- 'ChadoSchema',
- TRIPAL_WARNING,
- 'Unable to check the type of !table!column since it doesn\'t appear to exist in your site database.',
- array('!column' => $column, '!table' => $table)
- );
- return FALSE;
- }
-
- if ($expected_type === NULL) {
- $schema = $this->getTableSchema($table, $column);
- if (is_array($schema) AND isset($schema['fields'][$column])) {
- $expected_type = $schema['fields'][$column]['type'];
- }
- else {
- tripal_report_error(
- 'ChadoSchema',
- TRIPAL_WARNING,
- 'Unable to check the type of !table!column due to being unable to find the schema definition.',
- array('!column' => $column, '!table' => $table)
- );
- return FALSE;
- }
- }
-
-
- switch ($expected_type) {
- case 'int':
- $expected_type = 'integer';
- break;
- case 'serial':
- $expected_type = 'integer';
- break;
- case 'varchar':
- $expected_type = 'character varying';
- break;
- case 'datetime':
- $expected_type = 'timestamp without time zone';
- break;
- case 'char':
- $expected_type = 'character';
- break;
- }
-
- $query = 'SELECT data_type
- FROM information_schema.columns
- WHERE
- table_name = :table AND
- column_name = :column AND
- table_schema = :schema
- ORDER BY ordinal_position
- LIMIT 1';
- $type = db_query($query,
- array(':table' => $table, ':column' => $column, ':schema' => $this->schema_name))->fetchField();
-
- if ($type === $expected_type) {
- return TRUE;
- }
- elseif (($expected_type == 'float') AND (($type == 'double precision') OR ($type == 'real'))) {
- return TRUE;
- }
- elseif ($type == 'smallint' AND $expected_type == 'integer') {
- return TRUE;
- }
- elseif ($type == 'bigint' AND $expected_type == 'integer') {
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
-
- public function checkSequenceExists($table, $column) {
- $prefixed_table = $this->schema_name.'.'.$table;
- $sequence_name = db_query('SELECT pg_get_serial_sequence(:table, :column);',
- array(':table' => $prefixed_table, ':column' => $column))->fetchField();
-
- $sequence_name = str_replace($this->schema_name.'.', '', $sequence_name);
- return chado_sequence_exists($sequence_name);
- }
-
- public function checkPrimaryKey($table, $column = NULL) {
-
- if ($column === NULL) {
- $table_schema = $this->getTableSchema($table);
- $column = $table_schema['primary key'][0];
- }
-
-
- if (empty($column)) {
- tripal_report_error(
- 'ChadoSchema',
- TRIPAL_NOTICE,
- 'Cannot check the validity of the primary key for "!table" since there is no record of one.',
- array('!table' => $table)
- );
- return NULL;
- }
-
- $column_exists = $this->checkColumnExists($table, $column);
- if (!$column_exists) {
- return FALSE;
- }
-
- $sequence_exists = $this->checkSequenceExists($table, $column);
- if (!$sequence_exists) {
- return FALSE;
- }
-
- $constraint_exists = chado_query(
- "SELECT 1
- FROM information_schema.table_constraints
- WHERE table_name=:table AND constraint_type = 'PRIMARY KEY'",
- array(':table' => $table))->fetchField();
- if (!$constraint_exists) {
- return FALSE;
- }
- return TRUE;
- }
-
- function checkConstraintExists($table, $constraint_name, $type) {
-
- $constraint_exists = chado_query(
- "SELECT 1
- FROM information_schema.table_constraints
- WHERE table_name=:table AND constraint_type = :type AND constraint_name = :name",
- array(':table' => $table, ':name' => $constraint_name, ':type' => $type))->fetchField();
- if (!$constraint_exists) {
- return FALSE;
- }
- return TRUE;
- }
-
- function checkFKConstraintExists($base_table, $base_column) {
-
-
-
-
- $constraint_name = $base_table . '_' . $base_column . '_fkey';
- return $this->checkConstraintExists($base_table, $constraint_name, 'FOREIGN KEY');
- }
-
- function checkIndexExists($table, $name) {
- return chado_index_exists($table, $name);
- }
- }
|