<?php
// $Id: flexicharge.install,v 1.2.4.3 2007/02/27 03:33:07 sime Exp $

function flexicharge_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {ec_flexicharge} (
        chid int unsigned NOT NULL auto_increment,
        provider varchar(48) NOT NULL default '',
        method varchar(48) NOT NULL default '',
        display varchar(48) NOT NULL default '',
        operator varchar(4) NOT NULL default '',
        rate decimal(10,2) NOT NULL default '0.00',
        position int unsigned NOT NULL default '0',
        subtotal_before tinyint unsigned NOT NULL default '0',
        subtotal_after tinyint unsigned NOT NULL default '0',
        already_added tinyint unsigned NOT NULL default '0',
        hide_if_zero tinyint unsigned NOT NULL default '0',
        ptypes text NOT NULL,
        roles text NOT NULL,
        taxonomy text NOT NULL,
        settings text NOT NULL,
        PRIMARY KEY  (chid)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {ec_flexicharge_product} (
        vid int(10) unsigned NOT NULL default '0',
        nid int(10) unsigned NOT NULL default '0',
        attributes TEXT NOT NULL default '',
        KEY (vid),
        KEY (nid)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;

    case 'pgsql':
      // NOTE: this pgsql statement was converted using an online
      //       sql conversion tool - it may not work.
      db_query("CREATE TABLE {ec_flexicharge} (
        chid SERIAL,
        provider VARCHAR(48) NOT NULL DEFAULT '',
        method VARCHAR(48) NOT NULL DEFAULT '',
        display VARCHAR(48) NOT NULL DEFAULT '',
        operator VARCHAR(4) NOT NULL DEFAULT '',
        rate DECIMAL(10, 2) NOT NULL DEFAULT 0.00,
        position INTEGER NOT NULL DEFAULT 0,
        subtotal_before SMALLINT NOT NULL DEFAULT 0,
        subtotal_after SMALLINT NOT NULL DEFAULT 0,
        already_added SMALLINT NOT NULL DEFAULT 0,
        hide_if_zero SMALLINT NOT NULL DEFAULT 0,
        ptypes TEXT NOT NULL,
        roles TEXT NOT NULL,
        taxonomy TEXT NOT NULL,
        settings TEXT NOT NULL,
        CONSTRAINT ec_flexicharge_pk PRIMARY KEY (chid));"
      );
      db_query("CREATE TABLE {ec_flexicharge_product} (
        vid int NOT NULL default '0',
        nid int NOT NULL default '0',
        attributes TEXT NOT NULL default ''
      )");
    break;
  }
  drupal_set_message(t('E-Commerce: Flexicharge tables have been created.'));
}

/**
 * Add the additional filters columns to ec_flexicharge.
 */
function flexicharge_update_1() {
  $ret = array();
  switch($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {ec_flexicharge} ADD hide_if_zero SMALLINT NOT NULL DEFAULT 0');
      $ret[] = update_sql('ALTER TABLE {ec_flexicharge} ADD ptypes TEXT NOT NULL');
      $ret[] = update_sql('ALTER TABLE {ec_flexicharge} ADD roles TEXT NOT NULL');
      $ret[] = update_sql('ALTER TABLE {ec_flexicharge} ADD taxonomy TEXT NOT NULL');
      break;
    case 'pgsql':
      db_add_column($ret, 'ec_flexicharge', 'token', 'SMALLINT', array('not null' => TRUE, 'default' => "0"));
      db_add_column($ret, 'ec_flexicharge', 'token', 'TEXT', array('not null' => TRUE, 'default' => "''"));
      db_add_column($ret, 'ec_flexicharge', 'token', 'TEXT', array('not null' => TRUE, 'default' => "''"));
      db_add_column($ret, 'ec_flexicharge', 'token', 'TEXT', array('not null' => TRUE, 'default' => "''"));
      break;
  }
  return $ret;
}

/**
 * Add a table for storing product attributes.
 */
function flexicharge_update_2() {
  $ret = array();
  switch($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {ec_flexicharge_product} (
        vid int(10) unsigned NOT NULL default '0',
        nid int(10) unsigned NOT NULL default '0',
        attributes TEXT NOT NULL default '',
        KEY (vid),
        KEY (nid)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {ec_flexicharge_product} (
        vid int NOT NULL default '0',
        nid int NOT NULL default '0',
        attributes TEXT NOT NULL default ''
      )");
      break;
  }
  return $ret;
}

