mirror of https://github.com/alibaba/druid.git
Fix multiline problem
This commit is contained in:
parent
0444f4f532
commit
d422ca488f
|
@ -5,11 +5,18 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||||
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
|
import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
|
||||||
import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause;
|
import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause;
|
||||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||||
|
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||||
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
|
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
|
||||||
import com.alibaba.druid.sql.dialect.clickhouse.ast.CKSelectQueryBlock;
|
import com.alibaba.druid.sql.dialect.clickhouse.ast.CKSelectQueryBlock;
|
||||||
import com.alibaba.druid.sql.parser.*;
|
import com.alibaba.druid.sql.parser.Lexer;
|
||||||
|
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||||
|
import com.alibaba.druid.sql.parser.SQLSelectListCache;
|
||||||
|
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||||
|
import com.alibaba.druid.sql.parser.Token;
|
||||||
import com.alibaba.druid.util.FnvHash;
|
import com.alibaba.druid.util.FnvHash;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class CKSelectParser
|
public class CKSelectParser
|
||||||
extends SQLSelectParser {
|
extends SQLSelectParser {
|
||||||
public CKSelectParser(Lexer lexer) {
|
public CKSelectParser(Lexer lexer) {
|
||||||
|
@ -85,12 +92,29 @@ public class CKSelectParser
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void parseFrom(SQLSelectQueryBlock queryBlock) {
|
public void parseFrom(SQLSelectQueryBlock queryBlock) {
|
||||||
super.parseFrom(queryBlock);
|
List<String> comments = null;
|
||||||
if (lexer.token() == Token.FINAL) {
|
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||||
lexer.nextToken();
|
comments = lexer.readAndResetComments();
|
||||||
((CKSelectQueryBlock) queryBlock).setFinal(true);
|
}
|
||||||
|
|
||||||
|
if (lexer.nextIf(Token.FROM)) {
|
||||||
|
SQLTableSource from = parseTableSource();
|
||||||
|
|
||||||
|
if (comments != null) {
|
||||||
|
from.addBeforeComment(comments);
|
||||||
|
}
|
||||||
|
|
||||||
|
queryBlock.setFrom(from);
|
||||||
} else {
|
} else {
|
||||||
((CKSelectQueryBlock) queryBlock).setFinal(false);
|
if (comments != null) {
|
||||||
|
queryBlock.addAfterComment(comments);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lexer.nextIf(Token.FINAL)) {
|
||||||
|
if (queryBlock instanceof CKSelectQueryBlock) {
|
||||||
|
((CKSelectQueryBlock) queryBlock).setFinal(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,9 @@ import com.alibaba.druid.sql.dialect.clickhouse.ast.CKCreateTableStatement;
|
||||||
import com.alibaba.druid.sql.dialect.clickhouse.ast.CKSelectQueryBlock;
|
import com.alibaba.druid.sql.dialect.clickhouse.ast.CKSelectQueryBlock;
|
||||||
import com.alibaba.druid.sql.dialect.clickhouse.ast.ClickhouseColumnCodec;
|
import com.alibaba.druid.sql.dialect.clickhouse.ast.ClickhouseColumnCodec;
|
||||||
import com.alibaba.druid.sql.dialect.clickhouse.ast.ClickhouseColumnTTL;
|
import com.alibaba.druid.sql.dialect.clickhouse.ast.ClickhouseColumnTTL;
|
||||||
|
import com.alibaba.druid.sql.parser.CharTypes;
|
||||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||||
|
import com.alibaba.druid.sql.visitor.VisitorFeature;
|
||||||
import com.alibaba.druid.util.StringUtils;
|
import com.alibaba.druid.util.StringUtils;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -230,6 +232,55 @@ public class CKOutputVisitor extends SQLASTOutputVisitor implements CKASTVisitor
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void printComment(String comment) {
|
||||||
|
if (comment == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isEnabled(VisitorFeature.OutputSkipMultilineComment) && comment.startsWith("/*")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isEnabled(VisitorFeature.OutputSkipSingleLineComment)
|
||||||
|
&& (comment.startsWith("-") || comment.startsWith("#"))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (comment.startsWith("--")
|
||||||
|
&& comment.length() > 2
|
||||||
|
&& comment.charAt(2) != ' '
|
||||||
|
&& comment.charAt(2) != '-') {
|
||||||
|
print0("-- ");
|
||||||
|
print0(comment.substring(2));
|
||||||
|
} else if (comment.startsWith("#")
|
||||||
|
&& comment.length() > 1
|
||||||
|
&& comment.charAt(1) != ' '
|
||||||
|
&& comment.charAt(1) != '#') {
|
||||||
|
print0("# ");
|
||||||
|
print0(comment.substring(1));
|
||||||
|
} else if (comment.startsWith("/*")) {
|
||||||
|
println();
|
||||||
|
print0(comment);
|
||||||
|
} else if (comment.startsWith("--")) {
|
||||||
|
print0(comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
char first = '\0';
|
||||||
|
for (int i = 0; i < comment.length(); i++) {
|
||||||
|
char c = comment.charAt(i);
|
||||||
|
if (CharTypes.isWhitespace(c)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
first = c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (first == '-' || first == '#') {
|
||||||
|
endLineComment = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void printAfterFetch(SQLSelectQueryBlock queryBlock) {
|
protected void printAfterFetch(SQLSelectQueryBlock queryBlock) {
|
||||||
if (queryBlock instanceof CKSelectQueryBlock) {
|
if (queryBlock instanceof CKSelectQueryBlock) {
|
||||||
|
@ -274,6 +325,19 @@ public class CKOutputVisitor extends SQLASTOutputVisitor implements CKASTVisitor
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void printFrom(SQLSelectQueryBlock x) {
|
protected void printFrom(SQLSelectQueryBlock x) {
|
||||||
|
SQLTableSource from = x.getFrom();
|
||||||
|
if (from == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> beforeComments = from.getBeforeCommentsDirect();
|
||||||
|
if (beforeComments != null) {
|
||||||
|
for (String comment : beforeComments) {
|
||||||
|
println();
|
||||||
|
print0(comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
super.printFrom(x);
|
super.printFrom(x);
|
||||||
if (x instanceof CKSelectQueryBlock && ((CKSelectQueryBlock) x).isFinal()) {
|
if (x instanceof CKSelectQueryBlock && ((CKSelectQueryBlock) x).isFinal()) {
|
||||||
print0(ucase ? " FINAL" : " final");
|
print0(ucase ? " FINAL" : " final");
|
||||||
|
|
|
@ -3,10 +3,15 @@ package com.alibaba.druid.sql.dialect.hologres.visitor;
|
||||||
import com.alibaba.druid.DbType;
|
import com.alibaba.druid.DbType;
|
||||||
import com.alibaba.druid.sql.ast.SQLPartitionBy;
|
import com.alibaba.druid.sql.ast.SQLPartitionBy;
|
||||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||||
|
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||||
|
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||||
|
import com.alibaba.druid.sql.dialect.clickhouse.ast.CKSelectQueryBlock;
|
||||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor;
|
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor;
|
||||||
import com.alibaba.druid.sql.parser.CharTypes;
|
import com.alibaba.druid.sql.parser.CharTypes;
|
||||||
import com.alibaba.druid.sql.visitor.VisitorFeature;
|
import com.alibaba.druid.sql.visitor.VisitorFeature;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class HologresOutputVisitor extends PGOutputVisitor {
|
public class HologresOutputVisitor extends PGOutputVisitor {
|
||||||
public HologresOutputVisitor(StringBuilder appender, boolean parameterized) {
|
public HologresOutputVisitor(StringBuilder appender, boolean parameterized) {
|
||||||
super(appender, DbType.hologres, parameterized);
|
super(appender, DbType.hologres, parameterized);
|
||||||
|
@ -30,6 +35,27 @@ public class HologresOutputVisitor extends PGOutputVisitor {
|
||||||
partitionBy.accept(this);
|
partitionBy.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void printFrom(SQLSelectQueryBlock x) {
|
||||||
|
SQLTableSource from = x.getFrom();
|
||||||
|
if (from == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> beforeComments = from.getBeforeCommentsDirect();
|
||||||
|
if (beforeComments != null) {
|
||||||
|
for (String comment : beforeComments) {
|
||||||
|
println();
|
||||||
|
print0(comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
super.printFrom(x);
|
||||||
|
if (x instanceof CKSelectQueryBlock && ((CKSelectQueryBlock) x).isFinal()) {
|
||||||
|
print0(ucase ? " FINAL" : " final");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void printComment(String comment) {
|
public void printComment(String comment) {
|
||||||
if (comment == null) {
|
if (comment == null) {
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
/*
|
||||||
|
this is comment 1
|
||||||
|
this is comment 2
|
||||||
|
*/
|
||||||
|
select 1
|
||||||
|
--------------------
|
||||||
|
/*
|
||||||
|
this is comment 1
|
||||||
|
this is comment 2
|
||||||
|
*/
|
||||||
|
SELECT 1
|
||||||
|
------------------------------------------------------------------------------------------------------------------------
|
||||||
|
/* this is comment 1 */
|
||||||
|
select 1
|
||||||
|
--------------------
|
||||||
|
/* this is comment 1 */
|
||||||
|
SELECT 1
|
||||||
|
------------------------------------------------------------------------------------------------------------------------
|
||||||
|
select a /* xxxx
|
||||||
|
xxx
|
||||||
|
xxxx*/ from test
|
||||||
|
--------------------
|
||||||
|
SELECT a
|
||||||
|
/* xxxx
|
||||||
|
xxx
|
||||||
|
xxxx*/
|
||||||
|
FROM test
|
||||||
|
------------------------------------------------------------------------------------------------------------------------
|
||||||
|
select a /* xxxxxxxx*/ from test
|
||||||
|
--------------------
|
||||||
|
SELECT a
|
||||||
|
/* xxxxxxxx*/
|
||||||
|
FROM test
|
Loading…
Reference in New Issue