improved sql format output

This commit is contained in:
shaojin.wensj
2015-07-11 14:27:29 +08:00
parent 60b7139911
commit b36374f7bf
26 changed files with 612 additions and 35 deletions
@@ -29,6 +29,7 @@ public class MySqlFormatTest extends TestCase {
"\ta INT, \n" + //
"\tb CHAR(20), \n" + //
"\tINDEX(a)\n" + //
")", SQLUtils.format(text, JdbcUtils.MYSQL));
")" //
+ "\n;", SQLUtils.format(text, JdbcUtils.MYSQL));
}
}
@@ -0,0 +1,34 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest extends TestCase {
public void test_column_comment() throws Exception {
String sql = "select f1 -- aaa"
+ "\n from t1";
Assert.assertEquals("SELECT f1 -- aaa" //
+ "\nFROM t1", SQLUtils.formatOdps(sql));
}
public void test_column_2_comment() throws Exception {
String sql = "select f1 -- aaa"
+ "\n, f2 -- bbb"
+ "\n from t1";
Assert.assertEquals("SELECT f1, -- aaa" //
+ "\n\tf2 -- bbb" //
+ "\nFROM t1", SQLUtils.formatOdps(sql));
}
public void test_column_2_multi_comment() throws Exception {
String sql = "select f1 /*aa*/"
+ "\n, f2 -- bbb"
+ "\n from t1";
Assert.assertEquals("SELECT f1, /*aa*/" //
+ "\n\tf2 -- bbb" //
+ "\nFROM t1", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,27 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest2 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "--[Subject -]" //
+ "\n--[Author -高铁/035139]"//
+ "\n--[Created -2015-06-10 13:19:18]"//
+ "\n--[Update ]"//
+ "\nset odps.sql.mapper.split.size=2048;"//
+ "\nselect * from dual;";
Assert.assertEquals("--[Subject -]"
+ "\n--[Author -高铁/035139]"
+ "\n--[Created -2015-06-10 13:19:18]"
+ "\n--[Update ]"
+ "\nSET odps.sql.mapper.split.size = 2048;"
+ "\nSELECT *"
+ "\nFROM dual"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,30 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest3 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "-- 使用服务模式" //
+ "\nset odps.service.mode=all;" //
+ "\n-- 使用新引擎"//
+ "\nset odps.nvm.enabled=true;"//
+ "\nselect f1 -- aa"//
+ "\nfrom t0;"//
+ "\nselect f2 -- aa"//
+ "\nfrom t1;";//
Assert.assertEquals("-- 使用服务模式"//
+ "\nSET odps.service.mode = all;"//
+ "\n-- 使用新引擎"//
+ "\nSET odps.nvm.enabled = true;"//
+ "\nSELECT f1 -- aa"//
+ "\nFROM t0;"//
+ "\nSELECT f2 -- aa"//
+ "\nFROM t1"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,40 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest4 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "from (select * from xxx) a"
+ "\n -- comment_0" //
+ "\ninsert overwrite table b partition(ds='20150711')" //
+ "\nselect f0, f1, f2" //
+ "\nwhere name rlike 'xxxx'"
+ "\n -- comment_1" //
+ "\ninsert overwrite table c partition(ds='20150711')" //
+ "\nselect f0, f1, f2" //
+ "\nwhere name rlike 'kk';"
;//
Assert.assertEquals("FROM (" //
+ "\n\tSELECT *"//
+ "\n\tFROM xxx"//
+ "\n) a" //
+ "\n-- comment_0"//
+ "\nINSERT OVERWRITE TABLE b PARTITION (ds='20150711')"//
+ "\nSELECT f0, "//
+ "\n\tf1, "//
+ "\n\tf2"//
+ "\nWHERE name RLIKE 'xxxx'" //
+ "\n-- comment_1"//
+ "\nINSERT OVERWRITE TABLE c PARTITION (ds='20150711')"//
+ "\nSELECT f0, "//
+ "\n\tf1, "//
+ "\n\tf2"//
+ "\nWHERE name RLIKE 'kk'"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest5 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "select *" //
+ "\nfrom t -- xxxx"//
+ "\nwhere id > 0;"
;//
Assert.assertEquals("SELECT *" //
+ "\nFROM t -- xxxx" //
+ "\nWHERE id > 0"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_column_comment_as() throws Exception {
String sql = "select *" //
+ "\nfrom xxxx a-- xxxx"//
+ "\nwhere id > 0;"
;//
Assert.assertEquals("SELECT *" //
+ "\nFROM xxxx a -- xxxx" //
+ "\nWHERE id > 0"
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_column_comment_subquery() throws Exception {
String sql = "select *" //
+ "\nfrom (" //
+ "\n-- comment_xxx" //
+ "\nselect * from t" //
+ "\n) a;"
;//
Assert.assertEquals("SELECT *" //
+ "\nFROM (" //
+ "\n\t-- comment_xxx" //
+ "\n\tSELECT *" //
+ "\n\tFROM t" //
+ "\n) a"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,25 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest6 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "select *" //
+ "\nfrom t "//
+ "\nwhere status = '20' -- comment xxx"
+ "\nand flag & 127 > 0 -- comment kkkkk"
+ "\n;"
;//
Assert.assertEquals("SELECT *"
+ "\nFROM t"
+ "\nWHERE status = '20' -- comment xxx"
+ "\n\tAND flag & 127 > 0 -- comment kkkkk"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,26 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatTest extends TestCase {
public void test_format() throws Exception {
String sql = "select * from t1; ;select * from t2;";
Assert.assertEquals("SELECT *" //
+ "\nFROM t1;" //
+ "\nSELECT *" //
+ "\nFROM t2"//
+ "\n;", SQLUtils.formatOdps(sql));
}
public void test_no_semi() throws Exception {
String sql = "select * from t1; ;select * from t2";
Assert.assertEquals("SELECT *" //
+ "\nFROM t1;" //
+ "\nSELECT *" //
+ "\nFROM t2", SQLUtils.formatOdps(sql));
}
}
@@ -30,6 +30,7 @@ public class PGFormatTest extends TestCase {
"\n\tfooid int," + //
"\n\tfoosubid int," + //
"\n\tfooname text" + //
"\n)", formatedSql);
"\n)"
+ "\n;", formatedSql);
}
}