-
-
Notifications
You must be signed in to change notification settings - Fork 23
Script support
Mark Rotteveel edited this page Jul 19, 2015
·
1 revision
Q: Does Jaybird support script execution?
A: No, currently Jaybird does not support script execution. You have either to call isql from Java code through Runtime.exec(...)
call or you parse the script yourself and submit each command separately. In latter case pay attention to SET ...
commands, especially to SET TERM
.
Simple script executor can look like this:
import java.sql.Connection;
import java.io.IOException;
import java.sql.SQLException;
import java.io.Reader;
import java.sql.Statement;
public class ScriptExecutor {
public static final String DEFAULT_SET_TERM = "SET TERM";
public static final String DEFAULT_TERM = ";";
private String setTermCommand = DEFAULT_SET_TERM;
private String term = DEFAULT_TERM;
public ScriptExecutor() {
}
public ScriptExecutor(String defaultTerm, String setTermCommand) {
this.term = defaultTerm;
this.setTermCommand = setTermCommand;
}
public void executeScript(Connection connection, Reader in)
throws SQLException, IOException
{
StringBuffer sb = new StringBuffer();
Statement stmt = connection.createStatement();
try {
int ch = 0;
while ((ch = in.read()) != -1) {
sb.append((char)ch);
String possibleTerm =
sb.substring(sb.length() - term.length());
if (possibleTerm.equals(term)) {
String command =
sb.substring(0, sb.length() - term.length()).trim();
processCommand(stmt, command);
}
}
} finally {
stmt.close();
}
}
protected void processCommand(Statement stmt, String command)
throws SQLException, IOException
{
if (command.toUpperCase().startsWith(setTermCommand)) {
term = command.substring(setTermCommand.length()).trim();
} else
stmt.execute(command);
}
}