use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr in project druid by alibaba.
the class MySqlExprParser method primaryRest.
public final SQLExpr primaryRest(SQLExpr expr) {
if (expr == null) {
throw new IllegalArgumentException("expr");
}
if (lexer.token() == Token.LITERAL_CHARS) {
if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
String ident = identExpr.getName();
if (ident.equalsIgnoreCase("x")) {
String charValue = lexer.stringVal();
lexer.nextToken();
expr = new SQLHexExpr(charValue);
return primaryRest(expr);
} else if (ident.equalsIgnoreCase("b")) {
String charValue = lexer.stringVal();
lexer.nextToken();
expr = new SQLBinaryExpr(charValue);
return primaryRest(expr);
} else if (ident.startsWith("_")) {
String charValue = lexer.stringVal();
lexer.nextToken();
MySqlCharExpr mysqlCharExpr = new MySqlCharExpr(charValue);
mysqlCharExpr.setCharset(identExpr.getName());
if (identifierEquals("COLLATE")) {
lexer.nextToken();
String collate = lexer.stringVal();
mysqlCharExpr.setCollate(collate);
accept(Token.IDENTIFIER);
}
expr = mysqlCharExpr;
return primaryRest(expr);
}
} else if (expr instanceof SQLCharExpr) {
SQLMethodInvokeExpr concat = new SQLMethodInvokeExpr("CONCAT");
concat.addParameter(expr);
do {
String chars = lexer.stringVal();
concat.addParameter(new SQLCharExpr(chars));
lexer.nextToken();
} while (lexer.token() == Token.LITERAL_CHARS || lexer.token() == Token.LITERAL_ALIAS);
expr = concat;
}
} else if (lexer.token() == Token.IDENTIFIER) {
if (expr instanceof SQLHexExpr) {
if ("USING".equalsIgnoreCase(lexer.stringVal())) {
lexer.nextToken();
if (lexer.token() != Token.IDENTIFIER) {
throw new ParserException("syntax error, illegal hex");
}
String charSet = lexer.stringVal();
lexer.nextToken();
expr.getAttributes().put("USING", charSet);
return primaryRest(expr);
}
} else if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
lexer.nextToken();
}
if (lexer.token() != Token.IDENTIFIER) {
throw new ParserException("syntax error");
}
String collate = lexer.stringVal();
lexer.nextToken();
SQLBinaryOpExpr binaryExpr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.COLLATE, new SQLIdentifierExpr(collate), JdbcConstants.MYSQL);
expr = binaryExpr;
return primaryRest(expr);
} else if (expr instanceof SQLVariantRefExpr) {
if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) {
lexer.nextToken();
if (lexer.token() != Token.IDENTIFIER) {
throw new ParserException("syntax error");
}
String collate = lexer.stringVal();
lexer.nextToken();
expr.putAttribute("COLLATE", collate);
return primaryRest(expr);
}
} else if (expr instanceof SQLIntegerExpr) {
SQLIntegerExpr intExpr = (SQLIntegerExpr) expr;
String binaryString = lexer.stringVal();
if (intExpr.getNumber().intValue() == 0 && binaryString.startsWith("b")) {
lexer.nextToken();
expr = new SQLBinaryExpr(binaryString.substring(1));
return primaryRest(expr);
}
}
}
if (lexer.token() == Token.LPAREN && expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
String ident = identExpr.getName();
if ("EXTRACT".equalsIgnoreCase(ident)) {
lexer.nextToken();
if (lexer.token() != Token.IDENTIFIER) {
throw new ParserException("syntax error");
}
String unitVal = lexer.stringVal();
MySqlIntervalUnit unit = MySqlIntervalUnit.valueOf(unitVal.toUpperCase());
lexer.nextToken();
accept(Token.FROM);
SQLExpr value = expr();
MySqlExtractExpr extract = new MySqlExtractExpr();
extract.setValue(value);
extract.setUnit(unit);
accept(Token.RPAREN);
expr = extract;
return primaryRest(expr);
} else if ("SUBSTRING".equalsIgnoreCase(ident)) {
lexer.nextToken();
SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
for (; ; ) {
SQLExpr param = expr();
methodInvokeExpr.addParameter(param);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else if (lexer.token() == Token.FROM) {
lexer.nextToken();
SQLExpr from = expr();
methodInvokeExpr.addParameter(from);
if (lexer.token() == Token.FOR) {
lexer.nextToken();
SQLExpr forExpr = expr();
methodInvokeExpr.addParameter(forExpr);
}
break;
} else if (lexer.token() == Token.RPAREN) {
break;
} else {
throw new ParserException("syntax error");
}
}
accept(Token.RPAREN);
expr = methodInvokeExpr;
return primaryRest(expr);
} else if ("TRIM".equalsIgnoreCase(ident)) {
lexer.nextToken();
SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
if (lexer.token() == Token.IDENTIFIER) {
String flagVal = lexer.stringVal();
if ("LEADING".equalsIgnoreCase(flagVal)) {
lexer.nextToken();
methodInvokeExpr.getAttributes().put("TRIM_TYPE", "LEADING");
} else if ("BOTH".equalsIgnoreCase(flagVal)) {
lexer.nextToken();
methodInvokeExpr.getAttributes().put("TRIM_TYPE", "BOTH");
} else if ("TRAILING".equalsIgnoreCase(flagVal)) {
lexer.nextToken();
methodInvokeExpr.putAttribute("TRIM_TYPE", "TRAILING");
}
}
SQLExpr param = expr();
methodInvokeExpr.addParameter(param);
if (lexer.token() == Token.FROM) {
lexer.nextToken();
SQLExpr from = expr();
methodInvokeExpr.putAttribute("FROM", from);
}
accept(Token.RPAREN);
expr = methodInvokeExpr;
return primaryRest(expr);
} else if ("MATCH".equalsIgnoreCase(ident)) {
lexer.nextToken();
MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr();
if (lexer.token() == Token.RPAREN) {
lexer.nextToken();
} else {
exprList(matchAgainstExpr.getColumns(), matchAgainstExpr);
accept(Token.RPAREN);
}
acceptIdentifier("AGAINST");
accept(Token.LPAREN);
SQLExpr against = primary();
matchAgainstExpr.setAgainst(against);
if (lexer.token() == Token.IN) {
lexer.nextToken();
if (identifierEquals("NATURAL")) {
lexer.nextToken();
acceptIdentifier("LANGUAGE");
acceptIdentifier("MODE");
if (lexer.token() == Token.WITH) {
lexer.nextToken();
acceptIdentifier("QUERY");
acceptIdentifier("EXPANSION");
matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
} else {
matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE);
}
} else if (identifierEquals("BOOLEAN")) {
lexer.nextToken();
acceptIdentifier("MODE");
matchAgainstExpr.setSearchModifier(SearchModifier.IN_BOOLEAN_MODE);
} else {
throw new ParserException("TODO");
}
} else if (lexer.token() == Token.WITH) {
throw new ParserException("TODO");
}
accept(Token.RPAREN);
expr = matchAgainstExpr;
return primaryRest(expr);
} else if (("CONVERT".equalsIgnoreCase(ident)) || ("CHAR".equalsIgnoreCase(ident))) {
lexer.nextToken();
SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
if (lexer.token() != Token.RPAREN) {
exprList(methodInvokeExpr.getParameters(), methodInvokeExpr);
}
if (identifierEquals("USING")) {
lexer.nextToken();
if (lexer.token() != Token.IDENTIFIER) {
throw new ParserException("syntax error");
}
String charset = lexer.stringVal();
lexer.nextToken();
methodInvokeExpr.putAttribute("USING", charset);
}
accept(Token.RPAREN);
expr = methodInvokeExpr;
return primaryRest(expr);
} else if ("POSITION".equalsIgnoreCase(ident)) {
accept(Token.LPAREN);
SQLExpr subStr = this.primary();
accept(Token.IN);
SQLExpr str = this.expr();
accept(Token.RPAREN);
SQLMethodInvokeExpr locate = new SQLMethodInvokeExpr("LOCATE");
locate.addParameter(subStr);
locate.addParameter(str);
expr = locate;
return primaryRest(expr);
}
}
if (lexer.token() == Token.VARIANT && "@".equals(lexer.stringVal())) {
lexer.nextToken();
MySqlUserName userName = new MySqlUserName();
if (expr instanceof SQLCharExpr) {
userName.setUserName(((SQLCharExpr) expr).toString());
} else {
userName.setUserName(((SQLIdentifierExpr) expr).getName());
}
if (lexer.token() == Token.LITERAL_CHARS) {
userName.setHost("'" + lexer.stringVal() + "'");
} else {
userName.setHost(lexer.stringVal());
}
lexer.nextToken();
return userName;
}
if (lexer.token() == Token.ERROR) {
throw new ParserException("syntax error, token: " + lexer.token() + " " + lexer.stringVal() + ", pos : " + lexer.pos());
}
return super.primaryRest(expr);
}
use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr in project druid by alibaba.
the class SQLExprParser method parseSelectItem.
public SQLSelectItem parseSelectItem() {
SQLExpr expr;
boolean connectByRoot = false;
Token token = lexer.token;
int startPos = lexer.startPos;
if (token == Token.IDENTIFIER && // x'123' X'123'
!(lexer.hash_lower() == -5808529385363204345L && lexer.charAt(lexer.pos) == '\'' && dbType == DbType.mysql)) {
String ident = lexer.stringVal();
long hash_lower = lexer.hash_lower();
lexer.nextTokenComma();
if (hash_lower == FnvHash.Constants.CONNECT_BY_ROOT) {
connectByRoot = lexer.token != Token.LPAREN;
if (connectByRoot) {
expr = new SQLIdentifierExpr(lexer.stringVal());
lexer.nextToken();
} else {
expr = new SQLIdentifierExpr(ident);
}
} else if (lexer.identifierEquals(FnvHash.Constants.COLLATE) && dbType == DbType.mysql && lexer.stringVal().charAt(0) != '`') {
lexer.nextToken();
String collate = lexer.stringVal();
lexer.nextToken();
SQLBinaryOpExpr binaryExpr = new SQLBinaryOpExpr(new SQLIdentifierExpr(ident), SQLBinaryOperator.COLLATE, new SQLIdentifierExpr(collate), DbType.mysql);
expr = binaryExpr;
} else if (lexer.identifierEquals(FnvHash.Constants.REGEXP) && lexer.stringVal().charAt(0) != '`' && dbType == DbType.mysql) {
lexer.nextToken();
SQLExpr rightExp = bitOr();
SQLBinaryOpExpr binaryExpr = new SQLBinaryOpExpr(new SQLIdentifierExpr(ident), SQLBinaryOperator.RegExp, rightExp, DbType.mysql);
expr = binaryExpr;
expr = relationalRest(expr);
} else if (FnvHash.Constants.DATE == hash_lower && lexer.stringVal().charAt(0) != '`' && lexer.token == Token.LITERAL_CHARS && (SQLDateExpr.isSupport(dbType))) {
String literal = lexer.stringVal();
lexer.nextToken();
SQLDateExpr dateExpr = new SQLDateExpr();
dateExpr.setLiteral(literal);
expr = dateExpr;
} else if (FnvHash.Constants.TIMESTAMP == hash_lower && lexer.stringVal().charAt(0) != '`' && lexer.token == Token.LITERAL_CHARS && dbType != DbType.oracle) {
String literal = lexer.stringVal();
lexer.nextToken();
SQLTimestampExpr ts = new SQLTimestampExpr(literal);
expr = ts;
if (lexer.identifierEquals(FnvHash.Constants.AT)) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
String timeZone = null;
if (lexer.identifierEquals(FnvHash.Constants.TIME)) {
lexer.nextToken();
if (lexer.identifierEquals(FnvHash.Constants.ZONE)) {
lexer.nextToken();
timeZone = lexer.stringVal();
lexer.nextToken();
}
}
if (timeZone == null) {
lexer.reset(mark);
} else {
ts.setTimeZone(timeZone);
}
}
} else if (FnvHash.Constants.DATETIME == hash_lower && lexer.stringVal().charAt(0) != '`' && lexer.token == Token.LITERAL_CHARS && dbType != DbType.oracle) {
String literal = lexer.stringVal();
lexer.nextToken();
SQLDateTimeExpr ts = new SQLDateTimeExpr(literal);
expr = ts;
} else if (FnvHash.Constants.BOOLEAN == hash_lower && lexer.stringVal().charAt(0) != '`' && lexer.token == Token.LITERAL_CHARS && dbType == DbType.mysql) {
String literal = lexer.stringVal();
lexer.nextToken();
SQLBooleanExpr ts = new SQLBooleanExpr(Boolean.valueOf(literal));
expr = ts;
} else if ((FnvHash.Constants.CHAR == hash_lower || FnvHash.Constants.VARCHAR == hash_lower) && lexer.token == Token.LITERAL_CHARS && dbType == DbType.mysql) {
String literal = lexer.stringVal();
lexer.nextToken();
SQLCharExpr charExpr = new SQLCharExpr(literal);
expr = charExpr;
} else if (FnvHash.Constants.TIME == hash_lower && lexer.token == Token.LITERAL_CHARS) {
String literal = lexer.stringVal();
lexer.nextToken();
expr = new SQLTimeExpr(literal);
} else if (hash_lower == FnvHash.Constants.DECIMAL && lexer.token == Token.LITERAL_CHARS) {
String decimal = lexer.stringVal();
expr = new SQLDecimalExpr(decimal);
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.REAL && lexer.token == Token.LITERAL_CHARS) {
String decimal = lexer.stringVal();
expr = new SQLRealExpr(decimal);
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.DOUBLE && lexer.token == Token.LITERAL_CHARS) {
String decimal = lexer.stringVal();
expr = new SQLDoubleExpr(decimal);
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.FLOAT && lexer.token == Token.LITERAL_CHARS) {
String decimal = lexer.stringVal();
expr = new SQLFloatExpr(decimal);
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.BIGINT && lexer.token == Token.LITERAL_CHARS) {
String strVal = lexer.stringVal();
if (strVal.startsWith("--")) {
strVal = strVal.substring(2);
}
expr = new SQLBigIntExpr(strVal);
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.INTEGER && lexer.token == Token.LITERAL_CHARS) {
String strVal = lexer.stringVal();
if (strVal.startsWith("--")) {
strVal = strVal.substring(2);
}
SQLIntegerExpr integerExpr = SQLIntegerExpr.ofIntOrLong(Long.parseLong(strVal));
integerExpr.setType("INTEGER");
expr = integerExpr;
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.SMALLINT && lexer.token == Token.LITERAL_CHARS) {
String decimal = lexer.stringVal();
expr = new SQLSmallIntExpr(decimal);
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.TINYINT && lexer.token == Token.LITERAL_CHARS) {
String decimal = lexer.stringVal();
expr = new SQLTinyIntExpr(decimal);
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.JSON && lexer.token == Token.LITERAL_CHARS) {
String decimal = lexer.stringVal();
expr = new SQLJSONExpr(decimal);
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.TRY_CAST) {
accept(Token.LPAREN);
SQLCastExpr cast = new SQLCastExpr();
cast.setTry(true);
cast.setExpr(expr());
accept(Token.AS);
cast.setDataType(parseDataType(false));
accept(Token.RPAREN);
expr = cast;
} else if (FnvHash.Constants.CURRENT_DATE == hash_lower && ident.charAt(0) != '`' && lexer.token != Token.LPAREN && (dbType == DbType.mysql || dbType == DbType.hive)) {
expr = new SQLCurrentTimeExpr(SQLCurrentTimeExpr.Type.CURRENT_DATE);
} else if (FnvHash.Constants.CURRENT_TIMESTAMP == hash_lower && ident.charAt(0) != '`' && lexer.token != Token.LPAREN && (dbType == DbType.mysql || dbType == DbType.hive)) {
expr = new SQLCurrentTimeExpr(SQLCurrentTimeExpr.Type.CURRENT_TIMESTAMP);
} else if (FnvHash.Constants.CURRENT_TIME == hash_lower && ident.charAt(0) != '`' && lexer.token != Token.LPAREN && (dbType == DbType.mysql || dbType == DbType.hive)) {
expr = new SQLCurrentTimeExpr(SQLCurrentTimeExpr.Type.CURRENT_TIME);
} else if (FnvHash.Constants.CURDATE == hash_lower && ident.charAt(0) != '`' && lexer.token != Token.LPAREN && (dbType == DbType.mysql || dbType == DbType.hive)) {
expr = new SQLCurrentTimeExpr(SQLCurrentTimeExpr.Type.CURDATE);
} else if (FnvHash.Constants.LOCALTIME == hash_lower && ident.charAt(0) != '`' && lexer.token != Token.LPAREN && (dbType == DbType.mysql || dbType == DbType.hive)) {
expr = new SQLCurrentTimeExpr(SQLCurrentTimeExpr.Type.LOCALTIME);
} else if (FnvHash.Constants.LOCALTIMESTAMP == hash_lower && ident.charAt(0) != '`' && lexer.token != Token.LPAREN && (dbType == DbType.mysql || dbType == DbType.hive)) {
expr = new SQLCurrentTimeExpr(SQLCurrentTimeExpr.Type.LOCALTIMESTAMP);
} else if (FnvHash.Constants.CURRENT_USER == hash_lower && ident.charAt(0) != '`' && lexer.token != Token.LPAREN && isEnabled(SQLParserFeature.EnableCurrentUserExpr)) {
expr = new SQLCurrentUserExpr();
} else if ((FnvHash.Constants._LATIN1 == hash_lower) && ident.charAt(0) != '`' && dbType == DbType.mysql) {
String hexString;
if (lexer.token == Token.LITERAL_HEX) {
hexString = lexer.hexString();
lexer.nextToken();
} else if (lexer.token == Token.LITERAL_CHARS) {
hexString = null;
} else {
acceptIdentifier("X");
hexString = lexer.stringVal();
accept(Token.LITERAL_CHARS);
}
if (hexString == null) {
String str = lexer.stringVal();
lexer.nextToken();
String collate = null;
if (lexer.identifierEquals(FnvHash.Constants.COLLATE)) {
lexer.nextToken();
collate = lexer.stringVal();
if (lexer.token() == Token.LITERAL_CHARS) {
lexer.nextToken();
} else {
accept(Token.IDENTIFIER);
}
}
expr = new MySqlCharExpr(str, "_latin1", collate);
} else {
expr = new MySqlCharExpr(hexString, "_latin1");
}
} else if ((FnvHash.Constants._UTF8 == hash_lower || FnvHash.Constants._UTF8MB4 == hash_lower) && ident.charAt(0) != '`' && dbType == DbType.mysql) {
String hexString;
if (lexer.token == Token.LITERAL_HEX) {
hexString = lexer.hexString();
lexer.nextToken();
} else if (lexer.token == Token.LITERAL_CHARS) {
hexString = null;
} else {
acceptIdentifier("X");
hexString = lexer.stringVal();
accept(Token.LITERAL_CHARS);
}
if (hexString == null) {
String str = lexer.stringVal();
lexer.nextToken();
String collate = null;
if (lexer.identifierEquals(FnvHash.Constants.COLLATE)) {
lexer.nextToken();
collate = lexer.stringVal();
if (lexer.token() == Token.LITERAL_CHARS) {
lexer.nextToken();
} else {
accept(Token.IDENTIFIER);
}
}
expr = new MySqlCharExpr(str, "_utf8", collate);
} else {
expr = new SQLCharExpr(MySqlUtils.utf8(hexString));
}
} else if ((FnvHash.Constants._UTF16 == hash_lower || FnvHash.Constants._UCS2 == hash_lower) && ident.charAt(0) != '`' && dbType == DbType.mysql) {
String hexString;
if (lexer.token == Token.LITERAL_HEX) {
hexString = lexer.hexString();
lexer.nextToken();
} else if (lexer.token == Token.LITERAL_CHARS) {
hexString = null;
} else {
acceptIdentifier("X");
hexString = lexer.stringVal();
accept(Token.LITERAL_CHARS);
}
if (hexString == null) {
String str = lexer.stringVal();
hexString = HexBin.encode(str.getBytes(MySqlUtils.ASCII));
lexer.nextToken();
}
expr = new MySqlCharExpr(hexString, "_utf16");
} else if (FnvHash.Constants._UTF32 == hash_lower && ident.charAt(0) != '`' && dbType == DbType.mysql) {
String hexString;
if (lexer.token == Token.LITERAL_HEX) {
hexString = lexer.hexString();
lexer.nextToken();
} else if (lexer.token == Token.LITERAL_CHARS) {
hexString = null;
} else {
acceptIdentifier("X");
hexString = lexer.stringVal();
accept(Token.LITERAL_CHARS);
}
if (hexString == null) {
String str = lexer.stringVal();
lexer.nextToken();
expr = new MySqlCharExpr(str, "_utf32");
} else {
expr = new SQLCharExpr(MySqlUtils.utf32(hexString));
}
} else if (FnvHash.Constants._GBK == hash_lower && ident.charAt(0) != '`' && dbType == DbType.mysql) {
String hexString;
if (lexer.token == Token.LITERAL_HEX) {
hexString = lexer.hexString();
lexer.nextToken();
} else if (lexer.token == Token.LITERAL_CHARS) {
hexString = null;
} else {
acceptIdentifier("X");
hexString = lexer.stringVal();
accept(Token.LITERAL_CHARS);
}
if (hexString == null) {
String str = lexer.stringVal();
lexer.nextToken();
expr = new MySqlCharExpr(str, "_gbk");
} else {
expr = new SQLCharExpr(MySqlUtils.gbk(hexString));
}
} else if (FnvHash.Constants._BIG5 == hash_lower && ident.charAt(0) != '`' && dbType == DbType.mysql) {
String hexString;
if (lexer.token == Token.LITERAL_HEX) {
hexString = lexer.hexString();
lexer.nextToken();
} else if (lexer.token == Token.LITERAL_CHARS) {
hexString = null;
} else {
acceptIdentifier("X");
hexString = lexer.stringVal();
accept(Token.LITERAL_CHARS);
}
if (hexString == null) {
String str = lexer.stringVal();
lexer.nextToken();
expr = new MySqlCharExpr(str, "_big5");
} else {
expr = new SQLCharExpr(MySqlUtils.big5(hexString));
}
} else {
if (lexer.isEnabled(SQLParserFeature.IgnoreNameQuotes)) {
ident = SQLUtils.normalize(ident, dbType);
}
if (ident.charAt(0) == '"' && ident.charAt(ident.length() - 1) == '"') {
hash_lower = FnvHash.hashCode64(ident);
}
SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(ident, hash_lower);
if (lexer.keepSourceLocation) {
lexer.computeRowAndColumn();
identifierExpr.setSourceLine(lexer.posLine);
identifierExpr.setSourceColumn(lexer.posColumn);
}
expr = identifierExpr;
}
token = lexer.token;
if (token == Token.DOT) {
lexer.nextTokenIdent();
String name;
long name_hash_lower;
if (lexer.token == Token.STAR) {
name = "*";
name_hash_lower = FnvHash.Constants.STAR;
} else {
name = lexer.stringVal();
name_hash_lower = lexer.hash_lower();
}
lexer.nextTokenComma();
token = lexer.token;
if (token == Token.LPAREN) {
boolean aggregate = hash_lower == FnvHash.Constants.WMSYS && name_hash_lower == FnvHash.Constants.WM_CONCAT;
expr = methodRest(expr, name, aggregate);
token = lexer.token;
} else {
if (name_hash_lower == FnvHash.Constants.NEXTVAL) {
expr = new SQLSequenceExpr((SQLIdentifierExpr) expr, SQLSequenceExpr.Function.NextVal);
} else if (name_hash_lower == FnvHash.Constants.CURRVAL) {
expr = new SQLSequenceExpr((SQLIdentifierExpr) expr, SQLSequenceExpr.Function.CurrVal);
} else if (name_hash_lower == FnvHash.Constants.PREVVAL) {
expr = new SQLSequenceExpr((SQLIdentifierExpr) expr, SQLSequenceExpr.Function.PrevVal);
} else {
if (lexer.isEnabled(SQLParserFeature.IgnoreNameQuotes)) {
name = SQLUtils.normalize(name, dbType);
}
if (name.charAt(0) == '"') {
name_hash_lower = FnvHash.hashCode64(name);
}
expr = new SQLPropertyExpr(expr, name, name_hash_lower);
}
}
}
if (token == Token.COMMA) {
return new SQLSelectItem(expr, (String) null, connectByRoot);
}
if (token == Token.AS) {
lexer.nextTokenAlias();
String as = null;
if (lexer.token != Token.COMMA && lexer.token != Token.FROM) {
as = lexer.stringVal();
if (isEnabled(SQLParserFeature.IgnoreNameQuotes) && as.length() > 1) {
as = StringUtils.removeNameQuotes(as);
}
lexer.nextTokenComma();
if (lexer.token == Token.DOT) {
lexer.nextToken();
as += '.' + lexer.stringVal();
lexer.nextToken();
}
}
return new SQLSelectItem(expr, as, connectByRoot);
}
if (token == Token.LITERAL_ALIAS) {
String as = lexer.stringVal();
if (isEnabled(SQLParserFeature.IgnoreNameQuotes) && as.length() > 1) {
as = StringUtils.removeNameQuotes(as);
}
lexer.nextTokenComma();
return new SQLSelectItem(expr, as, connectByRoot);
}
if (token == Token.IDENTIFIER && hash_lower != FnvHash.Constants.CURRENT) {
String as;
if (lexer.hash_lower == FnvHash.Constants.FORCE && DbType.mysql == dbType) {
String force = lexer.stringVal();
Lexer.SavePoint savePoint = lexer.mark();
lexer.nextToken();
if (lexer.token == Token.PARTITION) {
lexer.reset(savePoint);
as = null;
} else {
as = force;
if (isEnabled(SQLParserFeature.IgnoreNameQuotes) && as.length() > 1) {
as = StringUtils.removeNameQuotes(as);
}
lexer.nextTokenComma();
}
} else if (lexer.hash_lower == FnvHash.Constants.SOUNDS && DbType.mysql == dbType) {
String sounds = lexer.stringVal();
Lexer.SavePoint savePoint = lexer.mark();
lexer.nextToken();
if (lexer.token == Token.LIKE) {
lexer.reset(savePoint);
expr = exprRest(expr);
as = as();
} else {
as = sounds;
if (isEnabled(SQLParserFeature.IgnoreNameQuotes) && as.length() > 1) {
as = StringUtils.removeNameQuotes(as);
}
lexer.nextTokenComma();
}
} else if (lexer.hash_lower == FnvHash.Constants.COLLATE && lexer.stringVal().charAt(0) != '`' && DbType.mysql == dbType) {
expr = primaryRest(expr);
as = as();
} else if (lexer.hash_lower == FnvHash.Constants.REGEXP && lexer.stringVal().charAt(0) != '`' && DbType.mysql == dbType) {
expr = exprRest(expr);
as = as();
} else {
as = lexer.stringVal();
if (isEnabled(SQLParserFeature.IgnoreNameQuotes) && as.length() > 1) {
as = StringUtils.removeNameQuotes(as);
}
lexer.nextTokenComma();
}
return new SQLSelectItem(expr, as, connectByRoot);
}
if (token == Token.LPAREN) {
if (dbType == DbType.mysql) {
lexer.nextTokenValue();
} else {
lexer.nextToken();
}
expr = this.methodRest(expr, false);
} else {
expr = this.primaryRest(expr);
}
expr = this.exprRest(expr);
} else if (token == Token.STAR) {
expr = new SQLAllColumnExpr();
lexer.nextToken();
return new SQLSelectItem(expr, (String) null, connectByRoot);
} else if (token == Token.DO || token == Token.JOIN || token == Token.TABLESPACE) {
expr = this.name();
expr = this.exprRest(expr);
} else {
if (lexer.token == Token.DISTINCT && dbType == DbType.elastic_search) {
lexer.nextToken();
}
while (lexer.token == Token.HINT) {
lexer.nextToken();
}
expr = expr();
}
String alias;
List<String> aliasList = null;
switch(lexer.token) {
case FULL:
case TABLESPACE:
alias = lexer.stringVal();
lexer.nextToken();
break;
case AS:
lexer.nextTokenAlias();
if (lexer.token == Token.LITERAL_INT) {
alias = '"' + lexer.stringVal() + '"';
lexer.nextToken();
} else if (lexer.token == Token.LPAREN) {
lexer.nextToken();
aliasList = new ArrayList<String>();
for (; ; ) {
String stringVal = lexer.stringVal();
lexer.nextToken();
aliasList.add(stringVal);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
alias = null;
} else {
alias = alias();
}
break;
case EOF:
alias = null;
break;
default:
alias = as();
break;
}
if (alias == null && isEnabled(SQLParserFeature.SelectItemGenerateAlias) && (!(expr instanceof SQLName)) && !(expr instanceof SQLNumericLiteralExpr) && !(expr instanceof SQLCharExpr) && !(expr instanceof SQLNullExpr) && !(expr instanceof SQLBooleanExpr)) {
alias = lexer.text.substring(startPos, lexer.startPos);
if (lexer.comments != null) {
for (int i = lexer.comments.size() - 1; i >= 0; i--) {
String comment = lexer.comments.get(i);
int p = alias.lastIndexOf(comment);
if (p >= 0) {
alias = alias.substring(0, p - 1);
}
}
}
alias = CharTypes.trim(alias);
if (alias.length() > 0) {
boolean specialChar = false;
for (int i = 0; i < alias.length(); ++i) {
char ch = alias.charAt(i);
if (dbType == DbType.mysql) {
if (ch == '`') {
specialChar = true;
break;
}
} else if (!CharTypes.isIdentifierChar(ch)) {
specialChar = true;
break;
}
}
if (specialChar) {
if (dbType == DbType.mysql) {
alias = alias.replaceAll("`", "``");
alias = '`' + alias + '`';
} else {
alias = alias.replaceAll("\"", "\\\"");
}
}
}
}
SQLSelectItem selectItem;
if (aliasList != null) {
selectItem = new SQLSelectItem(expr, aliasList, connectByRoot);
} else {
selectItem = new SQLSelectItem(expr, alias, connectByRoot);
}
if (lexer.token == Token.HINT && !lexer.isEnabled(SQLParserFeature.StrictForWall)) {
String comment = "/*" + lexer.stringVal() + "*/";
selectItem.addAfterComment(comment);
lexer.nextToken();
}
return selectItem;
}
use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr in project druid by alibaba.
the class MySqlExprParser method parseColumnRest.
public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) {
if (lexer.token() == Token.ON) {
lexer.nextToken();
accept(Token.UPDATE);
SQLExpr expr = this.expr();
column.setOnUpdate(expr);
}
if (identifierEquals("CHARSET")) {
lexer.nextToken();
MySqlCharExpr charSetCollateExpr = new MySqlCharExpr();
charSetCollateExpr.setCharset(lexer.stringVal());
lexer.nextToken();
if (identifierEquals("COLLATE")) {
lexer.nextToken();
charSetCollateExpr.setCollate(lexer.stringVal());
lexer.nextToken();
}
column.setCharsetExpr(charSetCollateExpr);
return parseColumnRest(column);
}
if (identifierEquals("AUTO_INCREMENT")) {
lexer.nextToken();
column.setAutoIncrement(true);
return parseColumnRest(column);
}
if (identifierEquals("precision") && column.getDataType().getName().equalsIgnoreCase("double")) {
lexer.nextToken();
}
if (lexer.token() == Token.PARTITION) {
throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal());
}
if (identifierEquals("STORAGE")) {
lexer.nextToken();
SQLExpr expr = expr();
column.setStorage(expr);
}
if (lexer.token() == Token.AS) {
lexer.nextToken();
accept(Token.LPAREN);
SQLExpr expr = expr();
column.setAsExpr(expr);
accept(Token.RPAREN);
}
if (identifierEquals("STORED")) {
lexer.nextToken();
column.setSorted(true);
}
if (identifierEquals("VIRTUAL")) {
lexer.nextToken();
column.setVirtual(true);
}
super.parseColumnRest(column);
return column;
}
use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr in project druid by alibaba.
the class SQLExprParser method primary.
public SQLExpr primary() {
List<String> beforeComments = null;
if (lexer.isKeepComments() && lexer.hasComment()) {
beforeComments = lexer.readAndResetComments();
}
SQLExpr sqlExpr = null;
final Token tok = lexer.token;
switch(tok) {
case LPAREN:
int paranCount = 0;
lexer.nextToken();
if (lexer.token == Token.RPAREN) {
lexer.nextToken();
sqlExpr = new SQLMethodInvokeExpr();
break;
}
sqlExpr = expr();
if (lexer.token == Token.COMMA) {
SQLListExpr listExpr = new SQLListExpr();
listExpr.addItem(sqlExpr);
do {
lexer.nextToken();
listExpr.addItem(expr());
} while (lexer.token == Token.COMMA);
sqlExpr = listExpr;
}
if (sqlExpr instanceof SQLBinaryOpExpr) {
((SQLBinaryOpExpr) sqlExpr).setParenthesized(true);
}
if ((lexer.token == Token.UNION || lexer.token == Token.MINUS || lexer.token == Token.EXCEPT) && sqlExpr instanceof SQLQueryExpr) {
SQLQueryExpr queryExpr = (SQLQueryExpr) sqlExpr;
SQLSelectQuery query = this.createSelectParser().queryRest(queryExpr.getSubQuery().getQuery(), true);
queryExpr.getSubQuery().setQuery(query);
}
accept(Token.RPAREN);
break;
case INSERT:
lexer.nextToken();
if (lexer.token != Token.LPAREN) {
throw new ParserException("syntax error. " + lexer.info());
}
sqlExpr = new SQLIdentifierExpr("INSERT");
break;
case IDENTIFIER:
String ident = lexer.stringVal();
long hash_lower = lexer.hash_lower();
int sourceLine = -1, sourceColumn = -1;
if (lexer.keepSourceLocation) {
lexer.computeRowAndColumn();
sourceLine = lexer.posLine;
sourceColumn = lexer.posColumn;
}
lexer.nextToken();
if (hash_lower == FnvHash.Constants.TRY_CAST) {
accept(Token.LPAREN);
SQLCastExpr cast = new SQLCastExpr();
cast.setTry(true);
cast.setExpr(expr());
accept(Token.AS);
cast.setDataType(parseDataType(false));
accept(Token.RPAREN);
sqlExpr = cast;
} else if (hash_lower == FnvHash.Constants.DATE && (lexer.token == Token.LITERAL_CHARS || lexer.token == Token.VARIANT) && (SQLDateExpr.isSupport(dbType))) {
String literal = lexer.token == Token.LITERAL_CHARS ? lexer.stringVal() : "?";
lexer.nextToken();
SQLDateExpr dateExpr = new SQLDateExpr();
dateExpr.setLiteral(literal);
sqlExpr = dateExpr;
} else if (hash_lower == FnvHash.Constants.TIMESTAMP && (lexer.token == Token.LITERAL_CHARS || lexer.token == Token.VARIANT) && DbType.oracle != dbType) {
SQLTimestampExpr dateExpr = new SQLTimestampExpr(lexer.stringVal());
lexer.nextToken();
sqlExpr = dateExpr;
} else if (hash_lower == FnvHash.Constants.TIME && (lexer.token == Token.LITERAL_CHARS || lexer.token == Token.VARIANT)) {
SQLTimeExpr dateExpr = new SQLTimeExpr(lexer.stringVal());
lexer.nextToken();
sqlExpr = dateExpr;
} else if (hash_lower == FnvHash.Constants.TIME && lexer.token == Token.LITERAL_ALIAS) {
SQLTimeExpr dateExpr = new SQLTimeExpr(SQLUtils.normalize(lexer.stringVal()));
lexer.nextToken();
sqlExpr = dateExpr;
} else if (hash_lower == FnvHash.Constants.DATETIME && (lexer.token == Token.LITERAL_CHARS || lexer.token == Token.VARIANT)) {
SQLDateTimeExpr dateExpr = new SQLDateTimeExpr(lexer.stringVal());
lexer.nextToken();
sqlExpr = dateExpr;
} else if (hash_lower == FnvHash.Constants.DATETIME && lexer.token == Token.LITERAL_ALIAS) {
SQLDateTimeExpr dateExpr = new SQLDateTimeExpr(SQLUtils.normalize(lexer.stringVal()));
lexer.nextToken();
sqlExpr = dateExpr;
} else if (hash_lower == FnvHash.Constants.BOOLEAN && lexer.token == Token.LITERAL_CHARS) {
sqlExpr = new SQLBooleanExpr(Boolean.valueOf(lexer.stringVal()));
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.VARCHAR && lexer.token == Token.LITERAL_CHARS) {
if (dbType == DbType.mysql) {
MySqlCharExpr mysqlChar = new MySqlCharExpr(lexer.stringVal());
mysqlChar.setType("VARCHAR");
sqlExpr = mysqlChar;
} else {
sqlExpr = new SQLCharExpr(lexer.stringVal());
}
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.CHAR && lexer.token == Token.LITERAL_CHARS) {
if (dbType == DbType.mysql) {
MySqlCharExpr mysqlChar = new MySqlCharExpr(lexer.stringVal());
mysqlChar.setType("CHAR");
sqlExpr = mysqlChar;
} else {
sqlExpr = new SQLCharExpr(lexer.stringVal());
}
lexer.nextToken();
} else if (DbType.mysql == dbType && ident.startsWith("0x") && (ident.length() % 2) == 0) {
sqlExpr = new SQLHexExpr(ident.substring(2));
} else if (DbType.mysql == dbType && hash_lower == FnvHash.Constants.JSON && lexer.token == Token.LITERAL_CHARS) {
sqlExpr = new SQLJSONExpr(lexer.stringVal());
lexer.nextToken();
} else if (DbType.mysql == dbType && hash_lower == FnvHash.Constants.DECIMAL && lexer.token == Token.LITERAL_CHARS) {
sqlExpr = new SQLDecimalExpr(lexer.stringVal());
lexer.nextToken();
} else if (DbType.mysql == dbType && hash_lower == FnvHash.Constants.DOUBLE && lexer.token == Token.LITERAL_CHARS) {
sqlExpr = new SQLDoubleExpr(lexer.stringVal());
lexer.nextToken();
} else if (DbType.mysql == dbType && hash_lower == FnvHash.Constants.FLOAT && lexer.token == Token.LITERAL_CHARS) {
sqlExpr = new SQLFloatExpr(lexer.stringVal());
lexer.nextToken();
} else if (DbType.mysql == dbType && hash_lower == FnvHash.Constants.SMALLINT && lexer.token == Token.LITERAL_CHARS) {
sqlExpr = new SQLSmallIntExpr(lexer.stringVal());
lexer.nextToken();
} else if (DbType.mysql == dbType && hash_lower == FnvHash.Constants.TINYINT && lexer.token == Token.LITERAL_CHARS) {
sqlExpr = new SQLTinyIntExpr(lexer.stringVal());
lexer.nextToken();
} else if (DbType.mysql == dbType && hash_lower == FnvHash.Constants.BIGINT && lexer.token == Token.LITERAL_CHARS) {
String strVal = lexer.stringVal();
if (strVal.startsWith("--")) {
strVal = strVal.substring(2);
}
sqlExpr = new SQLBigIntExpr(strVal);
lexer.nextToken();
} else if (DbType.mysql == dbType && hash_lower == FnvHash.Constants.INTEGER && lexer.token == Token.LITERAL_CHARS) {
String strVal = lexer.stringVal();
if (strVal.startsWith("--")) {
strVal = strVal.substring(2);
}
SQLIntegerExpr integerExpr = SQLIntegerExpr.ofIntOrLong(Long.parseLong(strVal));
integerExpr.setType("INTEGER");
sqlExpr = integerExpr;
lexer.nextToken();
} else if (DbType.mysql == dbType && hash_lower == FnvHash.Constants.REAL && lexer.token == Token.LITERAL_CHARS) {
sqlExpr = new SQLRealExpr(lexer.stringVal());
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.DATE && lexer.token == Token.LITERAL_ALIAS) {
sqlExpr = new SQLDateExpr(lexer.stringVal());
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.DATETIME && lexer.token == Token.LITERAL_ALIAS) {
sqlExpr = new SQLDateTimeExpr(lexer.stringVal());
lexer.nextToken();
} else if (hash_lower == FnvHash.Constants.TIMESTAMP && lexer.token == Token.LITERAL_ALIAS) {
sqlExpr = new SQLTimestampExpr(lexer.stringVal());
lexer.nextToken();
} else {
char c0 = ident.charAt(0);
if (c0 == '`' || c0 == '[' || c0 == '"') {
if (lexer.isEnabled(SQLParserFeature.IgnoreNameQuotes)) {
ident = ident.substring(1, ident.length() - 1);
}
hash_lower = FnvHash.hashCode64(ident);
}
SQLIdentifierExpr identifierExpr = new SQLIdentifierExpr(ident, hash_lower);
if (sourceLine != -1) {
identifierExpr.setSourceLine(sourceLine);
identifierExpr.setSourceColumn(sourceColumn);
}
sqlExpr = identifierExpr;
}
break;
case NEW:
throw new ParserException("TODO " + lexer.info());
case LITERAL_INT:
Number number = lexer.integerValue();
lexer.nextToken();
if (lexer.identifierEquals(FnvHash.Constants.BD)) {
SQLDecimalExpr decimalExpr = new SQLDecimalExpr();
decimalExpr.setValue(new BigDecimal(number.intValue()));
lexer.nextToken();
sqlExpr = decimalExpr;
} else {
sqlExpr = new SQLIntegerExpr(number);
}
break;
case LITERAL_FLOAT:
sqlExpr = lexer.numberExpr();
lexer.nextToken();
if (lexer.identifierEquals(FnvHash.Constants.BD)) {
SQLDecimalExpr decimalExpr = new SQLDecimalExpr();
decimalExpr.setValue(new BigDecimal(sqlExpr.toString()));
lexer.nextToken();
sqlExpr = decimalExpr;
}
break;
case LITERAL_CHARS:
{
sqlExpr = new SQLCharExpr(lexer.stringVal());
if (DbType.mysql == dbType) {
lexer.nextTokenValue();
for (; ; ) {
if (lexer.token == Token.LITERAL_ALIAS) {
String concat = ((SQLCharExpr) sqlExpr).getText();
concat += lexer.stringVal();
lexer.nextTokenValue();
sqlExpr = new SQLCharExpr(concat);
} else if (lexer.token == Token.LITERAL_CHARS || lexer.token == Token.LITERAL_NCHARS) {
String concat = ((SQLCharExpr) sqlExpr).getText();
concat += lexer.stringVal();
lexer.nextTokenValue();
sqlExpr = new SQLCharExpr(concat);
} else {
break;
}
}
} else {
lexer.nextToken();
}
break;
}
case LITERAL_NCHARS:
sqlExpr = new SQLNCharExpr(lexer.stringVal());
lexer.nextToken();
if (DbType.mysql == dbType) {
SQLMethodInvokeExpr concat = null;
for (; ; ) {
if (lexer.token == Token.LITERAL_ALIAS) {
if (concat == null) {
concat = new SQLMethodInvokeExpr("CONCAT");
concat.addArgument(sqlExpr);
sqlExpr = concat;
}
String alias = lexer.stringVal();
lexer.nextToken();
SQLCharExpr concat_right = new SQLCharExpr(alias.substring(1, alias.length() - 1));
concat.addArgument(concat_right);
} else if (lexer.token == Token.LITERAL_CHARS || lexer.token == Token.LITERAL_NCHARS) {
if (concat == null) {
concat = new SQLMethodInvokeExpr("CONCAT");
concat.addArgument(sqlExpr);
sqlExpr = concat;
}
String chars = lexer.stringVal();
lexer.nextToken();
SQLCharExpr concat_right = new SQLCharExpr(chars);
concat.addArgument(concat_right);
} else {
break;
}
}
}
break;
case VARIANT:
{
String varName = lexer.stringVal();
lexer.nextToken();
if (varName.equals(":") && lexer.token == Token.IDENTIFIER && DbType.oracle == dbType) {
String part2 = lexer.stringVal();
lexer.nextToken();
varName += part2;
}
SQLVariantRefExpr varRefExpr = new SQLVariantRefExpr(varName);
if (varName.startsWith(":")) {
varRefExpr.setIndex(lexer.nextVarIndex());
}
if (varRefExpr.getName().equals("@") && (lexer.token == Token.LITERAL_CHARS || (lexer.token == Token.VARIANT && lexer.stringVal().startsWith("@")))) {
varRefExpr.setName("@'" + lexer.stringVal() + "'");
lexer.nextToken();
} else if (varRefExpr.getName().equals("@@") && lexer.token == Token.LITERAL_CHARS) {
varRefExpr.setName("@@'" + lexer.stringVal() + "'");
lexer.nextToken();
}
sqlExpr = varRefExpr;
}
break;
case DEFAULT:
if (dbType == DbType.clickhouse) {
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
} else {
sqlExpr = new SQLDefaultExpr();
}
lexer.nextToken();
break;
case DUAL:
case KEY:
// case DISTINCT:
case LIMIT:
case SCHEMA:
case COLUMN:
case IF:
case END:
case COMMENT:
case COMPUTE:
case ENABLE:
case DISABLE:
case INITIALLY:
case SEQUENCE:
case USER:
case EXPLAIN:
case WITH:
case GRANT:
case REPLACE:
case INDEX:
// case MODEL:
case PCTFREE:
case INITRANS:
case MAXTRANS:
case SEGMENT:
case CREATION:
case IMMEDIATE:
case DEFERRED:
case STORAGE:
case NEXT:
case MINEXTENTS:
case MAXEXTENTS:
case MAXSIZE:
case PCTINCREASE:
case FLASH_CACHE:
case CELL_FLASH_CACHE:
case NONE:
case LOB:
case STORE:
case ROW:
case CHUNK:
case CACHE:
case NOCACHE:
case LOGGING:
case NOCOMPRESS:
case KEEP_DUPLICATES:
case EXCEPTIONS:
case PURGE:
case FULL:
case TO:
case IDENTIFIED:
case PASSWORD:
case BINARY:
case WINDOW:
case OFFSET:
case SHARE:
case START:
case CONNECT:
case MATCHED:
case ERRORS:
case REJECT:
case UNLIMITED:
case BEGIN:
case EXCLUSIVE:
case MODE:
case ADVISE:
case VIEW:
case ESCAPE:
case OVER:
case ORDER:
case CONSTRAINT:
case TYPE:
case OPEN:
case REPEAT:
case TABLE:
case TRUNCATE:
case EXCEPTION:
case FUNCTION:
case IDENTITY:
case EXTRACT:
case DESC:
case DO:
case GROUP:
case MOD:
case CONCAT:
case PRIMARY:
case PARTITION:
case LEAVE:
case CLOSE:
case CONDITION:
case OUT:
case USE:
case EXCEPT:
case INTERSECT:
case MERGE:
case MINUS:
case UNTIL:
case TOP:
case SHOW:
case INOUT:
case OUTER:
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
lexer.nextToken();
break;
case CASE:
SQLCaseExpr caseExpr = new SQLCaseExpr();
lexer.nextToken();
if (lexer.token != Token.WHEN) {
caseExpr.setValueExpr(expr());
}
accept(Token.WHEN);
SQLExpr testExpr = expr();
accept(Token.THEN);
SQLExpr valueExpr = expr();
SQLCaseExpr.Item caseItem = new SQLCaseExpr.Item(testExpr, valueExpr);
caseExpr.addItem(caseItem);
while (lexer.token == Token.WHEN) {
lexer.nextToken();
testExpr = expr();
accept(Token.THEN);
valueExpr = expr();
caseItem = new SQLCaseExpr.Item(testExpr, valueExpr);
caseExpr.addItem(caseItem);
}
if (lexer.token == Token.ELSE) {
lexer.nextToken();
caseExpr.setElseExpr(expr());
}
accept(Token.END);
sqlExpr = caseExpr;
break;
case EXISTS:
{
String strVal = lexer.stringVal();
lexer.nextToken();
switch(lexer.token) {
case COMMA:
case DOT:
sqlExpr = new SQLIdentifierExpr(strVal);
break;
default:
accept(Token.LPAREN);
sqlExpr = new SQLExistsExpr(createSelectParser().select());
accept(Token.RPAREN);
parseQueryPlanHint(sqlExpr);
break;
}
break;
}
case NOT:
lexer.nextToken();
if (lexer.token == Token.EXISTS) {
lexer.nextToken();
accept(Token.LPAREN);
SQLExistsExpr exists = new SQLExistsExpr(createSelectParser().select(), true);
accept(Token.RPAREN);
parseQueryPlanHint(exists);
if (lexer.token == Token.EQ) {
exists.setNot(false);
SQLExpr relational = this.relationalRest(exists);
sqlExpr = new SQLNotExpr(relational);
} else {
sqlExpr = exists;
}
} else if (lexer.token == Token.LPAREN) {
lexer.nextToken();
SQLExpr notTarget = expr();
accept(Token.RPAREN);
notTarget = bitXorRest(notTarget);
notTarget = multiplicativeRest(notTarget);
notTarget = additiveRest(notTarget);
notTarget = shiftRest(notTarget);
notTarget = bitAndRest(notTarget);
notTarget = bitOrRest(notTarget);
notTarget = inRest(notTarget);
notTarget = relationalRest(notTarget);
sqlExpr = new SQLNotExpr(notTarget);
parseQueryPlanHint(sqlExpr);
return primaryRest(sqlExpr);
} else {
SQLExpr restExpr = relational();
sqlExpr = new SQLNotExpr(restExpr);
parseQueryPlanHint(sqlExpr);
}
break;
case FROM:
case SELECT:
SQLQueryExpr queryExpr = new SQLQueryExpr(createSelectParser().select());
sqlExpr = queryExpr;
break;
case CAST:
String castStr = lexer.stringVal();
lexer.nextToken();
if (lexer.token != Token.LPAREN) {
sqlExpr = new SQLIdentifierExpr(castStr);
} else {
lexer.nextToken();
SQLCastExpr cast = new SQLCastExpr();
cast.setExpr(expr());
accept(Token.AS);
cast.setDataType(parseDataType(false));
accept(Token.RPAREN);
sqlExpr = cast;
}
break;
case SUB:
lexer.nextToken();
switch(lexer.token) {
case LITERAL_INT:
Number integerValue = lexer.integerValue();
if (integerValue instanceof Integer) {
int intVal = integerValue.intValue();
if (intVal == Integer.MIN_VALUE) {
integerValue = Long.valueOf(((long) intVal) * -1);
} else {
integerValue = Integer.valueOf(intVal * -1);
}
} else if (integerValue instanceof Long) {
long longVal = ((Long) integerValue).longValue();
if (longVal == 2147483648L) {
integerValue = Integer.valueOf((int) (((long) longVal) * -1));
} else {
integerValue = Long.valueOf(longVal * -1);
}
} else {
integerValue = ((BigInteger) integerValue).negate();
}
sqlExpr = new SQLIntegerExpr(integerValue);
lexer.nextToken();
break;
case LITERAL_FLOAT:
sqlExpr = lexer.numberExpr(true);
lexer.nextToken();
break;
case LITERAL_CHARS:
case LITERAL_ALIAS:
if (dbType == DbType.mysql) {
sqlExpr = new SQLCharExpr(lexer.stringVal());
} else {
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
}
lexer.nextToken();
if (lexer.token == Token.LPAREN || lexer.token == Token.LBRACKET || lexer.token == Token.DOT) {
sqlExpr = primaryRest(sqlExpr);
}
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
break;
case QUES:
{
SQLVariantRefExpr variantRefExpr = new SQLVariantRefExpr("?");
variantRefExpr.setIndex(lexer.nextVarIndex());
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, variantRefExpr);
lexer.nextToken();
break;
}
case PLUS:
case SUB:
case LPAREN:
case IDENTIFIER:
case BANG:
case CASE:
case CAST:
case NULL:
case INTERVAL:
case LBRACE:
case IF:
case CHECK:
case INDEX:
case PRIMARY:
case KEY:
case REPLACE:
sqlExpr = primary();
while (lexer.token == Token.HINT) {
lexer.nextToken();
}
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
break;
case VARIANT:
sqlExpr = primary();
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
break;
default:
throw new ParserException("TODO : " + lexer.info());
}
break;
case PLUS:
lexer.nextToken();
switch(lexer.token) {
case LITERAL_CHARS:
case LITERAL_ALIAS:
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Plus, sqlExpr);
lexer.nextToken();
break;
case QUES:
{
SQLVariantRefExpr variantRefExpr = new SQLVariantRefExpr("?");
variantRefExpr.setIndex(lexer.nextVarIndex());
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Plus, variantRefExpr);
lexer.nextToken();
break;
}
case PLUS:
case SUB:
case LITERAL_FLOAT:
case LITERAL_INT:
case LPAREN:
case IDENTIFIER:
case BANG:
case CASE:
case CAST:
case NULL:
case INTERVAL:
case LBRACE:
case IF:
case CHECK:
case REPLACE:
sqlExpr = primary();
while (lexer.token == Token.HINT) {
lexer.nextToken();
}
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Plus, sqlExpr);
break;
default:
throw new ParserException("TODO " + lexer.info());
}
break;
case TILDE:
lexer.nextToken();
SQLExpr unaryValueExpr = primary();
SQLUnaryExpr unary = new SQLUnaryExpr(SQLUnaryOperator.Compl, unaryValueExpr);
sqlExpr = unary;
break;
case QUES:
if (dbType == DbType.mysql) {
lexer.nextTokenValue();
} else {
lexer.nextToken();
}
SQLVariantRefExpr quesVarRefExpr = new SQLVariantRefExpr("?");
quesVarRefExpr.setIndex(lexer.nextVarIndex());
sqlExpr = quesVarRefExpr;
break;
case LEFT:
sqlExpr = new SQLIdentifierExpr("LEFT");
lexer.nextToken();
break;
case RIGHT:
sqlExpr = new SQLIdentifierExpr("RIGHT");
lexer.nextToken();
break;
case INNER:
sqlExpr = new SQLIdentifierExpr("INNER");
lexer.nextToken();
break;
case DATABASE:
sqlExpr = new SQLIdentifierExpr("DATABASE");
lexer.nextToken();
break;
case LOCK:
sqlExpr = new SQLIdentifierExpr("LOCK");
lexer.nextToken();
break;
case NULL:
sqlExpr = new SQLNullExpr();
lexer.nextToken();
break;
case BANG:
lexer.nextToken();
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Not, primary());
break;
case BANGBANG:
{
if (dbType == DbType.hive) {
throw new ParserException(lexer.info());
}
lexer.nextToken();
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Not, primary());
break;
}
case BANG_TILDE:
{
lexer.nextToken();
SQLExpr bangExpr = primary();
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Not, new SQLUnaryExpr(SQLUnaryOperator.Compl, bangExpr));
break;
}
case LITERAL_HEX:
String hex = lexer.hexString();
sqlExpr = new SQLHexExpr(hex);
lexer.nextToken();
break;
case INTERVAL:
sqlExpr = parseInterval();
break;
case COLON:
lexer.nextToken();
if (lexer.token == Token.LITERAL_ALIAS) {
sqlExpr = new SQLVariantRefExpr(":\"" + lexer.stringVal() + "\"");
lexer.nextToken();
}
break;
case ANY:
sqlExpr = parseAny();
break;
case SOME:
sqlExpr = parseSome();
break;
case ALL:
sqlExpr = parseAll();
break;
case LITERAL_ALIAS:
sqlExpr = parseAliasExpr(lexer.stringVal());
lexer.nextToken();
break;
case EOF:
throw new EOFParserException();
case TRUE:
lexer.nextToken();
sqlExpr = new SQLBooleanExpr(true);
break;
case FALSE:
lexer.nextToken();
sqlExpr = new SQLBooleanExpr(false);
break;
case BITS:
{
String strVal = lexer.stringVal();
lexer.nextToken();
sqlExpr = new SQLBinaryExpr(strVal);
break;
}
case GLOBAL:
case CONTAINS:
sqlExpr = inRest(null);
break;
case SET:
{
Lexer.SavePoint savePoint = lexer.mark();
lexer.nextToken();
if (lexer.token == Token.SET && dbType == DbType.odps) {
lexer.nextToken();
}
if (lexer.token() == Token.LPAREN) {
sqlExpr = new SQLIdentifierExpr("SET");
} else if (lexer.token == Token.DOT) {
sqlExpr = new SQLIdentifierExpr("SET");
sqlExpr = this.primaryRest(sqlExpr);
} else {
lexer.reset(savePoint);
throw new ParserException("ERROR. " + lexer.info());
}
break;
}
case LBRACE:
{
lexer.nextToken();
if (lexer.identifierEquals(FnvHash.Constants.TS)) {
lexer.nextToken();
String literal = lexer.stringVal();
lexer.nextToken();
sqlExpr = new SQLTimestampExpr(literal);
} else if (lexer.identifierEquals(FnvHash.Constants.D) || lexer.identifierEquals(FnvHash.Constants.DATE)) {
lexer.nextToken();
String literal = lexer.stringVal();
if (literal.length() > 2 && literal.charAt(0) == '"' && literal.charAt(literal.length() - 1) == '"') {
literal = literal.substring(1, literal.length() - 1);
}
lexer.nextToken();
sqlExpr = new SQLDateExpr(literal);
} else if (lexer.identifierEquals(FnvHash.Constants.T)) {
lexer.nextToken();
String literal = lexer.stringVal();
lexer.nextToken();
sqlExpr = new SQLTimeExpr(literal);
} else if (lexer.identifierEquals(FnvHash.Constants.FN)) {
lexer.nextToken();
sqlExpr = this.expr();
} else if (DbType.mysql == dbType) {
// {identifier expr} is ODBC escape syntax and is accepted for ODBC compatibility.
sqlExpr = this.expr();
} else {
throw new ParserException("ERROR. " + lexer.info());
}
accept(Token.RBRACE);
break;
}
case VALUES:
case TRIGGER:
case FOR:
case CHECK:
case DELETE:
case BY:
case UPDATE:
case LOOP:
case LIKE:
case UNION:
case CREATE:
case COMMA:
case STAR:
case DIV:
case DISTRIBUTE:
case UNIQUE:
case PROCEDURE:
case REFERENCES:
case IS:
case REVOKE:
case DECLARE:
case DROP:
case RLIKE:
case FOREIGN:
case FETCH:
case ASC:
case CURSOR:
case ALTER:
if (dbType == DbType.odps || dbType == DbType.hive) {
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
lexer.nextToken();
break;
}
throw new ParserException("ERROR. " + lexer.info());
case AS:
{
if (dbType == DbType.odps) {
Lexer.SavePoint mark = lexer.mark();
String str = lexer.stringVal();
lexer.nextToken();
switch(lexer.token) {
case COMMA:
case RPAREN:
case AS:
case EQ:
case EQEQ:
case LT:
case LTEQ:
case GT:
case GTEQ:
case LTGT:
case SEMI:
sqlExpr = new SQLIdentifierExpr(str);
break;
case DOT:
sqlExpr = primaryRest(new SQLIdentifierExpr(str));
break;
default:
lexer.reset(mark);
break;
}
}
break;
}
case DISTINCT:
if (dbType == DbType.elastic_search || dbType == DbType.mysql) {
Lexer.SavePoint mark = lexer.mark();
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
lexer.nextToken();
if (lexer.token != Token.LPAREN) {
lexer.reset(mark);
throw new ParserException("ERROR. " + lexer.info());
}
break;
}
throw new ParserException("ERROR. " + lexer.info());
case BETWEEN:
case IN:
if (dbType == DbType.odps) {
String str = lexer.stringVal();
lexer.nextToken();
switch(lexer.token) {
case DOT:
case COMMA:
case LT:
case EQ:
case GT:
case RPAREN:
case IS:
case AS:
sqlExpr = new SQLIdentifierExpr(str);
break;
default:
break;
}
if (sqlExpr != null) {
break;
}
accept(Token.LPAREN);
SQLInListExpr in = new SQLInListExpr();
in.setExpr(this.expr());
if (lexer.token == Token.COMMA) {
lexer.nextToken();
this.exprList(in.getTargetList(), in);
}
accept(Token.RPAREN);
sqlExpr = in;
break;
}
throw new ParserException("ERROR. " + lexer.info());
case LBRACKET:
if (dbType == DbType.odps || dbType == DbType.clickhouse) {
SQLArrayExpr array = new SQLArrayExpr();
lexer.nextToken();
this.exprList(array.getValues(), array);
accept(Token.RBRACKET);
sqlExpr = array;
break;
}
throw new ParserException("ERROR. " + lexer.info());
case ON:
if (dbType == DbType.postgresql) {
String methodName = lexer.stringVal();
lexer.nextToken();
if (lexer.token == Token.LPAREN) {
sqlExpr = this.methodRest(new SQLIdentifierExpr(methodName), true);
break;
}
}
throw new ParserException("ERROR. " + lexer.info());
case COLONCOLON:
if (dbType == DbType.odps) {
lexer.nextToken();
SQLExpr temp = this.primary();
if (temp instanceof SQLArrayExpr) {
sqlExpr = temp;
} else {
SQLMethodInvokeExpr method = (SQLMethodInvokeExpr) temp;
method.setOwner(new SQLIdentifierExpr(""));
sqlExpr = method;
}
break;
}
throw new ParserException("ERROR. " + lexer.info());
case ARRAY:
{
SQLArrayExpr array = new SQLArrayExpr();
array.setExpr(new SQLIdentifierExpr("ARRAY"));
lexer.nextToken();
accept(Token.LBRACKET);
this.exprList(array.getValues(), array);
accept(Token.RBRACKET);
sqlExpr = array;
break;
}
default:
throw new ParserException("ERROR. " + lexer.info());
}
SQLExpr expr = primaryRest(sqlExpr);
if (beforeComments != null) {
expr.addBeforeComment(beforeComments);
}
return expr;
}
use of com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr in project druid by alibaba.
the class MySqlASTVisitorAdapterTest method test_adapter.
public void test_adapter() throws Exception {
MySqlASTVisitorAdapter adapter = new MySqlASTVisitorAdapter();
new SQLBooleanExpr().accept(adapter);
new SQLLimit().accept(adapter);
new MySqlTableIndex().accept(adapter);
new MySqlKey().accept(adapter);
new MySqlPrimaryKey().accept(adapter);
new SQLIntervalExpr().accept(adapter);
new SQLBinaryExpr().accept(adapter);
new MySqlPrepareStatement().accept(adapter);
new MySqlExecuteStatement().accept(adapter);
new MysqlDeallocatePrepareStatement().accept(adapter);
new MySqlDeleteStatement().accept(adapter);
new MySqlInsertStatement().accept(adapter);
new MySqlLoadXmlStatement().accept(adapter);
new SQLReplaceStatement().accept(adapter);
new SQLStartTransactionStatement().accept(adapter);
new SQLShowColumnsStatement().accept(adapter);
new SQLShowDatabasesStatement().accept(adapter);
new MySqlShowWarningsStatement().accept(adapter);
new MySqlShowStatusStatement().accept(adapter);
new CobarShowStatus().accept(adapter);
new MySqlKillStatement().accept(adapter);
new MySqlBinlogStatement().accept(adapter);
new MySqlResetStatement().accept(adapter);
new UserSpecification().accept(adapter);
new MySqlPartitionByKey().accept(adapter);
new MySqlOutFileExpr().accept(adapter);
new MySqlUpdateStatement().accept(adapter);
new MySqlSetTransactionStatement().accept(adapter);
new MySqlShowMasterLogsStatement().accept(adapter);
new MySqlShowAuthorsStatement().accept(adapter);
new MySqlShowCollationStatement().accept(adapter);
new MySqlShowBinLogEventsStatement().accept(adapter);
new MySqlShowCharacterSetStatement().accept(adapter);
new MySqlShowContributorsStatement().accept(adapter);
new MySqlShowCreateDatabaseStatement().accept(adapter);
new MySqlShowCreateEventStatement().accept(adapter);
new MySqlShowCreateFunctionStatement().accept(adapter);
new MySqlShowCreateProcedureStatement().accept(adapter);
new SQLShowCreateTableStatement().accept(adapter);
new MySqlShowCreateTriggerStatement().accept(adapter);
new SQLShowCreateViewStatement().accept(adapter);
new MySqlShowEngineStatement().accept(adapter);
new MySqlShowEnginesStatement().accept(adapter);
new MySqlShowErrorsStatement().accept(adapter);
new MySqlShowEventsStatement().accept(adapter);
new MySqlShowFunctionCodeStatement().accept(adapter);
new MySqlShowFunctionStatusStatement().accept(adapter);
new MySqlShowGrantsStatement().accept(adapter);
new MySqlUserName().accept(adapter);
new SQLShowIndexesStatement().accept(adapter);
new MySqlShowMasterStatusStatement().accept(adapter);
new MySqlShowOpenTablesStatement().accept(adapter);
new MySqlShowBinaryLogsStatement().accept(adapter);
new MySqlShowPluginsStatement().accept(adapter);
new MySqlShowPrivilegesStatement().accept(adapter);
new MySqlShowProcedureCodeStatement().accept(adapter);
new MySqlShowProcedureStatusStatement().accept(adapter);
new MySqlShowProcessListStatement().accept(adapter);
new MySqlShowProfileStatement().accept(adapter);
new MySqlShowSlaveHostsStatement().accept(adapter);
new MySqlShowRelayLogEventsStatement().accept(adapter);
new MySqlShowSlaveStatusStatement().accept(adapter);
new MySqlShowTableStatusStatement().accept(adapter);
new MySqlShowTriggersStatement().accept(adapter);
new MySqlRenameTableStatement().accept(adapter);
new MySqlUseIndexHint().accept(adapter);
new MySqlIgnoreIndexHint().accept(adapter);
new MySqlLockTableStatement().accept(adapter);
new MySqlUnlockTablesStatement().accept(adapter);
new MySqlForceIndexHint().accept(adapter);
new MySqlAlterTableChangeColumn().accept(adapter);
new SQLAlterCharacter().accept(adapter);
new MySqlAlterTableOption().accept(adapter);
new MySqlCreateTableStatement().accept(adapter);
new MySqlCharExpr().accept(adapter);
new MySqlUnique().accept(adapter);
new MySqlAlterTableModifyColumn().accept(adapter);
new MySqlAlterTableDiscardTablespace().accept(adapter);
new MySqlAlterTableImportTablespace().accept(adapter);
new TableSpaceOption().accept(adapter);
}
Aggregations