<?php
// $Id: rdf_db.install,v 1.3 2008/02/10 23:01:15 arto Exp $
/**
 * rdf_db.install - RDF DB installation/uninstallation.
 *
 * @author Arto Bendiken <http://bendiken.net/>
 * @copyright Copyright (c) 2007-2008 Arto Bendiken. All rights reserved.
 * @license GPL <http://creativecommons.org/licenses/GPL/2.0/>
 * @package rdf.module
 * @subpackage rdf_db.module
 */

//////////////////////////////////////////////////////////////////////////////
// Core API hooks

/**
 * Implementation of hook_install().
 */
function rdf_db_install() {
  drupal_install_schema('rdf_db');
  variable_set('rdf_db_repository[local]', array('dc:title' => t('Local'), 'dc:description' => ''));

  db_query("UPDATE {system} SET weight = 10 WHERE name = 'rdf_db'");
}

/**
 * Implementation of hook_uninstall().
 */
function rdf_db_uninstall() {
  drupal_uninstall_schema('rdf_db');
  variable_del('rdf_db_repository[local]');
}

//////////////////////////////////////////////////////////////////////////////
// Schema API hooks

/**
 * Implementation of hook_schema().
 */
function rdf_db_schema() {
  $schema = array(
    'rdf_namespaces' => array(
      'description' => t(''),
      'fields' => array(
        'prefix'  => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE),
        'uri'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      ),
      'primary key' => array('prefix'),
      'indexes' => array('uri' => array('uri')),
    ),

    'rdf_resources' => array(
      'description' => t(''),
      'fields' => array(
        'rid'  => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
        'uri'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
      ),
      'primary key' => array('rid'),
      'unique keys' => array('uri' => array('uri')),
    ),

    'rdf_data' => array(
      'description' => t(''),
      'fields' => array(
        'did'  => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
        'gid'  => array('type' => 'int', 'unsigned' => TRUE),
        'sid'  => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
        'pid'  => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
        'oid'  => array('type' => 'int', 'unsigned' => TRUE),
        'tid'  => array('type' => 'int', 'unsigned' => TRUE),
        'lang' => array('type' => 'varchar', 'length' => 12),
        'data' => array('type' => 'text', 'size' => 'big'),
      ),
      'primary key' => array('did'),
      'indexes' => array(
        'gspo' => array('gid', 'sid', 'pid', 'oid'),
        'gpos' => array('gid', 'pid', 'oid', 'sid'),
        'gosp' => array('gid', 'oid', 'sid', 'pid'),
      ),
    ),
  );

  // This is not executed on installation/uninstallation, but only when the
  // schema is loaded at runtime; it's needed in order for RDF repositories
  // created by third-party modules to have a schema without them having to
  // duplicate the definition of the {rdf_data} table, above.
  if (function_exists('rdf_db_get_repository_tables')) {
    foreach (rdf_db_get_repository_tables() as $table) {
      if ($table != RDF_DB_TABLE_DEFAULT) {
        $schema[$table] = $schema[RDF_DB_TABLE_DEFAULT];
      }
    }
  }

  return $schema;
}
