📄 sqlfile.java
字号:
private void processSpecial(String inString) throws BadSpecial, QuitNow, SQLException, SqlToolError { int index = 0; int special; String arg1, other = null; if (inString.length() < 1) { throw new BadSpecial("Null special command"); } if (plMode) { inString = dereference(inString, false); } StringTokenizer toker = new StringTokenizer(inString); arg1 = toker.nextToken(); if (toker.hasMoreTokens()) { other = toker.nextToken("").trim(); } switch (arg1.charAt(0)) { case 'q' : if (other != null) { throw new QuitNow(other); } throw new QuitNow(); case 'H' : htmlMode = !htmlMode; stdprintln("HTML Mode is now set to: " + htmlMode); return; case 'd' : if (arg1.length() == 2) { listTables(arg1.charAt(1), other); return; } if (arg1.length() == 1 && other != null) { int space = other.indexOf(' '); if (space < 0) { describe(other, null); } else { describe(other.substring(0, space), other.substring(space + 1).trim()); } return; } throw new BadSpecial("Describe commands must be like " + "'\\dX' or like '\\d OBJECTNAME'."); case 'o' : if (other == null) { if (pwQuery == null) { throw new BadSpecial( "There is no query output file to close"); } closeQueryOutputStream(); return; } if (pwQuery != null) { stdprintln( "Closing current query output file and opening " + "new one"); closeQueryOutputStream(); } try { pwQuery = new PrintWriter( new OutputStreamWriter( new FileOutputStream(other, true), charset)); /* Opening in append mode, so it's possible that we will * be adding superfluous <HTML> and <BODY> tages. * I think that browsers can handle that */ pwQuery.println((htmlMode ? "<HTML>\n<!--" : "#") + " " + (new java.util.Date()) + ". Query output from " + getClass().getName() + (htmlMode ? ". -->\n\n<BODY>" : ".\n")); pwQuery.flush(); } catch (Exception e) { throw new BadSpecial("Failed to write to file '" + other + "': " + e); } return; case 'w' : if (other == null) { throw new BadSpecial( "You must supply a destination file name"); } if (commandFromHistory(0).length() == 0) { throw new BadSpecial("Empty command in buffer"); } try { PrintWriter pw = new PrintWriter( new OutputStreamWriter( new FileOutputStream(other, true), charset)); pw.println(commandFromHistory(0) + ';'); pw.flush(); pw.close(); } catch (Exception e) { throw new BadSpecial("Failed to append to file '" + other + "': " + e); } return; case 'i' : if (other == null) { throw new BadSpecial("You must supply an SQL file name"); } try { SqlFile sf = new SqlFile(new File(other), false, userVars); sf.recursed = true; // Share the possiblyUncommitted state sf.possiblyUncommitteds = possiblyUncommitteds; sf.plMode = plMode; sf.execute(curConn, continueOnError); } catch (ContinueException ce) { throw ce; } catch (BreakException be) { String beMessage = be.getMessage(); if (beMessage != null &&!beMessage.equals("file")) { throw be; } } catch (QuitNow qe) { throw qe; } catch (Exception e) { throw new BadSpecial("Failed to execute SQL from file '" + other + "': " + e.getMessage()); } 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"); } static private 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]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -