📄 sqlfile.java
字号:
/*
* This is reset upon each execute() invocation (to true if interactive,
* false otherwise).
*/
private boolean continueOnError = false;
private static final String DEFAULT_CHARSET = "US-ASCII";
private BufferedReader br = null;
private String charset = null;
/**
* Process all the commands in the file (or stdin) associated with
* "this" object.
* Run SQL in the file through the given database connection.
*
* This is synchronized so that I can use object variables to keep
* track of current line number, command, connection, i/o streams, etc.
*
* Sets encoding character set to that specified with System Property
* 'sqlfile.charset'. Defaults to "US-ASCII".
*
* @param conn The JDBC connection to use for SQL Commands.
*/
public synchronized void execute(Connection conn, PrintStream stdIn,
PrintStream errIn,
Boolean coeOverride)
throws IOException, SqlToolError,
SQLException {
psStd = stdIn;
psErr = errIn;
curConn = conn;
curLinenum = -1;
String inputLine;
String trimmedCommand;
String trimmedInput;
String deTerminated;
boolean inComment = false; // Globbling up a comment
int postCommentIndex;
boolean gracefulExit = false;
continueOnError = (coeOverride == null) ? interactive
: coeOverride.booleanValue();
if (userVars != null && userVars.size() > 0) {
plMode = true;
}
String specifiedCharSet = System.getProperty("sqlfile.charset");
charset = ((specifiedCharSet == null) ? DEFAULT_CHARSET
: specifiedCharSet);
try {
br = new BufferedReader(new InputStreamReader((file == null)
? System.in
: new FileInputStream(file), charset));
curLinenum = 0;
if (interactive) {
stdprintln(BANNER);
}
while (true) {
if (interactive) {
psStd.print((stringBuffer.length() == 0)
? (chunking ? chunkPrompt
: primaryPrompt)
: contPrompt);
}
inputLine = br.readLine();
if (inputLine == null) {
/*
* This is because interactive EOD on some OSes doesn't
* send a line-break, resulting in no linebreak at all
* after the SqlFile prompt or whatever happens to be
* on their screen.
*/
if (interactive) {
psStd.println();
}
break;
}
curLinenum++;
if (chunking) {
if (inputLine.equals(".")) {
chunking = false;
setBuf(stringBuffer.toString());
stringBuffer.setLength(0);
if (interactive) {
stdprintln("Raw SQL chunk moved into buffer. "
+ "Run \":;\" to execute the chunk.");
}
} else {
if (stringBuffer.length() > 0) {
stringBuffer.append('\n');
}
stringBuffer.append(inputLine);
}
continue;
}
if (inComment) {
postCommentIndex = inputLine.indexOf("*/") + 2;
if (postCommentIndex > 1) {
// I see no reason to leave comments in history.
inputLine = inputLine.substring(postCommentIndex);
// Empty the buffer. The non-comment remainder of
// this line is either the beginning of a new SQL
// or Special command, or an empty line.
stringBuffer.setLength(0);
inComment = false;
} else {
// Just completely ignore the input line.
continue;
}
}
trimmedInput = inputLine.trim();
try {
// This is the try for SQLException. SQLExceptions are
// normally thrown below in Statement processing, but
// could be called up above if a Special processing
// executes a SQL command from history.
if (stringBuffer.length() == 0) {
if (trimmedInput.startsWith("/*")) {
postCommentIndex = trimmedInput.indexOf("*/", 2)
+ 2;
if (postCommentIndex > 1) {
// I see no reason to leave comments in
// history.
inputLine = inputLine.substring(
postCommentIndex + inputLine.length()
- trimmedInput.length());
trimmedInput = inputLine.trim();
} else {
// Just so we get continuation lines:
stringBuffer.append("COMMENT");
inComment = true;
continue;
}
}
// This is just to filter out useless newlines at
// beginning of commands.
if (trimmedInput.length() == 0) {
continue;
}
if (trimmedInput.charAt(0) == '*'
&& (trimmedInput.length() < 2
|| trimmedInput.charAt(1) != '{')) {
try {
processPL((trimmedInput.length() == 1) ? ""
: trimmedInput
.substring(1)
.trim());
} catch (BadSpecial bs) {
errprintln("Error at '"
+ ((file == null) ? "stdin"
: file.toString()) + "' line "
+ curLinenum
+ ":\n\""
+ inputLine
+ "\"\n"
+ bs.getMessage());
if (!continueOnError) {
throw new SqlToolError(bs);
}
}
continue;
}
if (trimmedInput.charAt(0) == '\\') {
try {
processSpecial(trimmedInput.substring(1));
} catch (BadSpecial bs) {
errprintln("Error at '"
+ ((file == null) ? "stdin"
: file.toString()) + "' line "
+ curLinenum
+ ":\n\""
+ inputLine
+ "\"\n"
+ bs.getMessage());
if (!continueOnError) {
throw new SqlToolError(bs);
}
}
continue;
}
if (trimmedInput.charAt(0) == ':'
&& (interactive
|| (trimmedInput.charAt(1) == ';'))) {
try {
processBuffer(trimmedInput.substring(1));
} catch (BadSpecial bs) {
errprintln("Error at '"
+ ((file == null) ? "stdin"
: file.toString()) + "' line "
+ curLinenum
+ ":\n\""
+ inputLine
+ "\"\n"
+ bs.getMessage());
if (!continueOnError) {
throw new SqlToolError(bs);
}
}
continue;
}
String ucased = trimmedInput.toUpperCase();
if (ucased.startsWith("DECLARE")
|| ucased.startsWith("BEGIN")) {
chunking = true;
stringBuffer.append(inputLine);
if (interactive) {
stdprintln(
"Enter RAW SQL. No \\, :, * commands. "
+ "End with a line containing only \".\":");
}
continue;
}
}
if (trimmedInput.length() == 0) {
// Blank lines delimit commands ONLY IN INTERACTIVE
// MODE!
if (interactive &&!inComment) {
setBuf(stringBuffer.toString());
stringBuffer.setLength(0);
stdprintln("Current input moved into buffer.");
}
continue;
}
deTerminated = deTerminated(inputLine);
// A null terminal line (i.e., /\s*;\s*$/) is never useful.
if (!trimmedInput.equals(";")) {
if (stringBuffer.length() > 0) {
stringBuffer.append('\n');
}
stringBuffer.append((deTerminated == null) ? inputLine
: deTerminated);
}
if (deTerminated == null) {
continue;
}
// If we reach here, then stringBuffer contains a complete
// SQL command.
curCommand = stringBuffer.toString();
trimmedCommand = curCommand.trim();
if (trimmedCommand.length() == 0) {
throw new SQLException("Empty SQL Statement");
}
setBuf(curCommand);
processSQL();
} catch (SQLException se) {
errprintln("SQL Error at '" + ((file == null) ? "stdin"
: file.toString()) + "' line "
+ curLinenum
+ ":\n\""
+ curCommand
+ "\"\n"
+ se
.getMessage());
if (!continueOnError) {
throw se;
}
} catch (BreakException be) {
String msg = be.getMessage();
if ((!recursed) && (msg != null &&!msg.equals("file"))) {
errprintln("Unsatisfied break statement"
+ ((msg == null) ? ""
: (" (type " + msg
+ ')')) + '.');
} else {
gracefulExit = true;
}
if (recursed ||!continueOnError) {
throw be;
}
} catch (ContinueException ce) {
String msg = ce.getMessage();
if (!recursed) {
errprintln("Unsatisfied continue statement"
+ ((msg == null) ? ""
: (" (type " + msg
+ ')')) + '.');
} else {
gracefulExit = true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -