mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #1179 from zhujunxxxxx/master
增加MySQL存储过程语法中定义异常语法的识别(区别于上次提交,上次是异常处理,这次是异常定义)
This commit is contained in:
+66
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 1999-2101 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.sql.dialect.mysql.ast.clause;
|
||||
|
||||
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStatementImpl;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
/**
|
||||
*
|
||||
* @author zhujun [455910092@qq.com]
|
||||
* @date 2016-04-14
|
||||
*/
|
||||
public class MySqlDeclareConditionStatement extends MySqlStatementImpl{
|
||||
|
||||
/*
|
||||
DECLARE condition_name CONDITION FOR condition_value
|
||||
|
||||
condition_value:
|
||||
SQLSTATE [VALUE] sqlstate_value
|
||||
| mysql_error_code
|
||||
*/
|
||||
|
||||
//condition_name
|
||||
private String conditionName;
|
||||
//sp statement
|
||||
private ConditionValue conditionValue;
|
||||
|
||||
public String getConditionName() {
|
||||
return conditionName;
|
||||
}
|
||||
|
||||
public void setConditionName(String conditionName) {
|
||||
this.conditionName = conditionName;
|
||||
}
|
||||
|
||||
public ConditionValue getConditionValue() {
|
||||
return conditionValue;
|
||||
}
|
||||
|
||||
public void setConditionValue(ConditionValue conditionValue) {
|
||||
this.conditionValue = conditionValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
// TODO Auto-generated method stub
|
||||
visitor.visit(this);
|
||||
visitor.endVisit(this);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -92,6 +92,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue.ConditionTy
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement.MySqlWhenStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCursorDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareConditionStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareHandlerStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlHandlerType;
|
||||
@@ -179,7 +180,6 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTriggersStatem
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowVariantsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnlockTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
@@ -3249,15 +3249,18 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
int markBp = lexer.bp();
|
||||
lexer.nextToken();
|
||||
lexer.nextToken();
|
||||
System.out.println(lexer.token());
|
||||
if (lexer.token() == Token.CURSOR)// cursor declare statement
|
||||
{
|
||||
lexer.reset(markBp, markChar, Token.DECLARE);
|
||||
statementList.add(this.parseCursorDeclare());
|
||||
} else if (lexer.token() == Token.HANDLER) {
|
||||
//DECLARE处理程序 [add by zhujun 2016-04-16]
|
||||
//DECLARE异常处理程序 [add by zhujun 2016-04-16]
|
||||
lexer.reset(markBp, markChar, Token.DECLARE);
|
||||
statementList.add(this.parseDeclareHandler());
|
||||
} else if (lexer.token() == Token.CONDITION) {
|
||||
//DECLARE异常 [add by zhujun 2016-04-17]
|
||||
lexer.reset(markBp, markChar, Token.DECLARE);
|
||||
statementList.add(this.parseDeclareCondition());
|
||||
} else {
|
||||
lexer.reset(markBp, markChar, Token.DECLARE);
|
||||
statementList.add(this.parseDeclare());
|
||||
@@ -3789,7 +3792,6 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
accept(Token.COMMA);
|
||||
continue;
|
||||
} else if (lexer.token() != Token.EOF) {
|
||||
//TODO
|
||||
break;
|
||||
} else {
|
||||
throw new ParserException("declare handle not eof");
|
||||
@@ -3805,4 +3807,48 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
/**
|
||||
* zhujun [455910092@qq.com]
|
||||
* 2016-04-17
|
||||
* 定义条件
|
||||
* @return
|
||||
*/
|
||||
public MySqlDeclareConditionStatement parseDeclareCondition() {
|
||||
/*
|
||||
DECLARE condition_name CONDITION FOR condition_value
|
||||
|
||||
condition_value:
|
||||
SQLSTATE [VALUE] sqlstate_value
|
||||
| mysql_error_code
|
||||
*/
|
||||
MySqlDeclareConditionStatement stmt = new MySqlDeclareConditionStatement();
|
||||
accept(Token.DECLARE);
|
||||
|
||||
stmt.setConditionName(exprParser.name().toString());
|
||||
|
||||
accept(Token.CONDITION);
|
||||
|
||||
accept(Token.FOR);
|
||||
|
||||
String tokenName = lexer.stringVal();
|
||||
ConditionValue condition = new ConditionValue();
|
||||
if(tokenName.equalsIgnoreCase("SQLSTATE")) { //for SQLSTATE (SQLSTATE '10001')
|
||||
condition.setType(ConditionType.SQLSTATE);
|
||||
lexer.nextToken();
|
||||
condition.setValue(exprParser.name().toString());
|
||||
} else if (lexer.token() == Token.LITERAL_INT) {
|
||||
condition.setType(ConditionType.MYSQL_ERROR_CODE);
|
||||
condition.setValue(lexer.integerValue().toString());
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
throw new ParserException("declare condition grammer error.");
|
||||
}
|
||||
|
||||
stmt.setConditionValue(condition);
|
||||
|
||||
accept(Token.SEMI);
|
||||
|
||||
return stmt;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement.MySqlWhenStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCursorDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareConditionStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareHandlerStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlIterateStatement;
|
||||
@@ -586,5 +587,9 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
boolean visit(MySqlDeclareHandlerStatement x);
|
||||
|
||||
void endVisit(MySqlDeclareHandlerStatement x);
|
||||
|
||||
boolean visit(MySqlDeclareConditionStatement x);
|
||||
|
||||
void endVisit(MySqlDeclareConditionStatement x);
|
||||
|
||||
} //
|
||||
|
||||
@@ -26,6 +26,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement.MySqlWhenStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCursorDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareConditionStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareHandlerStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlIterateStatement;
|
||||
@@ -1292,4 +1293,16 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlDeclareConditionStatement x) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlDeclareConditionStatement x) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
} //
|
||||
|
||||
@@ -59,6 +59,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue.ConditionTy
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement.MySqlWhenStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCursorDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareConditionStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareHandlerStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlIterateStatement;
|
||||
@@ -3429,7 +3430,6 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlDeclareHandlerStatement x) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
print0(ucase ? "DECLARE " : "declare ");
|
||||
print0(ucase ? x.getHandleType().toString().toUpperCase() : x.getHandleType().toString().toLowerCase());
|
||||
@@ -3458,7 +3458,28 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlDeclareHandlerStatement x) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlDeclareConditionStatement x) {
|
||||
print0(ucase ? "DECLARE " : "declare ");
|
||||
print0(x.getConditionName());
|
||||
print0(ucase ? " CONDITION FOR " : " condition for ");
|
||||
|
||||
if (x.getConditionValue().getType() == ConditionType.SQLSTATE) {
|
||||
print0(ucase ? "SQLSTATE " : "sqlstate ");
|
||||
print0(x.getConditionValue().getValue());
|
||||
} else {
|
||||
print0(x.getConditionValue().getValue());
|
||||
}
|
||||
|
||||
println();
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlDeclareConditionStatement x) {
|
||||
|
||||
}
|
||||
} //
|
||||
|
||||
@@ -39,6 +39,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement.MySqlWhenStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCursorDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareConditionStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareHandlerStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlIterateStatement;
|
||||
@@ -1401,4 +1402,16 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlDeclareConditionStatement x) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlDeclareConditionStatement x) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,6 +157,7 @@ public class Keywords {
|
||||
map.put("SQLWARNING", Token.SQLWARNING);
|
||||
map.put("SQLEXCEPTION", Token.SQLEXCEPTION);
|
||||
map.put("FOUND", Token.FOUND);
|
||||
map.put("CONDITION", Token.CONDITION);
|
||||
|
||||
DEFAULT_KEYWORDS = new Keywords(map);
|
||||
}
|
||||
|
||||
@@ -145,6 +145,7 @@ public enum Token {
|
||||
SQLWARNING("SQLWARNING"),
|
||||
SQLEXCEPTION("SQLEXCEPTION"),
|
||||
FOUND("FOUND"),
|
||||
CONDITION("CONDITION"),
|
||||
|
||||
//postgresql
|
||||
WINDOW("WINDOW"),
|
||||
|
||||
Reference in New Issue
Block a user