/*
 * $Id: Interbase2MS_SqlServer.java,v 1.2 2008/04/17 10:01:01 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: Interbase2MS_SqlServer.java,v $
 * Revision 1.2  2008/04/17 10:01:01  marbobo
 * + Velocity-Templates angepasst
 *
 * Revision 1.1  2007/08/28 12:16:08  korowa
 * Export- und Importfunktionen hinzugefügt
 *
 * Revision 1.8  2005/03/21 20:30:46  marbobo
 * + DocSize auf Type Long umgestellt
 *
 * Revision 1.7  2005/01/07 20:19:29  jonyp
 * *** empty log message ***
 *
 * Revision 1.6  2004/11/22 12:50:19  koppy
 * Refactoring: Die innere Klasse Options nach vkc.util verschoben
 *
 * Revision 1.5  2004/11/19 13:59:45  koppy
 * Auf Interbase/Firebird SQL-Parser.
 *
 * Revision 1.4  2004/10/22 12:13:53  marbobo
 * - Unnötige Castings entfernt
 *
 * Revision 1.3  2004/10/14 10:15:09  jonyp
 * increment generator value by one
 *
 * Revision 1.2  2004/10/12 13:47:11  jonyp
 * added start/stop comments
 *
 * 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 Interbase2MS_SqlServer {
	
	
	public Interbase2MS_SqlServer(){
	}
	

	/**
	 * 
	 */
	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_oracle9i_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 " + ( Integer.parseInt( ss[7] ) + 1 ) +  " 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;
			}
			
			// Oracle-spezifisches Zeug ersetzen
			s = s.replaceAll( "BIGINT", "INTEGER" );
			s = s.replaceAll( "VARCHAR", "VARCHAR2" );
			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 ) {
		
	    System.out.println( "start converting interbase to MS SQL Server setup sql ..." );
	    Interbase2MS_SqlServer i2o = new Interbase2MS_SqlServer( );
		
		try {
			i2o.convert( "db/dbschema/scripts/interbase/vkc_db_interbase_setup.sql" );
		} catch ( IOException e ) {
			e.printStackTrace();
		}
		
		System.out.println( "...finished" );		
	}

}