<?php
/**
 * @file
 * Install for a tripal data entity - creates the base table for our entity.
 */


/**
 * Implements hook_schema().
 */
function tripal_entities_schema() {

  // Adds a table for managing TripalEntity entities.
  $schema['tripal_vocab'] = tripal_entities_tripal_vocab_schema();
  $schema['tripal_term'] = tripal_entities_tripal_term_schema();
  $schema['tripal_entity'] = tripal_entities_tripal_entity_schema();
  $schema['tripal_bundle'] = tripal_entities_tripal_bundle_schema();

  return $schema;
}

/**
 * @section
 * Schema Definitions.
 */

/**
 * The base table for Biological Data Entities.
 *
 * This contains the actual data. For example, if you have a 5 genes and 10 mRNA then
 * this table will have 15 records and include both genes and mRNA's.
 */
function tripal_entities_tripal_entity_schema() {

  $schema = array(
    'description' => 'The base table for Tripal Vocabulary-based entities.',
    'fields' => array(
      'id' => array(
        'description' => 'The primary identifier for a vocabulary entity.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'The type of entity. This should be an official vocabulary ID (e.g. SO, RO, GO).',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'bundle' => array(
        'description' => 'The type of bundle. This should be an official vocabulary ID (e.g. SO, RO, GO) followed by an underscore and the term accession.',
        'type' => 'varchar',
        'length' => 1024,
        'not null' => TRUE,
        'default' => '',
      ),
      'term_id' => array(
        'description' => 'The term_id for the type of entity. This term_id corresponds to a TripalTerm record.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'title' => array(
        'description' => 'The title of this node, always treated as non-markup plain text.',
        'type' => 'text',
        'not null' => TRUE,
        'default' => '',
      ),
      'uid' => array(
        'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'status' => array(
        'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the node was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the node was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'term_id' => array('term_id'),
      'entity_changed' => array('changed'),
      'entity_created' => array('created'),
      'type' => array('type'),
      'uid' => array('uid'),
    ),
    'unique keys' => array(),
    'primary key' => array('id'),
  );
  return $schema;
}

/**
 * The base table for TripalVocab schema.
 *
 * This contains the actual data. For example, if you have a 5 genes and 10 mRNA then
 * this table will have 15 records and include both genes and mRNA's.
 */
function tripal_entities_tripal_vocab_schema() {

  // This schema only provides enough information to assign a unique ID
  // to the vocabulary. Any additonal information is added to the Entity object
  // by the selected database back-end.
  $schema = array(
    'description' => 'The base table for TripalVocab entities.',
    'fields' => array(
      'id' => array(
        'description' => 'The primary identifier for a vocab entity.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'namespace' => array(
        'description' => 'The namespace for the vocabulary (e.g. SO, PATO, etc.).',
        'type' => 'varchar',
        'length' => 10,
        'not null' => TRUE,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the entity was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the entity was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'namespace' => array('namespace'),
      'entity_changed' => array('changed'),
      'entity_created' => array('created'),
    ),
    'unique keys' => array('namespace' => array('namespace')),
    'primary key' => array('id'),
  );
  return $schema;
}

/**
 * The base table for TripalTerm entities.
 *
 * This contains the actual data. For example, if you have a 5 genes and 10 mRNA then
 * this table will have 15 records and include both genes and mRNA's.
 */
function tripal_entities_tripal_term_schema() {

  // This schema only provides enough information to assign a unique ID
  // to the term and associate it to it's vocabulary. Any additonal information
  // is added to the Entity object by the selected database back-end.
  $schema = array(
    'description' => 'The base table for TripalTerm entities.',
    'fields' => array(
      'id' => array(
        'description' => 'The primary identifier for a term entity.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vocab_id' => array(
        'description' => 'The vocabulary_id of the TripalVocab entity to which this term belongs.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'term_id' => array(
        'description' => 'The id (or accession) of this term in the vocabulary.',
        'type' => 'varchar',
        'length' => 1024,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the entity was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the entity was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'vocab_id' => array('vocab_id'),
      'term_id' => array('term_id'),
      'entity_changed' => array('changed'),
      'entity_created' => array('created'),

    ),
    'foreign keys' => array(
      'tripal_vocab' => array(
        'table' => 'tripal_vocab',
        'columns' => array(
          'vocab_id' => 'vocab_id',
        ),
      ),
    ),
    'unique keys' => array('vocab_term' => array('vocab_id', 'term_id')),
    'primary key' => array('id'),
  );
  return $schema;
}

/**
 * The base table for TripalEntity entities.
 *
 * This table contains a list of Biological Data Types.
 * For the example above (5 genes and 10 mRNAs), there would only be two records in
 * this table one for "gene" and another for "mRNA".
 */
function tripal_entities_tripal_bundle_schema() {

  $schema = array(
    'description' => 'Stores information about defined tripal data types.',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique numeric ID.',
      ),
      'type' => array(
        'description' => 'The type of entity. This should be an official vocabulary ID (e.g. SO, RO, GO).',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'bundle' => array(
        'description' => 'The type of bundle. This should be an official vocabulary ID (e.g. SO, RO, GO) followed by an underscore and the term accession.',
        'type' => 'varchar',
        'length' => 1024,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'The human-readable name of this bundle.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'bundle' => array('bundle'),
    ),
  );
  return $schema;
}