in jisql/src/main/java/org/apache/util/sql/Jisql.java [353:565]
public void doIsql() throws IOException, SQLException {
BufferedReader reader;
Statement statement;
ResultSet resultSet = null;
ResultSetMetaData resultSetMetaData;
StringBuilder query;
if (inputFileName != null) {
try {
reader = new BufferedReader(new FileReader(inputFileName));
} catch (FileNotFoundException fnfe) {
System.err.println("Unable to open file \"" + inputFileName + "\"");
fnfe.printStackTrace(System.err);
throw fnfe;
}
} else {
reader = new BufferedReader(new InputStreamReader(System.in));
}
if (printDebug) {
printAllExceptions(connection.getWarnings());
}
statement = connection.createStatement();
connection.clearWarnings();
try {
while (true) {
int linecount = 1;
query = new StringBuilder();
try {
if ((inputFileName == null) && (inputQuery == null)) {
System.out.print("\nEnter a query:\n");
}
while (true) {
if ((inputFileName == null) && (inputQuery == null)) {
System.out.print(linecount++ + " > ");
System.out.flush();
}
String line;
if (inputQuery == null) {
line = reader.readLine();
} else {
line = inputQuery;
}
if (line == null || line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit")) {
if ((inputFileName != null) && (inputQuery != null)) {
break;
} else {
return;
}
}
if (line.equals("reset")) {
query = new StringBuilder();
break;
}
String trimmedLine = line.trim();
if (trimmedLine.startsWith("--") || trimmedLine.isEmpty()) {
continue;
}
if (connectString.toLowerCase().startsWith("jdbc:oracle") && inputFileName != null) {
if (trimmedLine.startsWith("/") || trimmedLine.length() < 2) {
commandTerminator = ";";
continue;
}
if (trimmedLine.toUpperCase().startsWith("DECLARE")) {
commandTerminator = "/";
}
if ((trimmedLine.toUpperCase().startsWith("CREATE OR REPLACE PROCEDURE")) || (trimmedLine.toUpperCase().startsWith("CREATE OR REPLACE FUNCTION"))) {
commandTerminator = "/";
}
}
if (connectString.toLowerCase().startsWith("jdbc:postgresql") && inputFileName != null) {
if (trimmedLine.toLowerCase().startsWith("select 'delimiter start';")) {
commandTerminator = "select 'delimiter end';";
continue;
}
}
if (line.trim().equalsIgnoreCase(commandTerminator) || line.trim().endsWith(commandTerminator)) {
if (line.trim().endsWith(commandTerminator)) {
line = line.substring(0, line.length() - commandTerminator.length());
query.append("\n");
query.append(line);
}
break;
}
query.append("\n");
query.append(line);
}
if (query.toString().isEmpty()) {
continue;
}
if (printDebug) {
System.out.println("executing: " + query);
}
boolean moreResults = statement.execute(query.toString());
int rowsAffected = 0;
do {
if (printDebug) {
printAllExceptions(statement.getWarnings());
}
statement.clearWarnings();
if (moreResults) {
resultSet = statement.getResultSet();
if (printDebug) {
printAllExceptions(resultSet.getWarnings());
}
resultSet.clearWarnings();
resultSetMetaData = resultSet.getMetaData();
formatter.formatHeader(System.out, resultSetMetaData);
formatter.formatData(System.out, resultSet, resultSetMetaData);
formatter.formatFooter(System.out, resultSetMetaData);
int rowsSelected = statement.getUpdateCount();
if (rowsSelected >= 0 && printDebug) {
System.out.println(rowsSelected + " rows affected.");
}
} else {
rowsAffected = statement.getUpdateCount();
if (printDebug) {
printAllExceptions(statement.getWarnings());
}
statement.clearWarnings();
if (rowsAffected >= 0 && printDebug) {
System.out.println(rowsAffected + " rows affected.");
}
}
//
// I was having problems with the PostgreSQL driver throwing
// a NullPointerException here so I just catch it and tell
// the loop that it is done if it happens.
//
try {
moreResults = statement.getMoreResults();
} catch (NullPointerException npe) {
moreResults = false;
}
}
while (moreResults || rowsAffected != -1);
} catch (SQLException sqle) {
printAllExceptions(sqle);
statement.cancel();
statement.clearWarnings();
throw sqle;
} catch (Exception e) {
e.printStackTrace(System.err);
}
if (inputQuery != null) {
return;
}
}
} finally {
try {
reader.close();
} catch (IOException ioe) {
// Ignore IOE when closing streams
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException sqle) {
// ignore
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException sqle) {
// Ignore
}
}
}
}