improve sql parser

This commit is contained in:
wenshao
2013-08-04 13:01:45 +08:00
parent 6de2b73fe1
commit 4d8548ba8b
38 changed files with 1019 additions and 190 deletions
@@ -0,0 +1,39 @@
/*
* Copyright 1999-2011 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 java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlAlterUserStatement extends MySqlStatementImpl {
private final List<SQLExpr> users = new ArrayList<SQLExpr>();
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, users);
}
visitor.endVisit(this);
}
public List<SQLExpr> getUsers() {
return users;
}
}
@@ -1,3 +1,18 @@
/*
* Copyright 1999-2011 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 java.util.ArrayList;
@@ -45,6 +45,7 @@ public class MySqlCreateUserStatement extends MySqlStatementImpl {
public static class UserSpecification extends MySqlObjectImpl {
private SQLExpr user;
private boolean passwordHash = false;
private SQLExpr password;
private SQLExpr authPlugin;
@@ -56,6 +57,14 @@ public class MySqlCreateUserStatement extends MySqlStatementImpl {
this.user = user;
}
public boolean isPasswordHash() {
return passwordHash;
}
public void setPasswordHash(boolean passwordHash) {
this.passwordHash = passwordHash;
}
public SQLExpr getPassword() {
return password;
}
@@ -406,7 +406,7 @@ public class MySqlExprParser extends SQLExprParser {
lexer.nextToken();
acceptIdentifier("LANGUAGE");
acceptIdentifier("MODE");
if (identifierEquals("WITH")) {
if (lexer.token() == Token.WITH) {
lexer.nextToken();
acceptIdentifier("QUERY");
acceptIdentifier("EXPANSION");
@@ -421,7 +421,7 @@ public class MySqlExprParser extends SQLExprParser {
} else {
throw new ParserException("TODO");
}
} else if (identifierEquals("WITH")) {
} else if (lexer.token() == Token.WITH) {
throw new ParserException("TODO");
}
@@ -242,7 +242,7 @@ public class MySqlSelectParser extends SQLSelectParser {
lexer.nextToken();
}
if (identifierEquals("WITH")) {
if (lexer.token() == Token.WITH) {
lexer.nextToken();
acceptIdentifier("ROLLUP");
@@ -68,6 +68,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTa
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterUserStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAnalyzeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement;
@@ -436,15 +437,14 @@ public class MySqlStatementParser extends SQLStatementParser {
if (identifierEquals("PASSWORD")) {
lexer.nextToken();
userSpec.setPasswordHash(true);
}
SQLCharExpr password = (SQLCharExpr) this.exprParser.expr();
userSpec.setPassword(password);
} else if (lexer.token() == Token.WITH) {
lexer.nextToken();
SQLCharExpr text = (SQLCharExpr) this.exprParser.expr();
userSpec.setAuthPlugin(text);
userSpec.setAuthPlugin(this.exprParser.expr());
}
}
@@ -500,7 +500,7 @@ public class MySqlStatementParser extends SQLStatementParser {
MySqlAnalyzeStatement stmt = new MySqlAnalyzeStatement();
List<SQLName> names = new ArrayList<SQLName>();
this.exprParser.names(names, stmt);
for (SQLName name : names) {
stmt.getTableSources().add(new SQLExprTableSource(name));
}
@@ -1450,7 +1450,7 @@ public class MySqlStatementParser extends SQLStatementParser {
MySqlStartTransactionStatement stmt = new MySqlStartTransactionStatement();
if (identifierEquals("WITH")) {
if (lexer.token() == Token.WITH) {
lexer.nextToken();
acceptIdentifier("CONSISTENT");
acceptIdentifier("SNAPSHOT");
@@ -2087,6 +2087,10 @@ public class MySqlStatementParser extends SQLStatementParser {
public SQLStatement parseAlter() {
accept(Token.ALTER);
if (lexer.token() == Token.USER) {
return parseAlterUser();
}
boolean ignore = false;
@@ -2536,6 +2540,26 @@ public class MySqlStatementParser extends SQLStatementParser {
}
}
public MySqlAlterUserStatement parseAlterUser() {
accept(Token.USER);
MySqlAlterUserStatement stmt = new MySqlAlterUserStatement();
for (;;) {
SQLExpr user = this.exprParser.expr();
acceptIdentifier("PASSWORD");
acceptIdentifier("EXPIRE");
stmt.getUsers().add(user);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
return stmt;
}
public MySqlExprParser getExprParser() {
return (MySqlExprParser) exprParser;
}
@@ -40,6 +40,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTa
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterUserStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAnalyzeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement;
@@ -557,4 +558,8 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlAnalyzeStatement x);
void endVisit(MySqlAnalyzeStatement x);
boolean visit(MySqlAlterUserStatement x);
void endVisit(MySqlAlterUserStatement x);
} //
@@ -40,6 +40,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTa
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterUserStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAnalyzeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement;
@@ -1236,4 +1237,14 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements MySq
public void endVisit(MySqlAnalyzeStatement x) {
}
@Override
public boolean visit(MySqlAlterUserStatement x) {
return true;
}
@Override
public void endVisit(MySqlAlterUserStatement x) {
}
} //
@@ -62,6 +62,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTa
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterUserStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAnalyzeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement;
@@ -1454,6 +1455,9 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
if (x.getPassword() != null) {
print(" IDENTIFIED BY ");
if (x.isPasswordHash()) {
print("PASSWORD ");
}
x.getPassword().accept(this);
}
@@ -3017,5 +3021,21 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlAnalyzeStatement x) {
}
@Override
public boolean visit(MySqlAlterUserStatement x) {
print("ALTER USER");
for (SQLExpr user : x.getUsers()) {
print(' ');
user.accept(this);
print(" PASSWORD EXPIRE");
}
return false;
}
@Override
public void endVisit(MySqlAlterUserStatement x) {
}
} //
@@ -53,6 +53,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTa
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterUserStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAnalyzeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement;
@@ -515,7 +516,7 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
@Override
public boolean visit(MySqlCreateUserStatement x) {
return true;
return false;
}
@Override
@@ -1319,5 +1320,15 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlAnalyzeStatement x) {
}
@Override
public boolean visit(MySqlAlterUserStatement x) {
return true;
}
@Override
public void endVisit(MySqlAlterUserStatement x) {
}
}