📄 parser.java
字号:
readThis(Expression.OPEN); r = readOr(); readThis(Expression.COMMA); Expression thenelse = readOr(); readThis(Expression.COMMA); // thenelse part is never evaluated; only init thenelse = new Expression(Expression.ALTERNATIVE, thenelse, readOr()); r = new Expression(type, r, thenelse); readThis(Expression.CLOSE); return r; } /** * Reads a CAST or CONVERT expression */ private Expression readCastExpression() throws HsqlException { boolean isConvert = iToken == Expression.CONVERT; read(); readThis(Expression.OPEN); Expression r = readOr(); if (isConvert) { readThis(Expression.COMMA); } else { readThis(Expression.AS); } int typeNr = Types.getTypeNr(sToken); int length = 0; int scale = 0; boolean hasLength = false; if (Types.acceptsPrecisionCreateParam(typeNr) && tokenizer.isGetThis(Token.T_OPENBRACKET)) { length = tokenizer.getInt(); hasLength = true; if (Types.acceptsScaleCreateParam(typeNr) && tokenizer.isGetThis(Token.T_COMMA)) { scale = tokenizer.getInt(); } tokenizer.getThis(Token.T_CLOSEBRACKET); } if (typeNr == Types.FLOAT && length > 53) { throw Trace.error(Trace.NUMERIC_VALUE_OUT_OF_RANGE); } if (typeNr == Types.TIMESTAMP) { if (!hasLength) { length = 6; } else if (length != 0 && length != 6) { throw Trace.error(Trace.NUMERIC_VALUE_OUT_OF_RANGE); } } if (r.isParam()) { r.setDataType(typeNr); } r = new Expression(Expression.CONVERT, r, typeNr, length, scale); read(); readThis(Expression.CLOSE); return r; } /** * reads a Column or Function expression */ private Expression readColumnExpression() throws HsqlException { String name = sToken; Expression r = new Expression(sTable, name); read(); if (iToken == Expression.OPEN) { String javaName = database.getJavaName(name); Function f = new Function(name, javaName, false); session.check(javaName, UserManager.ALL); int len = f.getArgCount(); int i = 0; read(); if (iToken != Expression.CLOSE) { while (true) { f.setArgument(i++, readOr()); if (iToken != Expression.COMMA) { break; } read(); } } readThis(Expression.CLOSE); // TODO: Maybe allow AS <alias> here r = new Expression(f); } else { String javaName = (String) simpleFunctions.get(name); if (javaName != null) { Function f = new Function(name, javaName, true); r = new Expression(f); } } return r; } /** * reads a CONCAT expression */ private Expression readConcatExpression() throws HsqlException { int type = iToken; read(); readThis(Expression.OPEN); Expression r = readOr(); readThis(Expression.COMMA); r = new Expression(type, r, readOr()); readThis(Expression.CLOSE); return r; } /** * Reads a NULLIF expression */ private Expression readNullIfExpression() throws HsqlException { // turn into a CASEWHEN read(); readThis(Expression.OPEN); Expression r = readOr(); readThis(Expression.COMMA); Expression thenelse = new Expression(Expression.ALTERNATIVE, new Expression(Types.NULL, null), r); r = new Expression(Expression.EQUAL, r, readOr()); r = new Expression(Expression.CASEWHEN, r, thenelse); readThis(Expression.CLOSE); return r; } /** * Reads a COALESE or IFNULL expression */ private Expression readCoalesceExpression() throws HsqlException { Expression r = null; // turn into a CASEWHEN read(); readThis(Expression.OPEN); Expression leaf = null; while (true) { Expression current = readOr(); Expression condition = new Expression(Expression.IS_NULL, current, null); Expression alternatives = new Expression(Expression.ALTERNATIVE, new Expression(Types.NULL, null), current); Expression casewhen = new Expression(Expression.CASEWHEN, condition, alternatives); if (r == null) { r = casewhen; } else { leaf.setLeftExpression(casewhen); } leaf = alternatives; if (iToken == Expression.CLOSE) { readThis(Expression.CLOSE); break; } readThis(Expression.COMMA); } return r; } /** * Reads an EXTRACT expression */ private Expression readExtractExpression() throws HsqlException { read(); readThis(Expression.OPEN); String name = sToken; // must be an accepted identifier if (!Expression.SQL_EXTRACT_FIELD_NAMES.contains(name)) { throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken); } readToken(); readThis(Expression.FROM); // the name argument is DAY, MONTH etc. - OK for now for CHECK constraints Function f = new Function(name, database.getJavaName(name), false); f.setArgument(0, readOr()); readThis(Expression.CLOSE); return new Expression(f); } /** * Reads a POSITION expression */ private Expression readPositionExpression() throws HsqlException { read(); readThis(Expression.OPEN); Function f = new Function(Token.T_POSITION, "org.hsqldb.Library.position", false); f.setArgument(0, readTerm()); readThis(Expression.IN); f.setArgument(1, readOr()); readThis(Expression.CLOSE); return new Expression(f); } /** * Reads a SUBSTRING expression */ private Expression readSubstringExpression() throws HsqlException { boolean commas = false; read(); readThis(Expression.OPEN); // OK for now for CHECK search conditions Function f = new Function(Token.T_SUBSTRING, "org.hsqldb.Library.substring", false); f.setArgument(0, readTerm()); if (iToken == Expression.FROM) { readThis(Expression.FROM); } else { readThis(Expression.COMMA); commas = true; } f.setArgument(1, readOr()); Expression count = null; if (!commas && iToken == Expression.FOR) { readThis(Expression.FOR); count = readTerm(); } else if (commas && iToken == Expression.COMMA) { readThis(Expression.COMMA); count = readTerm(); } f.setArgument(2, count); readThis(Expression.CLOSE); return new Expression(f); } private Expression readSequenceExpression() throws HsqlException { tokenizer.getThis(Token.T_VALUE); tokenizer.getThis(Token.T_FOR); String name = tokenizer.getName(); String schemaname = tokenizer.getLongNameFirst(); schemaname = session.getSchemaName(schemaname); // Read next because Tokenizer.back() will run after this. // (This is because usually when reading expressions, you need to // get the following token to know whether you have finished. tokenizer.getString(); NumberSequence sequence = database.schemaManager.getSequence(name, schemaname); return new Expression(sequence); } /** * Reads a TRIM expression */ private Expression readTrimExpression() throws HsqlException { read(); readThis(Expression.OPEN); String type = sToken; if (Expression.SQL_TRIM_SPECIFICATION.contains(type)) { read(); } else { type = Token.T_BOTH; } String trimstr; if (sToken.length() == 1) { trimstr = sToken; read(); } else { trimstr = " "; } readThis(Expression.FROM); Expression trim = new Expression(Types.CHAR, trimstr); Expression leading; Expression trailing; if (type.equals(Token.T_LEADING)) { leading = new Expression(true); trailing = new Expression(false); } else if (type.equals(Token.T_TRAILING)) { leading = new Expression(false); trailing = new Expression(true); } else { leading = trailing = new Expression(true); } // name argument is OK for now for CHECK constraints Function f = new Function(Token.T_TRIM, "org.hsqldb.Library.trim", false); f.setArgument(0, readOr()); f.setArgument(1, trim); f.setArgument(2, leading); f.setArgument(3, trailing); readThis(Expression.CLOSE); return new Expression(f); } /** * Reads a DEFAULT clause expression. */ Expression readDefaultClause(int dataType) throws HsqlException { Expression r = null; read(); switch (iToken) { case Expression.COLUMN : { String name = sToken; String javaName = (String) simpleFunctions.get(name); if (javaName != null) { Function f = new Function(name, javaName, true); return new Expression(f); } break; } case Expression.NEGATE : { int exprType = iToken; read(); if (iToken == Expression.VALUE) { oData = Column.convertObject(oData, dataType); return new Expression(exprType, new Expression(dataType, oData), null); } break; } case Expression.VALUE : { String name = sToken.toUpperCase(Locale.ENGLISH); String javaName = (String) simpleFunctions.get(name); if (Types.isDatetimeType(dataType) && javaName != null) { Function f = new Function(name, javaName, true); return new Expression(f); } oData = Column.convertObject(oData, dataType); return new Expression(dataType, oData); } } throw Trace.error(Trace.WRONG_DEFAULT_CLAUSE, sToken); } /** * Method declaration * * @throws HsqlException */ private void read() throws HsqlException { sToken = tokenizer.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -