📄 sqlfile.java
字号:
userVars = new HashMap();
}
updateUserSettings();
if (file != null &&!file.canRead()) {
throw new IOException(rb.getString(SqltoolRB.SQLFILE_READFAIL,
file.toString()));
}
if (interactive) {
history = new ArrayList();
String histLenString = System.getProperty("sqltool.historyLength");
if (histLenString != null) try {
maxHistoryLength = Integer.parseInt(histLenString);
} catch (Exception e) {
} else {
maxHistoryLength = DEFAULT_HISTORY_SIZE;
}
}
}
/**
* Constructor for reading stdin instead of a file for commands.
*
* @see #SqlFile(File,boolean)
*/
public SqlFile(boolean inInteractive, Map inVars) throws IOException {
this(null, inInteractive, inVars);
}
/**
* Process all the commands on stdin.
*
* @param conn The JDBC connection to use for SQL Commands.
* @see #execute(Connection,PrintStream,PrintStream,boolean)
*/
public void execute(Connection conn,
Boolean coeOverride)
throws SqlToolError, SQLException {
execute(conn, System.out, System.err, coeOverride);
}
/**
* Process all the commands on stdin.
*
* @param conn The JDBC connection to use for SQL Commands.
* @see #execute(Connection,PrintStream,PrintStream,boolean)
*/
public void execute(Connection conn,
boolean coeOverride)
throws SqlToolError, SQLException {
execute(conn, System.out, System.err, new Boolean(coeOverride));
}
// So we can tell how to handle quit and break commands.
public boolean recursed = false;
private String lastSqlStatement = null;
private int curLinenum = -1;
private PrintStream psStd = null;
private PrintStream psErr = null;
private PrintWriter pwQuery = null;
private PrintWriter pwDsv = null;
StringBuffer immCmdSB = new StringBuffer();
private boolean continueOnError = false;
/*
* This is reset upon each execute() invocation (to true if interactive,
* false otherwise).
*/
private static final String DEFAULT_CHARSET = null;
// Change to Charset.defaultCharset().name(); once we can use Java 1.5!
private BufferedReader br = null;
private String charset = null;
private String buffer = 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.
* @throws SQLExceptions thrown by JDBC driver.
* Only possible if in "\c false" mode.
* @throws SqlToolError all other errors.
* This includes including QuitNow, BreakException,
* ContinueException for recursive calls only.
*/
public synchronized void execute(Connection conn, PrintStream stdIn,
PrintStream errIn,
Boolean coeOverride)
throws SqlToolError,
SQLException {
psStd = stdIn;
psErr = errIn;
curConn = conn;
curLinenum = -1;
String inputLine;
String trimmedInput;
String deTerminated;
boolean inComment = false; // Gobbling up a comment
int postCommentIndex;
boolean rollbackUncoms = true;
continueOnError = (coeOverride == null) ? interactive
: coeOverride.booleanValue();
if (userVars.size() > 0) {
plMode = true;
}
String specifiedCharSet = System.getProperty("sqlfile.charset");
charset = ((specifiedCharSet == null) ? DEFAULT_CHARSET
: specifiedCharSet);
try {
br = new BufferedReader((charset == null)
? (new InputStreamReader((file == null)
? System.in : (new FileInputStream(file))))
: (new InputStreamReader(((file == null)
? System.in : (new FileInputStream(file))),
charset)));
// Replace with just "(new FileInputStream(file), charset)"
// once use defaultCharset from Java 1.5 in charset init. above.
curLinenum = 0;
if (interactive) {
stdprintln(rb.getString(SqltoolRB.SQLFILE_BANNER, revnum));
}
while (true) {
if (interactive && magicPrefix == null) {
psStd.print((immCmdSB.length() > 0 || rawMode == RAW_DATA)
? contPrompt : ((rawMode == RAW_FALSE)
? primaryPrompt : rawPrompt));
}
inputLine = br.readLine();
if (magicPrefix != null) {
inputLine = magicPrefix + inputLine;
magicPrefix = null;
}
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 (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.
immCmdSB.setLength(0);
inComment = false;
} else {
// Just completely ignore the input line.
continue;
}
}
trimmedInput = inputLine.trim();
try {
if (rawMode != RAW_FALSE) {
boolean rawExecute = inputLine.equals(".;");
if (rawExecute || inputLine.equals(":.")) {
if (rawMode == RAW_EMPTY) {
rawMode = RAW_FALSE;
throw new SqlToolError(
rb.getString(SqltoolRB.RAW_EMPTY));
}
rawMode = RAW_FALSE;
setBuf(immCmdSB.toString());
immCmdSB.setLength(0);
if (rawExecute) {
historize();
processSQL();
} else if (interactive) {
stdprintln(rb.getString(
SqltoolRB.RAW_MOVEDTOBUFFER));
}
} else {
if (rawMode == RAW_DATA) {
immCmdSB.append('\n');
}
rawMode = RAW_DATA;
if (inputLine.length() > 0) {
immCmdSB.append(inputLine);
}
}
continue;
}
if (immCmdSB.length() == 0) {
// NEW Immediate Command (i.e., not appending).
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:
immCmdSB.append("COMMENT");
inComment = true;
continue;
}
}
if (trimmedInput.length() == 0) {
// This is just to filter out useless newlines at
// beginning of commands.
continue;
}
if ((trimmedInput.charAt(0) == '*'
&& (trimmedInput.length() < 2
|| trimmedInput.charAt(1) != '{'))
|| trimmedInput.charAt(0) == '\\') {
setBuf(trimmedInput);
processFromBuffer();
continue;
}
if (trimmedInput.charAt(0) == ':' && interactive) {
processBuffHist(trimmedInput.substring(1));
continue;
}
String ucased = trimmedInput.toUpperCase();
if (ucased.startsWith("DECLARE")
|| ucased.startsWith("BEGIN")) {
rawMode = RAW_EMPTY;
immCmdSB.append(inputLine);
if (interactive) {
stdprintln(RAW_LEADIN_MSG);
}
continue;
}
}
if (trimmedInput.length() == 0 && interactive &&!inComment) {
// Blank lines delimit commands ONLY IN INTERACTIVE
// MODE!
setBuf(immCmdSB.toString());
immCmdSB.setLength(0);
stdprintln(rb.getString(SqltoolRB.INPUT_MOVEDTOBUFFER));
continue;
}
deTerminated = SqlFile.deTerminated(inputLine);
// A null terminal line (i.e., /\s*;\s*$/) is never useful.
if (!trimmedInput.equals(";")) {
if (immCmdSB.length() > 0) {
immCmdSB.append('\n');
}
immCmdSB.append((deTerminated == null) ? inputLine
: deTerminated);
}
if (deTerminated == null) {
continue;
}
// If we reach here, then immCmdSB contains a complete
// SQL command.
if (immCmdSB.toString().trim().length() == 0) {
immCmdSB.setLength(0);
throw new SqlToolError(rb.getString(
SqltoolRB.SQLSTATEMENT_EMPTY));
// There is nothing inherently wrong with issuing
// an empty command, like to test DB server health.
// But, this check effectively catches many syntax
// errors early, and the DB check can be done by
// sending a comment like "// comment".
}
setBuf(immCmdSB.toString());
immCmdSB.setLength(0);
historize();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -