All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.antiaction.raptor.sql.ExecuteSqlFile Maven / Gradle / Ivy

The newest version!
package com.antiaction.raptor.sql;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.AbstractMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ExecuteSqlFile {

	public static List> splitSql(File sqlFile, String charsetName) throws IOException {
		return splitSql( new FileInputStream( sqlFile ), charsetName, 8192 );
	}

	public static List> splitSql(File sqlFile, String charsetName, int bufferSize) throws IOException {
		return splitSql( new FileInputStream( sqlFile ), charsetName, bufferSize );
	}

	public static List> splitSql(InputStream in, String charsetName) throws IOException {
		return splitSql( new BufferedReader( new InputStreamReader( in, charsetName ), 8192 ) );
	}

	public static List> splitSql(InputStream in, String charsetName, int bufferSize) throws IOException {
		return splitSql( new BufferedReader( new InputStreamReader( in, charsetName ), bufferSize ) );
	}

	public static List> splitSql(BufferedReader reader) throws IOException {
		List> statements = new LinkedList>();
		StringBuilder sb = new StringBuilder();
		sb.setLength( 0 );
		String tmpStr;
		int startLine = 1;
		int currline = 0;
		boolean bStatement = false;
		while ( (tmpStr = reader.readLine()) != null ) {
			++currline;
			if ( tmpStr.length() > 0 && !"GO".equalsIgnoreCase( tmpStr ) ) {
				if ( !tmpStr.startsWith( "--" ) ) {
					if ( tmpStr.trim().endsWith(";") ) {
						tmpStr = tmpStr.substring( 0, tmpStr.lastIndexOf( ';' ) );
						bStatement = true;
					}
					sb.append( tmpStr );
					sb.append( "\n" );
				}
			}
			else {
				if ( sb.length() > 0 ) {
					bStatement = true;
				}
				else {
					startLine = currline + 1;
				}
			}
			if ( bStatement ) {
				statements.add( new AbstractMap.SimpleEntry( startLine + "-" + currline, sb.toString() ) );
				sb.setLength( 0 );
				bStatement = false;
				startLine = currline;
			}
		}
		if ( sb.length() > 0 ) {
			statements.add( new AbstractMap.SimpleEntry( startLine + "-" + currline, sb.toString() ) );
			sb.setLength( 0 );
		}
		reader.close();
		return statements;
	}

	public static void executeStatements(Connection conn, List> statements) throws SQLException {
		Iterator> iter = statements.iterator();
		Map.Entry entry;
		Statement stm = null;
		String interval = null;
		String sql = null;
		int res;
		try {
			while (iter.hasNext()) {
				entry = iter.next();
				interval = entry.getKey();
				sql = entry.getValue();
				stm = conn.createStatement();
				res = stm.executeUpdate( sql );
				// debug
				//System.out.println( res );
				/*
				if ( res <= 0 ) {
					throw new SQLException( "Statement.executeUpdate() rowcount not positive!" );
				}
				*/
				stm.close();
				stm = null;
			}
		}
		catch (SQLException e) {
			System.out.println( "SQLException executing (Line(s): " + interval + "): " + sql );
			e.printStackTrace();
			throw e;
		}
		finally {
			if ( stm != null ) {
				stm.close();
				stm = null;
			}
		}
	}

	public static void executeSqlFile(Connection conn, File sqlFile) throws IOException, SQLException {
		List> statements = splitSql( sqlFile, "UTF-8", 8192 );
		executeStatements( conn, statements );
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy