Skip to content

Instantly share code, notes, and snippets.

@kdehairy
Last active December 15, 2015 23:19
Show Gist options
  • Save kdehairy/5339450 to your computer and use it in GitHub Desktop.
Save kdehairy/5339450 to your computer and use it in GitHub Desktop.
Splits a sql script into an array of string statements
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