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
@@ -253,7 +253,20 @@ public class SQLUtils {
SQLStatement stmt = statementList.get(i);
if (i > 0) {
visitor.println(";");
visitor.print(";");
SQLStatement preStmt = statementList.get(i - 1);
List<String> comments = preStmt.getAfterCommentsDirect();
if (comments != null){
for (int j = 0; j < comments.size(); ++j) {
String comment = comments.get(j);
if (j != 0) {
visitor.println();
}
visitor.print(comment);
}
}
visitor.println();
}
{
List<String> comments = stmt.getBeforeCommentsDirect();
@@ -268,9 +281,9 @@ public class SQLUtils {
if (i == statementList.size() - 1) {
Boolean semi = (Boolean) stmt.getAttribute("format.semi");
if (semi != null && semi.booleanValue()) {
if (stmt.hasAfterComment()) {
visitor.println();
}
// if (stmt.hasAfterComment()) {
// visitor.println();
// }
visitor.print(";");
}
@@ -139,6 +139,7 @@ public class OdpsLexer extends Lexer {
for (;;) {
if (ch == '\r') {
if (charAt(pos + 1) == '\n') {
line++;
bufPos += 2;
scanChar();
break;
@@ -150,6 +151,7 @@ public class OdpsLexer extends Lexer {
}
if (ch == '\n') {
line++;
scanChar();
bufPos++;
break;
@@ -77,6 +77,8 @@ public class Lexer {
protected boolean endOfComment = false;
protected boolean keepComments = false;
protected int line = 0;
public Lexer(String input){
this(input, null);
@@ -293,6 +295,10 @@ public class Lexer {
for (;;) {
if (isWhitespace(ch)) {
if (ch == '\n') {
line++;
}
scanChar();
continue;
}
@@ -831,6 +837,7 @@ public class Lexer {
for (;;) {
if (ch == '\r') {
if (charAt(pos + 1) == '\n') {
line++;
scanChar();
break;
}
@@ -839,6 +846,7 @@ public class Lexer {
}
if (ch == '\n') {
line++;
scanChar();
break;
}
@@ -1185,4 +1193,8 @@ public class Lexer {
}
comments.add(stringVal);
}
public int getLine() {
return line;
}
}
@@ -159,9 +159,15 @@ public class SQLStatementParser extends SQLParser {
}
if (lexer.token() == Token.SEMI) {
int line0 = lexer.getLine();
lexer.nextToken();
if (lexer.isKeepComments()) {
int line1 = lexer.getLine();
if (lexer.isKeepComments() && statementList.size() > 0) {
SQLStatement stmt = statementList.get(statementList.size() - 1);
if (line1 - line0 <= 1) {
stmt.addAfterComment(lexer.readAndResetComments());
}
stmt.getAttributes().put("format.semi", Boolean.TRUE);
}
continue;
@@ -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));
}