Last active
December 15, 2015 23:19
-
-
Save kdehairy/5339450 to your computer and use it in GitHub Desktop.
Splits a sql script into an array of string statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String[] parseSqlFile( InputStream input ) throws IOException | |
{ | |
BufferedReader reader = new BufferedReader( new InputStreamReader( input ) ); | |
String line; | |
StringBuilder sql = new StringBuilder(); | |
String multiLineComment = null; | |
while ( (line = reader.readLine()) != null ) { | |
// line = line.trim(); | |
// Check for start of multi-line comment | |
if ( multiLineComment == null ) { | |
// Check for first multi-line comment type | |
if ( line.startsWith( "/*" ) ) { | |
if ( !line.endsWith( "}" ) ) { | |
multiLineComment = "/*"; | |
} | |
// Check for second multi-line comment type | |
} else if ( line.startsWith( "{" ) ) { | |
if ( !line.endsWith( "}" ) ) { | |
multiLineComment = "{"; | |
} | |
// Append line if line is not empty or a single line comment | |
} else if ( !line.startsWith( "--" ) && !line.equals( "" ) ) { | |
sql.append( line ); | |
} // Check for matching end comment | |
} else if ( multiLineComment.equals( "/*" ) ) { | |
if ( line.endsWith( "*/" ) ) { | |
multiLineComment = null; | |
} | |
// Check for matching end comment | |
} else if ( multiLineComment.equals( "{" ) ) { | |
if ( line.endsWith( "}" ) ) { | |
multiLineComment = null; | |
} | |
} | |
} | |
reader.close(); | |
return sql.toString().split( ";" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment