⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlfile.java

📁 hsqldb是100%java实现的数据库,是一个开放源代码的JAVA数据库 l 具有标准的SQL语法和JAVA接口 l HSQLDB可以自由使用和分发 l 非常简洁和快速的
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                        lineStop = fromHist.length();
                    }

                    // System.err.println("["
                    // + fromHist.substring(lineStart, lineStop) + ']');
                    int i;

                    if (modeGlobal) {
                        i = lineStop;

                        while ((i = fromHist.lastIndexOf(from, i - 1))
                                >= lineStart) {
                            sb.replace(i, i + from.length(), to);
                        }
                    } else if ((i = fromHist.indexOf(from, lineStart)) > -1
                               && i < lineStop) {
                        sb.replace(i, i + from.length(), to);
                    }

                    //statementHistory[curHist] = sb.toString();
                    curCommand = sb.toString();

                    setBuf(curCommand);
                    stdprintln((modeExecute ? "Executing"
                                            : "Current Buffer") + ":\n"
                                            + curCommand);

                    if (modeExecute) {
                        stdprintln();
                    }
                } catch (BadSwitch badswitch) {
                    throw new BadSpecial(
                        "Substitution syntax:  \":s/from this/to that/i;g2\".  "
                        + "Use '$' for line separations.  ["
                        + badswitch.getMessage() + ']');
                }

                if (modeExecute) {
                    processSQL();
                    stringBuffer.setLength(0);
                }

                return;

            case '?' :
                stdprintln(BUFFER_HELP_TEXT);

                return;
        }

        throw new BadSpecial("Unknown Buffer Command");
    }

    private boolean doPrepare   = false;
    private String  prepareVar  = null;
    private String  csvColDelim = null;
    private String  csvRowDelim = null;
    private static final String CSV_SYNTAX_MSG =
        "Export syntax:  x table_or_view_anme "
        + "[column_delimiter [record_delimiter]]";

    /**
     * Process a Special Command.
     *
     * @param inString Complete command, less the leading '\' character.
     * @throws SQLException Passed through from processSQL()
     * @throws BadSpecial Runtime error()
     * @throws QuitNot Command execution (but not the JVM!) should stop
     */
    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 'm' :
                if (arg1.length() != 1 || other == null) {
                    throw new BadSpecial();
                }

                csvColDelim =
                    convertEscapes((String) userVars.get("*CSV_COL_DELIM"));
                csvRowDelim =
                    convertEscapes((String) userVars.get("*CSV_ROW_DELIM"));
                csvNullRep = (String) userVars.get("*CSV_NULL_REP");

                if (csvColDelim == null) {
                    csvColDelim = DEFAULT_COL_DELIM;
                }

                if (csvRowDelim == null) {
                    csvRowDelim = DEFAULT_ROW_DELIM;
                }

                if (csvNullRep == null) {
                    csvNullRep = DEFAULT_NULL_REP;
                }

                try {
                    importCsv(other);
                } catch (IOException ioe) {
                    System.err.println("Failed to read in CSV file:  " + ioe);
                }

                return;

            case 'x' :
                try {
                    if (arg1.length() != 1 || other == null) {
                        throw new BadSpecial();
                    }

                    String tableName = ((other.indexOf(' ') > 0) ? null
                                                                 : other);

                    csvColDelim = convertEscapes(
                        (String) userVars.get("*CSV_COL_DELIM"));
                    csvRowDelim = convertEscapes(
                        (String) userVars.get("*CSV_ROW_DELIM"));
                    csvNullRep = (String) userVars.get("*CSV_NULL_REP");

                    String csvFilepath =
                        (String) userVars.get("*CSV_FILEPATH");

                    if (csvFilepath == null && tableName == null) {
                        throw new BadSpecial(
                            "You must set PL variable '*CSV_FILEPATH' in "
                            + "order to use the query variant of \\x");
                    }

                    File csvFile = new File((csvFilepath == null)
                                            ? (tableName + ".csv")
                                            : csvFilepath);

                    if (csvColDelim == null) {
                        csvColDelim = DEFAULT_COL_DELIM;
                    }

                    if (csvRowDelim == null) {
                        csvRowDelim = DEFAULT_ROW_DELIM;
                    }

                    if (csvNullRep == null) {
                        csvNullRep = DEFAULT_NULL_REP;
                    }

                    pwCsv = new PrintWriter(
                        new OutputStreamWriter(
                            new FileOutputStream(csvFile), charset));

                    displayResultSet(
                        null,
                        curConn.createStatement().executeQuery(
                            (tableName == null) ? other
                                                : ("SELECT * FROM "
                                                   + tableName)), null, null);
                    pwCsv.flush();
                    stdprintln("Wrote " + csvFile.length()
                               + " characters to file '" + csvFile + "'");
                } catch (Exception e) {
                    if (e instanceof BadSpecial) {

                        // Not sure this test is right.  Maybe .length() == 0?
                        if (e.getMessage() == null) {
                            throw new BadSpecial(CSV_SYNTAX_MSG);
                        } else {
                            throw (BadSpecial) e;
                        }
                    }

                    throw new BadSpecial("Failed to write to file '" + other
                                         + "':  " + e);
                } finally {

                    // Reset all state changes
                    if (pwCsv != null) {
                        pwCsv.close();
                    }

                    pwCsv       = null;
                    csvColDelim = null;
                    csvRowDelim = null;
                }

                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());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -