📄 sqlfile.java
字号:
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'"); } } throw new ContinueException(); } if (arg1.equals("break")) { if (toker.hasMoreTokens()) { String s = toker.nextToken("").trim(); if (s.equals("foreach") || s.equals("if") || s.equals("while") || s.equals("file")) { throw new BreakException(s); } else { throw new BadSpecial( "Bad break statement." + "You may use no argument or one of 'foreach', " + "'if', 'while', 'file'"); } } throw new BreakException(); } if (arg1.equals("list") || arg1.equals("listvalue")) { String s; boolean doValues = (arg1.equals("listvalue")); if (toker.countTokens() == 0) { stdprint(formatNicely(userVars, doValues)); } else { tokenArray = getTokenArray(toker.nextToken("")); if (doValues) { stdprintln("The outermost parentheses are not part of " + "the values."); } else { stdprintln("Showing variable names and length of values " + "(use 'listvalue' to see values)."); } for (int i = 0; i < tokenArray.length; i++) { s = (String) userVars.get(tokenArray[i]); stdprintln(" " + tokenArray[i] + ": " + (doValues ? ("(" + s + ')') : Integer.toString(s.length()))); } } return; } if (arg1.equals("dump") || arg1.equals("load")) { if (toker.countTokens() != 2) { throw new BadSpecial("Malformatted PL dump/load command"); } String varName = toker.nextToken(); File file = new File(toker.nextToken()); try { if (arg1.equals("dump")) { dump(varName, file); } else { load(varName, file); } } catch (Exception e) { throw new BadSpecial("Failed to dump/load variable '" + varName + "' to file '" + file + "'"); } return; } if (arg1.equals("prepare")) { if (toker.countTokens() != 1) { throw new BadSpecial("Malformatted prepare command"); } String s = toker.nextToken(); if (userVars.get(s) == null) { throw new SQLException("Use of unset PL variable: " + s); } prepareVar = s; doPrepare = true; return; } if (arg1.equals("foreach")) { if (toker.countTokens() < 2) { throw new BadSpecial("Malformatted PL foreach command (1)"); } String varName = toker.nextToken(); String parenExpr = toker.nextToken("").trim(); if (parenExpr.length() < 2 || parenExpr.charAt(0) != '(' || parenExpr.charAt(parenExpr.length() - 1) != ')') { throw new BadSpecial("Malformatted PL foreach command (2)"); } String[] values = getTokenArray(parenExpr.substring(1, parenExpr.length() - 1)); File tmpFile = null; String varVal; try { tmpFile = plBlockFile("foreach"); } catch (IOException ioe) { throw new BadSpecial( "Failed to write given PL block temp file: " + ioe); } String origval = (String) userVars.get(varName); try { SqlFile sf; for (int i = 0; i < values.length; i++) { try { varVal = values[i]; userVars.put(varName, varVal); sf = new SqlFile(tmpFile, false, userVars); sf.plMode = true; sf.recursed = true; // Share the possiblyUncommitted state sf.possiblyUncommitteds = possiblyUncommitteds; sf.execute(curConn, continueOnError); } catch (ContinueException ce) { String ceMessage = ce.getMessage(); if (ceMessage != null &&!ceMessage.equals("foreach")) { throw ce; } } } } catch (BreakException be) { String beMessage = be.getMessage(); if (beMessage != null &&!beMessage.equals("foreach")) { throw be; } } catch (QuitNow qe) { throw qe; } catch (Exception e) { throw new BadSpecial("Failed to execute SQL from PL block. " + e.getMessage()); } if (origval == null) { userVars.remove(varName); } else { userVars.put(varName, origval); } if (tmpFile != null &&!tmpFile.delete()) { throw new BadSpecial( "Error occurred while trying to remove temp file '" + tmpFile + "'"); } return; } if (arg1.equals("if")) { if (toker.countTokens() < 1) { throw new BadSpecial("Malformatted PL if command (1)"); } String parenExpr = toker.nextToken("").trim(); if (parenExpr.length() < 2 || parenExpr.charAt(0) != '(' || parenExpr.charAt(parenExpr.length() - 1) != ')') { throw new BadSpecial("Malformatted PL if command (2)"); } String[] values = getTokenArray(parenExpr.substring(1, parenExpr.length() - 1)); File tmpFile = null; try { tmpFile = plBlockFile("if"); } catch (IOException ioe) { throw new BadSpecial( "Failed to write given PL block temp file: " + ioe); } try { if (eval(values)) { SqlFile sf = new SqlFile(tmpFile, false, userVars); sf.plMode = true; sf.recursed = true; // Share the possiblyUncommitted state sf.possiblyUncommitteds = possiblyUncommitteds; sf.execute(curConn, continueOnError); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -