mysql sql format support keepComments.

This commit is contained in:
wenshao
2017-05-14 10:14:34 +08:00
parent 2c26d3623f
commit 5706f65fcf
9 changed files with 187 additions and 5 deletions
@@ -242,7 +242,7 @@ public class SQLUtils {
public static String format(String sql, String dbType, List<Object> parameters, FormatOption option) {
try {
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, true);
parser.setKeepComments(true);
List<SQLStatement> statementList = parser.parseStatementList();
@@ -438,6 +438,15 @@ public class SQLUtils {
return stmtList;
}
public static List<SQLStatement> parseStatements(String sql, String dbType, boolean keepComments) {
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, keepComments);
List<SQLStatement> stmtList = parser.parseStatementList();
if (parser.getLexer().token() != Token.EOF) {
throw new DruidRuntimeException("syntax error : " + sql);
}
return stmtList;
}
/**
* @author owenludong.lud
* @param columnName
@@ -73,6 +73,11 @@ public class MySqlExprParser extends SQLExprParser {
this.lexer.nextToken();
}
public MySqlExprParser(String sql, boolean keepComments){
this(new MySqlLexer(sql, keepComments, keepComments));
this.lexer.nextToken();
}
public SQLExpr relationalRest(SQLExpr expr) {
if (identifierEquals("REGEXP")) {
lexer.nextToken();
@@ -75,6 +75,13 @@ public class MySqlLexer extends Lexer {
super.keywods = DEFAULT_MYSQL_KEYWORDS;
}
public MySqlLexer(String input, boolean skipComment, boolean keepComments){
super(input, skipComment);
this.skipComment = skipComment;
this.keepComments = keepComments;
super.keywods = DEFAULT_MYSQL_KEYWORDS;
}
public void scanSharp() {
if (ch != '#') {
throw new ParserException("illegal stat");
@@ -87,6 +87,10 @@ public class MySqlSelectParser extends SQLSelectParser {
MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock();
if (lexer.hasComment() && lexer.isKeepComments()) {
queryBlock.addBeforeComment(lexer.readAndResetComments());
}
if (lexer.token() == Token.SELECT) {
lexer.nextToken();
@@ -231,6 +231,10 @@ public class MySqlStatementParser extends SQLStatementParser {
super(new MySqlExprParser(sql));
}
public MySqlStatementParser(String sql, boolean keepComments) {
super(new MySqlExprParser(sql, keepComments));
}
public MySqlStatementParser(Lexer lexer) {
super(new MySqlExprParser(lexer));
}
@@ -206,8 +206,8 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
}
public boolean visit(MySqlSelectQueryBlock x) {
if (x.getOrderBy() != null) {
x.getOrderBy().setParent(x);
if ((!isParameterized()) && isPrettyFormat() && x.hasBeforeComment()) {
printComment(x.getBeforeCommentsDirect(), "\n");
}
print0(ucase ? "SELECT " : "select ");
@@ -36,21 +36,32 @@ import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerExprParser;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerLexer;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.JdbcUtils;
public class SQLParserUtils {
public static SQLStatementParser createSQLStatementParser(String sql, String dbType) {
boolean keepComments;
if (JdbcConstants.ODPS.equals(dbType)) {
keepComments = true;
} else {
keepComments = false;
}
return createSQLStatementParser(sql, dbType, keepComments);
}
public static SQLStatementParser createSQLStatementParser(String sql, String dbType, boolean keepComments) {
if (JdbcUtils.ORACLE.equals(dbType) || JdbcUtils.ALI_ORACLE.equals(dbType)) {
return new OracleStatementParser(sql);
}
if (JdbcUtils.MYSQL.equals(dbType)) {
return new MySqlStatementParser(sql);
return new MySqlStatementParser(sql, keepComments);
}
if (JdbcUtils.MARIADB.equals(dbType)) {
return new MySqlStatementParser(sql);
return new MySqlStatementParser(sql, keepComments);
}
if (JdbcUtils.POSTGRESQL.equals(dbType)
@@ -0,0 +1,71 @@
/*
* Copyright 1999-2017 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.select;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import org.junit.Assert;
import java.util.List;
public class MySqlSelectTest_40 extends MysqlTest {
public void test_0() throws Exception {
String sql = "/*0abcde*/select max(id) from base_request_log";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, true);
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.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(0, visitor.getConditions().size());
// Assert.assertEquals(0, visitor.getOrderByColumns().size());
{
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("/*0abcde*/SELECT MAX(id)\n" +
"FROM base_request_log", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("/*0abcde*/select max(id)\n" +
"from base_request_log", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
Assert.assertEquals("SELECT MAX(id)\n" +
"FROM base_request_log", //
output);
}
}
}
@@ -0,0 +1,71 @@
/*
* Copyright 1999-2017 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.select;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import org.junit.Assert;
import java.util.List;
public class MySqlSelectTest_41 extends MysqlTest {
public void test_0() throws Exception {
String sql = "/*0abcde*/select max(id) from base_request_log";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL, true);
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.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(0, visitor.getConditions().size());
// Assert.assertEquals(0, visitor.getOrderByColumns().size());
{
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("/*0abcde*/SELECT MAX(id)\n" +
"FROM base_request_log", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("/*0abcde*/select max(id)\n" +
"from base_request_log", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
Assert.assertEquals("SELECT MAX(id)\n" +
"FROM base_request_log", //
output);
}
}
}