Search in sources :

Example 1 with SQLIntervalExpr

use of com.alibaba.druid.sql.ast.expr.SQLIntervalExpr 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 SQLIntervalExpr

use of com.alibaba.druid.sql.ast.expr.SQLIntervalExpr in project druid by alibaba.

the class EqualTest_interval_mysql method test_exits.

public void test_exits() throws Exception {
    String sql = "INTERVAL 3 YEAR";
    String sql_c = "INTERVAL 3 MONTH";
    SQLIntervalExpr exprA, exprB, exprC;
    {
        SQLExprParser parser = new MySqlExprParser(sql);
        exprA = (SQLIntervalExpr) parser.expr();
    }
    {
        SQLExprParser parser = new MySqlExprParser(sql);
        exprB = (SQLIntervalExpr) parser.expr();
    }
    {
        SQLExprParser parser = new MySqlExprParser(sql_c);
        exprC = (SQLIntervalExpr) parser.expr();
    }
    Assert.assertEquals(exprA, exprB);
    Assert.assertNotEquals(exprA, exprC);
    Assert.assertTrue(exprA.equals(exprA));
    Assert.assertFalse(exprA.equals(new Object()));
    Assert.assertEquals(exprA.hashCode(), exprB.hashCode());
    Assert.assertEquals(new SQLIntervalExpr(), new SQLIntervalExpr());
    Assert.assertEquals(new SQLIntervalExpr().hashCode(), new SQLIntervalExpr().hashCode());
}
Also used : SQLIntervalExpr(com.alibaba.druid.sql.ast.expr.SQLIntervalExpr) MySqlExprParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser) SQLExprParser(com.alibaba.druid.sql.parser.SQLExprParser)

Example 3 with SQLIntervalExpr

use of com.alibaba.druid.sql.ast.expr.SQLIntervalExpr 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);
}
Also used : MySqlASTVisitorAdapter(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter) SQLBinaryExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryExpr) UserSpecification(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification) TableSpaceOption(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) MySqlUserName(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName) SQLIntervalExpr(com.alibaba.druid.sql.ast.expr.SQLIntervalExpr) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) MySqlCharExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr) MySqlOutFileExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr)

Aggregations

SQLIntervalExpr (com.alibaba.druid.sql.ast.expr.SQLIntervalExpr)3 SQLDataType (com.alibaba.druid.sql.ast.SQLDataType)1 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)1 SQLBinaryExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryExpr)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)1 MySqlCharExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr)1 MySqlOutFileExpr (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr)1 MySqlUserName (com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName)1 TableSpaceOption (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption)1 UserSpecification (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification)1 MySqlExprParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser)1 MySqlASTVisitorAdapter (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter)1 OracleSysdateExpr (com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleSysdateExpr)1 SQLExprParser (com.alibaba.druid.sql.parser.SQLExprParser)1