use of com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSysdateExpr in project druid by alibaba.
the class OracleExprParser method primary.
public SQLExpr primary() {
final Token tok = lexer.token();
SQLExpr sqlExpr = null;
switch(tok) {
case SYSDATE:
lexer.nextToken();
OracleSysdateExpr sysdate = new OracleSysdateExpr();
if (lexer.token() == Token.MONKEYS_AT) {
lexer.nextToken();
accept(Token.BANG);
sysdate.setOption("!");
}
sqlExpr = sysdate;
return primaryRest(sqlExpr);
case PRIOR:
lexer.nextToken();
sqlExpr = expr();
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Prior, sqlExpr);
return primaryRest(sqlExpr);
case COLON:
lexer.nextToken();
if (lexer.token() == Token.LITERAL_INT) {
String name = ":" + lexer.numberString();
lexer.nextToken();
return new SQLVariantRefExpr(name);
} else if (lexer.token() == Token.IDENTIFIER) {
String name = lexer.stringVal();
if (name.charAt(0) == 'B' || name.charAt(0) == 'b') {
lexer.nextToken();
return new SQLVariantRefExpr(":" + name);
}
throw new ParserException("syntax error : " + lexer.token() + " " + lexer.stringVal());
} else {
throw new ParserException("syntax error : " + lexer.token());
}
case LITERAL_ALIAS:
String alias = '"' + lexer.stringVal() + '"';
lexer.nextToken();
return primaryRest(new SQLIdentifierExpr(alias));
case BINARY_FLOAT:
OracleBinaryFloatExpr floatExpr = new OracleBinaryFloatExpr();
floatExpr.setValue(Float.parseFloat(lexer.numberString()));
lexer.nextToken();
return primaryRest(floatExpr);
case BINARY_DOUBLE:
OracleBinaryDoubleExpr doubleExpr = new OracleBinaryDoubleExpr();
doubleExpr.setValue(Double.parseDouble(lexer.numberString()));
lexer.nextToken();
return primaryRest(doubleExpr);
case TABLE:
lexer.nextToken();
return primaryRest(new SQLIdentifierExpr("TABLE"));
case PLUS:
lexer.nextToken();
switch(lexer.token()) {
case LITERAL_INT:
sqlExpr = new SQLIntegerExpr(lexer.integerValue());
lexer.nextToken();
break;
case LITERAL_FLOAT:
sqlExpr = new SQLNumberExpr(lexer.decimalValue());
lexer.nextToken();
break;
case BINARY_FLOAT:
sqlExpr = new OracleBinaryFloatExpr(Float.parseFloat(lexer.numberString()));
lexer.nextToken();
break;
case BINARY_DOUBLE:
sqlExpr = new OracleBinaryDoubleExpr(Double.parseDouble(lexer.numberString()));
lexer.nextToken();
break;
case LPAREN:
lexer.nextToken();
sqlExpr = expr();
accept(Token.RPAREN);
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Plus, sqlExpr);
break;
default:
throw new ParserException("TODO");
}
return primaryRest(sqlExpr);
case SUB:
lexer.nextToken();
switch(lexer.token()) {
case LITERAL_INT:
Number integerValue = lexer.integerValue();
if (integerValue instanceof Integer) {
int intVal = ((Integer) 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 = new SQLNumberExpr(lexer.decimalValue().negate());
lexer.nextToken();
break;
case BINARY_FLOAT:
sqlExpr = new OracleBinaryFloatExpr(Float.parseFloat(lexer.numberString()) * -1);
lexer.nextToken();
break;
case BINARY_DOUBLE:
sqlExpr = new OracleBinaryDoubleExpr(Double.parseDouble(lexer.numberString()) * -1);
lexer.nextToken();
break;
case VARIANT:
case IDENTIFIER:
sqlExpr = expr();
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
break;
case LPAREN:
lexer.nextToken();
sqlExpr = expr();
accept(Token.RPAREN);
sqlExpr = new SQLUnaryExpr(SQLUnaryOperator.Negative, sqlExpr);
break;
default:
throw new ParserException("TODO " + lexer.token());
}
return primaryRest(sqlExpr);
case CURSOR:
lexer.nextToken();
accept(Token.LPAREN);
OracleSelect select = createSelectParser().select();
OracleCursorExpr cursorExpr = new OracleCursorExpr(select);
accept(Token.RPAREN);
sqlExpr = cursorExpr;
return primaryRest(sqlExpr);
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 KEEP:
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:
sqlExpr = new SQLIdentifierExpr(lexer.stringVal());
lexer.nextToken();
return primaryRest(sqlExpr);
default:
return super.primary();
}
}
use of com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSysdateExpr in project druid by alibaba.
the class SQLTransformUtils method transformOracleToPostgresql.
public static SQLExpr transformOracleToPostgresql(SQLMethodInvokeExpr x) {
final long nameHashCode64 = x.methodNameHashCode64();
List<SQLExpr> parameters = x.getArguments();
if (nameHashCode64 == FnvHash.Constants.SYS_GUID) {
SQLMethodInvokeExpr uuid_generate_v4 = new SQLMethodInvokeExpr("uuid_generate_v4");
uuid_generate_v4.setParent(x.getParent());
return uuid_generate_v4;
}
if (nameHashCode64 == FnvHash.Constants.TRUNC) {
if (parameters.size() == 1) {
SQLExpr param0 = parameters.get(0);
if (param0 instanceof OracleSysdateExpr || (param0 instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) param0).nameHashCode64() == FnvHash.Constants.CURRENT_TIMESTAMP)) {
SQLMethodInvokeExpr current_timestamp = new SQLMethodInvokeExpr("CURRENT_TIMESTAMP");
current_timestamp.addArgument(new SQLIntegerExpr(0));
current_timestamp.setParent(x.getParent());
return current_timestamp;
}
}
}
if (nameHashCode64 == FnvHash.Constants.CURRENT_TIMESTAMP) {
if (parameters.size() == 0 && x.getParent() instanceof SQLColumnDefinition) {
SQLDataType dataType = ((SQLColumnDefinition) x.getParent()).getDataType();
if (dataType.nameHashCode64() == FnvHash.Constants.TIMESTAMP && dataType.getArguments().size() == 1) {
x.addArgument(dataType.getArguments().get(0).clone());
} else {
x.addArgument(new SQLIntegerExpr(0));
}
return x;
}
}
if (nameHashCode64 == FnvHash.Constants.SYSTIMESTAMP) {
SQLMethodInvokeExpr xx = x.clone();
xx.setMethodName("SYSTIMESTAMP");
xx.setParent(x.getParent());
return xx;
}
if (nameHashCode64 == FnvHash.Constants.LOCALTIMESTAMP) {
SQLMethodInvokeExpr xx = x.clone();
xx.setMethodName("LOCALTIMESTAMP");
xx.setParent(x.getParent());
return xx;
}
if (nameHashCode64 == FnvHash.Constants.USERENV) {
if (x.getArguments().size() == 1) {
SQLExpr param0 = x.getArguments().get(0);
if (param0 instanceof SQLCharExpr) {
String text = ((SQLCharExpr) param0).getText();
if ("SESSIONID".equalsIgnoreCase(text)) {
SQLMethodInvokeExpr xx = new SQLMethodInvokeExpr();
xx.setMethodName("get_session_id");
xx.setParent(x.getParent());
return xx;
}
}
}
}
if (nameHashCode64 == FnvHash.Constants.USERENV) {
if (x.getArguments().size() == 1) {
SQLExpr param0 = x.getArguments().get(0);
if (param0 instanceof SQLCharExpr) {
String text = ((SQLCharExpr) param0).getText();
if ("SESSIONID".equalsIgnoreCase(text)) {
SQLMethodInvokeExpr xx = new SQLMethodInvokeExpr();
xx.setMethodName("get_session_id");
xx.setParent(x.getParent());
return xx;
}
}
}
}
if (nameHashCode64 == FnvHash.Constants.NUMTODSINTERVAL) {
if (x.getArguments().size() == 2) {
SQLExpr param0 = x.getArguments().get(0);
SQLExpr param1 = x.getArguments().get(1);
if (param0 instanceof SQLIntegerExpr && param1 instanceof SQLCharExpr) {
String text = ((SQLCharExpr) param1).getText();
if ("DAY".equalsIgnoreCase(text)) {
SQLIntervalExpr intervalExpr = new SQLIntervalExpr();
intervalExpr.setValue(new SQLCharExpr(param0.toString() + " DAYS"));
intervalExpr.setParent(x.getParent());
return intervalExpr;
}
}
}
}
return x;
}
use of com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSysdateExpr in project druid by alibaba.
the class OracleSysdateTest method test_0.
public void test_0() throws Exception {
OracleSysdateExpr sysdate = new OracleSysdateExpr();
StringBuffer buf = new StringBuffer();
SQLASTOutputVisitor v = new SQLASTOutputVisitor(buf);
sysdate.accept(v);
assertEquals("SYSDATE", buf.toString());
}
Aggregations