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

📄 simpletextstatement.java

📁 codebook!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            pos++;            if (pos >= parsedSQL.length) {                throw new SQLException(                        "Column name expected after WHERE clause");            }            // The next word is a column name.  Make sure it exists in            // the table            resultSetFilter.column = findColumn(columnList, parsedSQL[pos]);            if (resultSetFilter.column == null) {                throw new SQLException("Column not found: " + parsedSQL[pos]);            }            // Make sure the column is searchable            if (!resultSetFilter.column.searchable) {                throw new SQLException(                        "Column is not searchable: " + parsedSQL[pos]);            }            pos++;            // The next word is the operator.  Some operators may take            // 2 words (i.e <>)            if (pos >= parsedSQL.length) {                throw new SQLException("Operator expected in WHERE clause");            }            if (parsedSQL[pos].equals("=")) {                resultSetFilter.operator = SimpleTextFilter.OP_EQ;            }            else if (parsedSQL[pos].equals("<")) {                resultSetFilter.operator = SimpleTextFilter.OP_LT;            }            else if (parsedSQL[pos].equals(">")) {                resultSetFilter.operator = SimpleTextFilter.OP_GT;            }            else {                throw new SQLException("Invalid operator: " + parsedSQL[pos]);            }            // The next word may be our value, or it may be the second part            // of an operator.            pos++;            if (pos >= parsedSQL.length) {                throw new SQLException("Value expected in WHERE clause");            }            if ((resultSetFilter.operator == SimpleTextFilter.OP_LT) &&                (parsedSQL[pos].equals(">"))) {                resultSetFilter.operator = SimpleTextFilter.OP_NE;                pos++;                if (pos >= parsedSQL.length) {                    throw new SQLException("Value expected in WHERE clause");                }            }            // Get the data value and validate            Hashtable whereList = new Hashtable();            Hashtable dataList = new Hashtable();            column = new SimpleTextColumn(parsedSQL[pos]);            whereList.put(new Integer(1), resultSetFilter.column);            dataList.put(new Integer(1), column);            validateData(whereList, dataList, prepareOnly);            String s = parsedSQL[pos];            // validateData could have massaged the data value (such as            // in executing a prepared statement with parameters).  Get            // the value back            s = ((SimpleTextColumn) dataList.get(new Integer(1))).name;            // Strip off any quotes            if (s.startsWith("'") &&                s.endsWith("'")) {                s = s.substring(1,s.length() - 1);            }            resultSetFilter.value = new CommonValue(s);            pos++;            // Check for extra junk at the end of the statement            if (pos < parsedSQL.length) {                throw new SQLException(                        "Invalid characters following WHERE clause");            }        }        // Set the catalog name, table name, and column Hashtable for        // the result set        resultSetCatalog = ownerConnection.getCatalog();        resultSetTable = tableName;        resultSetColumns = validList;    }    //------------------------------------------------------------------------    // findColumn    // Given a SimpleTextColumn Hashtable and a column name, return    // the SimpleTextColumn that matches.  Null if no match.  The column    // numbers are 1-based    //------------------------------------------------------------------------    protected SimpleTextColumn findColumn(        Hashtable list,        String name)    {        SimpleTextColumn column;        for (int i = 1; i <= list.size(); i++) {            column = (SimpleTextColumn) list.get(new Integer(i));            if (column != null) {                if (column.name.equalsIgnoreCase(name)) {                    return column;                }            }        }        return null;    }    //------------------------------------------------------------------------    // findColumnNumber    // Given a SimpleTextColumn Hashtable and a column name, return    // the column number that matches.  0 if no match.  The column    // numbers are 1-based    //------------------------------------------------------------------------    protected int findColumnNumber(        Hashtable list,        String name)    {        SimpleTextColumn column;        for (int i = 1; i <= list.size(); i++) {            column = (SimpleTextColumn) list.get(new Integer(i));            if (column != null) {                if (column.name.equalsIgnoreCase(name)) {                    return i;                }            }        }        return 0;    }    //------------------------------------------------------------------------    // buildList    // Given a parsed SQL statement, the current position, and the ending    // word, build a list of the comma separated words from the SQL    // statement.  This is used for the insert column list, insert values,    // and select list.  Returns the new position in the parsed SQL    //------------------------------------------------------------------------    public int buildList(        String sql[],        int pos,        String endWord,        Hashtable list)        throws SQLException    {        SimpleTextColumn column;        boolean done = false;        String name;        int colNo = 1;        // Loop while more data is present        while (!done) {            // Get the next column            name = sql[pos];            column = new SimpleTextColumn(name);            list.put(new Integer(colNo), column);            colNo++;            pos++;            if (pos >= sql.length) {                if (endWord.length() > 0) {                    throw new SQLException (                        "Invalid statement after " + name);                }                else {                    done = true;                    break;                }            }            // If the next word is not a comma, it must be our ending            // word            if (!sql[pos].equals(",")) {                // Found the ending word?  exit the loop                if (sql[pos].equalsIgnoreCase(endWord)) {                    done = true;                    break;                }                if (endWord.length() == 0) {                    throw new SQLException("Invalid data format");                }                throw new SQLException (                        "Invalid statement after " + name);            }            pos++;            if (pos >= sql.length) {                if (endWord.length() > 0) {                    throw new SQLException (                        "Invalid statement after " + name);                }                else {                    done = true;                    break;                }            }        }        return pos;    }    //------------------------------------------------------------------------    // validateData    // Given an insert list and a data list, verify the each data element    // is proper for the given type and precision    //------------------------------------------------------------------------    protected void validateData(        Hashtable insertList,        Hashtable dataList,        boolean prepareOnly)        throws SQLException    {        SimpleTextColumn insert;        SimpleTextColumn data;        int precision = 0;        int paramNum = 0;        // Init number of parameters if we are preparing        if (prepareOnly) {            paramCount = 0;        }        for (int i = 1; i <= insertList.size(); i++) {            insert = (SimpleTextColumn) insertList.get(new Integer(i));            data = (SimpleTextColumn) dataList.get(new Integer(i));            // If a parameter marker is found, either continue to the            // next list item because we are preparing, or replace it            // with a bound parameter value            if (data.name.equals("?")) {                if (prepareOnly) {                    // Increment number of parameter markers                    paramCount++;                    continue;                }                // Increment current parameter number                paramNum++;                // Get String value for the bound parameter from the                // boundParams Hashtable.  If it is not found, throw                // an exception indicating that not all of the parameters                // have been set.                if (boundParams != null) {                    String s = (String) boundParams.get(new Integer(paramNum));                    if (s == null) {                        throw new SQLException(                                "Not all parameters have been set");                    }                    // Set the value into the SimpleTextColumn entry                    // If the data is a string or binary type, enclose it                    // in quotes                    switch(insert.type) {                    case Types.VARCHAR:                    case Types.VARBINARY:                        data.name = "'" + s + "'";                        break;                    default:                        data.name = s;                        break;                    }                }            }            switch(insert.type) {            case Types.VARCHAR:                if (!data.name.startsWith("'") ||                    (data.name.length() < 2) ||                    !data.name.endsWith("'")) {                    throw new SQLException(                        "String data must be enclosed in single quotes: " +                                data.name);                }                precision = data.name.length() - 2;                break;            case Types.INTEGER:                try {                    Integer.valueOf(data.name);                }                catch (Exception ex) {                    throw new SQLException("Invalid numeric data: " +                                data.name);                }                precision = data.name.length();                break;            case Types.BINARY:                if (!data.name.startsWith("'") ||                    (data.name.length() < 2) ||                    !data.name.endsWith("'")) {                    throw new SQLException(                        "Binary data must be enclosed in single quotes: " +                                data.name);                }                if ((data.name.length() % 2) != 0) {                    throw new SQLException(                        "Binary data must have even number of hex digits:" +                                data.name);                }                precision = (data.name.length() - 2) / 2;                break;            }            if (precision > insert.precision) {                throw new SQLException("Invalid data precision for " +                            insert.name);            }        }    }    //------------------------------------------------------------------------    // validateName    // Verify that the given name does not contain any invalid characters.    // This will be used for both table names and column names    //------------------------------------------------------------------------    protected void validateName(        String name,        String type)        throws SQLException    {        // Invalid characters other than a-z, 0-9, and A-Z        String invalid = "@#./\\()";        char c;        int j;        for (int i = 0; i < name.length(); i++) {            c = name.charAt(i);            // If it's not an alpha numeric or numeric character,            // check the list of invalid characters            if (!((c >= 'a') && (c <= 'z')) &&                !((c >= '0') && (c <= '9')) &&                !((c >= 'A') && (c <= 'Z'))) {                for (j = 0; j < invalid.length(); j++) {                    if (c == invalid.charAt(j)) {                        throw new SQLException("Invalid " + type + " name: " +                                    name);                    }                }            }        }    }    //------------------------------------------------------------------------    // getConnection    // Returns the owner connection object    //------------------------------------------------------------------------    public SimpleTextIConnection getConnection()    {        return ownerConnection;    }    // Owning connection object    protected SimpleTextIConnection ownerConnection;    // SQLWarning chain    protected SQLWarning lastWarning;    // The current SQL statement    protected String sqlStatement;    // The String array of parsed SQL words    protected String parsedSQL[];    // The current SQL statement type (i.e. SQL_SELECT, SQL_CREATE, etc.)    protected int statementType;    // Update count for the last statement that executed    protected int updateCount;    // Attributes used for creating a result set    String resultSetCatalog;    String resultSetTable;    Hashtable resultSetColumns;    // If a filter exists for a select statement, a SimpleTextFilter object    // will be created    SimpleTextFilter resultSetFilter;    // Our current result set    ResultSet currentResultSet;    // A Hashtable for each bound parameter.  Only valid for PreparedStatements    Hashtable boundParams;    // The count of parameter markers.  Only valid for PreparedStatements    int paramCount;}

⌨️ 快捷键说明

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