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

📄 parser.java

📁 一个用java写的开源的数据库系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                int    current = i;                Table  table   = null;                String n       = e.getTableName();                for (int t = 0; t < filter.length; t++) {                    TableFilter f = filter[t];                    e.resolve(f);                    if (n != null &&!n.equals(f.getName())) {                        continue;                    }                    table = f.getTable();                    int col = table.getColumnCount();                    for (int c = 0; c < col; c++) {                        Expression ins = new Expression(                            f.getName(), table.getColumn(c).columnName.name,                            table.getColumn(c).columnName.isNameQuoted);                        vcolumn.insertElementAt(ins, current++);                        // now there is one element more to parse                        len++;                    }                }                Trace.check(table != null, Trace.TABLE_NOT_FOUND, n);                // minus the asterix element                len--;                vcolumn.removeElementAt(current);            } else if (e.getType() == Expression.COLUMN) {                if (e.getTableName() == null) {                    for (int filterIndex = 0; filterIndex < filter.length;                            filterIndex++) {                        e.resolve(filter[filterIndex]);                    }                }            }        }        select.iResultLen = len;        // where        token = tTokenizer.getString();        if (token.equals("WHERE")) {            condition = addCondition(condition, parseExpression());            token     = tTokenizer.getString();        }        select.eCondition = condition;// fredt@users 20020215 - patch 1.7.0 by fredt// to support GROUP BY with more than one column        if (token.equals("GROUP")) {            tTokenizer.getThis("BY");            len = 0;            do {                Expression e = parseExpression();                e = doOrderGroup(e, vcolumn);                vcolumn.addElement(e);                token = tTokenizer.getString();                len++;            } while (token.equals(","));            select.iGroupLen = len;        }        if (token.equals("HAVING")) {            //fredt - not yet!            Expression hcondition = null;            addCondition(hcondition, parseExpression());            select.havingCondition = hcondition;            token                  = tTokenizer.getString();            throw Trace.error(Trace.FUNCTION_NOT_SUPPORTED);        }        if (token.equals("ORDER")) {            tTokenizer.getThis("BY");            len = 0;            do {                Expression e = parseExpression();                e     = doOrderGroup(e, vcolumn);                token = tTokenizer.getString();                if (token.equals("DESC")) {                    e.setDescending();                    token = tTokenizer.getString();                } else if (token.equals("ASC")) {                    token = tTokenizer.getString();                }                vcolumn.addElement(e);                len++;            } while (token.equals(","));            select.iOrderLen = len;        }        len            = vcolumn.size();        select.eColumn = new Expression[len];        vcolumn.copyInto(select.eColumn);        if (token.equals("UNION")) {            token = tTokenizer.getString();            if (token.equals("ALL")) {                select.iUnionType = Select.UNIONALL;            } else {                select.iUnionType = Select.UNION;                tTokenizer.back();            }            tTokenizer.getThis("SELECT");            select.sUnion = parseSelect();        } else if (token.equals("INTERSECT")) {            tTokenizer.getThis("SELECT");            select.iUnionType = Select.INTERSECT;            select.sUnion     = parseSelect();        } else if (token.equals("EXCEPT") || token.equals("MINUS")) {            tTokenizer.getThis("SELECT");            select.iUnionType = Select.EXCEPT;            select.sUnion     = parseSelect();        } else {            tTokenizer.back();        }        return select;    }    /**     *  Description of the Method     *     * @param  e                          Description of the Parameter     * @param  vcolumn                    Description of the Parameter     * @return                            Description of the Return Value     * @exception  java.sql.SQLException  Description of the Exception     */    private Expression doOrderGroup(Expression e,                                    Vector vcolumn)                                    throws java.sql.SQLException {        if (e.getType() == Expression.VALUE) {            // order by 1,2,3            if (e.getDataType() == Types.INTEGER) {                int i = ((Integer) e.getValue()).intValue();                e = (Expression) vcolumn.elementAt(i - 1);            }        } else if (e.getType() == Expression.COLUMN                   && e.getTableName() == null) {            // this could be an alias column            String s = e.getColumnName();            for (int i = 0, vSize = vcolumn.size(); i < vSize; i++) {                Expression ec = (Expression) vcolumn.elementAt(i);                if (s.equals(ec.getAlias())) {                    e = ec;                    break;                }            }        }        return e;    }    /**     *  Method declaration     *     * @param  outerjoin     * @return     * @throws  SQLException     */    private TableFilter parseTableFilter(boolean outerjoin)    throws SQLException {        String token = tTokenizer.getString();        Table  t     = null;        if (token.equals("(")) {            tTokenizer.getThis("SELECT");            Select s = parseSelect();            Result r = s.getResult(0);            // it's not a problem that this table has not a unique name            t = new Table(dDatabase, new HsqlName("SYSTEM_SUBQUERY", false),                          Table.SYSTEM_TABLE, null);            tTokenizer.getThis(")");            t.addColumns(r);            t.createPrimaryKey();            // subquery creation can't fail because constraint violation            t.insert(r, cSession);        } else {            cSession.check(token, UserManager.SELECT);            t = dDatabase.getTable(token, cSession);// fredt@users 20020420 - patch523880 by leptipre@users - VIEW support            if (t.isView()) {                String Viewname    = token;                int    CurrentPos  = tTokenizer.getPosition();                int    sLength     = tTokenizer.getLength();                int    TokenLength = token.length();                int    NewCurPos   = CurrentPos;                token = tTokenizer.getString();                if (token.equals("AS")) {                    Viewname  = tTokenizer.getName();                    NewCurPos = tTokenizer.getPosition();                } else if (tTokenizer.wasName()) {                    Viewname  = token;                    NewCurPos = tTokenizer.getPosition();                } else {                    tTokenizer.back();                }                String sLeft = tTokenizer.getPart(0, CurrentPos                                                  - TokenLength);                String       sRight = tTokenizer.getPart(NewCurPos, sLength);                View         v         = (View) t;                String       sView     = v.getStatement();                StringBuffer sFromView = new StringBuffer(128);                sFromView.append(sLeft);                sFromView.append('(');                sFromView.append(sView);                sFromView.append(") ");                sFromView.append(Viewname);                sFromView.append(sRight);                tTokenizer.setString(sFromView.toString(),                                     CurrentPos - TokenLength + 1);                tTokenizer.getThis("SELECT");                Select s = parseSelect();                Result r = s.getResult(0);                // it's not a problem that this table has not a unique name                t = new Table(dDatabase,                              new HsqlName("SYSTEM_SUBQUERY", false),                              Table.SYSTEM_TABLE, null);                tTokenizer.getThis(")");                t.addColumns(r);                t.createPrimaryKey();                // subquery creation can't fail because constraint violation                t.insert(r, cSession);            }        }        String sAlias = null;        token = tTokenizer.getString();        if (token.equals("AS")) {            sAlias = tTokenizer.getName();        } else if (tTokenizer.wasName()) {            sAlias = token;        } else {            tTokenizer.back();        }        return new TableFilter(t, sAlias, outerjoin);    }    /**     *  Method declaration     *     * @param  e1     * @param  e2     * @return     */    private Expression addCondition(Expression e1, Expression e2) {        if (e1 == null) {            return e2;        } else if (e2 == null) {            return e1;        } else {            return new Expression(Expression.AND, e1, e2);        }    }    /**     *  Method declaration     *     * @param  type     * @return     * @throws  SQLException     */    private Object getValue(int type) throws SQLException {        Expression r = parseExpression();        r.resolve(null);        return r.getValue(type);    }// thertz@users 20020320 - patch 473613 - outer join condition bug    /**     * parses the expression that can be used behind a     * [..] JOIN table ON (exp).     * This expression should always be in the form "tab.col=tab2.col"     * with optional brackets (to support automated query tools).<br>     * this method is used from the parseSelect method     *     * @return the expression     * @throws  SQLException if the syntax was not correct     */    private Expression parseOuterJoinCondition() throws SQLException {        boolean parens = false;        read();        if (iToken == Expression.OPEN) {            parens = true;            read();        }        Trace.check(iToken == Expression.COLUMN, Trace.OUTER_JOIN_CONDITION);        Expression left = new Expression(sTable, sToken);        read();        Trace.check(iToken == Expression.EQUAL, Trace.OUTER_JOIN_CONDITION);        read();        Trace.check(iToken == Expression.COLUMN, Trace.OUTER_JOIN_CONDITION);        Expression right = new Expression(sTable, sToken);        if (parens) {            read();            Trace.check(iToken == Expression.CLOSE,                        Trace.OUTER_JOIN_CONDITION);        }        return new Expression(Expression.EQUAL, left, right);    }    /**     *  Method declaration     *     * @return     * @throws  SQLException     */    private Expression parseExpression() throws SQLException {        read();        // todo: really this should be in readTerm        // but then grouping is much more complex        if (Expression.isAggregate(iToken)) {            boolean distinct = false;            int     type     = iToken;            read();            if (tTokenizer.getString().equals("DISTINCT")) {                distinct = true;            } else {                tTokenizer.back();            }            Expression r = new Expression(type, readOr(), null);            r.setDistinctAggregate(distinct);            tTokenizer.back();            return r;        }        Expression r = readOr();        tTokenizer.back();        return r;    }    /**     *  Method declaration     *     * @return     * @throws  SQLException     */    private Expression readOr() throws SQLException {        Expression r = readAnd();        while (iToken == Expression.OR) {            int        type = iToken;            Expression a    = r;            read();            r = new Expression(type, a, readAnd());        }        return r;    }    /**     *  Method declaration     *     * @return     * @throws  SQLException     */    private Expression readAnd() throws SQLException {        Expression r = readCondition();        while (iToken == Expression.AND) {            int        type = iToken;            Expression a    = r;            read();            r = new Expression(type, a, readCondition());        }        return r;    }    /**     *  Method declaration     *     * @return     * @throws  SQLException     */    private Expression readCondition() throws SQLException {

⌨️ 快捷键说明

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