mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor SQLUtils
This commit is contained in:
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.PGTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
|
||||
@@ -36,13 +37,42 @@ public class PGSelectTest18 extends PGTest {
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
{
|
||||
String result = SQLUtils.toPGString(stmt);
|
||||
Assert.assertEquals("WITH RECURSIVE "
|
||||
+ "\n\tt (n)"
|
||||
+ "\n\tAS"
|
||||
+ "\n\t("
|
||||
+ "\n\t\tSELECT 1"
|
||||
+ "\n\t\tUNION ALL"
|
||||
+ "\n\t\tSELECT n + 1"
|
||||
+ "\n\t\tFROM t"
|
||||
+ "\n\t)"
|
||||
+ "\nSELECT n"
|
||||
+ "\nFROM t"
|
||||
+ "\nLIMIT 100", result);
|
||||
}
|
||||
{
|
||||
String result = SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
Assert.assertEquals("with recursive "
|
||||
+ "\n\tt (n)"
|
||||
+ "\n\tas"
|
||||
+ "\n\t("
|
||||
+ "\n\t\tselect 1"
|
||||
+ "\n\t\tunion all"
|
||||
+ "\n\t\tselect n + 1"
|
||||
+ "\n\t\tfrom t"
|
||||
+ "\n\t)"
|
||||
+ "\nselect n"
|
||||
+ "\nfrom t"
|
||||
+ "\nlimit 100", result);
|
||||
}
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.PGTest;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
|
||||
@@ -31,16 +32,18 @@ public class PGSelectTest24 extends PGTest {
|
||||
|
||||
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
Assert.assertEquals("SELECT wm_concat(article_id) OVER ()"
|
||||
+ "\nFROM t_nds_web_article", output(statementList));
|
||||
+ "\nFROM t_nds_web_article", SQLUtils.toPGString(stmt));
|
||||
|
||||
Assert.assertEquals("select wm_concat(article_id) over ()"
|
||||
+ "\nfrom t_nds_web_article", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
Reference in New Issue
Block a user