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

📄 transfersqltext.java

📁 hsqldb是100%java实现的数据库,是一个开放源代码的JAVA数据库 l 具有标准的SQL语法和JAVA接口 l HSQLDB可以自由使用和分发 l 非常简洁和快速的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    void parseFileForTheRest(TransferTable TTable,                             DataAccessPoint Dest)                             throws DataAccessPointException {        StringTokenizer Tokenizer;        StructureAlreadyParsed = true;        if (WTextRead == null) {            try {                WTextRead = new BufferedReader(new FileReader(sFileName));            } catch (IOException e) {                throw new DataAccessPointException(e.getMessage());            }        }        String        currentLine  = "";        String        Token        = "";        String        name         = "";        TransferTable relatedTable = null;        try {            while ((currentLine = WTextRead.readLine()) != null) {                currentLine = currentLine.trim() + ";";                Tokenizer   = new StringTokenizer(currentLine);                try {                    Token = Tokenizer.nextToken();                } catch (NoSuchElementException NSE) {                    continue;                }                if (Token == null) {                    continue;                }                if (Token.toUpperCase().equals("INSERT")) {                    try {                        if (!Tokenizer.nextToken().toUpperCase().equals(                                "INTO")) {                            throw new DataAccessPointException(                                "Error in INSERT statement: no INTO found");                        }                        Token = Tokenizer.nextToken();                        if ((relatedTable =                                (TransferTable) DbStmts.get(Token)) != null) {                            relatedTable.Stmts.bDelete     = true;                            relatedTable.Stmts.bInsert     = true;                            relatedTable.Stmts.sDestInsert = currentLine;                            relatedTable.Stmts.sDestDelete =                                "DELETE FROM "                                + relatedTable.Stmts.sSourceTable + ";";                        }                        continue;                    } catch (NoSuchElementException NSE) {                        continue;                    }                } else if (Token.toUpperCase().equals("ALTER")) {                    try {                        if (!Tokenizer.nextToken().toUpperCase().equals(                                "TABLE")) {                            continue;                        }                        name  = Tokenizer.nextToken();                        Token = Tokenizer.nextToken().toUpperCase();                        if (!Token.equals("ADD")) {                            continue;                        }                        do {                            Token = Tokenizer.nextToken().toUpperCase();                        } while (!Token.equals("CONSTRAINT"));                        if ((relatedTable = (TransferTable) DbStmts.get(name))                                != null) {                            if (relatedTable.Stmts.sDestAlter == null) {                                relatedTable.Stmts.sDestAlter = "";                            }                            relatedTable.Stmts.bAlter     = true;                            relatedTable.Stmts.sDestAlter += currentLine;                        } else {                            throw new DataAccessPointException(                                "table not found");                        }                        Token = Tokenizer.nextToken();                        if (relatedTable.Stmts.sDestDrop == null) {                            relatedTable.Stmts.sDestDrop = "";                        }                        relatedTable.Stmts.bDrop = true;                        relatedTable.Stmts.sDestDrop =                            "ALTER TABLE " + name + " DROP CONSTRAINT "                            + Token + ";" + relatedTable.Stmts.sDestDrop;                        continue;                    } catch (NoSuchElementException NSE) {                        continue;                    }                } else if (!Token.toUpperCase().equals("CREATE")) {                    continue;                }                Token = Tokenizer.nextToken().toUpperCase();                if (Token.equals("TABLE") || Token.equals("VIEW")) {                    try {                        name = Tokenizer.nextToken(" (;");                        if (!DbStmts.containsKey(name)) {                            throw new DataAccessPointException(                                "error: index is created before the table");                        }                        relatedTable = (TransferTable) DbStmts.get(name);                        relatedTable.Stmts.bCreate = true;                        relatedTable.Stmts.bDrop   = true;//                        relatedTable.Stmts.sDestCreate = currentLine;                        relatedTable.Stmts.sDestCreate =                            translateTypes(currentLine, TTable, Dest);                        relatedTable.Stmts.sDestDrop =                            "DROP " + relatedTable.Stmts.sType + " " + name                            + ";";                        DbStmts.put(relatedTable.Stmts.sSourceTable,                                    relatedTable);                    } catch (NoSuchElementException NSE) {                        continue;                    }                }                if (Token.equals("INDEX") || Token.equals("UNIQUE")) {                    try {                        while ((Token =                                Tokenizer.nextToken()).toUpperCase().equals(                                    "INDEX")) {                            ;                        }                        String IndexdropCommand = "DROP INDEX " + Token                                                  + " ;";                        while ((Token = Tokenizer.nextToken(                                " (")).toUpperCase().equals("ON")) {                            ;                        }                        name = Token;                        if (!DbStmts.containsKey(Token)) {                            throw new DataAccessPointException(                                "error: index is created before the table");                        }                        relatedTable = (TransferTable) DbStmts.get(Token);                        if (relatedTable.Stmts.sDestCreateIndex == null) {                            relatedTable.Stmts.sDestCreateIndex = "";                        }                        if (relatedTable.Stmts.sDestDropIndex == null) {                            relatedTable.Stmts.sDestDropIndex = "";                        }                        relatedTable.Stmts.bCreateIndex     = true;                        relatedTable.Stmts.bDropIndex       = true;                        relatedTable.Stmts.sDestCreateIndex += currentLine;                        relatedTable.Stmts.sDestDropIndex += IndexdropCommand;                    } catch (NoSuchElementException NSE) {                        continue;                    }                }            }        } catch (IOException IOe) {            throw new DataAccessPointException(IOe.getMessage());        }    }    Vector getTables(String sCatalog,                     String[] sSchemas) throws DataAccessPointException {        Vector AllTables = new Vector();        if (DbStmts == null) {            DbStmts = new Hashtable();        }        if (WTextRead != null) {            try {                WTextRead.close();                WTextRead = null;            } catch (IOException e) {}        }        this.parseFileForTables();        StructureAlreadyParsed = false;        Enumeration e = DbStmts.elements();        while (e.hasMoreElements()) {            AllTables.addElement(e.nextElement());        }        return AllTables;    }    void getTableStructure(TransferTable TTable,                           DataAccessPoint Dest)                           throws DataAccessPointException {        if (!StructureAlreadyParsed) {            if (WTextRead != null) {                try {                    WTextRead.close();                    WTextRead = null;                } catch (IOException e) {}            }            this.parseFileForTheRest(TTable, Dest);        }    }    TransferResultSet getData(String statement)    throws DataAccessPointException {        StringTokenizer Tokenizer;        String          tableName = "";        try {            Tokenizer = new StringTokenizer(statement);            while (!Tokenizer.nextToken().toUpperCase().equals("FROM")) {                ;            }            tableName = Tokenizer.nextToken(" ;");        } catch (NoSuchElementException NSE) {            throw new DataAccessPointException(                "Table name not found in statement: " + statement);        }        if (WTextRead != null) {            try {                WTextRead.close();                WTextRead = null;            } catch (IOException e) {}        }        return (this.parseFileForData(tableName));    }    TransferResultSet parseFileForData(String tableName)    throws DataAccessPointException {        TransferResultSet trsData = new TransferResultSet();        StringTokenizer   Tokenizer;        if (WTextRead == null) {            try {                WTextRead = new BufferedReader(new FileReader(sFileName));            } catch (IOException e) {                throw new DataAccessPointException(e.getMessage());            }        }        String currentLine = "";        String Token;        try {            while ((currentLine = WTextRead.readLine()) != null) {                currentLine = currentLine.trim() + ";";                Tokenizer   = new StringTokenizer(currentLine);                try {                    Token = Tokenizer.nextToken();                } catch (NoSuchElementException NSE) {                    continue;                }                if (Token == null) {                    continue;                }                if (!Token.toUpperCase().equals("INSERT")) {                    continue;                }                try {                    if (!Tokenizer.nextToken().toUpperCase().equals("INTO")) {                        throw new DataAccessPointException(                            "Error in INSERT statement: no INTO found");                    }                    Token = Tokenizer.nextToken();                    if (!Token.equals(tableName)) {                        continue;                    }                    int    iParsedRows   = 0;                    Vector vColumnNames  = new Vector();                    Vector vColumnValues = new Vector();                    Vector vColumnTypes  = new Vector();                    while ((currentLine = WTextRead.readLine()) != null) {                        currentLine = currentLine.trim();                        boolean newLine = (currentLine.length() == 0);                        if (newLine) {                            int iColumnNb = 0;                            iParsedRows++;                            iColumnNb = vColumnNames.size();                            String[] Names  = new String[iColumnNb + 1];                            int[]    Types  = new int[iColumnNb + 1];                            Object[] Values = new Object[iColumnNb + 1];                            for (int Idx = 0; Idx < iColumnNb; Idx++) {                                Names[Idx + 1] =                                    (String) vColumnNames.elementAt(Idx);                                Types[Idx + 1] =                                    ((Integer) vColumnTypes.elementAt(                                        Idx)).intValue();                                Values[Idx + 1] =                                    vColumnValues.elementAt(Idx);                            }                            try {                                trsData.addRow(Names, Types, Values,                                               iColumnNb);                            } catch (Exception e) {                                throw new DataAccessPointException(                                    e.getMessage());                            }                            iColumnNb = 0;                            vColumnNames.removeAllElements();                            vColumnValues.removeAllElements();                            vColumnTypes.removeAllElements();                            continue;                        }                        Tokenizer = new StringTokenizer(currentLine);                        Token     = Tokenizer.nextToken("=");                        if (Token.equals("Number of Rows")) {                            int iNbRows =                                Integer.parseInt(Tokenizer.nextToken());                            if (iNbRows != iParsedRows) {                                throw new DataAccessPointException(                                    "Number of parsed rows (" + iParsedRows                                    + ") is different from the expected ("                                    + iNbRows + ")");                            }                            return trsData;                        }                        if (Token.equals("column")) {                            Token = Tokenizer.nextToken(" =");                            vColumnNames.addElement(Token);                        }                        Token = Tokenizer.nextToken(" =");                        if (Token.equals("datatype")) {                            int iType;                            Token = Tokenizer.nextToken(" =");                            try {                                iType = JDBCT.toInt(Token.toUpperCase());                            } catch (Exception e) {                                throw new DataAccessPointException(                                    "Unknown type: " + Token);                            }                            vColumnTypes.addElement(new Integer(iType));                        }                        Token = Tokenizer.nextToken(" =");                        if (Token.equals("value")) {                            int iStart = currentLine.indexOf("value=") + 6;                            String sValue =                                currentLine.substring(iStart).trim();                            if (sValue.indexOf("<null>") >= 0) {                                vColumnValues.addElement(null);                            } else {                                int    i       = sValue.indexOf('\'') + 1;                                String sbToken = sValue.substring(i);                                i       = sbToken.lastIndexOf('\'');                                sbToken = sbToken.substring(0, i);                                Token   = sbToken;                                vColumnValues.addElement(Token);                            }                        }                    }                } catch (IndexOutOfBoundsException IOBe) {                    continue;                }            }        } catch (IOException IOe) {            throw new DataAccessPointException(IOe.getMessage());        }        return trsData;    }}

⌨️ 快捷键说明

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