📄 parser.java
字号:
a = new Expression(a, b, escape, hasCollation); return a; } private Expression parseBetweenPredicate(Expression a) throws HsqlException { read(); Expression l = new Expression(Expression.BIGGER_EQUAL, a, readConcat()); readThis(Expression.AND); Expression h = new Expression(Expression.SMALLER_EQUAL, a, readConcat()); if (l.getArg().isParam() && l.getArg2().isParam()) { throw Trace.error(Trace.UNRESOLVED_PARAMETER_TYPE, Trace.Parser_ambiguous_between1); } if (h.getArg().isParam() && h.getArg2().isParam()) { throw Trace.error(Trace.UNRESOLVED_PARAMETER_TYPE, Trace.Parser_ambiguous_between1); } return new Expression(Expression.AND, l, h); } private Expression parseInPredicate(Expression a) throws HsqlException { int type = iToken; read(); readThis(Expression.OPEN); Expression b = null; int brackets = 0; if (iToken == Expression.OPEN) { brackets += Parser.parseOpenBrackets(tokenizer) + 1; read(); } if (iToken == Expression.SELECT) { SubQuery sq = parseSubquery(brackets, null, false, Expression.IN); // until we support rows in IN predicates Trace.check(sq.select.iResultLen == 1, Trace.SINGLE_COLUMN_EXPECTED); b = new Expression(sq); read(); } else { tokenizer.back(); HsqlArrayList v = new HsqlArrayList(); while (true) { Expression value = parseExpression(); if (value.exprType == Expression.VALUE && value.valueData == null &&!value.isParam()) { throw Trace.error(Trace.NULL_IN_VALUE_LIST); } v.add(value); read(); if (iToken != Expression.COMMA) { break; } } Expression[] valueList; valueList = (Expression[]) v.toArray(new Expression[v.size()]); b = new Expression(valueList); } readThis(Expression.CLOSE); return new Expression(type, a, b); } private Expression parseAllAnyPredicate() throws HsqlException { int type = iToken; read(); readThis(Expression.OPEN); Expression b = null; int brackets = 0; if (iToken == Expression.OPEN) { brackets += Parser.parseOpenBrackets(tokenizer) + 1; read(); } if (iToken != Expression.SELECT) { throw Trace.error(Trace.INVALID_IDENTIFIER); } SubQuery sq = parseSubquery(brackets, null, false, type); Select select = sq.select; // until we support rows Trace.check(sq.select.iResultLen == 1, Trace.SINGLE_COLUMN_EXPECTED); b = new Expression(sq); read(); readThis(Expression.CLOSE); return new Expression(type, b, null); } /** * Method declaration * * @param type * @throws HsqlException */ private void readThis(int type) throws HsqlException { Trace.check(iToken == type, Trace.UNEXPECTED_TOKEN); read(); } /** * Method declaration * * @return a concatenation, possibly degenerate * @throws HsqlException */ private Expression readConcat() throws HsqlException { Expression r = readSum(); while (iToken == Expression.STRINGCONCAT) { int type = Expression.CONCAT; Expression a = r; read(); r = new Expression(type, a, readSum()); } return r; } static HashMap simpleFunctions = new HashMap(); static { simpleFunctions.put(Token.T_CURRENT_DATE, "org.hsqldb.Library.curdate"); simpleFunctions.put(Token.T_CURRENT_TIME, "org.hsqldb.Library.curtime"); simpleFunctions.put(Token.T_CURRENT_TIMESTAMP, "org.hsqldb.Library.now"); simpleFunctions.put(Token.T_CURRENT_USER, "org.hsqldb.Library.user"); simpleFunctions.put(Token.T_SYSDATE, "org.hsqldb.Library.curdate"); simpleFunctions.put(Token.T_NOW, "org.hsqldb.Library.now"); simpleFunctions.put(Token.T_TODAY, "org.hsqldb.Library.curdate"); } /** * Method declaration * * @return a summation, possibly degenerate * @throws HsqlException */ private Expression readSum() throws HsqlException { Expression r = readFactor(); while (true) { int type; if (iToken == Expression.PLUS) { type = Expression.ADD; } else if (iToken == Expression.NEGATE) { type = Expression.SUBTRACT; } else { break; } Expression a = r; read(); r = new Expression(type, a, readFactor()); } return r; } /** * Method declaration * * @return a product, possibly degenerate * @throws HsqlException */ private Expression readFactor() throws HsqlException { Expression r = readTerm(); while (iToken == Expression.MULTIPLY || iToken == Expression.DIVIDE) { int type = iToken; Expression a = r; read(); r = new Expression(type, a, readTerm()); } return r; } /** * Method declaration * * @return a term, possibly composite * @throws HsqlException */ private Expression readTerm() throws HsqlException { Expression r = null; switch (iToken) { case Expression.COLUMN : { r = readColumnExpression(); break; } case Expression.NEGATE : { int type = iToken; read(); r = new Expression(type, readTerm(), null); Trace.check(!r.getArg().isParam(), Trace.Expression_resolveTypes1); break; } case Expression.PLUS : { read(); r = readTerm(); Trace.check(!r.isParam(), Trace.UNRESOLVED_PARAMETER_TYPE, Trace.getMessage(Trace.Expression_resolveTypes1)); break; } case Expression.OPEN : { read(); r = readOr(); if (iToken != Expression.CLOSE) { throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken); } read(); break; } case Expression.VALUE : { r = new Expression(iType, oData); read(); break; } case Expression.PARAM : { r = new Expression(Types.NULL, null, true); parameters.add(r); read(); break; } case Expression.SELECT : { // accept ORDRY BY with LIMIT Select select = parseSelect(0, false, false, true, true); select.resolve(session); SubQuery sq = new SubQuery(); sq.select = select; r = new Expression(sq); read(); break; } case Expression.ANY : case Expression.ALL : { r = parseAllAnyPredicate();// read(); break; } case Expression.MULTIPLY : { r = new Expression(sTable, (String) null); read(); break; } case Expression.CONCAT : return readConcatExpression(); case Expression.CASEWHEN : return readCaseWhenExpression(); case Expression.CASE : return readCaseExpression(); case Expression.NULLIF : return readNullIfExpression(); case Expression.COALESCE : case Expression.IFNULL : return readCoalesceExpression(); case Expression.SEQUENCE : return readSequenceExpression(); case Expression.CAST : case Expression.CONVERT : return readCastExpression(); case Expression.EXTRACT : return readExtractExpression(); case Expression.TRIM : return readTrimExpression(); case Expression.POSITION : return readPositionExpression(); case Expression.SUBSTRING : return readSubstringExpression(); default : if (Expression.isAggregate(iToken)) { return readAggregate(); } else { throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken); } } return r; } /** * reads a CASE .. WHEN expression */ Expression readCaseExpression() throws HsqlException { int type = Expression.CASEWHEN; Expression r = null; Expression predicand = null; read(); if (iToken != Expression.WHEN) { predicand = readOr(); } Expression leaf = null; while (true) { Expression casewhen = parseCaseWhen(predicand); if (r == null) { r = casewhen; } else { leaf.setRightExpression(casewhen); } leaf = casewhen.getRightExpression(); if (iToken != Expression.WHEN) { break; } } if (iToken == Expression.ELSE) { readThis(Expression.ELSE); Expression elsexpr = readOr(); leaf.setRightExpression(elsexpr); } readThis(Expression.ENDWHEN); return r; } /** * Reads part of a CASE .. WHEN expression */ private Expression parseCaseWhen(Expression r) throws HsqlException { readThis(Expression.WHEN); Expression condition; if (r == null) { condition = readOr(); } else { condition = new Expression(Expression.EQUAL, r, readOr()); } readThis(Expression.THEN); Expression current = readOr(); Expression alternatives = new Expression(Expression.ALTERNATIVE, current, new Expression(Types.NULL, null)); Expression casewhen = new Expression(Expression.CASEWHEN, condition, alternatives); return casewhen; } /** * reads a CASEWHEN expression */ private Expression readCaseWhenExpression() throws HsqlException { int type = iToken; Expression r = null; read();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -