diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/MySqlObject.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/MySqlObject.java new file mode 100644 index 0000000000..c2c6559c2d --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/MySqlObject.java @@ -0,0 +1,9 @@ +package com.alibaba.druid.sql.dialect.mysql.ast; + +import com.alibaba.druid.sql.ast.SQLObject; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor; + + +public interface MySqlObject extends SQLObject { + void accept0(MySqlASTVisitor visitor); +} diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/MySqlObjectImpl.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/MySqlObjectImpl.java new file mode 100644 index 0000000000..4b95e609c9 --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/MySqlObjectImpl.java @@ -0,0 +1,36 @@ +/* + * 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; + +import com.alibaba.druid.sql.ast.SQLObjectImpl; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor; +import com.alibaba.druid.sql.visitor.SQLASTVisitor; + +public abstract class MySqlObjectImpl extends SQLObjectImpl implements MySqlObject { + + private static final long serialVersionUID = 1L; + + @Override + protected void accept0(SQLASTVisitor visitor) { + if (visitor instanceof MySqlASTVisitor) { + accept0((MySqlASTVisitor) visitor); + } else { + throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName()); + } + } + + public abstract void accept0(MySqlASTVisitor visitor); +} diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlCreateUserStatement.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlCreateUserStatement.java new file mode 100644 index 0000000000..996d389b43 --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlCreateUserStatement.java @@ -0,0 +1,73 @@ +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.ast.MySqlObjectImpl; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor; + +public class MySqlCreateUserStatement extends MySqlStatementImpl { + + private static final long serialVersionUID = 1L; + + private List users = new ArrayList(2); + + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } + + @Override + public void accept0(MySqlASTVisitor visitor) { + if (visitor.visit(this)) { + acceptChild(visitor, users); + } + } + + public static class UserSpecification extends MySqlObjectImpl { + + private static final long serialVersionUID = 1L; + + private SQLExpr user; + private SQLExpr password; + private SQLExpr authPlugin; + + public SQLExpr getUser() { + return user; + } + + public void setUser(SQLExpr user) { + this.user = user; + } + + public SQLExpr getPassword() { + return password; + } + + public void setPassword(SQLExpr password) { + this.password = password; + } + + public SQLExpr getAuthPlugin() { + return authPlugin; + } + + public void setAuthPlugin(SQLExpr authPlugin) { + this.authPlugin = authPlugin; + } + + @Override + public void accept0(MySqlASTVisitor visitor) { + if (visitor.visit(this)) { + acceptChild(visitor, user); + acceptChild(visitor, password); + acceptChild(visitor, authPlugin); + } + } + + } +} diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlLexer.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlLexer.java index 9a8bb0b9fd..74bfba168d 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlLexer.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlLexer.java @@ -104,11 +104,14 @@ public class MySqlLexer extends Lexer { map.put("SOME", Token.SOME); map.put("ANY", Token.ANY); map.put("TRUNCATE", Token.TRUNCATE); + map.put("USER", Token.USER); map.put("TRUE", Token.TRUE); map.put("FALSE", Token.FALSE); map.put("LIMIT", Token.LIMIT); map.put("KILL", Token.KILL); + map.put("IDENTIFIED", Token.IDENTIFIED); + map.put("PASSWORD", Token.PASSWORD); DEFAULT_MYSQL_KEYWORDS = new Keywords(map); } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java index 8fda33806b..c81d98ae86 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java @@ -21,6 +21,8 @@ import com.alibaba.druid.sql.ast.SQLExpr; import com.alibaba.druid.sql.ast.SQLName; import com.alibaba.druid.sql.ast.SQLOrderBy; import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr; import com.alibaba.druid.sql.ast.expr.SQLQueryExpr; import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement; @@ -33,6 +35,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableParser; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; @@ -58,854 +61,903 @@ import com.alibaba.druid.sql.parser.Token; public class MySqlStatementParser extends SQLStatementParser { - public MySqlStatementParser(String sql) throws ParserException { - this(new MySqlLexer(sql)); - this.lexer.nextToken(); - } - - public MySqlStatementParser(Lexer lexer) { - super(lexer); - } - - protected SQLExprParser createExprParser() { - return new MySqlExprParser(lexer); - } - - public SQLCreateTableStatement parseCreateTable() throws ParserException { - MySqlCreateTableParser parser = new MySqlCreateTableParser(lexer); - return parser.parseCrateTable(); - } - - public SQLSelectStatement parseSelect() throws ParserException { - return new SQLSelectStatement(new MySqlSelectParser(lexer).select()); - } - - public SQLDeleteStatement parseDeleteStatement() throws ParserException { - accept(Token.DELETE); - - MySqlDeleteStatement deleteStatement = new MySqlDeleteStatement(); - - if (identifierEquals("LOW_PRIORITY")) { - deleteStatement.setLowPriority(true); - lexer.nextToken(); - } - - if (identifierEquals("QUICK")) { - deleteStatement.setQuick(true); - lexer.nextToken(); - } - - if (identifierEquals("IGNORE")) { - deleteStatement.setIgnore(true); - lexer.nextToken(); - } - - if (lexer.token() == Token.IDENTIFIER) { - exprParser.names(deleteStatement.getTableNames()); - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLTableSource tableSource = createSQLSelectParser() - .parseTableSource(); - deleteStatement.setFrom(tableSource); - } - } else { - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - } - exprParser.names(deleteStatement.getTableNames()); - } - - if (identifierEquals("USING")) { - lexer.nextToken(); - - SQLTableSource tableSource = createSQLSelectParser() - .parseTableSource(); - deleteStatement.setUsing(tableSource); - } - - if (lexer.token() == (Token.WHERE)) { - lexer.nextToken(); - SQLExpr where = this.exprParser.expr(); - deleteStatement.setWhere(where); - } - - if (lexer.token() == (Token.ORDER)) { - SQLOrderBy orderBy = exprParser.parseOrderBy(); - deleteStatement.setOrderBy(orderBy); - } - - if (lexer.token() == Token.LIMIT) { - lexer.nextToken(); - - MySqlSelectQueryBlock.Limit limit = new MySqlSelectQueryBlock.Limit(); - SQLExpr rowCount = exprParser.expr(); - limit.setRowCount(rowCount); - - deleteStatement.setLimit(limit); - } - - return deleteStatement; - } - - public SQLStatement parseCreate() throws ParserException { - accept(Token.CREATE); - - if (lexer.token() == Token.TABLE || identifierEquals("TEMPORARY")) { - MySqlCreateTableParser parser = new MySqlCreateTableParser(lexer); - return parser.parseCrateTable(false); - } - - throw new ParserException("TODO " + lexer.token()); - } - - public SQLStatement parseKill() { - accept(Token.KILL); - - MySqlKillStatement stmt = new MySqlKillStatement(); - - if (identifierEquals("CONNECTION")) { - stmt.setType(MySqlKillStatement.Type.CONNECTION); - lexer.nextToken(); - } else if (identifierEquals("QUERY")) { - stmt.setType(MySqlKillStatement.Type.QUERY); - lexer.nextToken(); - } else { - throw new ParserException("not support kill type " + lexer.token()); - } - - SQLExpr threadId = this.exprParser.expr(); - stmt.setThreadId(threadId); - - return stmt; - } - - public SQLStatement parseBinlog() { - acceptIdentifier("binlog"); - - MySqlBinlogStatement stmt = new MySqlBinlogStatement(); - - SQLExpr expr = this.exprParser.expr(); - stmt.setExpr(expr); - - return stmt; - } - - public SQLStatement parseReset() { - acceptIdentifier("RESET"); - - MySqlResetStatement stmt = new MySqlResetStatement(); - - for (;;) { - if (lexer.token() == Token.IDENTIFIER) { - if (identifierEquals("QUERY")) { - lexer.nextToken(); - acceptIdentifier("CACHE"); - stmt.getOptions().add("QUERY CACHE"); - } else { - stmt.getOptions().add(lexer.stringVal()); - lexer.nextToken(); - } - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } - } - break; - } - - return stmt; - } - - public boolean parseStatementListDialect(List statementList) { - if (lexer.token() == Token.KILL) { - SQLStatement stmt = parseKill(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("PREPARE")) { - MySqlPrepareStatement stmt = parsePrepare(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("EXECUTE")) { - MySqlExecuteStatement stmt = parseExecute(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("LOAD")) { - SQLStatement stmt = parseLoad(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("REPLACE")) { - MySqlReplicateStatement stmt = parseReplicate(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("START")) { - MySqlStartTransactionStatement stmt = parseStart(); - statementList.add(stmt); - return true; - } + public MySqlStatementParser(String sql) throws ParserException{ + this(new MySqlLexer(sql)); + this.lexer.nextToken(); + } + + public MySqlStatementParser(Lexer lexer){ + super(lexer); + } + + protected SQLExprParser createExprParser() { + return new MySqlExprParser(lexer); + } + + public SQLCreateTableStatement parseCreateTable() throws ParserException { + MySqlCreateTableParser parser = new MySqlCreateTableParser(lexer); + return parser.parseCrateTable(); + } + + public SQLSelectStatement parseSelect() throws ParserException { + return new SQLSelectStatement(new MySqlSelectParser(lexer).select()); + } + + public SQLDeleteStatement parseDeleteStatement() throws ParserException { + accept(Token.DELETE); + + MySqlDeleteStatement deleteStatement = new MySqlDeleteStatement(); + + if (identifierEquals("LOW_PRIORITY")) { + deleteStatement.setLowPriority(true); + lexer.nextToken(); + } + + if (identifierEquals("QUICK")) { + deleteStatement.setQuick(true); + lexer.nextToken(); + } + + if (identifierEquals("IGNORE")) { + deleteStatement.setIgnore(true); + lexer.nextToken(); + } + + if (lexer.token() == Token.IDENTIFIER) { + exprParser.names(deleteStatement.getTableNames()); + + if (lexer.token() == Token.FROM) { + lexer.nextToken(); + SQLTableSource tableSource = createSQLSelectParser().parseTableSource(); + deleteStatement.setFrom(tableSource); + } + } else { + if (lexer.token() == Token.FROM) { + lexer.nextToken(); + } + exprParser.names(deleteStatement.getTableNames()); + } + + if (identifierEquals("USING")) { + lexer.nextToken(); + + SQLTableSource tableSource = createSQLSelectParser().parseTableSource(); + deleteStatement.setUsing(tableSource); + } + + if (lexer.token() == (Token.WHERE)) { + lexer.nextToken(); + SQLExpr where = this.exprParser.expr(); + deleteStatement.setWhere(where); + } + + if (lexer.token() == (Token.ORDER)) { + SQLOrderBy orderBy = exprParser.parseOrderBy(); + deleteStatement.setOrderBy(orderBy); + } + + if (lexer.token() == Token.LIMIT) { + lexer.nextToken(); + + MySqlSelectQueryBlock.Limit limit = new MySqlSelectQueryBlock.Limit(); + SQLExpr rowCount = exprParser.expr(); + limit.setRowCount(rowCount); + + deleteStatement.setLimit(limit); + } + + return deleteStatement; + } + + public SQLStatement parseCreate() throws ParserException { + accept(Token.CREATE); + + if (lexer.token() == Token.TABLE || identifierEquals("TEMPORARY")) { + MySqlCreateTableParser parser = new MySqlCreateTableParser(lexer); + return parser.parseCrateTable(false); + } + + if (lexer.token() == Token.USER) { + return parseCreateUser(); + } + + throw new ParserException("TODO " + lexer.token()); + } + + public SQLStatement parseCreateUser() throws ParserException { + accept(Token.USER); + + MySqlCreateUserStatement stmt = new MySqlCreateUserStatement(); + + for (;;) { + MySqlCreateUserStatement.UserSpecification userSpec = new MySqlCreateUserStatement.UserSpecification(); + + SQLCharExpr expr = (SQLCharExpr) this.createExprParser().expr(); + String user = expr.toString(); + if (lexer.token() == Token.VARIANT) { + lexer.nextToken(); + SQLCharExpr expr2 = (SQLCharExpr) this.createExprParser().expr(); + user += '@'; + user += expr2.toString(); + } + userSpec.setUser(new SQLIdentifierExpr(user)); + + if (lexer.token() == Token.IDENTIFIED) { + lexer.nextToken(); + if (lexer.token() == Token.BY) { + lexer.nextToken(); + + if (lexer.token() == Token.PASSWORD) { + lexer.nextToken(); + } + + SQLCharExpr password = (SQLCharExpr) this.createExprParser().expr(); + userSpec.setPassword(password); + } else if (lexer.token() == Token.WITH) { + lexer.nextToken(); + + SQLCharExpr text = (SQLCharExpr) this.createExprParser().expr(); + userSpec.setAuthPlugin(text); + } + } + + stmt.getUsers().add(userSpec); + + if (lexer.token() == Token.COMMA) { + lexer.nextToken(); + continue; + } + + break; + } + + return stmt; + } + + public SQLStatement parseKill() { + accept(Token.KILL); + + MySqlKillStatement stmt = new MySqlKillStatement(); + + if (identifierEquals("CONNECTION")) { + stmt.setType(MySqlKillStatement.Type.CONNECTION); + lexer.nextToken(); + } else if (identifierEquals("QUERY")) { + stmt.setType(MySqlKillStatement.Type.QUERY); + lexer.nextToken(); + } else { + throw new ParserException("not support kill type " + lexer.token()); + } + + SQLExpr threadId = this.exprParser.expr(); + stmt.setThreadId(threadId); + + return stmt; + } + + public SQLStatement parseBinlog() { + acceptIdentifier("binlog"); + + MySqlBinlogStatement stmt = new MySqlBinlogStatement(); + + SQLExpr expr = this.exprParser.expr(); + stmt.setExpr(expr); + + return stmt; + } + + public SQLStatement parseReset() { + acceptIdentifier("RESET"); + + MySqlResetStatement stmt = new MySqlResetStatement(); + + for (;;) { + if (lexer.token() == Token.IDENTIFIER) { + if (identifierEquals("QUERY")) { + lexer.nextToken(); + acceptIdentifier("CACHE"); + stmt.getOptions().add("QUERY CACHE"); + } else { + stmt.getOptions().add(lexer.stringVal()); + lexer.nextToken(); + } + + if (lexer.token() == Token.COMMA) { + lexer.nextToken(); + continue; + } + } + break; + } + + return stmt; + } + + public boolean parseStatementListDialect(List statementList) { + if (lexer.token() == Token.KILL) { + SQLStatement stmt = parseKill(); + statementList.add(stmt); + return true; + } + + if (identifierEquals("PREPARE")) { + MySqlPrepareStatement stmt = parsePrepare(); + statementList.add(stmt); + return true; + } + + if (identifierEquals("EXECUTE")) { + MySqlExecuteStatement stmt = parseExecute(); + statementList.add(stmt); + return true; + } + + if (identifierEquals("LOAD")) { + SQLStatement stmt = parseLoad(); + statementList.add(stmt); + return true; + } + + if (identifierEquals("REPLACE")) { + MySqlReplicateStatement stmt = parseReplicate(); + statementList.add(stmt); + return true; + } + + if (identifierEquals("START")) { + MySqlStartTransactionStatement stmt = parseStart(); + statementList.add(stmt); + return true; + } - if (identifierEquals("COMMIT")) { - MySqlCommitStatement stmt = parseCommit(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("ROLLBACK")) { - MySqlRollbackStatement stmt = parseRollback(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("SHOW")) { - SQLStatement stmt = parseShow(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("BINLOG")) { - SQLStatement stmt = parseBinlog(); - statementList.add(stmt); - return true; - } - - if (identifierEquals("RESET")) { - SQLStatement stmt = parseReset(); - statementList.add(stmt); - return true; - } - - return false; - } - - public SQLStatement parseShow() throws ParserException { - acceptIdentifier("SHOW"); - - if (identifierEquals("FULL")) { - lexer.nextToken(); - acceptIdentifier("COLUMNS"); - - MySqlShowColumnsStatement stmt = parseShowColumns(); - stmt.setFull(true); - - return stmt; - } - - if (identifierEquals("COLUMNS")) { - lexer.nextToken(); - - MySqlShowColumnsStatement stmt = parseShowColumns(); - - return stmt; - } - - if (identifierEquals("TABLES")) { - lexer.nextToken(); - - MySqlShowTablesStatement stmt = parseShowTabless(); - - return stmt; - } - - if (identifierEquals("DATABASES")) { - lexer.nextToken(); - - MySqlShowDatabasesStatement stmt = parseShowDatabases(); - - return stmt; - } - - if (identifierEquals("WARNINGS")) { - lexer.nextToken(); - - MySqlShowWarningsStatement stmt = parseShowWarnings(); - - return stmt; - } - - if (identifierEquals("COUNT")) { - lexer.nextToken(); - accept(Token.LPAREN); - accept(Token.STAR); - accept(Token.RPAREN); - acceptIdentifier("WARNINGS"); - - MySqlShowWarningsStatement stmt = new MySqlShowWarningsStatement(); - stmt.setCount(true); - - return stmt; - } - - if (identifierEquals("STATUS")) { - lexer.nextToken(); + if (identifierEquals("COMMIT")) { + MySqlCommitStatement stmt = parseCommit(); + statementList.add(stmt); + return true; + } + + if (identifierEquals("ROLLBACK")) { + MySqlRollbackStatement stmt = parseRollback(); + statementList.add(stmt); + return true; + } + + if (identifierEquals("SHOW")) { + SQLStatement stmt = parseShow(); + statementList.add(stmt); + return true; + } + + if (identifierEquals("BINLOG")) { + SQLStatement stmt = parseBinlog(); + statementList.add(stmt); + return true; + } + + if (identifierEquals("RESET")) { + SQLStatement stmt = parseReset(); + statementList.add(stmt); + return true; + } + + return false; + } + + public SQLStatement parseShow() throws ParserException { + acceptIdentifier("SHOW"); + + if (identifierEquals("FULL")) { + lexer.nextToken(); + acceptIdentifier("COLUMNS"); + + MySqlShowColumnsStatement stmt = parseShowColumns(); + stmt.setFull(true); + + return stmt; + } + + if (identifierEquals("COLUMNS")) { + lexer.nextToken(); + + MySqlShowColumnsStatement stmt = parseShowColumns(); + + return stmt; + } + + if (identifierEquals("TABLES")) { + lexer.nextToken(); + + MySqlShowTablesStatement stmt = parseShowTabless(); + + return stmt; + } + + if (identifierEquals("DATABASES")) { + lexer.nextToken(); + + MySqlShowDatabasesStatement stmt = parseShowDatabases(); + + return stmt; + } + + if (identifierEquals("WARNINGS")) { + lexer.nextToken(); + + MySqlShowWarningsStatement stmt = parseShowWarnings(); + + return stmt; + } + + if (identifierEquals("COUNT")) { + lexer.nextToken(); + accept(Token.LPAREN); + accept(Token.STAR); + accept(Token.RPAREN); + acceptIdentifier("WARNINGS"); + + MySqlShowWarningsStatement stmt = new MySqlShowWarningsStatement(); + stmt.setCount(true); + + return stmt; + } + + if (identifierEquals("STATUS")) { + lexer.nextToken(); - MySqlShowStatusStatement stmt = parseShowStatus(); - - return stmt; - } + MySqlShowStatusStatement stmt = parseShowStatus(); - if (identifierEquals("GLOBAL")) { - lexer.nextToken(); - - if (identifierEquals("STATUS")) { - lexer.nextToken(); - MySqlShowStatusStatement stmt = parseShowStatus(); - stmt.setGlobal(true); - return stmt; - } - } + return stmt; + } - if (identifierEquals("SESSION")) { - lexer.nextToken(); - - if (identifierEquals("STATUS")) { - lexer.nextToken(); - MySqlShowStatusStatement stmt = parseShowStatus(); - stmt.setSession(true); - return stmt; - } - } + if (identifierEquals("GLOBAL")) { + lexer.nextToken(); - if (identifierEquals("COBAR_STATUS")) { - lexer.nextToken(); - return new CobarShowStatus(); - } + if (identifierEquals("STATUS")) { + lexer.nextToken(); + MySqlShowStatusStatement stmt = parseShowStatus(); + stmt.setGlobal(true); + return stmt; + } + } - throw new ParserException("TODO"); - } + if (identifierEquals("SESSION")) { + lexer.nextToken(); - private MySqlShowStatusStatement parseShowStatus() throws ParserException { - MySqlShowStatusStatement stmt = new MySqlShowStatusStatement(); + if (identifierEquals("STATUS")) { + lexer.nextToken(); + MySqlShowStatusStatement stmt = parseShowStatus(); + stmt.setSession(true); + return stmt; + } + } - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - SQLExpr like = exprParser.expr(); - stmt.setLike(like); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - SQLExpr where = exprParser.expr(); - stmt.setWhere(where); - } - - return stmt; - } - - private MySqlShowWarningsStatement parseShowWarnings() - throws ParserException { - MySqlShowWarningsStatement stmt = new MySqlShowWarningsStatement(); - - if (lexer.token() == Token.LIMIT) { - lexer.nextToken(); - - MySqlSelectQueryBlock.Limit limit = new MySqlSelectQueryBlock.Limit(); - - SQLExpr temp = this.createExprParser().expr(); - if (lexer.token() == (Token.COMMA)) { - limit.setOffset(temp); - lexer.nextToken(); - limit.setRowCount(createExprParser().expr()); - } else if (identifierEquals("OFFSET")) { - limit.setRowCount(temp); - lexer.nextToken(); - limit.setOffset(createExprParser().expr()); - } else { - limit.setRowCount(temp); - } - - stmt.setLimit(limit); - } - - return stmt; - } - - private MySqlShowDatabasesStatement parseShowDatabases() - throws ParserException { - MySqlShowDatabasesStatement stmt = new MySqlShowDatabasesStatement(); - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - SQLExpr like = exprParser.expr(); - stmt.setLike(like); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - SQLExpr where = exprParser.expr(); - stmt.setWhere(where); - } - - return stmt; - } - - private MySqlShowTablesStatement parseShowTabless() throws ParserException { - MySqlShowTablesStatement stmt = new MySqlShowTablesStatement(); - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLName database = exprParser.name(); - stmt.setDatabase(database); - } - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - SQLExpr like = exprParser.expr(); - stmt.setLike(like); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - SQLExpr where = exprParser.expr(); - stmt.setWhere(where); - } - - return stmt; - } - - private MySqlShowColumnsStatement parseShowColumns() throws ParserException { - MySqlShowColumnsStatement stmt = new MySqlShowColumnsStatement(); - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLName table = exprParser.name(); - stmt.setTable(table); - - if (lexer.token() == Token.FROM) { - lexer.nextToken(); - SQLName database = exprParser.name(); - stmt.setDatabase(database); - } - } - - if (lexer.token() == Token.LIKE) { - lexer.nextToken(); - SQLExpr like = exprParser.expr(); - stmt.setLike(like); - } - - if (lexer.token() == Token.WHERE) { - lexer.nextToken(); - SQLExpr where = exprParser.expr(); - stmt.setWhere(where); - } - - return stmt; - } - - public MySqlStartTransactionStatement parseStart() throws ParserException { - acceptIdentifier("START"); - acceptIdentifier("TRANSACTION"); - - MySqlStartTransactionStatement stmt = new MySqlStartTransactionStatement(); - - if (identifierEquals("WITH")) { - lexer.nextToken(); - acceptIdentifier("CONSISTENT"); - acceptIdentifier("SNAPSHOT"); - stmt.setConsistentSnapshot(true); - } - - if (identifierEquals("BEGIN")) { - lexer.nextToken(); - stmt.setBegin(true); - if (identifierEquals("WORK")) { - lexer.nextToken(); - stmt.setWork(true); - } - } - - return stmt; - } - - public MySqlRollbackStatement parseRollback() throws ParserException { - acceptIdentifier("ROLLBACK"); - - MySqlRollbackStatement stmt = new MySqlRollbackStatement(); - - if (identifierEquals("WORK")) { - lexer.nextToken(); - stmt.setWork(true); - } - - if (lexer.token() == Token.AND) { - lexer.nextToken(); - if (lexer.token() == Token.NOT) { - lexer.nextToken(); - acceptIdentifier("CHAIN"); - stmt.setChain(Boolean.FALSE); - } else { - acceptIdentifier("CHAIN"); - stmt.setChain(Boolean.TRUE); - } - } - - return stmt; - } - - public MySqlCommitStatement parseCommit() throws ParserException { - acceptIdentifier("COMMIT"); - - MySqlCommitStatement stmt = new MySqlCommitStatement(); - - if (identifierEquals("WORK")) { - lexer.nextToken(); - stmt.setWork(true); - } - - if (lexer.token() == Token.AND) { - lexer.nextToken(); - if (lexer.token() == Token.NOT) { - lexer.nextToken(); - acceptIdentifier("CHAIN"); - stmt.setChain(Boolean.FALSE); - } else { - acceptIdentifier("CHAIN"); - stmt.setChain(Boolean.TRUE); - } - } - - return stmt; - } - - public MySqlReplicateStatement parseReplicate() throws ParserException { - MySqlReplicateStatement stmt = new MySqlReplicateStatement(); - - acceptIdentifier("REPLACE"); - - if (identifierEquals("LOW_PRIORITY")) { - stmt.setLowPriority(true); - lexer.nextToken(); - } - - if (identifierEquals("DELAYED")) { - stmt.setDelayed(true); - lexer.nextToken(); - } - - if (lexer.token() == Token.INTO) { - lexer.nextToken(); - } - - SQLName tableName = exprParser.name(); - stmt.setTableName(tableName); - - if (lexer.token() == Token.VALUES || identifierEquals("VALUE")) { - lexer.nextToken(); - - for (;;) { - accept(Token.LPAREN); - SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(); - this.exprParser.exprList(values.getValues()); - stmt.getValuesList().add(values); - accept(Token.RPAREN); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } else { - break; - } - } - } else if (lexer.token() == Token.SELECT) { - SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr(); - stmt.setQuery(queryExpr); - } else if (lexer.token() == Token.SET) { - lexer.nextToken(); - for (;;) { - SQLUpdateSetItem item = new SQLUpdateSetItem(); - item.setColumn(this.exprParser.name()); - accept(Token.EQ); - item.setValue(this.exprParser.expr()); - - stmt.getSetItems().add(item); - - if (lexer.token() == (Token.COMMA)) { - lexer.nextToken(); - continue; - } - - break; - } - } - - return stmt; - } - - protected SQLStatement parseLoad() throws ParserException { - acceptIdentifier("LOAD"); - - if (identifierEquals("DATA")) { - SQLStatement stmt = parseLoadDataInFile(); - return stmt; - } - - if (identifierEquals("XML")) { - SQLStatement stmt = parseLoadXml(); - return stmt; - } - - throw new ParserException("TODO"); - } - - protected MySqlLoadXmlStatement parseLoadXml() throws ParserException { - acceptIdentifier("XML"); - - MySqlLoadXmlStatement stmt = new MySqlLoadXmlStatement(); - - if (identifierEquals("LOW_PRIORITY")) { - stmt.setLowPriority(true); - lexer.nextToken(); - } - - if (identifierEquals("CONCURRENT")) { - stmt.setConcurrent(true); - lexer.nextToken(); - } - - if (identifierEquals("LOCAL")) { - stmt.setLocal(true); - lexer.nextToken(); - } - - acceptIdentifier("INFILE"); - - SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr(); - stmt.setFileName(fileName); - - if (identifierEquals("REPLACE")) { - stmt.setReplicate(true); - lexer.nextToken(); - } - - if (identifierEquals("IGNORE")) { - stmt.setIgnore(true); - lexer.nextToken(); - } - - accept(Token.INTO); - accept(Token.TABLE); - - SQLName tableName = exprParser.name(); - stmt.setTableName(tableName); - - if (identifierEquals("CHARACTER")) { - lexer.nextToken(); - accept(Token.SET); - - if (lexer.token() != Token.LITERAL_CHARS) { - throw new ParserException("syntax error, illegal charset"); - } - - String charset = lexer.stringVal(); - lexer.nextToken(); - stmt.setCharset(charset); - } - - if (identifierEquals("ROWS")) { - lexer.nextToken(); - acceptIdentifier("IDENTIFIED"); - accept(Token.BY); - SQLExpr rowsIdentifiedBy = exprParser.expr(); - stmt.setRowsIdentifiedBy(rowsIdentifiedBy); - } + if (identifierEquals("COBAR_STATUS")) { + lexer.nextToken(); + return new CobarShowStatus(); + } - if (identifierEquals("IGNORE")) { - throw new ParserException("TODO"); - } + throw new ParserException("TODO"); + } - if (lexer.token() == Token.SET) { - throw new ParserException("TODO"); - } + private MySqlShowStatusStatement parseShowStatus() throws ParserException { + MySqlShowStatusStatement stmt = new MySqlShowStatusStatement(); - return stmt; - } + if (lexer.token() == Token.LIKE) { + lexer.nextToken(); + SQLExpr like = exprParser.expr(); + stmt.setLike(like); + } - protected MySqlLoadDataInFileStatement parseLoadDataInFile() - throws ParserException { - acceptIdentifier("DATA"); + if (lexer.token() == Token.WHERE) { + lexer.nextToken(); + SQLExpr where = exprParser.expr(); + stmt.setWhere(where); + } + + return stmt; + } + + private MySqlShowWarningsStatement parseShowWarnings() throws ParserException { + MySqlShowWarningsStatement stmt = new MySqlShowWarningsStatement(); + + if (lexer.token() == Token.LIMIT) { + lexer.nextToken(); + + MySqlSelectQueryBlock.Limit limit = new MySqlSelectQueryBlock.Limit(); + + SQLExpr temp = this.createExprParser().expr(); + if (lexer.token() == (Token.COMMA)) { + limit.setOffset(temp); + lexer.nextToken(); + limit.setRowCount(createExprParser().expr()); + } else if (identifierEquals("OFFSET")) { + limit.setRowCount(temp); + lexer.nextToken(); + limit.setOffset(createExprParser().expr()); + } else { + limit.setRowCount(temp); + } + + stmt.setLimit(limit); + } + + return stmt; + } + + private MySqlShowDatabasesStatement parseShowDatabases() throws ParserException { + MySqlShowDatabasesStatement stmt = new MySqlShowDatabasesStatement(); + + if (lexer.token() == Token.LIKE) { + lexer.nextToken(); + SQLExpr like = exprParser.expr(); + stmt.setLike(like); + } + + if (lexer.token() == Token.WHERE) { + lexer.nextToken(); + SQLExpr where = exprParser.expr(); + stmt.setWhere(where); + } + + return stmt; + } + + private MySqlShowTablesStatement parseShowTabless() throws ParserException { + MySqlShowTablesStatement stmt = new MySqlShowTablesStatement(); + + if (lexer.token() == Token.FROM) { + lexer.nextToken(); + SQLName database = exprParser.name(); + stmt.setDatabase(database); + } + + if (lexer.token() == Token.LIKE) { + lexer.nextToken(); + SQLExpr like = exprParser.expr(); + stmt.setLike(like); + } + + if (lexer.token() == Token.WHERE) { + lexer.nextToken(); + SQLExpr where = exprParser.expr(); + stmt.setWhere(where); + } + + return stmt; + } + + private MySqlShowColumnsStatement parseShowColumns() throws ParserException { + MySqlShowColumnsStatement stmt = new MySqlShowColumnsStatement(); + + if (lexer.token() == Token.FROM) { + lexer.nextToken(); + SQLName table = exprParser.name(); + stmt.setTable(table); + + if (lexer.token() == Token.FROM) { + lexer.nextToken(); + SQLName database = exprParser.name(); + stmt.setDatabase(database); + } + } + + if (lexer.token() == Token.LIKE) { + lexer.nextToken(); + SQLExpr like = exprParser.expr(); + stmt.setLike(like); + } + + if (lexer.token() == Token.WHERE) { + lexer.nextToken(); + SQLExpr where = exprParser.expr(); + stmt.setWhere(where); + } + + return stmt; + } + + public MySqlStartTransactionStatement parseStart() throws ParserException { + acceptIdentifier("START"); + acceptIdentifier("TRANSACTION"); + + MySqlStartTransactionStatement stmt = new MySqlStartTransactionStatement(); + + if (identifierEquals("WITH")) { + lexer.nextToken(); + acceptIdentifier("CONSISTENT"); + acceptIdentifier("SNAPSHOT"); + stmt.setConsistentSnapshot(true); + } + + if (identifierEquals("BEGIN")) { + lexer.nextToken(); + stmt.setBegin(true); + if (identifierEquals("WORK")) { + lexer.nextToken(); + stmt.setWork(true); + } + } + + return stmt; + } + + public MySqlRollbackStatement parseRollback() throws ParserException { + acceptIdentifier("ROLLBACK"); + + MySqlRollbackStatement stmt = new MySqlRollbackStatement(); + + if (identifierEquals("WORK")) { + lexer.nextToken(); + stmt.setWork(true); + } + + if (lexer.token() == Token.AND) { + lexer.nextToken(); + if (lexer.token() == Token.NOT) { + lexer.nextToken(); + acceptIdentifier("CHAIN"); + stmt.setChain(Boolean.FALSE); + } else { + acceptIdentifier("CHAIN"); + stmt.setChain(Boolean.TRUE); + } + } + + return stmt; + } + + public MySqlCommitStatement parseCommit() throws ParserException { + acceptIdentifier("COMMIT"); + + MySqlCommitStatement stmt = new MySqlCommitStatement(); + + if (identifierEquals("WORK")) { + lexer.nextToken(); + stmt.setWork(true); + } + + if (lexer.token() == Token.AND) { + lexer.nextToken(); + if (lexer.token() == Token.NOT) { + lexer.nextToken(); + acceptIdentifier("CHAIN"); + stmt.setChain(Boolean.FALSE); + } else { + acceptIdentifier("CHAIN"); + stmt.setChain(Boolean.TRUE); + } + } + + return stmt; + } + + public MySqlReplicateStatement parseReplicate() throws ParserException { + MySqlReplicateStatement stmt = new MySqlReplicateStatement(); + + acceptIdentifier("REPLACE"); + + if (identifierEquals("LOW_PRIORITY")) { + stmt.setLowPriority(true); + lexer.nextToken(); + } + + if (identifierEquals("DELAYED")) { + stmt.setDelayed(true); + lexer.nextToken(); + } + + if (lexer.token() == Token.INTO) { + lexer.nextToken(); + } + + SQLName tableName = exprParser.name(); + stmt.setTableName(tableName); + + if (lexer.token() == Token.VALUES || identifierEquals("VALUE")) { + lexer.nextToken(); + + for (;;) { + accept(Token.LPAREN); + SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(); + this.exprParser.exprList(values.getValues()); + stmt.getValuesList().add(values); + accept(Token.RPAREN); + + if (lexer.token() == Token.COMMA) { + lexer.nextToken(); + continue; + } else { + break; + } + } + } else if (lexer.token() == Token.SELECT) { + SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr(); + stmt.setQuery(queryExpr); + } else if (lexer.token() == Token.SET) { + lexer.nextToken(); + for (;;) { + SQLUpdateSetItem item = new SQLUpdateSetItem(); + item.setColumn(this.exprParser.name()); + accept(Token.EQ); + item.setValue(this.exprParser.expr()); + + stmt.getSetItems().add(item); + + if (lexer.token() == (Token.COMMA)) { + lexer.nextToken(); + continue; + } + + break; + } + } + + return stmt; + } + + protected SQLStatement parseLoad() throws ParserException { + acceptIdentifier("LOAD"); + + if (identifierEquals("DATA")) { + SQLStatement stmt = parseLoadDataInFile(); + return stmt; + } + + if (identifierEquals("XML")) { + SQLStatement stmt = parseLoadXml(); + return stmt; + } + + throw new ParserException("TODO"); + } + + protected MySqlLoadXmlStatement parseLoadXml() throws ParserException { + acceptIdentifier("XML"); + + MySqlLoadXmlStatement stmt = new MySqlLoadXmlStatement(); + + if (identifierEquals("LOW_PRIORITY")) { + stmt.setLowPriority(true); + lexer.nextToken(); + } + + if (identifierEquals("CONCURRENT")) { + stmt.setConcurrent(true); + lexer.nextToken(); + } + + if (identifierEquals("LOCAL")) { + stmt.setLocal(true); + lexer.nextToken(); + } + + acceptIdentifier("INFILE"); + + SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr(); + stmt.setFileName(fileName); + + if (identifierEquals("REPLACE")) { + stmt.setReplicate(true); + lexer.nextToken(); + } + + if (identifierEquals("IGNORE")) { + stmt.setIgnore(true); + lexer.nextToken(); + } + + accept(Token.INTO); + accept(Token.TABLE); + + SQLName tableName = exprParser.name(); + stmt.setTableName(tableName); + + if (identifierEquals("CHARACTER")) { + lexer.nextToken(); + accept(Token.SET); + + if (lexer.token() != Token.LITERAL_CHARS) { + throw new ParserException("syntax error, illegal charset"); + } + + String charset = lexer.stringVal(); + lexer.nextToken(); + stmt.setCharset(charset); + } + + if (identifierEquals("ROWS")) { + lexer.nextToken(); + acceptIdentifier("IDENTIFIED"); + accept(Token.BY); + SQLExpr rowsIdentifiedBy = exprParser.expr(); + stmt.setRowsIdentifiedBy(rowsIdentifiedBy); + } - MySqlLoadDataInFileStatement stmt = new MySqlLoadDataInFileStatement(); + if (identifierEquals("IGNORE")) { + throw new ParserException("TODO"); + } - if (identifierEquals("LOW_PRIORITY")) { - stmt.setLowPriority(true); - lexer.nextToken(); - } + if (lexer.token() == Token.SET) { + throw new ParserException("TODO"); + } - if (identifierEquals("CONCURRENT")) { - stmt.setConcurrent(true); - lexer.nextToken(); - } + return stmt; + } - if (identifierEquals("LOCAL")) { - stmt.setLocal(true); - lexer.nextToken(); - } + protected MySqlLoadDataInFileStatement parseLoadDataInFile() throws ParserException { + acceptIdentifier("DATA"); - acceptIdentifier("INFILE"); + MySqlLoadDataInFileStatement stmt = new MySqlLoadDataInFileStatement(); - SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr(); - stmt.setFileName(fileName); + if (identifierEquals("LOW_PRIORITY")) { + stmt.setLowPriority(true); + lexer.nextToken(); + } - if (identifierEquals("REPLACE")) { - stmt.setReplicate(true); - lexer.nextToken(); - } + if (identifierEquals("CONCURRENT")) { + stmt.setConcurrent(true); + lexer.nextToken(); + } - if (identifierEquals("IGNORE")) { - stmt.setIgnore(true); - lexer.nextToken(); - } + if (identifierEquals("LOCAL")) { + stmt.setLocal(true); + lexer.nextToken(); + } - accept(Token.INTO); - accept(Token.TABLE); + acceptIdentifier("INFILE"); - SQLName tableName = exprParser.name(); - stmt.setTableName(tableName); + SQLLiteralExpr fileName = (SQLLiteralExpr) exprParser.expr(); + stmt.setFileName(fileName); - if (identifierEquals("CHARACTER")) { - lexer.nextToken(); - accept(Token.SET); + if (identifierEquals("REPLACE")) { + stmt.setReplicate(true); + lexer.nextToken(); + } - if (lexer.token() != Token.LITERAL_CHARS) { - throw new ParserException("syntax error, illegal charset"); - } + if (identifierEquals("IGNORE")) { + stmt.setIgnore(true); + lexer.nextToken(); + } - String charset = lexer.stringVal(); - lexer.nextToken(); - stmt.setCharset(charset); - } + accept(Token.INTO); + accept(Token.TABLE); - if (identifierEquals("FIELDS") || identifierEquals("COLUMNS")) { - throw new ParserException("TODO"); - } - - if (identifierEquals("LINES")) { - throw new ParserException("TODO"); - } - - if (identifierEquals("IGNORE")) { - throw new ParserException("TODO"); - } - - if (lexer.token() == Token.SET) { - throw new ParserException("TODO"); - } - - return stmt; - } - - public MySqlPrepareStatement parsePrepare() throws ParserException { - acceptIdentifier("PREPARE"); - - SQLName name = exprParser.name(); - accept(Token.FROM); - SQLExpr from = exprParser.expr(); - - return new MySqlPrepareStatement(name, from); - } - - public MySqlExecuteStatement parseExecute() throws ParserException { - acceptIdentifier("EXECUTE"); + SQLName tableName = exprParser.name(); + stmt.setTableName(tableName); - MySqlExecuteStatement stmt = new MySqlExecuteStatement(); + if (identifierEquals("CHARACTER")) { + lexer.nextToken(); + accept(Token.SET); - SQLName statementName = exprParser.name(); - stmt.setStatementName(statementName); + if (lexer.token() != Token.LITERAL_CHARS) { + throw new ParserException("syntax error, illegal charset"); + } - if (identifierEquals("USING")) { - lexer.nextToken(); - exprParser.exprList(stmt.getParameters()); - } + String charset = lexer.stringVal(); + lexer.nextToken(); + stmt.setCharset(charset); + } - return stmt; - } + if (identifierEquals("FIELDS") || identifierEquals("COLUMNS")) { + throw new ParserException("TODO"); + } + + if (identifierEquals("LINES")) { + throw new ParserException("TODO"); + } - public SQLInsertStatement parseInsert() { - accept(Token.INSERT); + if (identifierEquals("IGNORE")) { + throw new ParserException("TODO"); + } + + if (lexer.token() == Token.SET) { + throw new ParserException("TODO"); + } + + return stmt; + } + + public MySqlPrepareStatement parsePrepare() throws ParserException { + acceptIdentifier("PREPARE"); + + SQLName name = exprParser.name(); + accept(Token.FROM); + SQLExpr from = exprParser.expr(); + + return new MySqlPrepareStatement(name, from); + } + + public MySqlExecuteStatement parseExecute() throws ParserException { + acceptIdentifier("EXECUTE"); - MySqlInsertStatement insertStatement = new MySqlInsertStatement(); - - if (identifierEquals("LOW_PRIORITY")) { - insertStatement.setLowPriority(true); - lexer.nextToken(); - } - - if (identifierEquals("DELAYED")) { - insertStatement.setDelayed(true); - lexer.nextToken(); - } + MySqlExecuteStatement stmt = new MySqlExecuteStatement(); - if (identifierEquals("HIGH_PRIORITY")) { - insertStatement.setHighPriority(true); - lexer.nextToken(); - } + SQLName statementName = exprParser.name(); + stmt.setStatementName(statementName); - if (identifierEquals("IGNORE")) { - insertStatement.setIgnore(true); - lexer.nextToken(); - } - - if (lexer.token() == Token.INTO) { - lexer.nextToken(); - } - - SQLName tableName = this.exprParser.name(); - insertStatement.setTableName(tableName); - - if (lexer.token() == Token.IDENTIFIER) { - insertStatement.setAlias(lexer.stringVal()); - lexer.nextToken(); - } - - if (lexer.token() == (Token.LPAREN)) { - lexer.nextToken(); - this.exprParser.exprList(insertStatement.getColumns()); - accept(Token.RPAREN); - } - - if (lexer.token() == (Token.VALUES)) { - lexer.nextToken(); - - for (;;) { - accept(Token.LPAREN); - SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(); - this.exprParser.exprList(values.getValues()); - insertStatement.getValuesList().add(values); - accept(Token.RPAREN); - - if (lexer.token() == Token.COMMA) { - lexer.nextToken(); - continue; - } else { - break; - } - } - - } else if (lexer.token() == (Token.SELECT)) { - SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr(); - insertStatement.setQuery(queryExpr.getSubQuery()); - } - - if (lexer.token() == Token.ON) { - lexer.nextToken(); - acceptIdentifier("DUPLICATE"); - accept(Token.KEY); - accept(Token.UPDATE); - - exprParser.exprList(insertStatement.getDuplicateKeyUpdate()); - } - - return insertStatement; - } + if (identifierEquals("USING")) { + lexer.nextToken(); + exprParser.exprList(stmt.getParameters()); + } + + return stmt; + } + + public SQLInsertStatement parseInsert() { + accept(Token.INSERT); + + MySqlInsertStatement insertStatement = new MySqlInsertStatement(); + + if (identifierEquals("LOW_PRIORITY")) { + insertStatement.setLowPriority(true); + lexer.nextToken(); + } + + if (identifierEquals("DELAYED")) { + insertStatement.setDelayed(true); + lexer.nextToken(); + } + + if (identifierEquals("HIGH_PRIORITY")) { + insertStatement.setHighPriority(true); + lexer.nextToken(); + } + + if (identifierEquals("IGNORE")) { + insertStatement.setIgnore(true); + lexer.nextToken(); + } + + if (lexer.token() == Token.INTO) { + lexer.nextToken(); + } + + SQLName tableName = this.exprParser.name(); + insertStatement.setTableName(tableName); + + if (lexer.token() == Token.IDENTIFIER) { + insertStatement.setAlias(lexer.stringVal()); + lexer.nextToken(); + } + + if (lexer.token() == (Token.LPAREN)) { + lexer.nextToken(); + this.exprParser.exprList(insertStatement.getColumns()); + accept(Token.RPAREN); + } + + if (lexer.token() == (Token.VALUES)) { + lexer.nextToken(); + + for (;;) { + accept(Token.LPAREN); + SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(); + this.exprParser.exprList(values.getValues()); + insertStatement.getValuesList().add(values); + accept(Token.RPAREN); + + if (lexer.token() == Token.COMMA) { + lexer.nextToken(); + continue; + } else { + break; + } + } + + } else if (lexer.token() == (Token.SELECT)) { + SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr(); + insertStatement.setQuery(queryExpr.getSubQuery()); + } + + if (lexer.token() == Token.ON) { + lexer.nextToken(); + acceptIdentifier("DUPLICATE"); + accept(Token.KEY); + accept(Token.UPDATE); + + exprParser.exprList(insertStatement.getDuplicateKeyUpdate()); + } + + return insertStatement; + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitor.java index 339092595f..873dd0cdaa 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitor.java @@ -25,6 +25,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr; import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; @@ -163,4 +164,12 @@ public interface MySqlASTVisitor extends SQLASTVisitor { void endVisit(MySqlResetStatement x); boolean visit(MySqlResetStatement x); + + void endVisit(MySqlCreateUserStatement x); + + boolean visit(MySqlCreateUserStatement x); + + void endVisit(MySqlCreateUserStatement.UserSpecification x); + + boolean visit(MySqlCreateUserStatement.UserSpecification x); } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java index a45a245e64..7b930721ee 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java @@ -10,6 +10,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr; import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; @@ -345,4 +347,24 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements return true; } + @Override + public void endVisit(MySqlCreateUserStatement x) { + + } + + @Override + public boolean visit(MySqlCreateUserStatement x) { + return true; + } + + @Override + public void endVisit(UserSpecification x) { + + } + + @Override + public boolean visit(UserSpecification x) { + return true; + } + } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java index 1ac636eca1..15bee85a18 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java @@ -40,6 +40,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; @@ -53,6 +54,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRollbackStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSQLColumnDefinition; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement; @@ -63,1201 +65,1223 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStartTransactionSt import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex; import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; -public class MySqlOutputVisitor extends SQLASTOutputVisitor implements - MySqlASTVisitor { - - public MySqlOutputVisitor(Appendable appender) { - super(appender); - } - - public boolean visit(MySqlBooleanExpr x) { - print(x.getValue() ? "true" : "false"); - - return false; - } - - public void endVisit(MySqlBooleanExpr x) { - } - - @Override - public boolean visit(SQLSelectQueryBlock select) { - if (select instanceof MySqlSelectQueryBlock) { - return visit((MySqlSelectQueryBlock) select); - } - - return false; - } - - public boolean visit(MySqlSelectQueryBlock select) { - print("SELECT "); - - if (SQLSetQuantifier.ALL == select.getDistionOption()) - print("ALL "); - else if (SQLSetQuantifier.DISTINCT == select.getDistionOption()) - print("DISTINCT "); - else if (SQLSetQuantifier.DISTINCTROW == select.getDistionOption()) { - print("DISTINCTROW "); - } - - if (select.isHignPriority()) { - print("HIGH_PRIORITY "); - } - - if (select.isSmallResult()) { - print("SQL_SMALL_RESULT "); - } - - if (select.isBigResult()) { - print("SQL_BIG_RESULT "); - } - - if (select.isBufferResult()) { - print("SQL_BUFFER_RESULT "); - } - - if (select.getCache() != null) { - if (select.getCache().booleanValue()) { - print("SQL_CACHE "); - } else { - print("SQL_NO_CACHE "); - } - } - - if (select.isCalcFoundRows()) { - print("SQL_CALC_FOUND_ROWS "); - } - - printSelectList(select.getSelectList()); - - if (select.getOutFile() != null) { - println(); - print("INTO OUTFILE "); - select.getOutFile().accept(this); - if (select.getOutFileCharset() != null) { - print(" CHARACTER SET "); - print(select.getOutFileCharset()); - } - - if (select.getOutFileColumnsTerminatedBy() != null - || select.getOutFileColumnsEnclosedBy() != null - || select.getOutFileColumnsEscaped() != null) { - print(" COLUMNS"); - if (select.getOutFileColumnsTerminatedBy() != null) { - print(" TERMINATED BY "); - select.getOutFileColumnsTerminatedBy().accept(this); - } - - if (select.getOutFileColumnsEnclosedBy() != null) { - if (select.isOutFileColumnsEnclosedOptionally()) { - print(" OPTIONALLY"); - } - print(" ENCLOSED BY "); - select.getOutFileColumnsEnclosedBy().accept(this); - } - - if (select.getOutFileColumnsEscaped() != null) { - print(" ESCAPED BY "); - select.getOutFileColumnsEscaped().accept(this); - } - } - - if (select.getOutFileLinesStartingBy() != null - || select.getOutFileLinesTerminatedBy() != null) { - print(" LINES"); - if (select.getOutFileLinesStartingBy() != null) { - print(" STARTING BY "); - select.getOutFileLinesStartingBy().accept(this); - } - - if (select.getOutFileLinesTerminatedBy() != null) { - print(" TERMINATED BY "); - select.getOutFileLinesTerminatedBy().accept(this); - } - } - } - - if (select.getFrom() != null) { - println(); - print("FROM "); - select.getFrom().accept(this); - } - - if (select.getWhere() != null) { - println(); - print("WHERE "); - select.getWhere().accept(this); - } - - if (select.getGroupBy() != null) { - println(); - select.getGroupBy().accept(this); - } - - if (select.getOrderBy() != null) { - println(); - select.getOrderBy().accept(this); - } - - if (select.getLimit() != null) { - println(); - select.getLimit().accept(this); - } - - if (select.getProcedureName() != null) { - print(" PROCEDURE "); - select.getProcedureName().accept(this); - if (select.getProcedureArgumentList().size() > 0) { - print("("); - printAndAccept(select.getProcedureArgumentList(), ", "); - print(")"); - } - } - - if (select.isForUpdate()) { - println(); - print("FOR UPDATE"); - } - - if (select.isLockInShareMode()) { - println(); - print("LOCK IN SHARE MODE"); - } - - return false; - } - - public boolean visit(SQLColumnDefinition x) { - MySqlSQLColumnDefinition mysqlColumn = null; - - if (x instanceof MySqlSQLColumnDefinition) { - mysqlColumn = (MySqlSQLColumnDefinition) x; - } - - x.getName().accept(this); - print(' '); - x.getDataType().accept(this); - - if (x.getDefaultExpr() != null) { - print(" DEFAULT "); - x.getDefaultExpr().accept(this); - } - - if (mysqlColumn != null && mysqlColumn.isAutoIncrement()) { - print(" AUTO_INCREMENT"); - } - - for (SQLColumnConstraint item : x.getConstaints()) { - print(' '); - item.accept(this); - } - - return false; - } - - public boolean visit(MySqlSelectQueryBlock.Limit x) { - print("LIMIT "); - if (x.getOffset() != null) { - x.getOffset().accept(this); - print(", "); - } - x.getRowCount().accept(this); - - return false; - } - - public boolean visit(SQLDataType x) { - print(x.getName()); - if (x.getArguments().size() > 0) { - print("("); - printAndAccept(x.getArguments(), ", "); - print(")"); - } - - if (x instanceof SQLCharactorDataType) { - SQLCharactorDataType charType = (SQLCharactorDataType) x; - if (charType.getCharSetName() != null) { - print(" CHARACTER SET "); - print(charType.getCharSetName()); - - if (charType.getCollate() != null) { - print(" COLLATE "); - print(charType.getCollate()); - } - } - } - return false; - } - - @Override - public void endVisit(Limit x) { - - } - - @Override - public void endVisit(MySqlTableIndex x) { - - } - - @Override - public boolean visit(MySqlTableIndex x) { - print("INDEX"); - if (x.getName() != null) { - print(" "); - x.getName().accept(this); - } - - if (x.getIndexType() != null) { - print(" USING "); - print(x.getIndexType()); - } - - print("("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - return false; - } - - public boolean visit(SQLCreateTableStatement x) { - - MySqlCreateTableStatement mysqlCreateTableStatement = null; - if (x instanceof MySqlCreateTableStatement) { - mysqlCreateTableStatement = (MySqlCreateTableStatement) x; - } - - if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) { - print("CREATE TEMPORARY TABLE "); - } else { - print("CREATE TABLE "); - } - - if (mysqlCreateTableStatement != null - && mysqlCreateTableStatement.isIfNotExiists()) { - print("IF NOT EXISTS "); - } - - x.getName().accept(this); - print(" ("); - incrementIndent(); - println(); - for (int i = 0, size = x.getTableElementList().size(); i < size; ++i) { - if (i != 0) { - print(", "); - println(); - } - x.getTableElementList().get(i).accept(this); - } - decrementIndent(); - println(); - print(")"); - - if (mysqlCreateTableStatement != null) { - for (Map.Entry option : mysqlCreateTableStatement - .getTableOptions().entrySet()) { - print(" "); - print(option.getKey()); - print(" = "); - print(option.getValue()); - } - } - - if (mysqlCreateTableStatement != null - && mysqlCreateTableStatement.getQuery() != null) { - print(" "); - incrementIndent(); - println(); - mysqlCreateTableStatement.getQuery().accept(this); - decrementIndent(); - } - - return false; - } - - @Override - public void endVisit(MySqlKey x) { - - } - - @Override - public void endVisit(MySqlPrimaryKey x) { - - } - - @Override - public boolean visit(MySqlKey x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.accept(this); - print(' '); - } - - print("KEY"); - - if (x.getIndexType() != null) { - print(" USING "); - print(x.getIndexType()); - } - - print(" ("); - - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - - return false; - } - - @Override - public boolean visit(MySqlPrimaryKey x) { - if (x.getName() != null) { - print("CONSTRAINT "); - x.accept(this); - print(' '); - } - - print("PRIAMRY KEY"); - - if (x.getIndexType() != null) { - print(" USING "); - print(x.getIndexType()); - } - - print(" ("); - - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - - return false; - } - - public boolean visit(SQLCharExpr x) { - if (x instanceof MySqlCharExpr) { - print(x.toString()); - } else if ((x.getText() == null) || (x.getText().length() == 0)) { - print("NULL"); - } else { - print("'"); - print(x.getText().replaceAll("'", "''")); - print("'"); - } - - return false; - } - - public boolean visit(SQLVariantRefExpr x) { - print(x.getName()); - - String collate = (String) x.getAttribute("COLLATE"); - if (collate != null) { - print(" COLLATE "); - print(collate); - } - - return false; - } - - public boolean visit(SQLMethodInvokeExpr x) { - if ("SUBSTRING".equalsIgnoreCase(x.getMethodName())) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - printAndAccept(x.getParameters(), ", "); - SQLExpr from = (SQLExpr) x.getAttribute("FROM"); - if (from != null) { - print(" FROM "); - from.accept(this); - } - - SQLExpr _for = (SQLExpr) x.getAttribute("FOR"); - if (_for != null) { - print(" FOR "); - _for.accept(this); - } - print(")"); - - return false; - } - - if ("TRIM".equalsIgnoreCase(x.getMethodName())) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - - String trimType = (String) x.getAttribute("TRIM_TYPE"); - if (trimType != null) { - print(trimType); - print(' '); - } - - printAndAccept(x.getParameters(), ", "); - - SQLExpr from = (SQLExpr) x.getAttribute("FROM"); - if (from != null) { - print(" FROM "); - from.accept(this); - } - - print(")"); - - return false; - } - - if ("CONVERT".equalsIgnoreCase(x.getMethodName())) { - if (x.getOwner() != null) { - x.getOwner().accept(this); - print("."); - } - print(x.getMethodName()); - print("("); - printAndAccept(x.getParameters(), ", "); - - String charset = (String) x.getAttribute("USING"); - if (charset != null) { - print(" USING "); - print(charset); - } - print(")"); - return false; - } - - return super.visit(x); - } - - @Override - public void endVisit(MySqlIntervalExpr x) { - - } - - @Override - public boolean visit(MySqlIntervalExpr x) { - print("INTERVAL "); - x.getValue().accept(this); - print(' '); - print(x.getUnit().name()); - return false; - } - - @Override - public boolean visit(MySqlExtractExpr x) { - print("EXTRACT("); - print(x.getUnit().name()); - print(" FROM "); - x.getValue().accept(this); - print(')'); - return false; - } - - @Override - public void endVisit(MySqlExtractExpr x) { - - } - - @Override - public void endVisit(MySqlMatchAgainstExpr x) { - - } - - @Override - public boolean visit(MySqlMatchAgainstExpr x) { - print("MATCH ("); - printAndAccept(x.getColumns(), ", "); - print(")"); - - print(" AGAINST ("); - x.getAgainst().accept(this); - if (x.getSearchModifier() != null) { - print(' '); - print(x.getSearchModifier().name); - } - print(')'); - - return false; - } - - @Override - public void endVisit(MySqlBinaryExpr x) { - - } - - @Override - public boolean visit(MySqlBinaryExpr x) { - print("b'"); - print(x.getValue()); - print('\''); - - return false; - } - - @Override - public void endVisit(MySqlPrepareStatement x) { - } - - @Override - public boolean visit(MySqlPrepareStatement x) { - print("PREPARE "); - x.getName().accept(this); - print(" FROM "); - x.getFrom().accept(this); - return false; - } - - @Override - public void endVisit(MySqlExecuteStatement x) { - - } - - @Override - public boolean visit(MySqlExecuteStatement x) { - print("EXECUTE "); - x.getStatementName().accept(this); - if (x.getParameters().size() > 0) { - print(" USING "); - printAndAccept(x.getParameters(), ", "); - } - return false; - } - - @Override - public void endVisit(MySqlDeleteStatement x) { - - } - - @Override - public boolean visit(MySqlDeleteStatement x) { - print("DELETE "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isQuick()) { - print("QUICK "); - } - - if (x.isIgnore()) { - print("IGNORE "); - } - - if (x.getFrom() == null) { - print("FROM "); - printAndAccept(x.getTableNames(), ", "); - } else { - printAndAccept(x.getTableNames(), ", "); - println(); - print("FROM "); - x.getFrom().accept(this); - } - - if (x.getUsing() != null) { - println(); - print("USING "); - x.getUsing().accept(this); - } - - if (x.getWhere() != null) { - println(); - print("WHERE "); - x.getWhere().accept(this); - } - - if (x.getOrderBy() != null) { - println(); - x.getOrderBy().accept(this); - } - - if (x.getLimit() != null) { - println(); - x.getLimit().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlInsertStatement x) { - - } - - @Override - public boolean visit(MySqlInsertStatement x) { - print("INSERT "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isDelayed()) { - print("DELAYED "); - } - - if (x.isHighPriority()) { - print("HIGH_PRIORITY "); - } - - if (x.isIgnore()) { - print("IGNORE "); - } - - print("INTO "); - - x.getTableName().accept(this); - - if (x.getColumns().size() > 0) { - incrementIndent(); - println(); - print("("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - if (i % 5 == 0) { - println(); - } - print(", "); - } - - x.getColumns().get(i).accept(this); - } - print(")"); - decrementIndent(); - } - - if (x.getValuesList().size() != 0) { - println(); - print("VALUES"); - println(); - for (int i = 0, size = x.getValuesList().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getValuesList().get(i).accept(this); - } - - } - - if (x.getQuery() != null) { - print(" "); - x.getQuery().accept(this); - } - - if (x.getDuplicateKeyUpdate().size() != 0) { - print(" ON DUPLICATE KEY UPDATE "); - printAndAccept(x.getDuplicateKeyUpdate(), ", "); - } - - return false; - } - - @Override - public void endVisit(MySqlLoadDataInFileStatement x) { - - } - - @Override - public boolean visit(MySqlLoadDataInFileStatement x) { - print("LOAD DATA "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isConcurrent()) { - print("CONCURRENT "); - } - - if (x.isLocal()) { - print("LOCAL "); - } - - print("INFILE "); - - x.getFileName().accept(this); - - if (x.isReplicate()) { - print(" REPLACE "); - } - - if (x.isIgnore()) { - print(" IGNORE "); - } - - print(" INTO TABLE "); - x.getTableName().accept(this); - - if (x.getColumnsTerminatedBy() != null - || x.getColumnsEnclosedBy() != null - || x.getColumnsEscaped() != null) { - print(" COLUMNS"); - if (x.getColumnsTerminatedBy() != null) { - print(" TERMINATED BY "); - x.getColumnsTerminatedBy().accept(this); - } - - if (x.getColumnsEnclosedBy() != null) { - if (x.isColumnsEnclosedOptionally()) { - print(" OPTIONALLY"); - } - print(" ENCLOSED BY "); - x.getColumnsEnclosedBy().accept(this); - } - - if (x.getColumnsEscaped() != null) { - print(" ESCAPED BY "); - x.getColumnsEscaped().accept(this); - } - } - - if (x.getLinesStartingBy() != null || x.getLinesTerminatedBy() != null) { - print(" LINES"); - if (x.getLinesStartingBy() != null) { - print(" STARTING BY "); - x.getLinesStartingBy().accept(this); - } - - if (x.getLinesTerminatedBy() != null) { - print(" TERMINATED BY "); - x.getLinesTerminatedBy().accept(this); - } - } - - if (x.getSetList().size() != 0) { - print(" SET "); - printAndAccept(x.getSetList(), ", "); - } - - return false; - } - - @Override - public void endVisit(MySqlReplicateStatement x) { - - } - - @Override - public boolean visit(MySqlReplicateStatement x) { - print("REPLACE "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isDelayed()) { - print("DELAYED "); - } - - print("INTO "); - - x.getTableName().accept(this); - - if (x.getColumns().size() > 0) { - print(" ("); - for (int i = 0, size = x.getColumns().size(); i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getColumns().get(i).accept(this); - } - print(")"); - } - - if (x.getValuesList().size() != 0) { - println(); - print("VALUES "); - int size = x.getValuesList().size(); - if (size == 0) { - print("()"); - } else { - for (int i = 0; i < size; ++i) { - if (i != 0) { - print(", "); - } - x.getValuesList().get(i).accept(this); - } - } - } - - if (x.getQuery() != null) { - x.getQuery().accept(this); - } - - if (x.getSetItems().size() != 0) { - println(); - print("SET "); - printAndAccept(x.getSetItems(), ", "); - } - - return false; - } - - @Override - public void endVisit(MySqlSelectGroupBy x) { - - } - - @Override - public boolean visit(MySqlSelectGroupBy x) { - super.visit(x); - - if (x.isRollUp()) { - print(" WITH ROLLUP"); - } - - return false; - } - - @Override - public void endVisit(MySqlStartTransactionStatement x) { - - } - - @Override - public boolean visit(MySqlStartTransactionStatement x) { - print("START TRANSACTION"); - if (x.isConsistentSnapshot()) { - print(" WITH CONSISTENT SNAPSHOT"); - } - - if (x.isBegin()) { - print(" BEGIN"); - } - - if (x.isWork()) { - print(" WORK"); - } - - return false; - } - - @Override - public void endVisit(MySqlCommitStatement x) { - - } - - @Override - public boolean visit(MySqlCommitStatement x) { - print("COMMIT"); - - if (x.isWork()) { - print(" WORK"); - } - - if (x.getChain() != null) { - if (x.getChain().booleanValue()) { - print(" AND CHAIN"); - } else { - print(" AND NO CHAIN"); - } - } - - if (x.getRelease() != null) { - if (x.getRelease().booleanValue()) { - print(" AND RELEASE"); - } else { - print(" AND NO RELEASE"); - } - } - - return false; - } - - @Override - public void endVisit(MySqlRollbackStatement x) { - - } - - @Override - public boolean visit(MySqlRollbackStatement x) { - print("ROLLBACK"); - - if (x.isWork()) { - print(" WORK"); - } - - if (x.getChain() != null) { - if (x.getChain().booleanValue()) { - print(" AND CHAIN"); - } else { - print(" AND NO CHAIN"); - } - } - - if (x.getRelease() != null) { - if (x.getRelease().booleanValue()) { - print(" AND RELEASE"); - } else { - print(" AND NO RELEASE"); - } - } - - return false; - } - - @Override - public void endVisit(MySqlShowColumnsStatement x) { - - } - - @Override - public boolean visit(MySqlShowColumnsStatement x) { - if (x.isFull()) { - print("SHOW FULL COLUMNS"); - } else { - print("SHOW COLUMNS"); - } - - if (x.getTable() != null) { - print(" FROM "); - x.getTable().accept(this); - } - - if (x.getDatabase() != null) { - print(" FROM "); - x.getDatabase().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlShowTablesStatement x) { - - } - - @Override - public boolean visit(MySqlShowTablesStatement x) { - if (x.isFull()) { - print("SHOW FULL TABLES"); - } else { - print("SHOW TABLES"); - } - - if (x.getDatabase() != null) { - print(" FROM "); - x.getDatabase().accept(this); - } - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } +public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTVisitor { + + public MySqlOutputVisitor(Appendable appender){ + super(appender); + } + + public boolean visit(MySqlBooleanExpr x) { + print(x.getValue() ? "true" : "false"); + + return false; + } + + public void endVisit(MySqlBooleanExpr x) { + } + + @Override + public boolean visit(SQLSelectQueryBlock select) { + if (select instanceof MySqlSelectQueryBlock) { + return visit((MySqlSelectQueryBlock) select); + } + + return false; + } + + public boolean visit(MySqlSelectQueryBlock select) { + print("SELECT "); + + if (SQLSetQuantifier.ALL == select.getDistionOption()) print("ALL "); + else if (SQLSetQuantifier.DISTINCT == select.getDistionOption()) print("DISTINCT "); + else if (SQLSetQuantifier.DISTINCTROW == select.getDistionOption()) { + print("DISTINCTROW "); + } + + if (select.isHignPriority()) { + print("HIGH_PRIORITY "); + } + + if (select.isSmallResult()) { + print("SQL_SMALL_RESULT "); + } + + if (select.isBigResult()) { + print("SQL_BIG_RESULT "); + } + + if (select.isBufferResult()) { + print("SQL_BUFFER_RESULT "); + } + + if (select.getCache() != null) { + if (select.getCache().booleanValue()) { + print("SQL_CACHE "); + } else { + print("SQL_NO_CACHE "); + } + } + + if (select.isCalcFoundRows()) { + print("SQL_CALC_FOUND_ROWS "); + } + + printSelectList(select.getSelectList()); + + if (select.getOutFile() != null) { + println(); + print("INTO OUTFILE "); + select.getOutFile().accept(this); + if (select.getOutFileCharset() != null) { + print(" CHARACTER SET "); + print(select.getOutFileCharset()); + } + + if (select.getOutFileColumnsTerminatedBy() != null || select.getOutFileColumnsEnclosedBy() != null + || select.getOutFileColumnsEscaped() != null) { + print(" COLUMNS"); + if (select.getOutFileColumnsTerminatedBy() != null) { + print(" TERMINATED BY "); + select.getOutFileColumnsTerminatedBy().accept(this); + } + + if (select.getOutFileColumnsEnclosedBy() != null) { + if (select.isOutFileColumnsEnclosedOptionally()) { + print(" OPTIONALLY"); + } + print(" ENCLOSED BY "); + select.getOutFileColumnsEnclosedBy().accept(this); + } + + if (select.getOutFileColumnsEscaped() != null) { + print(" ESCAPED BY "); + select.getOutFileColumnsEscaped().accept(this); + } + } + + if (select.getOutFileLinesStartingBy() != null || select.getOutFileLinesTerminatedBy() != null) { + print(" LINES"); + if (select.getOutFileLinesStartingBy() != null) { + print(" STARTING BY "); + select.getOutFileLinesStartingBy().accept(this); + } + + if (select.getOutFileLinesTerminatedBy() != null) { + print(" TERMINATED BY "); + select.getOutFileLinesTerminatedBy().accept(this); + } + } + } + + if (select.getFrom() != null) { + println(); + print("FROM "); + select.getFrom().accept(this); + } + + if (select.getWhere() != null) { + println(); + print("WHERE "); + select.getWhere().accept(this); + } + + if (select.getGroupBy() != null) { + println(); + select.getGroupBy().accept(this); + } + + if (select.getOrderBy() != null) { + println(); + select.getOrderBy().accept(this); + } + + if (select.getLimit() != null) { + println(); + select.getLimit().accept(this); + } + + if (select.getProcedureName() != null) { + print(" PROCEDURE "); + select.getProcedureName().accept(this); + if (select.getProcedureArgumentList().size() > 0) { + print("("); + printAndAccept(select.getProcedureArgumentList(), ", "); + print(")"); + } + } + + if (select.isForUpdate()) { + println(); + print("FOR UPDATE"); + } + + if (select.isLockInShareMode()) { + println(); + print("LOCK IN SHARE MODE"); + } + + return false; + } + + public boolean visit(SQLColumnDefinition x) { + MySqlSQLColumnDefinition mysqlColumn = null; + + if (x instanceof MySqlSQLColumnDefinition) { + mysqlColumn = (MySqlSQLColumnDefinition) x; + } + + x.getName().accept(this); + print(' '); + x.getDataType().accept(this); + + if (x.getDefaultExpr() != null) { + print(" DEFAULT "); + x.getDefaultExpr().accept(this); + } + + if (mysqlColumn != null && mysqlColumn.isAutoIncrement()) { + print(" AUTO_INCREMENT"); + } + + for (SQLColumnConstraint item : x.getConstaints()) { + print(' '); + item.accept(this); + } + + return false; + } + + public boolean visit(MySqlSelectQueryBlock.Limit x) { + print("LIMIT "); + if (x.getOffset() != null) { + x.getOffset().accept(this); + print(", "); + } + x.getRowCount().accept(this); + + return false; + } + + public boolean visit(SQLDataType x) { + print(x.getName()); + if (x.getArguments().size() > 0) { + print("("); + printAndAccept(x.getArguments(), ", "); + print(")"); + } + + if (x instanceof SQLCharactorDataType) { + SQLCharactorDataType charType = (SQLCharactorDataType) x; + if (charType.getCharSetName() != null) { + print(" CHARACTER SET "); + print(charType.getCharSetName()); + + if (charType.getCollate() != null) { + print(" COLLATE "); + print(charType.getCollate()); + } + } + } + return false; + } + + @Override + public void endVisit(Limit x) { + + } + + @Override + public void endVisit(MySqlTableIndex x) { + + } + + @Override + public boolean visit(MySqlTableIndex x) { + print("INDEX"); + if (x.getName() != null) { + print(" "); + x.getName().accept(this); + } + + if (x.getIndexType() != null) { + print(" USING "); + print(x.getIndexType()); + } + + print("("); + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getColumns().get(i).accept(this); + } + print(")"); + return false; + } + + public boolean visit(SQLCreateTableStatement x) { + + MySqlCreateTableStatement mysqlCreateTableStatement = null; + if (x instanceof MySqlCreateTableStatement) { + mysqlCreateTableStatement = (MySqlCreateTableStatement) x; + } + + if (SQLCreateTableStatement.Type.GLOBAL_TEMPORARY.equals(x.getType())) { + print("CREATE TEMPORARY TABLE "); + } else { + print("CREATE TABLE "); + } + + if (mysqlCreateTableStatement != null && mysqlCreateTableStatement.isIfNotExiists()) { + print("IF NOT EXISTS "); + } + + x.getName().accept(this); + print(" ("); + incrementIndent(); + println(); + for (int i = 0, size = x.getTableElementList().size(); i < size; ++i) { + if (i != 0) { + print(", "); + println(); + } + x.getTableElementList().get(i).accept(this); + } + decrementIndent(); + println(); + print(")"); + + if (mysqlCreateTableStatement != null) { + for (Map.Entry option : mysqlCreateTableStatement.getTableOptions().entrySet()) { + print(" "); + print(option.getKey()); + print(" = "); + print(option.getValue()); + } + } + + if (mysqlCreateTableStatement != null && mysqlCreateTableStatement.getQuery() != null) { + print(" "); + incrementIndent(); + println(); + mysqlCreateTableStatement.getQuery().accept(this); + decrementIndent(); + } + + return false; + } + + @Override + public void endVisit(MySqlKey x) { + + } + + @Override + public void endVisit(MySqlPrimaryKey x) { + + } + + @Override + public boolean visit(MySqlKey x) { + if (x.getName() != null) { + print("CONSTRAINT "); + x.accept(this); + print(' '); + } + + print("KEY"); + + if (x.getIndexType() != null) { + print(" USING "); + print(x.getIndexType()); + } + + print(" ("); + + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getColumns().get(i).accept(this); + } + print(")"); + + return false; + } + + @Override + public boolean visit(MySqlPrimaryKey x) { + if (x.getName() != null) { + print("CONSTRAINT "); + x.accept(this); + print(' '); + } + + print("PRIAMRY KEY"); + + if (x.getIndexType() != null) { + print(" USING "); + print(x.getIndexType()); + } + + print(" ("); + + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getColumns().get(i).accept(this); + } + print(")"); + + return false; + } + + public boolean visit(SQLCharExpr x) { + if (x instanceof MySqlCharExpr) { + print(x.toString()); + } else if ((x.getText() == null) || (x.getText().length() == 0)) { + print("NULL"); + } else { + print("'"); + print(x.getText().replaceAll("'", "''")); + print("'"); + } + + return false; + } + + public boolean visit(SQLVariantRefExpr x) { + print(x.getName()); + + String collate = (String) x.getAttribute("COLLATE"); + if (collate != null) { + print(" COLLATE "); + print(collate); + } + + return false; + } + + public boolean visit(SQLMethodInvokeExpr x) { + if ("SUBSTRING".equalsIgnoreCase(x.getMethodName())) { + if (x.getOwner() != null) { + x.getOwner().accept(this); + print("."); + } + print(x.getMethodName()); + print("("); + printAndAccept(x.getParameters(), ", "); + SQLExpr from = (SQLExpr) x.getAttribute("FROM"); + if (from != null) { + print(" FROM "); + from.accept(this); + } + + SQLExpr _for = (SQLExpr) x.getAttribute("FOR"); + if (_for != null) { + print(" FOR "); + _for.accept(this); + } + print(")"); + + return false; + } + + if ("TRIM".equalsIgnoreCase(x.getMethodName())) { + if (x.getOwner() != null) { + x.getOwner().accept(this); + print("."); + } + print(x.getMethodName()); + print("("); + + String trimType = (String) x.getAttribute("TRIM_TYPE"); + if (trimType != null) { + print(trimType); + print(' '); + } + + printAndAccept(x.getParameters(), ", "); + + SQLExpr from = (SQLExpr) x.getAttribute("FROM"); + if (from != null) { + print(" FROM "); + from.accept(this); + } + + print(")"); + + return false; + } + + if ("CONVERT".equalsIgnoreCase(x.getMethodName())) { + if (x.getOwner() != null) { + x.getOwner().accept(this); + print("."); + } + print(x.getMethodName()); + print("("); + printAndAccept(x.getParameters(), ", "); + + String charset = (String) x.getAttribute("USING"); + if (charset != null) { + print(" USING "); + print(charset); + } + print(")"); + return false; + } + + return super.visit(x); + } + + @Override + public void endVisit(MySqlIntervalExpr x) { + + } + + @Override + public boolean visit(MySqlIntervalExpr x) { + print("INTERVAL "); + x.getValue().accept(this); + print(' '); + print(x.getUnit().name()); + return false; + } + + @Override + public boolean visit(MySqlExtractExpr x) { + print("EXTRACT("); + print(x.getUnit().name()); + print(" FROM "); + x.getValue().accept(this); + print(')'); + return false; + } + + @Override + public void endVisit(MySqlExtractExpr x) { + + } + + @Override + public void endVisit(MySqlMatchAgainstExpr x) { + + } + + @Override + public boolean visit(MySqlMatchAgainstExpr x) { + print("MATCH ("); + printAndAccept(x.getColumns(), ", "); + print(")"); + + print(" AGAINST ("); + x.getAgainst().accept(this); + if (x.getSearchModifier() != null) { + print(' '); + print(x.getSearchModifier().name); + } + print(')'); + + return false; + } + + @Override + public void endVisit(MySqlBinaryExpr x) { + + } + + @Override + public boolean visit(MySqlBinaryExpr x) { + print("b'"); + print(x.getValue()); + print('\''); + + return false; + } + + @Override + public void endVisit(MySqlPrepareStatement x) { + } + + @Override + public boolean visit(MySqlPrepareStatement x) { + print("PREPARE "); + x.getName().accept(this); + print(" FROM "); + x.getFrom().accept(this); + return false; + } + + @Override + public void endVisit(MySqlExecuteStatement x) { + + } + + @Override + public boolean visit(MySqlExecuteStatement x) { + print("EXECUTE "); + x.getStatementName().accept(this); + if (x.getParameters().size() > 0) { + print(" USING "); + printAndAccept(x.getParameters(), ", "); + } + return false; + } + + @Override + public void endVisit(MySqlDeleteStatement x) { + + } + + @Override + public boolean visit(MySqlDeleteStatement x) { + print("DELETE "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isQuick()) { + print("QUICK "); + } + + if (x.isIgnore()) { + print("IGNORE "); + } + + if (x.getFrom() == null) { + print("FROM "); + printAndAccept(x.getTableNames(), ", "); + } else { + printAndAccept(x.getTableNames(), ", "); + println(); + print("FROM "); + x.getFrom().accept(this); + } + + if (x.getUsing() != null) { + println(); + print("USING "); + x.getUsing().accept(this); + } + + if (x.getWhere() != null) { + println(); + print("WHERE "); + x.getWhere().accept(this); + } + + if (x.getOrderBy() != null) { + println(); + x.getOrderBy().accept(this); + } + + if (x.getLimit() != null) { + println(); + x.getLimit().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlInsertStatement x) { + + } + + @Override + public boolean visit(MySqlInsertStatement x) { + print("INSERT "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isDelayed()) { + print("DELAYED "); + } + + if (x.isHighPriority()) { + print("HIGH_PRIORITY "); + } + + if (x.isIgnore()) { + print("IGNORE "); + } + + print("INTO "); + + x.getTableName().accept(this); + + if (x.getColumns().size() > 0) { + incrementIndent(); + println(); + print("("); + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + if (i % 5 == 0) { + println(); + } + print(", "); + } + + x.getColumns().get(i).accept(this); + } + print(")"); + decrementIndent(); + } + + if (x.getValuesList().size() != 0) { + println(); + print("VALUES"); + println(); + for (int i = 0, size = x.getValuesList().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getValuesList().get(i).accept(this); + } + + } + + if (x.getQuery() != null) { + print(" "); + x.getQuery().accept(this); + } + + if (x.getDuplicateKeyUpdate().size() != 0) { + print(" ON DUPLICATE KEY UPDATE "); + printAndAccept(x.getDuplicateKeyUpdate(), ", "); + } + + return false; + } + + @Override + public void endVisit(MySqlLoadDataInFileStatement x) { + + } + + @Override + public boolean visit(MySqlLoadDataInFileStatement x) { + print("LOAD DATA "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isConcurrent()) { + print("CONCURRENT "); + } + + if (x.isLocal()) { + print("LOCAL "); + } + + print("INFILE "); + + x.getFileName().accept(this); + + if (x.isReplicate()) { + print(" REPLACE "); + } + + if (x.isIgnore()) { + print(" IGNORE "); + } + + print(" INTO TABLE "); + x.getTableName().accept(this); + + if (x.getColumnsTerminatedBy() != null || x.getColumnsEnclosedBy() != null || x.getColumnsEscaped() != null) { + print(" COLUMNS"); + if (x.getColumnsTerminatedBy() != null) { + print(" TERMINATED BY "); + x.getColumnsTerminatedBy().accept(this); + } + + if (x.getColumnsEnclosedBy() != null) { + if (x.isColumnsEnclosedOptionally()) { + print(" OPTIONALLY"); + } + print(" ENCLOSED BY "); + x.getColumnsEnclosedBy().accept(this); + } + + if (x.getColumnsEscaped() != null) { + print(" ESCAPED BY "); + x.getColumnsEscaped().accept(this); + } + } + + if (x.getLinesStartingBy() != null || x.getLinesTerminatedBy() != null) { + print(" LINES"); + if (x.getLinesStartingBy() != null) { + print(" STARTING BY "); + x.getLinesStartingBy().accept(this); + } + + if (x.getLinesTerminatedBy() != null) { + print(" TERMINATED BY "); + x.getLinesTerminatedBy().accept(this); + } + } + + if (x.getSetList().size() != 0) { + print(" SET "); + printAndAccept(x.getSetList(), ", "); + } + + return false; + } + + @Override + public void endVisit(MySqlReplicateStatement x) { + + } + + @Override + public boolean visit(MySqlReplicateStatement x) { + print("REPLACE "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isDelayed()) { + print("DELAYED "); + } + + print("INTO "); + + x.getTableName().accept(this); + + if (x.getColumns().size() > 0) { + print(" ("); + for (int i = 0, size = x.getColumns().size(); i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getColumns().get(i).accept(this); + } + print(")"); + } + + if (x.getValuesList().size() != 0) { + println(); + print("VALUES "); + int size = x.getValuesList().size(); + if (size == 0) { + print("()"); + } else { + for (int i = 0; i < size; ++i) { + if (i != 0) { + print(", "); + } + x.getValuesList().get(i).accept(this); + } + } + } + + if (x.getQuery() != null) { + x.getQuery().accept(this); + } + + if (x.getSetItems().size() != 0) { + println(); + print("SET "); + printAndAccept(x.getSetItems(), ", "); + } + + return false; + } + + @Override + public void endVisit(MySqlSelectGroupBy x) { + + } + + @Override + public boolean visit(MySqlSelectGroupBy x) { + super.visit(x); + + if (x.isRollUp()) { + print(" WITH ROLLUP"); + } + + return false; + } + + @Override + public void endVisit(MySqlStartTransactionStatement x) { + + } + + @Override + public boolean visit(MySqlStartTransactionStatement x) { + print("START TRANSACTION"); + if (x.isConsistentSnapshot()) { + print(" WITH CONSISTENT SNAPSHOT"); + } + + if (x.isBegin()) { + print(" BEGIN"); + } + + if (x.isWork()) { + print(" WORK"); + } + + return false; + } + + @Override + public void endVisit(MySqlCommitStatement x) { + + } + + @Override + public boolean visit(MySqlCommitStatement x) { + print("COMMIT"); + + if (x.isWork()) { + print(" WORK"); + } + + if (x.getChain() != null) { + if (x.getChain().booleanValue()) { + print(" AND CHAIN"); + } else { + print(" AND NO CHAIN"); + } + } + + if (x.getRelease() != null) { + if (x.getRelease().booleanValue()) { + print(" AND RELEASE"); + } else { + print(" AND NO RELEASE"); + } + } + + return false; + } + + @Override + public void endVisit(MySqlRollbackStatement x) { + + } + + @Override + public boolean visit(MySqlRollbackStatement x) { + print("ROLLBACK"); + + if (x.isWork()) { + print(" WORK"); + } + + if (x.getChain() != null) { + if (x.getChain().booleanValue()) { + print(" AND CHAIN"); + } else { + print(" AND NO CHAIN"); + } + } + + if (x.getRelease() != null) { + if (x.getRelease().booleanValue()) { + print(" AND RELEASE"); + } else { + print(" AND NO RELEASE"); + } + } + + return false; + } + + @Override + public void endVisit(MySqlShowColumnsStatement x) { + + } + + @Override + public boolean visit(MySqlShowColumnsStatement x) { + if (x.isFull()) { + print("SHOW FULL COLUMNS"); + } else { + print("SHOW COLUMNS"); + } + + if (x.getTable() != null) { + print(" FROM "); + x.getTable().accept(this); + } + + if (x.getDatabase() != null) { + print(" FROM "); + x.getDatabase().accept(this); + } + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowTablesStatement x) { + + } + + @Override + public boolean visit(MySqlShowTablesStatement x) { + if (x.isFull()) { + print("SHOW FULL TABLES"); + } else { + print("SHOW TABLES"); + } + + if (x.getDatabase() != null) { + print(" FROM "); + x.getDatabase().accept(this); + } + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } - return false; - } + return false; + } - @Override - public void endVisit(MySqlShowDatabasesStatement x) { + @Override + public void endVisit(MySqlShowDatabasesStatement x) { - } - - @Override - public boolean visit(MySqlShowDatabasesStatement x) { - print("SHOW DATABASES"); - - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } - - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } + } + + @Override + public boolean visit(MySqlShowDatabasesStatement x) { + print("SHOW DATABASES"); + + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } + + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } + + return false; + } - @Override - public void endVisit(MySqlShowWarningsStatement x) { + @Override + public void endVisit(MySqlShowWarningsStatement x) { - } + } - @Override - public boolean visit(MySqlShowWarningsStatement x) { - if (x.isCount()) { - print("SHOW COUNT(*) WARNINGS"); - } else { - print("SHOW WARNINGS"); - if (x.getLimit() != null) { - print(' '); - x.getLimit().accept(this); - } - } + @Override + public boolean visit(MySqlShowWarningsStatement x) { + if (x.isCount()) { + print("SHOW COUNT(*) WARNINGS"); + } else { + print("SHOW WARNINGS"); + if (x.getLimit() != null) { + print(' '); + x.getLimit().accept(this); + } + } - return false; - } + return false; + } - @Override - public void endVisit(MySqlShowStatusStatement x) { + @Override + public void endVisit(MySqlShowStatusStatement x) { - } + } - @Override - public boolean visit(MySqlShowStatusStatement x) { - print("SHOW "); + @Override + public boolean visit(MySqlShowStatusStatement x) { + print("SHOW "); - if (x.isGlobal()) { - print("GLOBAL "); - } + if (x.isGlobal()) { + print("GLOBAL "); + } - if (x.isSession()) { - print("SESSION "); - } + if (x.isSession()) { + print("SESSION "); + } - print("STATUS"); + print("STATUS"); - if (x.getLike() != null) { - print(" LIKE "); - x.getLike().accept(this); - } + if (x.getLike() != null) { + print(" LIKE "); + x.getLike().accept(this); + } - if (x.getWhere() != null) { - print(" WHERE "); - x.getWhere().accept(this); - } - - return false; - } - - @Override - public void endVisit(MySqlLoadXmlStatement x) { - - } - - @Override - public boolean visit(MySqlLoadXmlStatement x) { - print("LOAD XML "); - - if (x.isLowPriority()) { - print("LOW_PRIORITY "); - } - - if (x.isConcurrent()) { - print("CONCURRENT "); - } - - if (x.isLocal()) { - print("LOCAL "); - } - - print("INFILE "); - - x.getFileName().accept(this); - - if (x.isReplicate()) { - print(" REPLACE "); - } - - if (x.isIgnore()) { - print(" IGNORE "); - } - - print(" INTO TABLE "); - x.getTableName().accept(this); - - if (x.getCharset() != null) { - print(" CHARSET "); - print(x.getCharset()); - } - - if (x.getRowsIdentifiedBy() != null) { - print(" ROWS IDENTIFIED BY "); - x.getRowsIdentifiedBy().accept(this); - } - - if (x.getSetList().size() != 0) { - print(" SET "); - printAndAccept(x.getSetList(), ", "); - } - - return false; - } - - @Override - public void endVisit(CobarShowStatus x) { - - } - - @Override - public boolean visit(CobarShowStatus x) { - print("SHOW COBAR_STATUS"); - return false; - } - - @Override - public void endVisit(MySqlKillStatement x) { - - } - - @Override - public boolean visit(MySqlKillStatement x) { - if (MySqlKillStatement.Type.CONNECTION.equals(x.getType())) { - print("KILL CONNECTION "); - } else if (MySqlKillStatement.Type.QUERY.equals(x.getType())) { - print("KILL QUERY "); - } - x.getThreadId().accept(this); - return false; - } - - @Override - public void endVisit(MySqlBinlogStatement x) { - - } - - @Override - public boolean visit(MySqlBinlogStatement x) { - print("BINLOG "); - x.getExpr().accept(this); - return false; - } - - @Override - public void endVisit(MySqlResetStatement x) { - - } - - @Override - public boolean visit(MySqlResetStatement x) { - print("RESET "); - for (int i = 0; i < x.getOptions().size(); ++i) { - if (i != 0) { - print(", "); - } - print(x.getOptions().get(i)); - } - return false; - } + if (x.getWhere() != null) { + print(" WHERE "); + x.getWhere().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlLoadXmlStatement x) { + + } + + @Override + public boolean visit(MySqlLoadXmlStatement x) { + print("LOAD XML "); + + if (x.isLowPriority()) { + print("LOW_PRIORITY "); + } + + if (x.isConcurrent()) { + print("CONCURRENT "); + } + + if (x.isLocal()) { + print("LOCAL "); + } + + print("INFILE "); + + x.getFileName().accept(this); + + if (x.isReplicate()) { + print(" REPLACE "); + } + + if (x.isIgnore()) { + print(" IGNORE "); + } + + print(" INTO TABLE "); + x.getTableName().accept(this); + + if (x.getCharset() != null) { + print(" CHARSET "); + print(x.getCharset()); + } + + if (x.getRowsIdentifiedBy() != null) { + print(" ROWS IDENTIFIED BY "); + x.getRowsIdentifiedBy().accept(this); + } + + if (x.getSetList().size() != 0) { + print(" SET "); + printAndAccept(x.getSetList(), ", "); + } + + return false; + } + + @Override + public void endVisit(CobarShowStatus x) { + + } + + @Override + public boolean visit(CobarShowStatus x) { + print("SHOW COBAR_STATUS"); + return false; + } + + @Override + public void endVisit(MySqlKillStatement x) { + + } + + @Override + public boolean visit(MySqlKillStatement x) { + if (MySqlKillStatement.Type.CONNECTION.equals(x.getType())) { + print("KILL CONNECTION "); + } else if (MySqlKillStatement.Type.QUERY.equals(x.getType())) { + print("KILL QUERY "); + } + x.getThreadId().accept(this); + return false; + } + + @Override + public void endVisit(MySqlBinlogStatement x) { + + } + + @Override + public boolean visit(MySqlBinlogStatement x) { + print("BINLOG "); + x.getExpr().accept(this); + return false; + } + + @Override + public void endVisit(MySqlResetStatement x) { + + } + + @Override + public boolean visit(MySqlResetStatement x) { + print("RESET "); + for (int i = 0; i < x.getOptions().size(); ++i) { + if (i != 0) { + print(", "); + } + print(x.getOptions().get(i)); + } + return false; + } + + @Override + public void endVisit(MySqlCreateUserStatement x) { + + } + + @Override + public boolean visit(MySqlCreateUserStatement x) { + print("CREATE USER "); + printAndAccept(x.getUsers(), ", "); + return false; + } + + @Override + public void endVisit(UserSpecification x) { + + } + + @Override + public boolean visit(UserSpecification x) { + x.getUser().accept(this); + + if (x.getPassword() != null) { + print(" IDENTIFIED BY "); + x.getPassword().accept(this); + } + + if (x.getAuthPlugin() != null) { + print(" IDENTIFIED WITH "); + x.getAuthPlugin().accept(this); + } + return false; + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java index 8a3082c4af..19e1304bbc 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java @@ -15,6 +15,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr; import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlBinlogStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCommitStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement; @@ -26,6 +27,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplicateStatement import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlResetStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRollbackStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectGroupBy; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowColumnsStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement; @@ -38,376 +40,393 @@ import com.alibaba.druid.sql.visitor.SchemaStatVisitor; import com.alibaba.druid.stat.TableStat; import com.alibaba.druid.stat.TableStat.Mode; -public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements - MySqlASTVisitor { - - public boolean visit(MySqlDeleteStatement x) { - aliasLocal.set(new HashMap()); +public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlASTVisitor { - x.putAttribute("_original_use_mode", modeLocal.get()); - modeLocal.set(Mode.Delete); + public boolean visit(MySqlDeleteStatement x) { + aliasLocal.set(new HashMap()); - aliasLocal.set(new HashMap()); + x.putAttribute("_original_use_mode", modeLocal.get()); + modeLocal.set(Mode.Delete); - if (x.getTableNames().size() == 1) { - String ident = ((SQLIdentifierExpr) x.getTableNames().get(0)) - .getName(); - currentTableLocal.set(ident); - } + aliasLocal.set(new HashMap()); - for (SQLName tableName : x.getTableNames()) { - String ident = tableName.toString(); - TableStat stat = tableStats.get(ident); - if (stat == null) { - stat = new TableStat(); - tableStats.put(new TableStat.Name(ident), stat); - } - stat.incrementDeleteCount(); - } + if (x.getTableNames().size() == 1) { + String ident = ((SQLIdentifierExpr) x.getTableNames().get(0)).getName(); + currentTableLocal.set(ident); + } - accept(x.getWhere()); - accept(x.getFrom()); - accept(x.getUsing()); - accept(x.getOrderBy()); - accept(x.getLimit()); + for (SQLName tableName : x.getTableNames()) { + String ident = tableName.toString(); + TableStat stat = tableStats.get(ident); + if (stat == null) { + stat = new TableStat(); + tableStats.put(new TableStat.Name(ident), stat); + } + stat.incrementDeleteCount(); + } - return false; - } + accept(x.getWhere()); + accept(x.getFrom()); + accept(x.getUsing()); + accept(x.getOrderBy()); + accept(x.getLimit()); - public void endVisit(MySqlDeleteStatement x) { - aliasLocal.set(null); - } + return false; + } - @Override - public void endVisit(MySqlInsertStatement x) { - Mode originalMode = (Mode) x.getAttribute("_original_use_mode"); - modeLocal.set(originalMode); - } + public void endVisit(MySqlDeleteStatement x) { + aliasLocal.set(null); + } - @Override - public boolean visit(MySqlInsertStatement x) { - x.putAttribute("_original_use_mode", modeLocal.get()); - modeLocal.set(Mode.Insert); + @Override + public void endVisit(MySqlInsertStatement x) { + Mode originalMode = (Mode) x.getAttribute("_original_use_mode"); + modeLocal.set(originalMode); + } - aliasLocal.set(new HashMap()); + @Override + public boolean visit(MySqlInsertStatement x) { + x.putAttribute("_original_use_mode", modeLocal.get()); + modeLocal.set(Mode.Insert); - String originalTable = currentTableLocal.get(); + aliasLocal.set(new HashMap()); - if (x.getTableName() instanceof SQLIdentifierExpr) { - String ident = ((SQLIdentifierExpr) x.getTableName()).getName(); - currentTableLocal.set(ident); - x.putAttribute("_old_local_", originalTable); + String originalTable = currentTableLocal.get(); - TableStat stat = tableStats.get(ident); - if (stat == null) { - stat = new TableStat(); - tableStats.put(new TableStat.Name(ident), stat); - } - stat.incrementInsertCount(); + if (x.getTableName() instanceof SQLIdentifierExpr) { + String ident = ((SQLIdentifierExpr) x.getTableName()).getName(); + currentTableLocal.set(ident); + x.putAttribute("_old_local_", originalTable); - Map aliasMap = aliasLocal.get(); - if (aliasMap != null) { - if (x.getAlias() != null) { - aliasMap.put(x.getAlias(), ident); - } - aliasMap.put(ident, ident); - } - } + TableStat stat = tableStats.get(ident); + if (stat == null) { + stat = new TableStat(); + tableStats.put(new TableStat.Name(ident), stat); + } + stat.incrementInsertCount(); - accept(x.getColumns()); - accept(x.getValuesList()); - accept(x.getQuery()); - accept(x.getDuplicateKeyUpdate()); + Map aliasMap = aliasLocal.get(); + if (aliasMap != null) { + if (x.getAlias() != null) { + aliasMap.put(x.getAlias(), ident); + } + aliasMap.put(ident, ident); + } + } - return false; - } + accept(x.getColumns()); + accept(x.getValuesList()); + accept(x.getQuery()); + accept(x.getDuplicateKeyUpdate()); - @Override - public boolean visit(MySqlBooleanExpr x) { + return false; + } - return true; - } + @Override + public boolean visit(MySqlBooleanExpr x) { - @Override - public void endVisit(MySqlBooleanExpr x) { + return true; + } - } + @Override + public void endVisit(MySqlBooleanExpr x) { - @Override - public boolean visit(Limit x) { + } - return true; - } + @Override + public boolean visit(Limit x) { - @Override - public void endVisit(Limit x) { + return true; + } - } + @Override + public void endVisit(Limit x) { - @Override - public boolean visit(MySqlTableIndex x) { + } - return true; - } + @Override + public boolean visit(MySqlTableIndex x) { - @Override - public void endVisit(MySqlTableIndex x) { + return true; + } - } + @Override + public void endVisit(MySqlTableIndex x) { - @Override - public boolean visit(MySqlKey x) { + } - return true; - } + @Override + public boolean visit(MySqlKey x) { - @Override - public void endVisit(MySqlKey x) { + return true; + } - } + @Override + public void endVisit(MySqlKey x) { - @Override - public boolean visit(MySqlPrimaryKey x) { + } - return true; - } + @Override + public boolean visit(MySqlPrimaryKey x) { - @Override - public void endVisit(MySqlPrimaryKey x) { + return true; + } - } + @Override + public void endVisit(MySqlPrimaryKey x) { - @Override - public void endVisit(MySqlIntervalExpr x) { + } - } + @Override + public void endVisit(MySqlIntervalExpr x) { - @Override - public boolean visit(MySqlIntervalExpr x) { + } - return true; - } + @Override + public boolean visit(MySqlIntervalExpr x) { - @Override - public void endVisit(MySqlExtractExpr x) { + return true; + } - } + @Override + public void endVisit(MySqlExtractExpr x) { - @Override - public boolean visit(MySqlExtractExpr x) { + } - return true; - } + @Override + public boolean visit(MySqlExtractExpr x) { - @Override - public void endVisit(MySqlMatchAgainstExpr x) { + return true; + } - } + @Override + public void endVisit(MySqlMatchAgainstExpr x) { - @Override - public boolean visit(MySqlMatchAgainstExpr x) { + } - return true; - } + @Override + public boolean visit(MySqlMatchAgainstExpr x) { - @Override - public void endVisit(MySqlBinaryExpr x) { + return true; + } - } + @Override + public void endVisit(MySqlBinaryExpr x) { - @Override - public boolean visit(MySqlBinaryExpr x) { + } - return true; - } + @Override + public boolean visit(MySqlBinaryExpr x) { - @Override - public void endVisit(MySqlPrepareStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlPrepareStatement x) { - @Override - public boolean visit(MySqlPrepareStatement x) { + } - return true; - } + @Override + public boolean visit(MySqlPrepareStatement x) { - @Override - public void endVisit(MySqlExecuteStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlExecuteStatement x) { - @Override - public boolean visit(MySqlExecuteStatement x) { + } - return true; - } + @Override + public boolean visit(MySqlExecuteStatement x) { - @Override - public void endVisit(MySqlLoadDataInFileStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlLoadDataInFileStatement x) { - @Override - public boolean visit(MySqlLoadDataInFileStatement x) { + } - return true; - } + @Override + public boolean visit(MySqlLoadDataInFileStatement x) { - @Override - public void endVisit(MySqlLoadXmlStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlLoadXmlStatement x) { - @Override - public boolean visit(MySqlLoadXmlStatement x) { + } - return true; - } + @Override + public boolean visit(MySqlLoadXmlStatement x) { - @Override - public void endVisit(MySqlReplicateStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlReplicateStatement x) { - @Override - public boolean visit(MySqlReplicateStatement x) { + } - return true; - } + @Override + public boolean visit(MySqlReplicateStatement x) { - @Override - public void endVisit(MySqlSelectGroupBy x) { + return true; + } - } + @Override + public void endVisit(MySqlSelectGroupBy x) { - @Override - public boolean visit(MySqlSelectGroupBy x) { + } - return true; - } + @Override + public boolean visit(MySqlSelectGroupBy x) { - @Override - public void endVisit(MySqlStartTransactionStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlStartTransactionStatement x) { - @Override - public boolean visit(MySqlStartTransactionStatement x) { + } - return true; - } + @Override + public boolean visit(MySqlStartTransactionStatement x) { - @Override - public void endVisit(MySqlCommitStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlCommitStatement x) { - @Override - public boolean visit(MySqlCommitStatement x) { + } - return true; - } + @Override + public boolean visit(MySqlCommitStatement x) { - @Override - public void endVisit(MySqlRollbackStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlRollbackStatement x) { - @Override - public boolean visit(MySqlRollbackStatement x) { + } - return true; - } + @Override + public boolean visit(MySqlRollbackStatement x) { - @Override - public void endVisit(MySqlShowColumnsStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlShowColumnsStatement x) { - @Override - public boolean visit(MySqlShowColumnsStatement x) { - return true; - } + } - @Override - public void endVisit(MySqlShowTablesStatement x) { + @Override + public boolean visit(MySqlShowColumnsStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlShowTablesStatement x) { - @Override - public boolean visit(MySqlShowTablesStatement x) { - return true; - } + } - @Override - public void endVisit(MySqlShowDatabasesStatement x) { + @Override + public boolean visit(MySqlShowTablesStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlShowDatabasesStatement x) { - @Override - public boolean visit(MySqlShowDatabasesStatement x) { - return true; - } + } - @Override - public void endVisit(MySqlShowWarningsStatement x) { + @Override + public boolean visit(MySqlShowDatabasesStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlShowWarningsStatement x) { - @Override - public boolean visit(MySqlShowWarningsStatement x) { - return true; - } + } - @Override - public void endVisit(MySqlShowStatusStatement x) { + @Override + public boolean visit(MySqlShowWarningsStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlShowStatusStatement x) { - @Override - public boolean visit(MySqlShowStatusStatement x) { - return true; - } + } - @Override - public void endVisit(CobarShowStatus x) { + @Override + public boolean visit(MySqlShowStatusStatement x) { + return true; + } - } + @Override + public void endVisit(CobarShowStatus x) { - @Override - public boolean visit(CobarShowStatus x) { - return true; - } + } - @Override - public void endVisit(MySqlKillStatement x) { + @Override + public boolean visit(CobarShowStatus x) { + return true; + } - } + @Override + public void endVisit(MySqlKillStatement x) { - @Override - public boolean visit(MySqlKillStatement x) { - return true; - } + } - @Override - public void endVisit(MySqlBinlogStatement x) { + @Override + public boolean visit(MySqlKillStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlBinlogStatement x) { - @Override - public boolean visit(MySqlBinlogStatement x) { - return true; - } + } - @Override - public void endVisit(MySqlResetStatement x) { + @Override + public boolean visit(MySqlBinlogStatement x) { + return true; + } - } + @Override + public void endVisit(MySqlResetStatement x) { - @Override - public boolean visit(MySqlResetStatement x) { - return true; - } + } + @Override + public boolean visit(MySqlResetStatement x) { + return true; + } + + @Override + public void endVisit(MySqlCreateUserStatement x) { + + } + + @Override + public boolean visit(MySqlCreateUserStatement x) { + return true; + } + + @Override + public void endVisit(UserSpecification x) { + + } + + @Override + public boolean visit(UserSpecification x) { + return true; + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/parser/PGLexer.java b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/parser/PGLexer.java index e224f4bb04..6b3322f565 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/postgresql/parser/PGLexer.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/postgresql/parser/PGLexer.java @@ -83,6 +83,7 @@ public class PGLexer extends Lexer { map.put("SOME", Token.SOME); map.put("ANY", Token.ANY); map.put("TRUNCATE", Token.TRUNCATE); + map.put("USER", Token.USER); map.put("LIMIT", Token.LIMIT); map.put("OFFSET", Token.OFFSET); diff --git a/src/main/java/com/alibaba/druid/sql/parser/Token.java b/src/main/java/com/alibaba/druid/sql/parser/Token.java index 610c88e908..69189a70a3 100644 --- a/src/main/java/com/alibaba/druid/sql/parser/Token.java +++ b/src/main/java/com/alibaba/druid/sql/parser/Token.java @@ -67,6 +67,7 @@ public enum Token { ON("ON"), SCHEMA("SCHEMA"), CAST("CAST"), + USER("USER"), AND("AND"), OR("OR"), @@ -99,6 +100,8 @@ public enum Token { FALSE("FALSE"), LIMIT("LIMIT"), KILL("KILL"), + IDENTIFIED("IDENTIFIED"), + PASSWORD("PASSWORD"), //postgresql WINDOW("WINDOW"), diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/CommitTest.java b/src/test/java/com/alibaba/druid/bvt/sql/mysql/CommitTest.java new file mode 100644 index 0000000000..910818fee1 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/mysql/CommitTest.java @@ -0,0 +1,34 @@ +package com.alibaba.druid.bvt.sql.mysql; + +import java.util.List; + +import junit.framework.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class CommitTest extends MysqlTest { + + public void test_0() throws Exception { + String sql = "COMMIT;"; + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List statementList = parser.parseStatementList(); + SQLStatement statemen = statementList.get(0); + System.out.println(output(statementList)); + + Assert.assertEquals(1, statementList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + statemen.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getFields()); + + Assert.assertTrue(visitor.getTables().size() == 0); + } + + +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/CreateUserTest.java b/src/test/java/com/alibaba/druid/bvt/sql/mysql/CreateUserTest.java new file mode 100644 index 0000000000..a13c490c77 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/mysql/CreateUserTest.java @@ -0,0 +1,34 @@ +package com.alibaba.druid.bvt.sql.mysql; + +import java.util.List; + +import junit.framework.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; + +public class CreateUserTest extends MysqlTest { + + public void test_0() throws Exception { + String sql = "CREATE USER 'jeffrey'@'localhost' IDENTIFIED BY 'mypass';"; + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List statementList = parser.parseStatementList(); + SQLStatement statemen = statementList.get(0); + System.out.println(output(statementList)); + + Assert.assertEquals(1, statementList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + statemen.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getFields()); + + Assert.assertTrue(visitor.getTables().size() == 0); + } + + +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/mysql/DropTableTest.java b/src/test/java/com/alibaba/druid/bvt/sql/mysql/DropTableTest.java new file mode 100644 index 0000000000..c151ffd305 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/mysql/DropTableTest.java @@ -0,0 +1,41 @@ +package com.alibaba.druid.bvt.sql.mysql; + +import java.util.List; + +import junit.framework.Assert; + +import com.alibaba.druid.sql.MysqlTest; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; +import com.alibaba.druid.stat.TableStat; + +public class DropTableTest extends MysqlTest { + + public void test_0() throws Exception { + String sql = "DROP TABLE films, distributors;"; + + MySqlStatementParser parser = new MySqlStatementParser(sql); + List statementList = parser.parseStatementList(); + SQLStatement statemen = statementList.get(0); + System.out.println(output(statementList)); + + Assert.assertEquals(1, statementList.size()); + + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + statemen.accept(visitor); + + System.out.println("Tables : " + visitor.getTables()); + System.out.println("fields : " + visitor.getFields()); + + Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("films"))); + Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("distributors"))); + + Assert.assertTrue(visitor.getTables().get(new TableStat.Name("films")).getDropCount() == 1); + Assert.assertTrue(visitor.getTables().get(new TableStat.Name("distributors")).getDropCount() == 1); + + Assert.assertTrue(visitor.getFields().size() == 0); + } + + +} diff --git a/src/test/java/com/alibaba/druid/sql/MysqlTest.java b/src/test/java/com/alibaba/druid/sql/MysqlTest.java new file mode 100644 index 0000000000..9c03e5b139 --- /dev/null +++ b/src/test/java/com/alibaba/druid/sql/MysqlTest.java @@ -0,0 +1,22 @@ +package com.alibaba.druid.sql; + +import java.util.List; + +import junit.framework.TestCase; + +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor; +import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor; + +public class MysqlTest extends TestCase { + protected String output(List stmtList) { + StringBuilder out = new StringBuilder(); + MySqlOutputVisitor visitor = new MySqlOutputVisitor(out); + + for (SQLStatement stmt : stmtList) { + stmt.accept(visitor); + } + + return out.toString(); + } +}