mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
mysql sql format support keepComments.
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user