📄 sqlfile.java
字号:
}
return;
case 'p' :
if (other == null) {
stdprintln(true);
} else {
stdprintln(other, true);
}
return;
case 'a' :
if (other != null) {
curConn.setAutoCommit(
Boolean.valueOf(other).booleanValue());
}
stdprintln("Auto-commit is set to: "
+ curConn.getAutoCommit());
return;
case 'b' :
if (arg1.length() == 1) {
fetchBinary = true;
return;
}
if (arg1.charAt(1) == 'p') {
doPrepare = true;
return;
}
if ((arg1.charAt(1) != 'd' && arg1.charAt(1) != 'l')
|| other == null) {
throw new BadSpecial("Malformatted binary command");
}
File file = new File(other);
try {
if (arg1.charAt(1) == 'd') {
dump(file);
} else {
load(file);
}
} catch (Exception e) {
throw new BadSpecial(
"Failed to load/dump binary data to file '" + other
+ "'");
}
return;
case '*' :
case 'c' :
if (other != null) {
// But remember that we have to abort on some I/O errors.
continueOnError = Boolean.valueOf(other).booleanValue();
}
stdprintln("Continue-on-error is set to: " + continueOnError);
return;
case 's' :
showHistory();
return;
case '-' :
int commandsAgo = 0;
String numStr;
boolean executeMode = arg1.charAt(arg1.length() - 1) == ';';
if (executeMode) {
// Trim off terminating ';'
arg1 = arg1.substring(0, arg1.length() - 1);
}
numStr = (arg1.length() == 1) ? null
: arg1.substring(1,
arg1.length());
if (numStr == null) {
commandsAgo = 0;
} else {
try {
commandsAgo = Integer.parseInt(numStr);
} catch (NumberFormatException nfe) {
throw new BadSpecial("Malformatted command number");
}
}
setBuf(commandFromHistory(commandsAgo));
if (executeMode) {
processBuffer(";");
} else {
stdprintln(
"RESTORED following command to buffer. Enter \":?\" "
+ "to see buffer commands:\n"
+ commandFromHistory(0));
}
return;
case '?' :
stdprintln(HELP_TEXT);
return;
case '!' :
InputStream stream;
byte[] ba = new byte[1024];
String extCommand = ((arg1.length() == 1) ? ""
: arg1.substring(1)) + ((arg1.length() > 1 && other != null)
? " "
: "") + ((other == null)
? ""
: other);
try {
Process proc = Runtime.getRuntime().exec(extCommand);
proc.getOutputStream().close();
int i;
stream = proc.getInputStream();
while ((i = stream.read(ba)) > 0) {
stdprint(new String(ba, 0, i));
}
stream.close();
stream = proc.getErrorStream();
while ((i = stream.read(ba)) > 0) {
errprint(new String(ba, 0, i));
}
stream.close();
if (proc.waitFor() != 0) {
throw new BadSpecial("External command failed: '"
+ extCommand + "'");
}
} catch (Exception e) {
throw new BadSpecial("Failed to execute command '"
+ extCommand + "': " + e);
}
return;
case '.' :
chunking = true;
if (interactive) {
stdprintln("Enter RAW SQL. No \\, :, * commands. "
+ "End with a line containing only \".\":");
}
return;
}
throw new BadSpecial("Unknown Special Command");
}
private static final char[] nonVarChars = {
' ', '\t', '=', '}', '\n', '\r'
};
/**
* Returns index specifying 1 past end of a variable name.
*
* @param inString String containing a variable name
* @param startIndex Index within inString where the variable name begins
* @returns Index within inString, 1 past end of the variable name
*/
static int pastName(String inString, int startIndex) {
String workString = inString.substring(startIndex);
int e = inString.length(); // Index 1 past end of var name.
int nonVarIndex;
for (int i = 0; i < nonVarChars.length; i++) {
nonVarIndex = workString.indexOf(nonVarChars[i]);
if (nonVarIndex > -1 && nonVarIndex < e) {
e = nonVarIndex;
}
}
return startIndex + e;
}
/**
* Deference PL variables.
*
* @throws SQLException This is really an inappropriate exception
* type. Only using it because I don't have time to do things properly.
*/
private String dereference(String inString,
boolean permitAlias) throws SQLException {
String varName, varValue;
StringBuffer expandBuffer = new StringBuffer(inString);
int b, e; // begin and end of name. end really 1 PAST name
int nonVarIndex;
if (permitAlias && inString.trim().charAt(0) == '/') {
int slashIndex = inString.indexOf('/');
e = pastName(inString.substring(slashIndex + 1), 0);
// In this case, e is the exact length of the var name.
if (e < 1) {
throw new SQLException("Malformed PL alias use");
}
varName = inString.substring(slashIndex + 1, slashIndex + 1 + e);
varValue = (String) userVars.get(varName);
if (varValue == null) {
throw new SQLException("Undefined PL variable: " + varName);
}
expandBuffer.replace(slashIndex, slashIndex + 1 + e,
(String) userVars.get(varName));
}
String s;
while (true) {
s = expandBuffer.toString();
b = s.indexOf("*{");
if (b < 0) {
// No more unexpanded variable uses
break;
}
e = s.indexOf('}', b + 2);
if (e == b + 2) {
throw new SQLException("Empty PL variable name");
}
if (e < 0) {
throw new SQLException("Unterminated PL variable name");
}
varName = s.substring(b + 2, e);
if (!userVars.containsKey(varName)) {
throw new SQLException("Use of undefined PL variable: "
+ varName);
}
expandBuffer.replace(b, e + 1, (String) userVars.get(varName));
}
return expandBuffer.toString();
}
public boolean plMode = false;
// PL variable name currently awaiting query output.
private String fetchingVar = null;
private boolean silentFetch = false;
private boolean fetchBinary = false;
/**
* Process a Process Language Command.
* Nesting not supported yet.
*
* @param inString Complete command, less the leading '\' character.
* @throws BadSpecial Runtime error()
*/
private void processPL(String inString)
throws BadSpecial, SqlToolError, SQLException {
if (inString.length() < 1) {
plMode = true;
stdprintln("PL variable expansion mode is now on");
return;
}
if (inString.charAt(0) == '?') {
stdprintln(PL_HELP_TEXT);
return;
}
if (plMode) {
inString = dereference(inString, false);
}
StringTokenizer toker = new StringTokenizer(inString);
String arg1 = toker.nextToken();
String[] tokenArray = null;
// If user runs any PL command, we turn PL mode on.
plMode = true;
if (userVars == null) {
userVars = new HashMap();
}
if (arg1.equals("end")) {
throw new BadSpecial("PL end statements may only occur inside of "
+ "a PL block");
}
if (arg1.equals("continue")) {
if (toker.hasMoreTokens()) {
String s = toker.nextToken("").trim();
if (s.equals("foreach") || s.equals("while")) {
throw new ContinueException(s);
} else {
throw new BadSpecial(
"Bad continue statement."
+ "You may use no argument or one of 'foreach', "
+ "'while'");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -