refactor sql parser.

This commit is contained in:
wenshao
2017-09-18 11:25:40 +08:00
parent 3c98a1eee5
commit 8343ca08c4
29 changed files with 201 additions and 499 deletions
@@ -1,55 +0,0 @@
/*
* Copyright 1999-2017 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.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlSetCharSetStatement extends MySqlStatementImpl {
private boolean isDefault = false;
private String charSet;
private String collate;
public void accept0(MySqlASTVisitor visitor) {
// visitor.visit(this);
// visitor.endVisit(this);
}
public String getCharSet() {
return charSet;
}
public void setCharSet(String charSet) {
this.charSet = charSet;
}
public String getCollate() {
return collate;
}
public void setCollate(String collate) {
this.collate = collate;
}
public boolean isDefault() {
return isDefault;
}
public void setDefault(boolean isDefault) {
this.isDefault = isDefault;
}
}
@@ -1,55 +0,0 @@
/*
* Copyright 1999-2017 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.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlSetNamesStatement extends MySqlStatementImpl {
private boolean isDefault = false;
private String charSet;
private String collate;
public void accept0(MySqlASTVisitor visitor) {
// visitor.visit(this);
// visitor.endVisit(this);
}
public String getCharSet() {
return charSet;
}
public void setCharSet(String charSet) {
this.charSet = charSet;
}
public String getCollate() {
return collate;
}
public void setCollate(String collate) {
this.collate = collate;
}
public boolean isDefault() {
return isDefault;
}
public void setDefault(boolean isDefault) {
this.isDefault = isDefault;
}
}
@@ -1,58 +0,0 @@
/*
* Copyright 1999-2017 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.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlSetPasswordStatement extends MySqlStatementImpl {
private SQLName user;
private SQLExpr password;
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, user);
acceptChild(visitor, password);
}
visitor.endVisit(this);
}
public SQLName getUser() {
return user;
}
public void setUser(SQLName user) {
if (user != null) {
user.setParent(this);
}
this.user = user;
}
public SQLExpr getPassword() {
return password;
}
public void setPassword(SQLExpr password) {
if (password != null) {
password.setParent(this);
}
this.password = password;
}
}
@@ -39,6 +39,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement
import com.alibaba.druid.sql.parser.*;
import com.alibaba.druid.util.FnvHash;
import com.alibaba.druid.util.JdbcConstants;
import jdk.nashorn.internal.runtime.regexp.joni.constants.OPCode;
import java.util.ArrayList;
import java.util.List;
@@ -2289,18 +2290,23 @@ public class MySqlStatementParser extends SQLStatementParser {
public SQLStatement parseSet() {
accept(Token.SET);
if (lexer.identifierEquals("PASSWORD")) {
if (lexer.identifierEquals(FnvHash.Constants.PASSWORD)) {
lexer.nextToken();
MySqlSetPasswordStatement stmt = new MySqlSetPasswordStatement();
SQLSetStatement stmt = new SQLSetStatement();
stmt.setDbType(dbType);
stmt.setOption(SQLSetStatement.Option.PASSWORD);
SQLExpr user = null;
if (lexer.token() == Token.FOR) {
lexer.nextToken();
stmt.setUser(this.exprParser.name());
user = this.exprParser.name();
}
accept(Token.EQ);
stmt.setPassword(this.exprParser.expr());
SQLExpr password = this.exprParser.expr();
stmt.set(user, password);
return stmt;
}
@@ -403,10 +403,6 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
void endVisit(MySqlOptimizeStatement x);
boolean visit(MySqlSetPasswordStatement x);
void endVisit(MySqlSetPasswordStatement x);
boolean visit(MySqlHintStatement x);
void endVisit(MySqlHintStatement x);
@@ -959,16 +959,6 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
}
@Override
public boolean visit(MySqlSetPasswordStatement x) {
return true;
}
@Override
public void endVisit(MySqlSetPasswordStatement x) {
}
@Override
public boolean visit(MySqlHintStatement x) {
return true;
@@ -2956,28 +2956,31 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
}
@Override
public boolean visit(MySqlSetPasswordStatement x) {
print0(ucase ? "SET PASSWORD " : "set password ");
if (x.getUser() != null) {
print0(ucase ? "FOR " : "for ");
x.getUser().accept(this);
public boolean visit(SQLSetStatement x) {
boolean printSet = x.getAttribute("parser.set") == Boolean.TRUE || !JdbcConstants.ORACLE.equals(dbType);
if (printSet) {
print0(ucase ? "SET " : "set ");
}
SQLSetStatement.Option option = x.getOption();
if (option != null) {
print(option.name());
print(' ');
}
print0("= ");
if (x.getPassword() != null) {
x.getPassword().accept(this);
if (option == SQLSetStatement.Option.PASSWORD) {
print0("FOR ");
}
printAndAccept(x.getItems(), ", ");
if (x.getHints() != null && x.getHints().size() > 0) {
print(' ');
printAndAccept(x.getHints(), " ");
}
return false;
}
@Override
public void endVisit(MySqlSetPasswordStatement x) {
}
@Override
public boolean visit(MySqlHintStatement x) {
List<SQLCommentHint> hints = x.getHints();
@@ -1095,16 +1095,6 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
}
@Override
public boolean visit(MySqlSetPasswordStatement x) {
return false;
}
@Override
public void endVisit(MySqlSetPasswordStatement x) {
}
@Override
public boolean visit(MySqlHintStatement x) {
return true;