Skip to content

Commit

Permalink
ADD testIntervalSQL_OracleToPg test
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengguanLi committed Jun 27, 2024
1 parent 3b47d4f commit 3b0e5d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.alibaba.druid.sql.parser;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectStatement;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.test.TestUtils;
import junit.framework.TestCase;
import org.junit.Assert;

import java.util.List;

/**
* Created by tianzhen.wtz on 2014/12/26 0026 20:44.
Expand All @@ -29,8 +33,17 @@ public void testIntervalSQL() {
private void equal(String targetSql, String resultSql) {
PGSQLStatementParser parser = new PGSQLStatementParser(targetSql);
PGSelectStatement statement = parser.parseSelect();
Assert.assertTrue(statement.toString().equals(resultSql));
assertEquals(statement.toString(), resultSql);

}

public void testIntervalSQL_OracleToPg() {
String sql = "SELECT (SYSTIMESTAMP - order_date) DAY(9) TO SECOND from orders WHERE order_id = 2458";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
assertEquals(1, statementList.size());

String output = TestUtils.outputPg(statementList);
assertEquals("SELECT (SYSTIMESTAMP - order_date) DAY(9) TO SECOND\nFROM orders\nWHERE order_id = 2458", output);
}
}
12 changes: 12 additions & 0 deletions core/src/test/java/com/alibaba/druid/sql/test/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor;
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerOutputVisitor;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;

Expand All @@ -38,6 +39,17 @@ public static String outputOracle(List<SQLStatement> stmtList) {

return out.toString();
}

public static String outputPg(List<SQLStatement> stmtList) {
StringBuilder out = new StringBuilder();
PGOutputVisitor visitor = new PGOutputVisitor(out);

for (SQLStatement stmt : stmtList) {
stmt.accept(visitor);
}

return out.toString();
}

public static String outputSqlServer(List<SQLStatement> stmtList) {
StringBuilder out = new StringBuilder();
Expand Down

0 comments on commit 3b0e5d2

Please sign in to comment.