use of com.alibaba.druid.sql.ast.SQLDataType 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.ast.SQLDataType in project druid by alibaba.
the class SQLTransformUtils method transformOracleToAliyunAds.
public static SQLDataType transformOracleToAliyunAds(SQLDataType x) {
final String dataTypeName = x.getName().toLowerCase();
SQLDataType dataType;
if (dataTypeName.equals("varchar2") || dataTypeName.equals("varchar") || dataTypeName.equals("char") || dataTypeName.equals("nchar") || dataTypeName.equals("nvarchar") || dataTypeName.equals("nvarchar2") || dataTypeName.equals("clob") || dataTypeName.equals("nclob") || dataTypeName.equals("blob") || dataTypeName.equals("long") || dataTypeName.equals("long raw") || dataTypeName.equals("raw")) {
dataType = new SQLCharacterDataType("varchar");
} else if (dataTypeName.equals("number") || dataTypeName.equals("decimal") || dataTypeName.equals("dec") || dataTypeName.equals("numeric")) {
int scale = 0;
if (x.getArguments().size() > 1) {
scale = ((SQLIntegerExpr) x.getArguments().get(1)).getNumber().intValue();
}
if (scale == 0) {
dataType = new SQLDataTypeImpl("bigint");
} else {
dataType = new SQLDataTypeImpl("double");
}
} else if (dataTypeName.equals("date") || dataTypeName.equals("datetime") || dataTypeName.equals("timestamp")) {
dataType = new SQLDataTypeImpl("timestamp");
} else if (dataTypeName.equals("float") || dataTypeName.equals("binary_float")) {
dataType = new SQLDataTypeImpl("float");
} else if (dataTypeName.equals("double") || dataTypeName.equals("binary_double")) {
dataType = new SQLDataTypeImpl("double");
} else {
dataType = x;
}
if (dataType != x) {
dataType.setParent(x.getParent());
}
return dataType;
}
use of com.alibaba.druid.sql.ast.SQLDataType in project druid by alibaba.
the class Oracle2PG_DataTypeTest_number_int method test_oracle2pg_int_9.
public void test_oracle2pg_int_9() throws Exception {
String sql = "number(9, 0)";
SQLDataType dataType = SQLParserUtils.createExprParser(sql, JdbcConstants.ORACLE).parseDataType();
SQLDataType pgDataType = SQLTransformUtils.transformOracleToPostgresql(dataType);
assertEquals("BIGINT", pgDataType.toString());
}
use of com.alibaba.druid.sql.ast.SQLDataType in project druid by alibaba.
the class Oracle2PG_DataTypeTest_number_int method test_oracle2pg_int_2.
public void test_oracle2pg_int_2() throws Exception {
String sql = "number(2, 0)";
SQLDataType dataType = SQLParserUtils.createExprParser(sql, JdbcConstants.ORACLE).parseDataType();
SQLDataType pgDataType = SQLTransformUtils.transformOracleToPostgresql(dataType);
assertEquals("SMALLINT", pgDataType.toString());
}
use of com.alibaba.druid.sql.ast.SQLDataType in project druid by alibaba.
the class Oracle2PG_DataTypeTest_number_int method test_oracle2pg_int_12.
public void test_oracle2pg_int_12() throws Exception {
String sql = "number(12, 0)";
SQLDataType dataType = SQLParserUtils.createExprParser(sql, JdbcConstants.ORACLE).parseDataType();
SQLDataType pgDataType = SQLTransformUtils.transformOracleToPostgresql(dataType);
assertEquals("BIGINT", pgDataType.toString());
}
Aggregations