mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve sqlwall and parser
This commit is contained in:
@@ -110,6 +110,8 @@ public class MySqlLexer extends Lexer {
|
||||
if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
|
||||
return;
|
||||
}
|
||||
|
||||
endOfComment = isEOF();
|
||||
|
||||
if (!isAllowComment() && (isEOF() || !isSafeComment(stringVal))) {
|
||||
throw new NotAllowCommentException();
|
||||
@@ -448,6 +450,8 @@ public class MySqlLexer extends Lexer {
|
||||
return;
|
||||
}
|
||||
|
||||
endOfComment = isEOF();
|
||||
|
||||
if (!isHint && !isAllowComment() && (isEOF() || !isSafeComment(stringVal))) {
|
||||
throw new NotAllowCommentException();
|
||||
}
|
||||
@@ -489,6 +493,8 @@ public class MySqlLexer extends Lexer {
|
||||
return;
|
||||
}
|
||||
|
||||
endOfComment = isEOF();
|
||||
|
||||
if (!isAllowComment() && (isEOF() || !isSafeComment(stringVal))) {
|
||||
throw new NotAllowCommentException();
|
||||
}
|
||||
|
||||
@@ -136,6 +136,7 @@ public class OdpsLexer extends Lexer {
|
||||
stringVal = subString(mark + 1, bufPos);
|
||||
token = Token.LINE_COMMENT;
|
||||
hasComment = true;
|
||||
endOfComment = isEOF();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,6 +274,7 @@ public class OracleLexer extends Lexer {
|
||||
stringVal = subString(mark + 1, bufPos);
|
||||
token = Token.LINE_COMMENT;
|
||||
hasComment = true;
|
||||
endOfComment = isEOF();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,6 +149,7 @@ public class SQLServerLexer extends Lexer {
|
||||
stringVal = subString(mark + 1, bufPos);
|
||||
token = Token.LINE_COMMENT;
|
||||
hasComment = true;
|
||||
endOfComment = isEOF();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,13 +64,15 @@ public class Lexer {
|
||||
/*
|
||||
* anti sql injection
|
||||
*/
|
||||
private boolean allowComment = true;
|
||||
private boolean allowComment = true;
|
||||
|
||||
private int varIndex = -1;
|
||||
private int varIndex = -1;
|
||||
|
||||
protected CommentHandler commentHandler;
|
||||
|
||||
protected boolean hasComment = false;
|
||||
|
||||
protected boolean hasComment = false;
|
||||
|
||||
protected boolean endOfComment = false;
|
||||
|
||||
public Lexer(String input){
|
||||
this(input, true);
|
||||
@@ -1058,4 +1060,9 @@ public class Lexer {
|
||||
pos = text.length();
|
||||
this.token = Token.EOF;
|
||||
}
|
||||
|
||||
public boolean isEndOfComment() {
|
||||
return endOfComment;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -169,6 +169,9 @@ public class SQLParser {
|
||||
} else if (lexer.token() == Token.USER) {
|
||||
alias = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token() == Token.END) {
|
||||
alias = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
switch (lexer.token()) {
|
||||
|
||||
@@ -604,6 +604,7 @@ public abstract class WallProvider {
|
||||
final List<Violation> violations = new ArrayList<Violation>();
|
||||
List<SQLStatement> statementList = new ArrayList<SQLStatement>();
|
||||
boolean syntaxError = false;
|
||||
boolean endOfComment = false;
|
||||
try {
|
||||
SQLStatementParser parser = createParser(sql);
|
||||
parser.getLexer().setCommentHandler(WallCommentHandler.instance);
|
||||
@@ -619,10 +620,11 @@ public abstract class WallProvider {
|
||||
parser.parseStatementList(statementList);
|
||||
|
||||
final Token lastToken = parser.getLexer().token();
|
||||
if (lastToken != Token.EOF) {
|
||||
if (lastToken != Token.EOF && config.isStrictSyntaxCheck()) {
|
||||
violations.add(new IllegalSQLObjectViolation(ErrorCode.SYNTAX_ERROR, "not terminal sql, token "
|
||||
+ lastToken, sql));
|
||||
}
|
||||
endOfComment = parser.getLexer().isEndOfComment();
|
||||
} catch (NotAllowCommentException e) {
|
||||
violations.add(new IllegalSQLObjectViolation(ErrorCode.COMMENT_STATEMENT_NOT_ALLOW, "comment not allow", sql));
|
||||
incrementCommentDeniedCount();
|
||||
@@ -643,6 +645,9 @@ public abstract class WallProvider {
|
||||
}
|
||||
|
||||
WallVisitor visitor = createWallVisitor();
|
||||
if (!endOfComment) {
|
||||
visitor.getConfig().setSelectUnionCheck(false);
|
||||
}
|
||||
|
||||
if (statementList.size() > 0) {
|
||||
boolean lastIsHint = false;
|
||||
|
||||
Reference in New Issue
Block a user