<?php
// $Id: uc_restrict_qty.install,v 1.1.2.3 2009/05/17 09:10:45 mrfelton Exp $

/**
 * @file
 * uc_restrict_qty module install file.
 */

/**
 * Define the table structures.
 *
 * @return
 *   The schema which contains the structure for the uc_restrict_qty module's tables.
 */
function uc_restrict_qty_schema() {
  $schema['uc_restrict_qty_products'] = array(
    'fields' => array(
      'rqpid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'pfid' => array(
        'description' => t('The ID of the product feature this is attached to.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'description' => t('The ID of the node this role feature is attached to.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'model' => array(
        'description' => t('The product model.'),
        'type' => 'varchar',
        'length' => 255,
        'default' => NULL,
      ),
      'qty' => array(
        'description' => t('The quantity restriction.'),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'lifetime' => array(
        'description' => t('Is restriction lifetime?'),
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
      'model' => array('model')
    ),
    'primary key' => array('rqpid'),
  );
  return $schema;
}


/**
 * Implementation of hook_install().
 *
 * Inserts the uc_restrict_qty module's schema in the SQL database.
 */
function uc_restrict_qty_install() {
  drupal_install_schema('uc_restrict_qty');
}

/**
 * Implementation of hook_uninstall().
 *
 * Remove the variables and schema corresponding to the uc_restrict_qty module.
 */
function uc_restrict_qty_uninstall() {
  drupal_uninstall_schema('uc_restrict_qty');
  db_query("DELETE FROM {variable} WHERE name LIKE 'uc_restrict_qty_%%'");
}

/**
 * Implementation of hook_update_N().
 *
 * If updating from an earlier version than 6.x-2.0, the database needs to be installed.
 */
function uc_restrict_qty_update_6200() {
  $ret = array();
  uc_restrict_qty_install();
  return $ret;
}


/**
 * Add lifetime value to the scheme.
 */
function uc_restrict_qty_update_6201() {
  $ret = array();
  db_add_field($ret, 'uc_restrict_qty_products', 'lifetime', array('type' => 'int', 'size' => 'tiny', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 1));
  return $ret;
}
