use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class OracleExprParser method primaryRest.
public SQLExpr primaryRest(SQLExpr expr) {
if (expr.getClass() == SQLIdentifierExpr.class) {
String ident = ((SQLIdentifierExpr) expr).getName();
if ("TIMESTAMP".equalsIgnoreCase(ident)) {
if (lexer.token() != Token.LITERAL_ALIAS && lexer.token() != Token.LITERAL_CHARS) {
return new SQLIdentifierExpr("TIMESTAMP");
}
SQLTimestampExpr timestamp = new SQLTimestampExpr();
String literal = lexer.stringVal();
timestamp.setLiteral(literal);
accept(Token.LITERAL_CHARS);
if (identifierEquals("AT")) {
lexer.nextToken();
acceptIdentifier("TIME");
acceptIdentifier("ZONE");
String timezone = lexer.stringVal();
timestamp.setTimeZone(timezone);
accept(Token.LITERAL_CHARS);
}
return primaryRest(timestamp);
}
}
if (lexer.token() == Token.IDENTIFIER && expr instanceof SQLNumericLiteralExpr) {
String ident = lexer.stringVal();
if (ident.length() == 1) {
char unit = ident.charAt(0);
switch(unit) {
case 'K':
case 'M':
case 'G':
case 'T':
case 'P':
case 'E':
case 'k':
case 'm':
case 'g':
case 't':
case 'p':
case 'e':
expr = new OracleSizeExpr(expr, OracleSizeExpr.Unit.valueOf(ident.toUpperCase()));
lexer.nextToken();
break;
default:
break;
}
}
}
if (lexer.token() == Token.DOTDOT) {
lexer.nextToken();
SQLExpr upBound = expr();
return new OracleRangeExpr(expr, upBound);
}
if (lexer.token() == Token.MONKEYS_AT) {
lexer.nextToken();
OracleDbLinkExpr dblink = new OracleDbLinkExpr();
dblink.setExpr(expr);
if (lexer.token() == Token.BANG) {
dblink.setDbLink("!");
lexer.nextToken();
} else {
String link = lexer.stringVal();
accept(Token.IDENTIFIER);
dblink.setDbLink(link);
}
expr = dblink;
}
if (identifierEquals("DAY") || identifierEquals("YEAR")) {
lexer.mark();
String name = lexer.stringVal();
lexer.nextToken();
if (lexer.token() == Token.COMMA) {
lexer.reset();
return expr;
}
OracleIntervalExpr interval = new OracleIntervalExpr();
interval.setValue(expr);
OracleIntervalType type = OracleIntervalType.valueOf(name);
interval.setType(type);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
if (lexer.token() != Token.LITERAL_INT) {
throw new ParserException("syntax error");
}
interval.setPrecision(lexer.integerValue().intValue());
lexer.nextToken();
accept(Token.RPAREN);
}
accept(Token.TO);
if (identifierEquals("SECOND")) {
lexer.nextToken();
interval.setToType(OracleIntervalType.SECOND);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
if (lexer.token() != Token.LITERAL_INT) {
throw new ParserException("syntax error");
}
interval.setFactionalSecondsPrecision(lexer.integerValue().intValue());
lexer.nextToken();
accept(Token.RPAREN);
}
} else {
interval.setToType(OracleIntervalType.MONTH);
lexer.nextToken();
}
expr = interval;
}
if (identifierEquals("AT")) {
char markChar = lexer.current();
int markBp = lexer.bp();
lexer.nextToken();
if (identifierEquals("LOCAL")) {
lexer.nextToken();
expr = new OracleDatetimeExpr(expr, new SQLIdentifierExpr("LOCAL"));
} else {
if (identifierEquals("TIME")) {
lexer.nextToken();
} else {
lexer.reset(markBp, markChar, Token.IDENTIFIER);
return expr;
}
acceptIdentifier("ZONE");
SQLExpr timeZone = primary();
expr = new OracleDatetimeExpr(expr, timeZone);
}
}
SQLExpr restExpr = super.primaryRest(expr);
if (restExpr != expr && restExpr instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvoke = (SQLMethodInvokeExpr) restExpr;
if (methodInvoke.getParameters().size() == 1) {
SQLExpr paramExpr = methodInvoke.getParameters().get(0);
if (paramExpr instanceof SQLIdentifierExpr && "+".equals(((SQLIdentifierExpr) paramExpr).getName())) {
OracleOuterExpr outerExpr = new OracleOuterExpr();
if (methodInvoke.getOwner() == null) {
outerExpr.setExpr(new SQLIdentifierExpr(methodInvoke.getMethodName()));
} else {
outerExpr.setExpr(new SQLPropertyExpr(methodInvoke.getOwner(), methodInvoke.getMethodName()));
}
return outerExpr;
}
}
}
return restExpr;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class OdpsExprParser method parseSelectItem.
@Override
public SQLSelectItem parseSelectItem() {
SQLExpr expr;
if (lexer.token() == Token.IDENTIFIER) {
expr = new SQLIdentifierExpr(lexer.stringVal());
lexer.nextTokenComma();
if (lexer.token() != Token.COMMA) {
expr = this.primaryRest(expr);
expr = this.exprRest(expr);
}
} else {
expr = expr();
}
String alias = null;
if (lexer.token() == Token.AS) {
lexer.nextToken();
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
OdpsUDTFSQLSelectItem selectItem = new OdpsUDTFSQLSelectItem();
selectItem.setExpr(expr);
for (; ; ) {
alias = lexer.stringVal();
lexer.nextToken();
selectItem.getAliasList().add(alias);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
accept(Token.RPAREN);
return selectItem;
} else {
alias = alias();
}
} else {
alias = as();
}
SQLSelectItem item = new SQLSelectItem(expr, alias);
if (lexer.hasComment() && lexer.isKeepComments()) {
item.addAfterComment(lexer.readAndResetComments());
}
return item;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class PGExprParser method primary.
public SQLExpr primary() {
if (lexer.token() == Token.ARRAY) {
SQLArrayExpr array = new SQLArrayExpr();
array.setExpr(new SQLIdentifierExpr(lexer.stringVal()));
lexer.nextToken();
accept(Token.LBRACKET);
this.exprList(array.getValues(), array);
accept(Token.RBRACKET);
return primaryRest(array);
} else if (lexer.token() == Token.POUND) {
lexer.nextToken();
if (lexer.token() == Token.LBRACE) {
lexer.nextToken();
String varName = lexer.stringVal();
lexer.nextToken();
accept(Token.RBRACE);
SQLVariantRefExpr expr = new SQLVariantRefExpr("#{" + varName + "}");
return primaryRest(expr);
} else {
SQLExpr value = this.primary();
SQLUnaryExpr expr = new SQLUnaryExpr(SQLUnaryOperator.Pound, value);
return primaryRest(expr);
}
}
return super.primary();
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class OracleSchemaStatVisitor method visit.
@Override
public boolean visit(OracleSelectJoin x) {
super.visit(x);
for (SQLExpr item : x.getUsing()) {
if (item instanceof SQLIdentifierExpr) {
String columnName = ((SQLIdentifierExpr) item).getName();
String leftTable = (String) x.getLeft().getAttribute(ATTR_TABLE);
String rightTable = (String) x.getRight().getAttribute(ATTR_TABLE);
if (leftTable != null && rightTable != null) {
Relationship relationship = new Relationship();
relationship.setLeft(new Column(leftTable, columnName));
relationship.setRight(new Column(rightTable, columnName));
relationship.setOperator("USING");
relationships.add(relationship);
}
if (leftTable != null) {
addColumn(leftTable, columnName);
}
if (rightTable != null) {
addColumn(rightTable, columnName);
}
}
}
return false;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class SQLServerStatementParser method parseSet.
public SQLStatement parseSet() {
accept(Token.SET);
if (identifierEquals("TRANSACTION")) {
lexer.nextToken();
acceptIdentifier("ISOLATION");
acceptIdentifier("LEVEL");
SQLServerSetTransactionIsolationLevelStatement stmt = new SQLServerSetTransactionIsolationLevelStatement();
if (identifierEquals("READ")) {
lexer.nextToken();
if (identifierEquals("UNCOMMITTED")) {
stmt.setLevel("READ UNCOMMITTED");
lexer.nextToken();
} else if (identifierEquals("COMMITTED")) {
stmt.setLevel("READ COMMITTED");
lexer.nextToken();
} else {
throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal());
}
} else if (identifierEquals("SERIALIZABLE")) {
stmt.setLevel("SERIALIZABLE");
lexer.nextToken();
} else if (identifierEquals("SNAPSHOT")) {
stmt.setLevel("SNAPSHOT");
lexer.nextToken();
} else if (identifierEquals("REPEATABLE")) {
lexer.nextToken();
if (identifierEquals("READ")) {
stmt.setLevel("REPEATABLE READ");
lexer.nextToken();
} else {
throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal());
}
} else {
throw new ParserException("UNKOWN TRANSACTION LEVEL : " + lexer.stringVal());
}
return stmt;
}
if (identifierEquals("STATISTICS")) {
lexer.nextToken();
SQLServerSetStatement stmt = new SQLServerSetStatement();
if (identifierEquals("IO") || identifierEquals("XML") || identifierEquals("PROFILE") || identifierEquals("TIME")) {
stmt.getItem().setTarget(new SQLIdentifierExpr("STATISTICS " + lexer.stringVal().toUpperCase()));
lexer.nextToken();
if (lexer.token() == Token.ON) {
stmt.getItem().setValue(new SQLIdentifierExpr("ON"));
lexer.nextToken();
} else if (identifierEquals("OFF")) {
stmt.getItem().setValue(new SQLIdentifierExpr("OFF"));
lexer.nextToken();
}
}
return stmt;
}
if (lexer.token() == Token.VARIANT) {
SQLSetStatement stmt = new SQLSetStatement(getDbType());
parseAssignItems(stmt.getItems(), stmt);
return stmt;
} else {
SQLServerSetStatement stmt = new SQLServerSetStatement();
stmt.getItem().setTarget(this.exprParser.expr());
if (lexer.token() == Token.ON) {
stmt.getItem().setValue(new SQLIdentifierExpr("ON"));
lexer.nextToken();
} else if (identifierEquals("OFF")) {
stmt.getItem().setValue(new SQLIdentifierExpr("OFF"));
lexer.nextToken();
} else {
stmt.getItem().setValue(this.exprParser.expr());
}
return stmt;
}
}
Aggregations