Merge pull request #922 from wenshao/master

improved sql formatter
This commit is contained in:
高铁
2015-07-27 14:02:57 +08:00
9 changed files with 153 additions and 7 deletions
@@ -0,0 +1,56 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.mysql;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
public class MySqlSelectTest_23 extends MysqlTest {
public void test_0() throws Exception {
String sql = "UPDATE qadmin_user set tree_code = REPLACE(tree_code, ?, ?) WHERE tree_code LIKE CONCAT(?, '%');";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(1, visitor.getColumns().size());
Assert.assertEquals(1, visitor.getConditions().size());
Assert.assertEquals(0, visitor.getOrderByColumns().size());
}
}
@@ -0,0 +1,16 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest17 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "set xxx=aaa;--ssss";
Assert.assertEquals("SET xxx = aaa;--ssss", 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 OdpsFormatCommentTest18 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "create table t (f1 string comment \"xxx\")";
Assert.assertEquals("CREATE TABLE tt ("
+ "\n\tf1 string COMMENT 'xxx'"
+ "\n)", SQLUtils.formatOdps(sql));
}
public void test_column_comment_2() throws Exception {
String sql = "create table t (f1 string comment \"xxx's\")";
Assert.assertEquals("CREATE TABLE tt ("
+ "\n\tf1 string COMMENT 'xxx''s'"
+ "\n)", SQLUtils.formatOdps(sql));
}
}
@@ -0,0 +1,18 @@
package com.alibaba.druid.bvt.sql.odps;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import junit.framework.TestCase;
public class OdpsFormatCommentTest19 extends TestCase {
public void test_column_comment() throws Exception {
String sql = "set xxx=aaa;--ssss"
+ "\nset yyy=123;";
Assert.assertEquals("SET xxx = aaa;--ssss"
+ "\nSET yyy = 123;", SQLUtils.formatOdps(sql));
}
}
@@ -19,8 +19,7 @@ public class OdpsFormatCommentTest8 extends TestCase {
+ "\nSELECT *"
+ "\nFROM table2;"
+ "\nSELECT *"
+ "\nFROM table3"
+ "\n;--comment2 goes here", SQLUtils.formatOdps(sql));
+ "\nFROM table3;--comment2 goes here", SQLUtils.formatOdps(sql));
}