Search in sources :

Example 1 with SQLDataType

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;
}
Also used : SQLIntervalExpr(com.alibaba.druid.sql.ast.expr.SQLIntervalExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) OracleSysdateExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSysdateExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 with SQLDataType

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;
}
Also used : SQLDataType(com.alibaba.druid.sql.ast.SQLDataType) SQLCharacterDataType(com.alibaba.druid.sql.ast.statement.SQLCharacterDataType) SQLDataTypeImpl(com.alibaba.druid.sql.ast.SQLDataTypeImpl)

Example 3 with SQLDataType

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());
}
Also used : SQLDataType(com.alibaba.druid.sql.ast.SQLDataType)

Example 4 with SQLDataType

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());
}
Also used : SQLDataType(com.alibaba.druid.sql.ast.SQLDataType)

Example 5 with SQLDataType

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());
}
Also used : SQLDataType(com.alibaba.druid.sql.ast.SQLDataType)

Aggregations

SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)93 SQLDataTypeImpl (com.alibaba.druid.sql.ast.SQLDataTypeImpl)7 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)7 SQLCharacterDataType (com.alibaba.druid.sql.ast.statement.SQLCharacterDataType)5 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)4 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)4 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)3 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLIntervalExpr (com.alibaba.druid.sql.ast.expr.SQLIntervalExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)1 SQLAlterTableAlterColumn (com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn)1 SQLColumnConstraint (com.alibaba.druid.sql.ast.statement.SQLColumnConstraint)1 SQLColumnPrimaryKey (com.alibaba.druid.sql.ast.statement.SQLColumnPrimaryKey)1 SQLColumnUniqueKey (com.alibaba.druid.sql.ast.statement.SQLColumnUniqueKey)1