mirror of https://github.com/alibaba/druid.git
Compare commits
3 Commits
8df91a8279
...
ed803566d7
| Author | SHA1 | Date |
|---|---|---|
|
|
ed803566d7 | |
|
|
ac484261c0 | |
|
|
0736876199 |
|
|
@ -39,6 +39,7 @@ public class CKLexer extends Lexer {
|
|||
map.put("FINAL", Token.FINAL);
|
||||
map.put("TTL", Token.TTL);
|
||||
map.put("CODEC", Token.CODEC);
|
||||
map.remove("ANY");
|
||||
|
||||
return new Keywords(map);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import com.alibaba.druid.DbType;
|
|||
import com.alibaba.druid.sql.ast.SQLPartitionBy;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor;
|
||||
import com.alibaba.druid.sql.parser.CharTypes;
|
||||
import com.alibaba.druid.sql.visitor.VisitorFeature;
|
||||
|
||||
public class HologresOutputVisitor extends PGOutputVisitor {
|
||||
public HologresOutputVisitor(StringBuilder appender, boolean parameterized) {
|
||||
|
|
@ -27,4 +29,53 @@ public class HologresOutputVisitor extends PGOutputVisitor {
|
|||
print0(ucase ? "PARTITION BY " : "partition by ");
|
||||
partitionBy.accept(this);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1677,10 +1677,6 @@ public class Lexer {
|
|||
scanChar();
|
||||
scanChar();
|
||||
token = Token.LTLT;
|
||||
} else if (c1 == '@') {
|
||||
scanChar();
|
||||
scanChar();
|
||||
token = Token.LT_MONKEYS_AT;
|
||||
} else if (c1 == '-' && charAt(pos + 2) == '>') {
|
||||
scanChar();
|
||||
scanChar();
|
||||
|
|
@ -2527,10 +2523,21 @@ public class Lexer {
|
|||
}
|
||||
|
||||
if (ch == '*' && charAt(pos + 1) == '/') {
|
||||
scanChar();
|
||||
scanChar();
|
||||
if (0 == --depth) {
|
||||
break;
|
||||
int curPos = pos;
|
||||
boolean terminated = true;
|
||||
// If '*/' has leading '--' in the same line, just skip it. For example '-- xxxx */'.
|
||||
while (curPos > 0 && charAt(curPos) != '\n') {
|
||||
if (charAt(curPos) == '-' && (curPos + 1) < text.length() && charAt(curPos + 1) == '-') {
|
||||
terminated = false;
|
||||
}
|
||||
curPos--;
|
||||
}
|
||||
if (terminated) {
|
||||
scanChar();
|
||||
scanChar();
|
||||
if (0 == --depth) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
|||
|
||||
protected transient int lines;
|
||||
private TimeZone timeZone;
|
||||
private boolean endLineComment;
|
||||
protected boolean endLineComment;
|
||||
|
||||
protected SQLDialect dialect;
|
||||
|
||||
|
|
|
|||
|
|
@ -3,16 +3,23 @@ package com.alibaba.druid.mysql;
|
|||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSetStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSetTransactionStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.BinaryOperator;
|
||||
|
||||
/**
|
||||
* Created by szf on 2017/11/16.
|
||||
|
|
@ -150,4 +157,54 @@ public class MysqlVarantRefTest {
|
|||
Assert.assertTrue(resultExpr5.isSession());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test7() {
|
||||
// in this case, < @sid is recognized as < operator and user-defined variables@sid
|
||||
String sql = "set @sid=1;select * from aaa where id < @sid; ";
|
||||
SQLStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> stmtList = parser.parseStatementList();
|
||||
|
||||
SQLSetStatement result0 = (SQLSetStatement) stmtList.get(0);
|
||||
SQLVariantRefExpr resultExpr0 = (SQLVariantRefExpr) result0.getItems().get(0).getTarget();
|
||||
Assert.assertEquals("@sid", resultExpr0.getName());
|
||||
|
||||
AtomicReference<SQLVariantRefExpr> resultExpr1Ref = new AtomicReference<>();
|
||||
AtomicInteger variableCnt = new AtomicInteger(0);
|
||||
AtomicInteger arrayContainerByOperatorCnt = new AtomicInteger(0);
|
||||
MySqlASTVisitor visitor = new MySqlASTVisitorAdapter() {
|
||||
@Override
|
||||
public boolean visit(SQLVariantRefExpr x) {
|
||||
resultExpr1Ref.set(x);
|
||||
variableCnt.addAndGet(1);
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBinaryOpExpr x) {
|
||||
if(SQLBinaryOperator.Array_ContainedBy.equals(x.getOperator())) {
|
||||
arrayContainerByOperatorCnt.addAndGet(1);
|
||||
}
|
||||
return super.visit(x);
|
||||
}
|
||||
};
|
||||
stmtList.get(1).accept(visitor);
|
||||
|
||||
|
||||
Assert.assertEquals(1, variableCnt.get());
|
||||
Assert.assertEquals(0, arrayContainerByOperatorCnt.get());
|
||||
Assert.assertEquals("@sid", resultExpr1Ref.get().getName());
|
||||
|
||||
// in this case, <@ is recognized as <@, instead of < user-defined variables@sid
|
||||
String sql2 = "select * from aaa where id <@ sid; ";
|
||||
SQLStatementParser parser2 = new MySqlStatementParser(sql2);
|
||||
|
||||
List<SQLStatement> stmtList2 = parser2.parseStatementList();
|
||||
variableCnt.set(0);
|
||||
arrayContainerByOperatorCnt.set(0);
|
||||
resultExpr1Ref.set(null);
|
||||
stmtList2.get(0).accept(visitor);
|
||||
Assert.assertEquals(0, variableCnt.get());
|
||||
Assert.assertEquals(1, arrayContainerByOperatorCnt.get());
|
||||
Assert.assertNull(resultExpr1Ref.get());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,33 @@
|
|||
select a from test
|
||||
-- origin
|
||||
-- ${if(gFinPeriod="季度","/*","")}
|
||||
-- origin end
|
||||
-- new
|
||||
/*
|
||||
-- new end
|
||||
where 1=1
|
||||
-- origin
|
||||
-- ${if(gFinPeriod="季度","","*/")}
|
||||
-- origin end
|
||||
-- new
|
||||
*/
|
||||
--new end
|
||||
--------------------
|
||||
SELECT a
|
||||
FROM test -- origin
|
||||
-- ${if(gFinPeriod="季度","/*","")}
|
||||
-- origin end
|
||||
-- new
|
||||
/*
|
||||
-- new end
|
||||
where 1=1
|
||||
-- origin
|
||||
-- ${if(gFinPeriod="季度","","*/")}
|
||||
-- origin end
|
||||
-- new
|
||||
*/
|
||||
-- new end
|
||||
------------------------------------------------------------------------------------------------------------------------
|
||||
SELECT sum(ret_amount) AS '应收金额'
|
||||
FROM test.test
|
||||
GROUP BY a ORDER BY '应收金额' DESC;
|
||||
|
|
|
|||
Loading…
Reference in New Issue