/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * published by the Free Software Foundation; either version 2.1 of
 * the license, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite
 * 330, Boston, MA  02111-1307, USA.
 * 
 */
package com.k_int.xrtree;

import java.sql.Connection;
import java.sql.SQLException;


/**
 * Title:		XRDbBuilder
 * @author:		Rob Tice (rob.tice@k-int.com)
 * @version:	$Id: $
 * Copyright:	Copyright 1999-2006 Knowledge Integration Ltd
 * Company: 	Knowledge Integration Ltd
 * Description:
 * 
 * 
 *
 * Created:		Jun 13, 2006
 * 
 * 
 * History
 *				$Log: $
 * 		
 */

public class XRDbBuilder
{
    @SuppressWarnings("unused")
	private Connection conn;
  
    public XRDbBuilder(Connection conn) throws SQLException
    {
        this.conn=conn;  
        
        try
        {
          conn.createStatement().execute("DROP TABLE xr_tree");
          conn.createStatement().execute("DROP TABLE data");
          conn.createStatement().execute("DROP TABLE relationships");
          conn.createStatement().execute("DROP TABLE next_xr");
          conn.createStatement().execute("DROP TABLE categories");
          conn.createStatement().execute("DROP TABLE assist_links");
          conn.createStatement().execute("DROP TABLE vocab_revision");
        }
        catch(Exception e)
        {
          
        }
         
          //-------------------------------------------------
          
          conn.createStatement().execute("CREATE TABLE  xr_tree(tree_index VARCHAR(255) NOT NULL PRIMARY KEY, data_id VARCHAR(255), source_id VARCHAR(255))"); 
          conn.createStatement().execute("CREATE TABLE  data (id VARCHAR(255), name VARCHAR(450), source VARCHAR(255), type INT,description VARCHAR(800),PRIMARY KEY(source, id))");
          conn.createStatement().execute("CREATE TABLE  relationships (source_id VARCHAR(255) NOT NULL, destination_id VARCHAR(255) NOT NULL, type INT NOT NULL)");
          conn.createStatement().execute("CREATE TABLE  next_xr (next_xr_index INT)");                
          conn.createStatement().execute("CREATE TABLE  categories (data_id VARCHAR(512), cat_id VARCHAR(512))");                
          conn.createStatement().execute("CREATE TABLE  assist_links (data_id VARCHAR(512), link_id VARCHAR(512))");                       
          conn.createStatement().execute("CREATE TABLE  vocab_revision (data_guid VARCHAR(512), data_id VARCHAR(512), revision VARCHAR(256), authority VARCHAR(128))");                       
          conn.createStatement().execute("CREATE INDEX  rel_index on relationships(source_id, destination_id)");
          conn.createStatement().execute("CREATE INDEX  xr_data_index on xr_tree(tree_index,data_id,source_id)");
          conn.createStatement().execute("CREATE INDEX  data_index on data(source,id,type)");
          conn.createStatement().execute("CREATE INDEX  cat_index on categories(data_id,cat_id)");
          conn.createStatement().execute("CREATE INDEX  link_index on assist_links(data_id,link_id)");
          conn.createStatement().execute("CREATE INDEX  revision_index on vocab_revision(data_guid,data_id,revision,authority)");
          
    }   
}
