/*
 * $Id: Interbase2Oracle8i.java,v 1.6 2008/04/17 10:01:01 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: Interbase2Oracle8i.java,v $
 * Revision 1.6  2008/04/17 10:01:01  marbobo
 * + Velocity-Templates angepasst
 *
 * Revision 1.5  2005/03/21 20:30:46  marbobo
 * + DocSize auf Type Long umgestellt
 *
 * Revision 1.4  2004/11/22 12:50:19  koppy
 * Refactoring: Die innere Klasse Options nach vkc.util verschoben
 *
 * Revision 1.3  2004/11/19 13:59:45  koppy
 * Auf Interbase/Firebird SQL-Parser.
 *
 * Revision 1.2  2004/10/22 12:13:53  marbobo
 * - Unnötige Castings entfernt
 *
 * Revision 1.1  2004/09/30 13:35:26  koppy
 * Interbase2Oracle SQL-Konverter
 *
 */
package vkc.db.helper;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * This class represents 
 * 
 * @created 16.04.2004
 * @author <a href="mailto:andreas.koop@c-lab.de">Andreas Koop</a>
 */
public class Interbase2Oracle8i {
	
	
	public Interbase2Oracle8i(){
	}
	

	/**
	 * 
	 */
	private void convert(String ddlFile) throws IOException{
	    
	    InterbaseSQLParser parser = new InterbaseSQLParser(ddlFile);
	    parser.setRemoveStmtLimiter(false);
	    
	    List stmts = parser.parseSQLFile();
		
		BufferedOutputStream bos = new BufferedOutputStream( 
		        new FileOutputStream( 
		                new File( parser.getFile().getParentFile().getParentFile(), 
		                        "oracle/vkc_db_oracle8i_setup.sql" ) ) );
		
		String lastGenStmt = "";
		
		for (Iterator iter = stmts.iterator(); iter.hasNext();) {
            String s = (String) iter.next();
				
			if ( s.toLowerCase().startsWith( "create generator " ) ) {
				lastGenStmt = s;
				continue;
			} else if ( s.toLowerCase().startsWith( "set generator " ) ) {
				s = lastGenStmt.substring( 0, lastGenStmt.indexOf( ";" ) ) + " " + s.substring( 0, s.length()-1 );
				String ss [] = s.split( "\\s" );
				s = "create sequence " + ss[2] + " start with " + ss[7] + " minvalue 0 increment by 1;";
				
			} else if ( s.toLowerCase().startsWith( "create unique asc " ) ) {
				String ss [] = s.split( "\\s|,|\\)|\\(|;" );
				s = "create unique index " + ss[4] + " on " + ss[6] + "(\n";

				for ( int i = 7; i < ss.length; i++ ) {
					if ( ss[i].trim().length() > 0 ) {
						s += ss[i] + " asc,\n";	
					}
				}
				
				s = s.substring( 0, s.length()-2 ) + "\n);";
			} else if ( s.toLowerCase().startsWith( "create asc " ) ) {
				String ss [] = s.split( "\\s|,|\\)|\\(|;" );
				s = "create index " + ss[3] + " on " + ss[5] + "(\n";
				
				for ( int i = 6; i < ss.length; i++ ) {
					if ( ss[i].trim().length() > 0 ) {
						s += ss[i] + " asc,\n";
					}
				}
				
				s = s.substring( 0, s.length()-2 ) + "\n);";
			}
			
			if ( s.toLowerCase().startsWith( "create table " ) || s.toLowerCase().startsWith( "create view " ) ) {
				//String ss [] = s.split( "\\s" );
				//s = "drop " + ss[1] + " " + ss[2] +"\n/\n" + s;
			}
			
			// Oracle8i-spezifisches zeug ersetzen
			s = s.replaceAll( "BIGINT", "INTEGER" );
			s = s.replaceAll( "VARCHAR", "VARCHAR2" );
			s = s.replaceAll( "\\sTIMESTAMP", " DATE" );
			s = s.replaceAll( "CURRENT_TIMESTAMP", "SYSDATE" );
			s = s.replaceAll( "\n\\s\n", "\n" );
			s = s.substring( 0, s.length()-1 ) + "\n/";
			
			bos.write( s.getBytes() );
			bos.write( '\n' );
			bos.write( '\n' );
			
		}
		
		bos.flush();
		bos.close();
	}
	
	public static void main(String[] args) {

	    Interbase2Oracle8i i2o = new Interbase2Oracle8i( );
		
		try {
			i2o.convert( "db/dbschema/scripts/interbase/vkc_db_interbase_setup.sql" );
		} catch ( IOException e ) {
			e.printStackTrace();
		} 
	}
}