improved odps sql formatter

This commit is contained in:
shaojin.wensj
2015-07-20 04:41:57 +08:00
parent 8f51368245
commit a5811a4f3e
38 changed files with 415 additions and 103 deletions
@@ -36,7 +36,8 @@ public class SQLUtilsTest extends TestCase {
String expected = "SELECT lottery_notice_issue, lottery_notice_date, lottery_notice_result"
+ "\nFROM tb_lottery_notice" + "\nWHERE lottery_type_id = 8"
+ "\n\tAND lottery_notice_issue <= 2014066" + "\nUNION ALL"
+ "\nSELECT NULL, NULL, NULL, NULL, NULL" + "\n\t, NULL";
+ "\nSELECT NULL, NULL, NULL, NULL, NULL" //
+ "\n\t, NULL# and lottery_notice_issue>=2014062 order by lottery_notice_issue desc";
Assert.assertEquals(expected, formattedSql);
}
}
@@ -0,0 +1,28 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest10 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "insert overwrite table ttt partition (ds='20150710',hh='07') select tt_split(content, 60) as (f0, f1, f2, f3,f4,f5,f6) from xxx;";
Assert.assertEquals("INSERT OVERWRITE TABLE ttt PARTITION (ds='20150710', hh='07')"
+ "\nSELECT tt_split(content, 60)"
+ "\n\tAS ("
+ "\n\t\tf0,"
+ "\n\t\tf1,"
+ "\n\t\tf2,"
+ "\n\t\tf3,"
+ "\n\t\tf4,"
+ "\n\t\tf5,"
+ "\n\t\tf6"
+ "\n\t)"
+ "\nFROM xxx"
+ "\n;", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,24 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest7 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "--这里是注释" //
+ "\nselect * from table1;" //
+ "\nselect * from table2;;"
;//
Assert.assertEquals("--这里是注释"
+ "\nSELECT *"
+ "\nFROM table1;"
+ "\nSELECT *"
+ "\nFROM table2"
+ "\n;", 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 OdpsFormatCommentTest8 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "select * from table1;--comment1 goes here" //
+ "\n" //
+ "\nselect * from table2;;select * from table3;" //
+ "\n--comment2 goes here"
;//
Assert.assertEquals("SELECT *"
+ "\nFROM table1;"
+ "\n--comment1 goes here"
+ "\nSELECT *"
+ "\nFROM table2;"
+ "\nSELECT *"
+ "\nFROM table3"
+ "\n;--comment2 goes her", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,20 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest9 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "insert overwrite table ttt partition (ds='20150710',hh='07') select tt_split(content, 60) as (f0, f1, f2, f3) from xxx;";
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));
}
}