change sql format

This commit is contained in:
shaojin.wensj
2015-07-22 01:18:51 +08:00
parent 0ace7d12bb
commit afdf17b154
29 changed files with 50 additions and 88 deletions
@@ -268,7 +268,9 @@ public class SQLUtils {
if (i == statementList.size() - 1) {
Boolean semi = (Boolean) stmt.getAttribute("format.semi");
if (semi != null && semi.booleanValue()) {
visitor.println();
if (stmt.hasAfterComment()) {
visitor.println();
}
visitor.print(";");
}
@@ -79,7 +79,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
element.accept(this);
if (i != size - 1) {
print(',');
print(",");
}
if (this.isPrettyFormat() && element.hasAfterComment()) {
print(' ');
@@ -29,7 +29,6 @@ public class MySqlFormatTest extends TestCase {
"\ta INT, \n" + //
"\tb CHAR(20), \n" + //
"\tINDEX(a)\n" + //
")" //
+ "\n;", SQLUtils.format(text, JdbcUtils.MYSQL));
");", SQLUtils.format(text, JdbcUtils.MYSQL));
}
}
@@ -9,37 +9,31 @@ import junit.framework.TestCase;
public class OdpsAddStatisticTest extends TestCase {
public void test_table_count() throws Exception {
String sql = "add statistic tbl_shop table_count;";
Assert.assertEquals("ADD STATISTIC tbl_shop TABLE_COUNT"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("ADD STATISTIC tbl_shop TABLE_COUNT;", SQLUtils.formatOdps(sql));
}
public void test_null_value() throws Exception {
String sql = "add statistic tbl_shop null_value shop_name;";
Assert.assertEquals("ADD STATISTIC tbl_shop NULL_VALUE shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("ADD STATISTIC tbl_shop NULL_VALUE shop_name;", SQLUtils.formatOdps(sql));
}
public void test_column_sum() throws Exception {
String sql = "add statistic tbl_shop column_sum shop_name;";
Assert.assertEquals("ADD STATISTIC tbl_shop COLUMN_SUM shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("ADD STATISTIC tbl_shop COLUMN_SUM shop_name;", SQLUtils.formatOdps(sql));
}
public void test_column_max() throws Exception {
String sql = "add statistic tbl_shop column_max shop_name;";
Assert.assertEquals("ADD STATISTIC tbl_shop COLUMN_MAX shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("ADD STATISTIC tbl_shop COLUMN_MAX shop_name;", SQLUtils.formatOdps(sql));
}
public void test_column_min() throws Exception {
String sql = "add statistic tbl_shop column_min shop_name;";
Assert.assertEquals("ADD STATISTIC tbl_shop COLUMN_MIN shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("ADD STATISTIC tbl_shop COLUMN_MIN shop_name;", SQLUtils.formatOdps(sql));
}
public void test_expression_condition() throws Exception {
String sql = "add statistic tbl_shop expression_condition tbl_shop='hangzhou';";
Assert.assertEquals("ADD STATISTIC tbl_shop EXPRESSION_CONDITION tbl_shop = 'hangzhou'"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("ADD STATISTIC tbl_shop EXPRESSION_CONDITION tbl_shop = 'hangzhou';", SQLUtils.formatOdps(sql));
}
}
@@ -11,7 +11,6 @@ public class OdpsAlterTableAddPartitionTest extends TestCase {
public void test_if() throws Exception {
String sql = "alter table sale_detail add if not exists partition (sale_date='201312', region='hangzhou');";
Assert.assertEquals("ALTER TABLE sale_detail" //
+ "\n\tADD IF NOT EXISTS PARTITION (sale_date = '201312', region = 'hangzhou')"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tADD IF NOT EXISTS PARTITION (sale_date = '201312', region = 'hangzhou');", SQLUtils.formatOdps(sql));
}
}
@@ -11,15 +11,13 @@ public class OdpsAlterTableDisableLifecycle extends TestCase {
public void test_no_partition() throws Exception {
String sql = "ALTER TABLE trans DISABLE LIFECYCLE;";
Assert.assertEquals("ALTER TABLE trans" //
+ "\n\tDISABLE LIFECYCLE" //
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tDISABLE LIFECYCLE;", SQLUtils.formatOdps(sql));
}
public void test_has_partition() throws Exception {
String sql = "ALTER TABLE trans PARTITION(dt='20141111') DISABLE LIFECYCLE;";
Assert.assertEquals("ALTER TABLE trans"
+ "\n\tPARTITION (dt = '20141111') DISABLE LIFECYCLE"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tPARTITION (dt = '20141111') DISABLE LIFECYCLE;", SQLUtils.formatOdps(sql));
}
}
@@ -11,14 +11,12 @@ public class OdpsAlterTableDropPartitionTest extends TestCase {
public void test_if() throws Exception {
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')"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tDROP IF NOT EXISTS PARTITION (sale_date = '201312', region = 'hangzhou');", SQLUtils.formatOdps(sql));
}
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"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tDROP PARTITION (ds = '20150618') PURGE;", SQLUtils.formatOdps(sql));
}
}
@@ -11,15 +11,13 @@ public class OdpsAlterTableEnableLifecycle extends TestCase {
public void test_no_partition() throws Exception {
String sql = "ALTER TABLE trans ENABLE LIFECYCLE;";
Assert.assertEquals("ALTER TABLE trans" //
+ "\n\tENABLE LIFECYCLE" //
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tENABLE LIFECYCLE;", SQLUtils.formatOdps(sql));
}
public void test_has_partition() throws Exception {
String sql = "ALTER TABLE trans PARTITION(dt='20141111') ENABLE LIFECYCLE;";
Assert.assertEquals("ALTER TABLE trans"
+ "\n\tPARTITION (dt = '20141111') ENABLE LIFECYCLE"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tPARTITION (dt = '20141111') ENABLE LIFECYCLE;", SQLUtils.formatOdps(sql));
}
}
@@ -12,7 +12,6 @@ public class OdpsAlterTableRenamePartitionTest extends TestCase {
String sql = "alter table sale_detail partition (sale_date='201312', region='hangzhou')"
+ "\nrename to partition(sale_date='201313', region='hangzhou');";
Assert.assertEquals("ALTER TABLE sale_detail"
+ "\n\tPARTITION (sale_date = '201312', region = 'hangzhou') RENAME TO PARTITION(sale_date = '201313', region = 'hangzhou')"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tPARTITION (sale_date = '201312', region = 'hangzhou') RENAME TO PARTITION(sale_date = '201313', region = 'hangzhou');", SQLUtils.formatOdps(sql));
}
}
@@ -11,7 +11,6 @@ public class OdpsAlterTableSetCommentTest extends TestCase {
public void test_if() throws Exception {
String sql = "alter table sale_detail set comment 'new coments for table sale_detail';";
Assert.assertEquals("ALTER TABLE sale_detail" //
+ "\n\tSET COMMENT 'new coments for table sale_detail'" //
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tSET COMMENT 'new coments for table sale_detail';", SQLUtils.formatOdps(sql));
}
}
@@ -11,7 +11,6 @@ public class OdpsAlterTableSetLifecycle extends TestCase {
public void test_if() throws Exception {
String sql = "alter table test_lifecycle set lifecycle 50;";
Assert.assertEquals("ALTER TABLE test_lifecycle" //
+ "\n\tSET LIFECYCLE 50" //
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tSET LIFECYCLE 50;", SQLUtils.formatOdps(sql));
}
}
@@ -11,14 +11,12 @@ public class OdpsAlterTableTouchTest extends TestCase {
public void test_touch() throws Exception {
String sql = "alter table test_lifecycle touch;";
Assert.assertEquals("ALTER TABLE test_lifecycle" //
+ "\n\tTOUCH" //
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tTOUCH;", SQLUtils.formatOdps(sql));
}
public void test_touch_partition() throws Exception {
String sql = "alter table test_lifecycle touch PARTITION (dt='20141111');";
Assert.assertEquals("ALTER TABLE test_lifecycle" //
+ "\n\tTOUCH PARTITION (dt = '20141111')" //
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tTOUCH PARTITION (dt = '20141111');", SQLUtils.formatOdps(sql));
}
}
@@ -10,7 +10,6 @@ public class OdpsAlterViewTest extends TestCase {
public void test_if() throws Exception {
String sql = "alter view view_name rename to new_view_name;";
Assert.assertEquals("ALTER VIEW view_name RENAME TO new_view_name"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("ALTER VIEW view_name RENAME TO new_view_name;", SQLUtils.formatOdps(sql));
}
}
@@ -26,8 +26,8 @@ public class OdpsCreateTableTest extends TestCase {
parser.match(Token.EOF);
String output = SQLUtils.toOdpsString(stmt);
Assert.assertEquals("CREATE TABLE IF NOT EXISTS sale_detailsale_detail ("//
+ "\n\tshop_name STRING, "//
+ "\n\tcustomer_id STRING, "//
+ "\n\tshop_name STRING,"//
+ "\n\tcustomer_id STRING,"//
+ "\n\ttotal_price DOUBLE"//
+ "\n)"//
+ "\nCOMMENT 'xxxx'"//
@@ -25,8 +25,7 @@ public class OdpsDropViewTest extends TestCase {
public void test_column_comment() throws Exception {
String sql = "drop view if exists view_name;";
Assert.assertEquals("DROP VIEW IF EXISTS view_name" //
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("DROP VIEW IF EXISTS view_name;", SQLUtils.formatOdps(sql));
}
@@ -20,8 +20,7 @@ public class OdpsFormatCommentTest10 extends TestCase {
+ "\n\t\tf5,"
+ "\n\t\tf6"
+ "\n\t)"
+ "\nFROM xxx"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\nFROM xxx;", SQLUtils.formatOdps(sql));
}
@@ -15,8 +15,7 @@ public class OdpsFormatCommentTest15 extends TestCase {
+ "\nPARTITIONED BY ("
+ "\n\tds string,"
+ "\n\thh string"
+ "\n)"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n);", SQLUtils.formatOdps(sql));
}
@@ -1,5 +1,7 @@
package com.alibaba.druid.bvt.sql.odps;
import java.net.URLDecoder;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
@@ -24,8 +26,7 @@ public class OdpsFormatCommentTest16 extends TestCase {
+ "\nPARTITIONED BY ("
+ "\n\tds string, -- c_1"
+ "\n\thh string -- c_2"
+ "\n)"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n);", SQLUtils.formatOdps(sql));
}
@@ -20,8 +20,7 @@ public class OdpsFormatCommentTest2 extends TestCase {
+ "\n--[Update ]"
+ "\nSET odps.sql.mapper.split.size = 2048;"
+ "\nSELECT *"
+ "\nFROM dual"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\nFROM dual;", SQLUtils.formatOdps(sql));
}
}
@@ -23,8 +23,7 @@ public class OdpsFormatCommentTest3 extends TestCase {
+ "\nSELECT f1 -- aa"//
+ "\nFROM t0;"//
+ "\nSELECT f2 -- aa"//
+ "\nFROM t1"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\nFROM t1;", SQLUtils.formatOdps(sql));
}
}
@@ -33,8 +33,7 @@ public class OdpsFormatCommentTest4 extends TestCase {
+ "\nSELECT f0, "//
+ "\n\tf1, "//
+ "\n\tf2"//
+ "\nWHERE name RLIKE 'kk'"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\nWHERE name RLIKE 'kk';", SQLUtils.formatOdps(sql));
}
}
@@ -14,8 +14,7 @@ public class OdpsFormatCommentTest5 extends TestCase {
;//
Assert.assertEquals("SELECT *" //
+ "\nFROM t -- xxxx" //
+ "\nWHERE id > 0"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\nWHERE id > 0;", SQLUtils.formatOdps(sql));
}
public void test_column_comment_as() throws Exception {
@@ -25,8 +24,7 @@ public class OdpsFormatCommentTest5 extends TestCase {
;//
Assert.assertEquals("SELECT *" //
+ "\nFROM xxxx a -- xxxx" //
+ "\nWHERE id > 0"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\nWHERE id > 0;", SQLUtils.formatOdps(sql));
}
@@ -42,7 +40,6 @@ public class OdpsFormatCommentTest5 extends TestCase {
+ "\n\t-- comment_xxx" //
+ "\n\tSELECT *" //
+ "\n\tFROM t" //
+ "\n) a"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n) a;", SQLUtils.formatOdps(sql));
}
}
@@ -17,8 +17,7 @@ public class OdpsFormatCommentTest6 extends TestCase {
Assert.assertEquals("SELECT *"
+ "\nFROM t"
+ "\nWHERE status = '20' -- comment xxx"
+ "\n\tAND flag & 127 > 0 -- comment kkkkk"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\n\tAND flag & 127 > 0 -- comment kkkkk;", SQLUtils.formatOdps(sql));
}
@@ -16,8 +16,7 @@ public class OdpsFormatCommentTest7 extends TestCase {
+ "\nSELECT *"
+ "\nFROM table1;"
+ "\nSELECT *"
+ "\nFROM table2"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\nFROM table2;", SQLUtils.formatOdps(sql));
}
@@ -12,8 +12,7 @@ public class OdpsFormatCommentTest9 extends TestCase {
Assert.assertEquals("INSERT OVERWRITE TABLE ttt PARTITION (ds='20150710', hh='07')"
+ "\nSELECT tt_split(content, 60)"
+ "\n\tAS (f0, f1, f2, f3)"
+ "\nFROM xxx"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\nFROM xxx;", SQLUtils.formatOdps(sql));
}
@@ -12,8 +12,7 @@ public class OdpsFormatTest extends TestCase {
Assert.assertEquals("SELECT *" //
+ "\nFROM t1;" //
+ "\nSELECT *" //
+ "\nFROM t2"//
+ "\n;", SQLUtils.formatOdps(sql));
+ "\nFROM t2;", SQLUtils.formatOdps(sql));
}
public void test_no_semi() throws Exception {
@@ -9,37 +9,31 @@ import junit.framework.TestCase;
public class OdpsRemoveStatisticTest extends TestCase {
public void test_table_count() throws Exception {
String sql = "remove statistic tbl_shop table_count;";
Assert.assertEquals("REMOVE STATISTIC tbl_shop TABLE_COUNT"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("REMOVE STATISTIC tbl_shop TABLE_COUNT;", SQLUtils.formatOdps(sql));
}
public void test_null_value() throws Exception {
String sql = "remove statistic tbl_shop null_value shop_name;";
Assert.assertEquals("REMOVE STATISTIC tbl_shop NULL_VALUE shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("REMOVE STATISTIC tbl_shop NULL_VALUE shop_name;", SQLUtils.formatOdps(sql));
}
public void test_column_sum() throws Exception {
String sql = "remove statistic tbl_shop column_sum shop_name;";
Assert.assertEquals("REMOVE STATISTIC tbl_shop COLUMN_SUM shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("REMOVE STATISTIC tbl_shop COLUMN_SUM shop_name;", SQLUtils.formatOdps(sql));
}
public void test_column_max() throws Exception {
String sql = "remove statistic tbl_shop column_max shop_name;";
Assert.assertEquals("REMOVE STATISTIC tbl_shop COLUMN_MAX shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("REMOVE STATISTIC tbl_shop COLUMN_MAX shop_name;", SQLUtils.formatOdps(sql));
}
public void test_column_min() throws Exception {
String sql = "remove statistic tbl_shop column_min shop_name;";
Assert.assertEquals("REMOVE STATISTIC tbl_shop COLUMN_MIN shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("REMOVE STATISTIC tbl_shop COLUMN_MIN shop_name;", SQLUtils.formatOdps(sql));
}
public void test_expression_condition() throws Exception {
String sql = "remove statistic tbl_shop expression_condition tbl_shop='hangzhou';";
Assert.assertEquals("REMOVE STATISTIC tbl_shop EXPRESSION_CONDITION tbl_shop = 'hangzhou'"
+ "\n;", SQLUtils.formatOdps(sql));
Assert.assertEquals("REMOVE STATISTIC tbl_shop EXPRESSION_CONDITION tbl_shop = 'hangzhou';", SQLUtils.formatOdps(sql));
}
}
@@ -34,8 +34,7 @@ public class OdpsSelectTest_mapjoin extends TestCase {
+ "\n\tb.customer_id, "
+ "\n\tb.total_price"
+ "\nFROM shop a"
+ "\nJOIN sale_detail b ON a.shop_name = b.shop_name"
+ "\n;", SQLUtils.formatOdps(sql));
+ "\nJOIN sale_detail b ON a.shop_name = b.shop_name;", SQLUtils.formatOdps(sql));
}
@@ -30,7 +30,6 @@ public class PGFormatTest extends TestCase {
"\n\tfooid int," + //
"\n\tfoosubid int," + //
"\n\tfooname text" + //
"\n)"
+ "\n;", formatedSql);
"\n);", formatedSql);
}
}