refactor SQLUtils

This commit is contained in:
高铁
2016-03-12 13:05:12 +08:00
parent 70f8c43a26
commit d9a6fde5d6
19 changed files with 382 additions and 197 deletions
@@ -66,29 +66,22 @@ public class SQLUtils {
private final static Log LOG = LogFactory.getLog(SQLUtils.class);
public static String toSQLString(SQLObject sqlObject, String dbType) {
if (JdbcUtils.MYSQL.equals(dbType) || //
JdbcUtils.MARIADB.equals(dbType) || //
JdbcUtils.H2.equals(dbType)) {
return toMySqlString(sqlObject);
return toSQLString(sqlObject, dbType, null);
}
public static String toSQLString(SQLObject sqlObject, String dbType, FormatOption option) {
StringBuilder out = new StringBuilder();
SQLASTOutputVisitor visitor = createOutputVisitor(out, dbType);
if (option == null) {
option = DEFAULT_FORMAT_OPTION;
}
visitor.setUppCase(option.isUppCase());
sqlObject.accept(visitor);
if (JdbcUtils.ORACLE.equals(dbType) || JdbcUtils.ALI_ORACLE.equals(dbType)) {
return toOracleString(sqlObject);
}
if (JdbcUtils.POSTGRESQL.equals(dbType)) {
return toPGString(sqlObject);
}
if (JdbcUtils.DB2.equals(dbType)) {
return toDB2String(sqlObject);
}
if (JdbcUtils.ODPS.equals(dbType)) {
return toOdpsString(sqlObject);
}
return toSQLServerString(sqlObject);
String sql = out.toString();
return sql;
}
public static String toSQLString(SQLObject sqlObject) {
@@ -100,11 +93,11 @@ public class SQLUtils {
}
public static String toOdpsString(SQLObject sqlObject) {
StringBuilder out = new StringBuilder();
sqlObject.accept(new OdpsOutputVisitor(out));
String sql = out.toString();
return sql;
return toOdpsString(sqlObject, null);
}
public static String toOdpsString(SQLObject sqlObject, FormatOption option) {
return toSQLString(sqlObject, JdbcConstants.ODPS, option);
}
public static String toMySqlString(SQLObject sqlObject) {
@@ -112,18 +105,7 @@ public class SQLUtils {
}
public static String toMySqlString(SQLObject sqlObject, FormatOption option) {
StringBuilder out = new StringBuilder();
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
if (option == null) {
option = DEFAULT_FORMAT_OPTION;
}
visitor.setUppCase(option.isUppCase());
sqlObject.accept(visitor);
String sql = out.toString();
return sql;
return toSQLString(sqlObject, JdbcConstants.MYSQL, option);
}
public static SQLExpr toMySqlExpr(String sql) {
@@ -163,19 +145,7 @@ public class SQLUtils {
}
public static String toOracleString(SQLObject sqlObject, FormatOption option) {
StringBuilder out = new StringBuilder();
OracleOutputVisitor visitor = new OracleOutputVisitor(out, false);
if (option == null) {
option = DEFAULT_FORMAT_OPTION;
}
visitor.setUppCase(option.isUppCase());
sqlObject.accept(visitor);
String sql = out.toString();
return sql;
return toSQLString(sqlObject, JdbcConstants.ORACLE, option);
}
public static String toPGString(SQLObject sqlObject) {
@@ -183,35 +153,23 @@ public class SQLUtils {
}
public static String toPGString(SQLObject sqlObject, FormatOption option) {
StringBuilder out = new StringBuilder();
PGOutputVisitor visitor = new PGOutputVisitor(out);
if (option == null) {
option = DEFAULT_FORMAT_OPTION;
}
visitor.setUppCase(option.isUppCase());
sqlObject.accept(visitor);
String sql = out.toString();
return sql;
return toSQLString(sqlObject, JdbcConstants.POSTGRESQL, option);
}
public static String toDB2String(SQLObject sqlObject) {
StringBuilder out = new StringBuilder();
sqlObject.accept(new DB2OutputVisitor(out));
String sql = out.toString();
return sql;
return toDB2String(sqlObject, null);
}
public static String toDB2String(SQLObject sqlObject, FormatOption option) {
return toSQLString(sqlObject, JdbcConstants.DB2, option);
}
public static String toSQLServerString(SQLObject sqlObject) {
StringBuilder out = new StringBuilder();
sqlObject.accept(new SQLServerOutputVisitor(out));
String sql = out.toString();
return sql;
return toSQLServerString(sqlObject, null);
}
public static String toSQLServerString(SQLObject sqlObject, FormatOption option) {
return toSQLString(sqlObject, JdbcConstants.SQL_SERVER, option);
}
public static String formatPGSql(String sql, FormatOption option) {
@@ -298,7 +256,11 @@ public class SQLUtils {
}
public static String toSQLString(List<SQLStatement> statementList, String dbType) {
return toSQLString(statementList, dbType, null);
return toSQLString(statementList, dbType, (List<Object>) null);
}
public static String toSQLString(List<SQLStatement> statementList, String dbType, FormatOption option) {
return toSQLString(statementList, dbType, null, option);
}
public static String toSQLString(List<SQLStatement> statementList, String dbType, List<Object> parameters) {
@@ -374,11 +336,16 @@ public class SQLUtils {
return out.toString();
}
public static SQLASTOutputVisitor createOutputVisitor(Appendable out, String dbType) {
return createFormatOutputVisitor(out, null, dbType);
}
public static SQLASTOutputVisitor createFormatOutputVisitor(Appendable out, List<SQLStatement> statementList,
public static SQLASTOutputVisitor createFormatOutputVisitor(Appendable out, //
List<SQLStatement> statementList, //
String dbType) {
if (JdbcUtils.ORACLE.equals(dbType) || JdbcUtils.ALI_ORACLE.equals(dbType)) {
if (statementList.size() == 1) {
if (statementList == null || statementList.size() == 1) {
return new OracleOutputVisitor(out, false);
} else {
return new OracleOutputVisitor(out, true);
@@ -16,5 +16,13 @@
package com.alibaba.druid.sql.ast;
public enum SQLOrderingSpecification {
ASC, DESC
ASC("ASC"), DESC("DESC");
public final String name;
public final String name_lcase;
private SQLOrderingSpecification(String name){
this.name = name;
this.name_lcase = name.toLowerCase();
}
}
@@ -34,6 +34,7 @@ import com.alibaba.druid.sql.ast.SQLKeep;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
import com.alibaba.druid.sql.ast.SQLOver;
import com.alibaba.druid.sql.ast.SQLParameter;
import com.alibaba.druid.sql.ast.SQLPartition;
@@ -801,7 +802,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
x.getExpr().accept(this);
if (x.getType() != null) {
print(' ');
print0(x.getType().name().toUpperCase());
SQLOrderingSpecification type = x.getType();
print0(ucase ? type.name : type.name_lcase);
}
if (x.getCollate() != null) {
@@ -1179,7 +1181,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
public boolean visit(SQLUnionQuery x) {
x.getLeft().accept(this);
println();
print0(x.getOperator().name);
print0(ucase ? x.getOperator().name : x.getOperator().name_lcase);
println();
boolean needParen = false;
@@ -31,8 +31,10 @@ public class MySqlAlterTableTest20 extends TestCase {
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("ALTER TABLE score1"
+ "\n\tADD COLUMN (test3 int(11), test4 int(11))", output);
+ "\n\tADD COLUMN (test3 int(11), test4 int(11))", SQLUtils.toMySqlString(stmt));
Assert.assertEquals("alter table score1"
+ "\n\tadd column (test3 int(11), test4 int(11))", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -42,15 +42,28 @@ public class MySqlCreateTableTest69 extends MysqlTest {
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE TABLE t1 ("
+ "\n\ts1 INT, "
+ "\n\ts2 INT AS (EXP(s1)) SORTED"
+ "\n)"
+ "\nPARTITION BY LIST (s2)"
+ "\n("
+ "\n\tPARTITION p1 VALUES IN (1)"
+ "\n)", output);
{
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE TABLE t1 ("
+ "\n\ts1 INT, "
+ "\n\ts2 INT AS (EXP(s1)) SORTED"
+ "\n)"
+ "\nPARTITION BY LIST (s2)"
+ "\n("
+ "\n\tPARTITION p1 VALUES IN (1)"
+ "\n)", output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("create table t1 ("
+ "\n\ts1 INT, "
+ "\n\ts2 INT as (EXP(s1)) sorted"
+ "\n)"
+ "\npartition by list (s2)"
+ "\n("
+ "\n\tpartition p1 values in (1)"
+ "\n)", output);
}
}
}
@@ -54,5 +54,10 @@ public class MySqlInsertTest_8 extends MysqlTest {
+ "\nSELECT *" //
+ "\nFROM bb.table3", //
SQLUtils.toMySqlString(insertStmt));
Assert.assertEquals("insert into dd.table1 (d, e)" //
+ "\nselect *" //
+ "\nfrom bb.table3", //
SQLUtils.toMySqlString(insertStmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -51,13 +51,24 @@ public class MySqlSelectTest_24 extends MysqlTest {
Assert.assertEquals(3, visitor.getConditions().size());
Assert.assertEquals(0, visitor.getOrderByColumns().size());
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("SELECT *"
+ "\nFROM company"
+ "\nWHERE id = 1"
+ "\n\tAND NOT name = 'e'"
+ "\n\tAND addr = 'a'", //
output);
{
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("SELECT *"
+ "\nFROM company"
+ "\nWHERE id = 1"
+ "\n\tAND NOT name = 'e'"
+ "\n\tAND addr = 'a'", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select *"
+ "\nfrom company"
+ "\nwhere id = 1"
+ "\n\tand not name = 'e'"
+ "\n\tand addr = 'a'", //
output);
}
}
@@ -56,11 +56,21 @@ public class MySqlUpdateTest_4 extends MysqlTest {
Assert.assertTrue(visitor.getColumns().contains(new Column("darenai_stat_url", "user")));
Assert.assertTrue(visitor.getColumns().contains(new Column("darenai_stat_url", "ip")));
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("INSERT INTO darenai_stat_url (user, ip, reffer, url, shopnick"
+ "\n\t, time)"
+ "\nVALUES ('nologin', '58.101.223.183', 'http://item.taobao.com/item.htm?spm=a230r.1.14.419.KDVewC&amp;id=17052767689', '/d/jingpinhui?spm=2013.1.0.0.zr4nLz&amp;ac=shop&amp;imageid=1019937265&amp;s=1259538&amp;s=1259538', '零利润3232'"
+ "\n\t, NOW())", //
output);
{
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("INSERT INTO darenai_stat_url (user, ip, reffer, url, shopnick"
+ "\n\t, time)"
+ "\nVALUES ('nologin', '58.101.223.183', 'http://item.taobao.com/item.htm?spm=a230r.1.14.419.KDVewC&amp;id=17052767689', '/d/jingpinhui?spm=2013.1.0.0.zr4nLz&amp;ac=shop&amp;imageid=1019937265&amp;s=1259538&amp;s=1259538', '零利润3232'"
+ "\n\t, NOW())", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("insert into darenai_stat_url (user, ip, reffer, url, shopnick"
+ "\n\t, time)"
+ "\nvalues ('nologin', '58.101.223.183', 'http://item.taobao.com/item.htm?spm=a230r.1.14.419.KDVewC&amp;id=17052767689', '/d/jingpinhui?spm=2013.1.0.0.zr4nLz&amp;ac=shop&amp;imageid=1019937265&amp;s=1259538&amp;s=1259538', '零利润3232'"
+ "\n\t, NOW())", //
output);
}
}
}
@@ -34,11 +34,16 @@ public class OceanbaseAlterTableDropPartitionTest extends MysqlTest {
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement stmt = stmtList.get(0);
String result = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("ALTER TABLE tnrange"
+ "\n\tDROP PARTITION p1",
result);
print(stmtList);
{
String result = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("ALTER TABLE tnrange" + //
"\n\tDROP PARTITION p1", result);
}
{
String result = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("alter table tnrange" + //
"\n\tdrop partition p1", result);
}
Assert.assertEquals(1, stmtList.size());
@@ -42,33 +42,61 @@ public class OceanbaseCreateTableTest_subPartition2 extends MysqlTest {
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement stmt = stmtList.get(0);
String result = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE TABLE ts ("
+ "\n\tid INT, "
+ "\n\tpurchased DATE"
+ "\n)"
+ "\nPARTITION BY RANGE (YEAR(purchased))"
+ "\nSUBPARTITION BY HASH (TO_DAYS(purchased))"
+ "\n("
+ "\n\tPARTITION p0 VALUES LESS THAN (1990)"
+ "\n\t("
+ "\n\t\tSUBPARTITION s0,"
+ "\n\t\tSUBPARTITION s1"
+ "\n\t),"
+ "\n\tPARTITION p1 VALUES LESS THAN (2000)"
+ "\n\t("
+ "\n\t\tSUBPARTITION s2,"
+ "\n\t\tSUBPARTITION s3"
+ "\n\t),"
+ "\n\tPARTITION p2 VALUES LESS THAN MAXVALUE"
+ "\n\t("
+ "\n\t\tSUBPARTITION s4,"
+ "\n\t\tSUBPARTITION s5"
+ "\n\t)"
+ "\n)",
result);
print(stmtList);
{
String result = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE TABLE ts (" + //
"\n\tid INT, " + //
"\n\tpurchased DATE" + //
"\n)" + //
"\nPARTITION BY RANGE (YEAR(purchased))" + //
"\nSUBPARTITION BY HASH (TO_DAYS(purchased))" + //
"\n(" + //
"\n\tPARTITION p0 VALUES LESS THAN (1990)" + //
"\n\t(" + //
"\n\t\tSUBPARTITION s0," + //
"\n\t\tSUBPARTITION s1" + //
"\n\t)," + //
"\n\tPARTITION p1 VALUES LESS THAN (2000)" + //
"\n\t(" + //
"\n\t\tSUBPARTITION s2," + //
"\n\t\tSUBPARTITION s3" + //
"\n\t)," + //
"\n\tPARTITION p2 VALUES LESS THAN MAXVALUE" + //
"\n\t(" + //
"\n\t\tSUBPARTITION s4," + //
"\n\t\tSUBPARTITION s5" + //
"\n\t)" + //
"\n)",
result);
}
{
String result = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("create table ts (" + //
"\n\tid INT, " + //
"\n\tpurchased DATE" + //
"\n)" + //
"\npartition by range (YEAR(purchased))" + //
"\nsubpartition by hash (TO_DAYS(purchased))" + //
"\n(" + //
"\n\tpartition p0 values less than (1990)" + //
"\n\t(" + //
"\n\t\tsubpartition s0," + //
"\n\t\tsubpartition s1" + //
"\n\t)," + //
"\n\tpartition p1 values less than (2000)" + //
"\n\t(" + //
"\n\t\tsubpartition s2," + //
"\n\t\tsubpartition s3" + //
"\n\t)," + //
"\n\tpartition p2 values less than maxvalue" + //
"\n\t(" + //
"\n\t\tsubpartition s4," + //
"\n\t\tsubpartition s5" + //
"\n\t)" + //
"\n)",
result);
System.out.println(result);
}
Assert.assertEquals(1, stmtList.size());
@@ -34,11 +34,16 @@ public class OceanbaseSelectPartitionTest extends MysqlTest {
List<SQLStatement> stmtList = parser.parseStatementList();
SQLStatement stmt = stmtList.get(0);
String result = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("SELECT *"
+ "\nFROM tnhash PARTITION (p0)",
result);
print(stmtList);
{
String result = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("SELECT *" + //
"\nFROM tnhash PARTITION (p0)", result);
}
{
String result = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select *" + //
"\nfrom tnhash partition (p0)", result);
}
Assert.assertEquals(1, stmtList.size());
@@ -12,11 +12,15 @@ public class OdpsAlterTableDropPartitionTest extends TestCase {
String sql = "alter table sale_detail drop if not exists partition (sale_date='201312', region='hangzhou');";
Assert.assertEquals("ALTER TABLE sale_detail" //
+ "\n\tDROP IF NOT EXISTS PARTITION (sale_date = '201312', region = 'hangzhou');", SQLUtils.formatOdps(sql));
Assert.assertEquals("alter table sale_detail" //
+ "\n\tdrop if not exists partition (sale_date = '201312', region = 'hangzhou');", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
public void test_purge() throws Exception {
String sql = "alter table my_log drop partition (ds='20150618') purge;";
Assert.assertEquals("ALTER TABLE my_log"
+ "\n\tDROP PARTITION (ds = '20150618') PURGE;", SQLUtils.formatOdps(sql));
Assert.assertEquals("alter table my_log"
+ "\n\tdrop partition (ds = '20150618') purge;", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -22,6 +22,16 @@ public class OdpsFormatCommentTest8 extends TestCase {
+ "\n"
+ "\nSELECT *"
+ "\nFROM table3;--comment2 goes here", SQLUtils.formatOdps(sql));
Assert.assertEquals("select *"
+ "\nfrom table1;"
+ "\n"
+ "\n--comment1 goes here"
+ "\nselect *"
+ "\nfrom table2;"
+ "\n"
+ "\nselect *"
+ "\nfrom table3;--comment2 goes here", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
@@ -42,6 +42,9 @@ public class OdpsSelectTest extends TestCase {
Assert.assertEquals("SELECT region" //
+ "\nFROM sale_detail" //
+ "\nDISTRIBUTE BY region SORT BY f1 ASC;", SQLUtils.formatOdps(sql));
Assert.assertEquals("select region" //
+ "\nfrom sale_detail" //
+ "\ndistribute by region sort by f1 asc;", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -20,6 +20,7 @@ import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
@@ -29,45 +30,111 @@ public class OracleSelectTest38 extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select * from " +
"(with vw_kreis_statics_t as" +
" (select substr(xzqh,1,6) xzqh,swrslx,sum(swrs_count) acd_totle from" +
" (select xzqh,sglx,case when (swrs7 < 3) then '1'" +
" when (swrs7 < 5) then '2' when (swrs7 <= 9) then '3' else '4' end swrslx,1 swrs_count" +
" from acduser.vw_acd_info where sglx='1' " +
" " +
" and sgfssj >= ?" +
" " +
" " +
" )" +
" group by substr(xzqh,1,6),swrslx)" +
"" +
" select e.\"XZQH\",e.\"LESS3\",e.\"F3TO5\",e.\"F5TO9\",e.\"MORE9\",kreis_code, kreis_name,px1,py1,px2,py2 from" +
" ( select" +
" xzqh," +
" nvl(max(decode(swrslx,'1',acd_totle)),0) less3," +
" nvl(max(decode(swrslx,'2',acd_totle)),0) f3to5," +
" nvl(max(decode(swrslx,'3',acd_totle)),0) f5to9," +
" nvl(max(decode(swrslx,'4',acd_totle)),0) more9" +
" from( select * from acduser.vw_kreis_statics_t) group by xzqh " +
" ) e" +
"" +
" left join" +
" acduser.vw_sc_kreis_code_lv2 f on e.xzqh = f.short_kreis_code) " +
" where kreis_code in" +
"(select * from " +
" (select tbek_code from acduser.vw_kreis_code start with tbek_code = ? connect by prior tbek_pk=tbek_parent ) " +
"where tbek_code != ?)"; //
"select * from " + "(with vw_kreis_statics_t as"
+ " (select substr(xzqh,1,6) xzqh,swrslx,sum(swrs_count) acd_totle from"
+ " (select xzqh,sglx,case when (swrs7 < 3) then '1'"
+ " when (swrs7 < 5) then '2' when (swrs7 <= 9) then '3' else '4' end swrslx,1 swrs_count"
+ " from acduser.vw_acd_info where sglx='1' " + " "
+ " and sgfssj >= ?" + " " + " "
+ " )" + " group by substr(xzqh,1,6),swrslx)" + ""
+ " select e.\"XZQH\",e.\"LESS3\",e.\"F3TO5\",e.\"F5TO9\",e.\"MORE9\",kreis_code, kreis_name,px1,py1,px2,py2 from"
+ " ( select" + " xzqh," + " nvl(max(decode(swrslx,'1',acd_totle)),0) less3,"
+ " nvl(max(decode(swrslx,'2',acd_totle)),0) f3to5,"
+ " nvl(max(decode(swrslx,'3',acd_totle)),0) f5to9,"
+ " nvl(max(decode(swrslx,'4',acd_totle)),0) more9"
+ " from( select * from acduser.vw_kreis_statics_t) group by xzqh " + " ) e" + ""
+ " left join" + " acduser.vw_sc_kreis_code_lv2 f on e.xzqh = f.short_kreis_code) "
+ " where kreis_code in" + "(select * from "
+ " (select tbek_code from acduser.vw_kreis_code start with tbek_code = ? connect by prior tbek_pk=tbek_parent ) "
+ "where tbek_code != ?)"; //
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
SQLStatement stmt = statementList.get(0);
{
String result = SQLUtils.toOracleString(stmt);
Assert.assertEquals("SELECT *"
+ "\nFROM ("
+ "\n\tWITH"
+ "\n\t\tvw_kreis_statics_t"
+ "\n\t\tAS"
+ "\n\t\t("
+ "\n\t\t\tSELECT substr(xzqh, 1, 6) AS xzqh, swrslx, SUM(swrs_count) AS acd_totle"
+ "\n\t\t\tFROM ("
+ "\n\t\t\t\tSELECT xzqh, sglx, CASE WHEN swrs7 < 3 THEN '1' WHEN swrs7 < 5 THEN '2' WHEN swrs7 <= 9 THEN '3' ELSE '4' END AS swrslx, 1 AS swrs_count"
+ "\n\t\t\t\tFROM acduser.vw_acd_info"
+ "\n\t\t\t\tWHERE sglx = '1'"
+ "\n\t\t\t\t\tAND sgfssj >= ?"
+ "\n\t\t\t)"
+ "\n\t\t\tGROUP BY substr(xzqh, 1, 6), swrslx"
+ "\n\t\t)"
+ "\n\tSELECT e.\"XZQH\", e.\"LESS3\", e.\"F3TO5\", e.\"F5TO9\", e.\"MORE9\""
+ "\n\t\t, kreis_code, kreis_name, px1, py1, px2"
+ "\n\t\t, py2"
+ "\n\tFROM ("
+ "\n\t\tSELECT xzqh, nvl(MAX(decode(swrslx, '1', acd_totle)), 0) AS less3, nvl(MAX(decode(swrslx, '2', acd_totle)), 0) AS f3to5, nvl(MAX(decode(swrslx, '3', acd_totle)), 0) AS f5to9, nvl(MAX(decode(swrslx, '4', acd_totle)), 0) AS more9"
+ "\n\t\tFROM ("
+ "\n\t\t\tSELECT *"
+ "\n\t\t\tFROM acduser.vw_kreis_statics_t"
+ "\n\t\t)"
+ "\n\t\tGROUP BY xzqh"
+ "\n\t) e"
+ "\n\t\tLEFT JOIN acduser.vw_sc_kreis_code_lv2 f ON e.xzqh = f.short_kreis_code "
+ "\n)"
+ "\nWHERE kreis_code IN (SELECT *"
+ "\n\tFROM ("
+ "\n\t\tSELECT tbek_code"
+ "\n\t\tFROM acduser.vw_kreis_code"
+ "\n\t\tSTART WITH tbek_code = ?"
+ "\n\t\tCONNECT BY PRIOR tbek_pk = tbek_parent"
+ "\n\t)"
+ "\n\tWHERE tbek_code != ?)", result);
}
{
String result = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("select *"
+ "\nfrom ("
+ "\n\twith"
+ "\n\t\tvw_kreis_statics_t"
+ "\n\t\tas"
+ "\n\t\t("
+ "\n\t\t\tselect substr(xzqh, 1, 6) as xzqh, swrslx, sum(swrs_count) as acd_totle"
+ "\n\t\t\tfrom ("
+ "\n\t\t\t\tselect xzqh, sglx, case when swrs7 < 3 then '1' when swrs7 < 5 then '2' when swrs7 <= 9 then '3' else '4' end as swrslx, 1 as swrs_count"
+ "\n\t\t\t\tfrom acduser.vw_acd_info"
+ "\n\t\t\t\twhere sglx = '1'"
+ "\n\t\t\t\t\tand sgfssj >= ?"
+ "\n\t\t\t)"
+ "\n\t\t\tgroup by substr(xzqh, 1, 6), swrslx"
+ "\n\t\t)"
+ "\n\tselect e.\"XZQH\", e.\"LESS3\", e.\"F3TO5\", e.\"F5TO9\", e.\"MORE9\""
+ "\n\t\t, kreis_code, kreis_name, px1, py1, px2"
+ "\n\t\t, py2"
+ "\n\tfrom ("
+ "\n\t\tselect xzqh, nvl(max(decode(swrslx, '1', acd_totle)), 0) as less3, nvl(max(decode(swrslx, '2', acd_totle)), 0) as f3to5, nvl(max(decode(swrslx, '3', acd_totle)), 0) as f5to9, nvl(max(decode(swrslx, '4', acd_totle)), 0) as more9"
+ "\n\t\tfrom ("
+ "\n\t\t\tselect *"
+ "\n\t\t\tfrom acduser.vw_kreis_statics_t"
+ "\n\t\t)"
+ "\n\t\tgroup by xzqh"
+ "\n\t) e"
+ "\n\t\tleft join acduser.vw_sc_kreis_code_lv2 f on e.xzqh = f.short_kreis_code "
+ "\n)"
+ "\nwhere kreis_code in (select *"
+ "\n\tfrom ("
+ "\n\t\tselect tbek_code"
+ "\n\t\tfrom acduser.vw_kreis_code"
+ "\n\t\tstart with tbek_code = ?"
+ "\n\t\tconnect by prior tbek_pk = tbek_parent"
+ "\n\t)"
+ "\n\twhere tbek_code != ?)", result);
}
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
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;
@@ -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());
@@ -43,10 +43,16 @@ public class SQLServerAlterTableTest_11 extends TestCase {
System.out.println("coditions : " + visitor.getConditions());
System.out.println("orderBy : " + visitor.getOrderByColumns());
String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
Assert.assertEquals("ALTER TABLE dbo.doc_exd" //
+ "\n\tWITH NOCHECK ADD CONSTRAINT exd_check CHECK (column_a > 1)", output);
{
String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
Assert.assertEquals("ALTER TABLE dbo.doc_exd" //
+ "\n\tWITH NOCHECK ADD CONSTRAINT exd_check CHECK (column_a > 1)", output);
}
{
String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("alter table dbo.doc_exd" //
+ "\n\twith nocheck add constraint exd_check check (column_a > 1)", output);
}
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(1, visitor.getColumns().size());
}
@@ -15,31 +15,37 @@
*/
package com.alibaba.druid.bvt.sql.sqlserver;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
import com.alibaba.druid.sql.test.TestUtils;
import junit.framework.TestCase;
public class SQLServerSelectTest21 extends TestCase {
public void test_simple() throws Exception {
String sql = "SELECT First_Name + ' ' + Last Name FROM Employees ORDER BY First_Name OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;"; //
String expect = "SELECT First_Name + ' ' + Last AS Name" //
+ "\nFROM Employees" //
+ "\nORDER BY First_Name" //
+ "\nOFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
String text = TestUtils.outputSqlServer(stmt);
{
String text = SQLUtils.toSQLServerString(stmt);
Assert.assertEquals(expect, text);
Assert.assertEquals("SELECT First_Name + ' ' + Last AS Name" //
+ "\nFROM Employees" //
+ "\nORDER BY First_Name" //
+ "\nOFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY", text);
}
{
String text = SQLUtils.toSQLServerString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
System.out.println(text);
Assert.assertEquals("select First_Name + ' ' + Last as Name" //
+ "\nfrom Employees" //
+ "\norder by First_Name" //
+ "\noffset 10 rows fetch next 5 rows only", text);
}
}
}