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

📄 simpletextstatement.java

📁 codebook!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        // Make sure we are not in read-only mode        if (ownerConnection.isReadOnly()) {            throw new SQLException(                "Unable to INSERT: connection is read-only");        }        // The next word is the table name.  Verify that it does not        // contain any invalid characters        String tableName = parsedSQL[2];        validateName(tableName, "table");        // Verify that the file exists.  If getColumns returns null,        // the table does not exist        Hashtable columnList = ownerConnection.getColumns(                                ownerConnection.getCatalog(), tableName);        if (columnList == null) {            throw new SQLException("Table does not exist: " + tableName);        }        int pos = 3;        Hashtable insertList = null;        Hashtable valueList = null;        int colNo = 1;        SimpleTextColumn column;        SimpleTextColumn column2;        String name;        // If the next word is a paren '(', the column names are being        // specified.  Build a list of columns that will have data        // inserted        if (parsedSQL[pos].equals("(")) {            insertList = new Hashtable();            pos++;            if (pos >= parsedSQL.length) {                throw new SQLException ("Invalid INSERT statement");            }            // Build our insert list.  Get each comma separated name until            // we read a close paren            pos = buildList(parsedSQL, pos, ")", insertList);            // Make sure at least one column was given            if (insertList.size() == 0) {                throw new SQLException ("No columns given");            }            // Now that we have the insert list, verify each name is in            // our target table and get the type and precision            for (int i = 1; i <= insertList.size(); i++) {                column = (SimpleTextColumn) insertList.get(new Integer(i));                column2 = findColumn(columnList, column.name);                if (column2 == null) {                    throw new SQLException("Column does not exist: " +                                column.name);                }                column.type = column2.type;                column.precision = column2.precision;            }            // Position to the next word after the closing paren            pos++;            if (pos >= parsedSQL.length) {                throw new SQLException(                        "Invalid INSERT statement; missing VALUES clause");            }        }        // The next word is VALUES; no column list was given, so assume        // all columns in the table        else if (parsedSQL[pos].equalsIgnoreCase("VALUES")) {            insertList = new Hashtable();            // Build the insertList with all columns in the table            for (colNo = 1; colNo <= columnList.size(); colNo++) {                column2 = (SimpleTextColumn)columnList.get(new Integer(colNo));                if (column2 == null) {                    throw new SQLException("Invalid column number: " + colNo);                }                column = new SimpleTextColumn(column2.name);                column.type = column2.type;                column.precision = column2.precision;                insertList.put(new Integer(colNo), column);            }        }        else {            // Invalid SQL statement            throw new SQLException(                            "Invalid INSERT statement, no VALUES clause");        }        // The next word must be VALUES.  If there was an insert list,        // we have positioned past it.        if (!parsedSQL[pos].equalsIgnoreCase("VALUES")) {            throw new SQLException(                    "Invalid INSERT statement; missing VALUES clause");        }        pos++;        if (pos >= parsedSQL.length) {            throw new SQLException (                    "Invalid INSERT statement, missing values");        }        // The next word must be the open paren that starts the values        if (!parsedSQL[pos].equals("(")) {            throw new SQLException (                    "Invalid INSERT statement, missing values");        }        pos++;        if (pos >= parsedSQL.length) {            throw new SQLException (                    "Invalid INSERT statement, missing values");        }        // Build our value list.  Get each comma separated value until        // we read a close paren        valueList = new Hashtable();        pos = buildList(parsedSQL, pos, ")", valueList);        // We could check for junk after the INSERT statement, but we won't        // Verify the the number of insert items matches the number        // of data items        if (insertList.size() != valueList.size()) {            throw new SQLException("Number of values does not equal the number of items in the insert list");        }        // Verify the data is correct        validateData(insertList, valueList, prepareOnly);        // If we are just preparing the statement, exit now        if (prepareOnly) {            return;        }        // Now we can build the line that will get written to the        // simple text file.  If there is any binary data, write it first        // so that we know what the offset will be.        String sdfPath = ownerConnection.getCatalog() + "/" + tableName +                                SimpleTextDefine.DATA_FILE_EXT;        String sbfPath = ownerConnection.getCatalog() + "/" + tableName +                                SimpleTextDefine.BINARY_FILE_EXT;        File sdf = new File(sdfPath);        File sbf = new File(sbfPath);        RandomAccessFile rafsdf = null;        RandomAccessFile rafsbf = null;        if (!sdf.exists()) {            throw new SQLException("Text file does not exist: " + sdfPath);        }        String line = "";        long binaryPos = 0;        for (int i = 1; i <= columnList.size(); i++) {            column2 = (SimpleTextColumn) columnList.get(new Integer(i));            // Separate the data by a comma            if (i > 1) {                line += ",";            }            // If there is no data for this column, skip it            colNo = findColumnNumber(insertList, column2.name);            if (colNo == 0) {                // No data, put in defaults                switch(column2.type) {                case Types.VARCHAR:                    line += "''";                    break;                case Types.VARBINARY:                    line += "-1";                    break;                default:                    line += "0";                    break;                }                continue;            }            column = (SimpleTextColumn) valueList.get(new Integer(colNo));            if (column2.type == Types.VARBINARY) {                if (rafsbf == null) {                    if (!sbf.exists()) {                        throw new SQLException("Binary file does not exist: "                                    + sbfPath);                    }                    try {                        rafsbf = new RandomAccessFile(sbf, "rw");                        // Position to the end of file                        rafsbf.seek(rafsbf.length());                    }                    catch (Exception ex) {                        throw new SQLException("Unable to access " +                                sbfPath + ": " + ex.getMessage());                    }                }                try {                    // Get the current position                    binaryPos = rafsbf.getFilePointer();                    // Create a new CommonValue with the hex digits (remove                    // the quotes.                    CommonValue value = new CommonValue(                        column.name.substring(1, column.name.length() - 1));                    // Now let CommonValue convert the hex string into                    // a byte array                    byte b[] = value.getBytes();                    // Write the length first                    rafsbf.writeInt(b.length);                    // Write the data                    rafsbf.write(b);                }                catch (Exception ex) {                    throw new SQLException("Unable to access " +                            sbfPath + " for column " + i +                             ": " + ex.getMessage());                }                // Put the offset pointer in the line                line += binaryPos;            }            // Else some kind of text data, put directly in the line            else {                line += column.name;            }        }        // If the binary file was opened, close it now        if (rafsbf != null) {            try {                rafsbf.close();            }            catch (Exception ex) {                throw new SQLException("Unable to close " +                            sbfPath + ": " + ex.getMessage());            }        }        // Now that we have the data line, write it out to the text        // file        long seekPos;		String msg = "";        try {		    msg = "open";            rafsdf = new RandomAccessFile(sdf, "rw");			msg = "get length";			seekPos = rafsdf.length();            // Position to the end of file			msg = "seek";            rafsdf.seek(seekPos);			// Write the data            msg = "write";            rafsdf.writeBytes(line);            rafsdf.writeBytes("\n");			msg = "close";            rafsdf.close();        }        catch (Exception ex) {		    ex.printStackTrace();            throw new SQLException("Unable to " + msg + " " +                    sdfPath + ": " + ex.getMessage());        }    }    //------------------------------------------------------------------------    // select    // Select data from a table    //    // Grammar:    //    // select-statement ::= SELECT select-list FROM table-name    //                            [WHERE search-condition]    //    // select-list ::= * | column-identifier [,column-identifier]...    // search-condition ::= column-identifier comparison-operator literal    // comparison-operator ::= < | > | = | <>    //    //------------------------------------------------------------------------    protected void select(        boolean prepareOnly)        throws SQLException    {        // Initialize the filter object        resultSetFilter = null;        // The SQL statement must have at least 4 elements:        //        // SELECT * FROM table        if (parsedSQL.length < 4) {            throw new SQLException ("Invalid SELECT statement");        }        Hashtable selectList = new Hashtable();        int pos = 1;        // Build our select list.  Get each comma separated name until        // we read a 'FROM'        pos = buildList(parsedSQL, pos, "FROM", selectList);        // There must be at least one column        if (selectList.size() == 0) {            throw new SQLException("Select list must be specified");        }        // Increment past the 'FROM' word. This is the table name        pos++;        if (pos >= parsedSQL.length) {            throw new SQLException("Missing table name");        }        // The next word is the table name.  Verify that it does not        // contain any invalid characters        String tableName = parsedSQL[pos];        validateName(tableName, "table");        // Verify that the file exists.  If getColumns returns null,        // the table does not exist        Hashtable columnList = ownerConnection.getColumns(                                ownerConnection.getCatalog(), tableName);        if (columnList == null) {            throw new SQLException("Table does not exist: " + tableName);        }        // No go back through the select list and verify that each        // column specified is contained in the table.  Also expand        // any * to be all columns        Hashtable validList = new Hashtable();        int validCount = 0;        SimpleTextColumn column;        SimpleTextColumn column2;        for (int i = 1; i <= selectList.size(); i++) {            // Get the next column from the select list            column = (SimpleTextColumn) selectList.get(new Integer(i));            // If it's an *, expand it to all columns in the table            if (column.name.equals("*")) {                for (int j = 1; j <= columnList.size(); j++) {                    column2 = (SimpleTextColumn)columnList.get(new Integer(j));                    validCount++;                    validList.put(new Integer(validCount), column2);                }            }            else {                // Make sure the column exists in the table                column2 = findColumn(columnList, column.name);                if (column2 == null) {                    throw new SQLException("Column not found: " + column.name);                }                // Put column on our valid list                validCount++;                validList.put(new Integer(validCount), column2);            }        }        // Now we know the table exists and have a list of valid columns.        // Process the WHERE clause if one exists        pos++;        if (pos < parsedSQL.length) {            // The next word should be WHERE            if (!parsedSQL[pos].equalsIgnoreCase ("WHERE")) {                throw new SQLException("WHERE clause expected");            }            // Create a filter object            resultSetFilter = new SimpleTextFilter();

⌨️ 快捷键说明

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