mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
mysql show open tables
This commit is contained in:
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
public class MySqlShowOpenTablesStatement extends MySqlStatementImpl {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private SQLExpr database;
|
||||
private SQLExpr like;
|
||||
private SQLExpr where;
|
||||
|
||||
public SQLExpr getLike() {
|
||||
return like;
|
||||
}
|
||||
|
||||
public void setLike(SQLExpr like) {
|
||||
this.like = like;
|
||||
}
|
||||
|
||||
public SQLExpr getWhere() {
|
||||
return where;
|
||||
}
|
||||
|
||||
public void setWhere(SQLExpr where) {
|
||||
this.where = where;
|
||||
}
|
||||
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, database);
|
||||
acceptChild(visitor, like);
|
||||
acceptChild(visitor, where);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
public void setDatabase(SQLExpr database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -82,6 +82,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStateme
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
|
||||
@@ -798,6 +799,28 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
if (identifierEquals("OPEN")) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("TABLES");
|
||||
MySqlShowOpenTablesStatement stmt = new MySqlShowOpenTablesStatement();
|
||||
|
||||
if (lexer.token() == Token.FROM || lexer.token() == Token.IN) {
|
||||
lexer.nextToken();
|
||||
stmt.setDatabase(this.exprParser.name());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.LIKE) {
|
||||
lexer.nextToken();
|
||||
stmt.setLike(this.exprParser.expr());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.WHERE) {
|
||||
lexer.nextToken();
|
||||
stmt.setWhere(this.exprParser.expr());
|
||||
}
|
||||
return stmt;
|
||||
}
|
||||
|
||||
throw new ParserException("TODO " + lexer.stringVal());
|
||||
}
|
||||
|
||||
@@ -73,6 +73,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStateme
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
|
||||
@@ -346,4 +347,8 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
boolean visit(MySqlShowMasterStatusStatement x);
|
||||
|
||||
void endVisit(MySqlShowMasterStatusStatement x);
|
||||
|
||||
boolean visit(MySqlShowOpenTablesStatement x);
|
||||
|
||||
void endVisit(MySqlShowOpenTablesStatement x);
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStateme
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
|
||||
@@ -750,4 +751,14 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
|
||||
public void endVisit(MySqlShowMasterStatusStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlShowOpenTablesStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlShowOpenTablesStatement x) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +92,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStateme
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
|
||||
@@ -1877,4 +1878,31 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
public void endVisit(MySqlShowMasterStatusStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlShowOpenTablesStatement x) {
|
||||
print("SHOW OPEN 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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlShowOpenTablesStatement x) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStateme
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
|
||||
@@ -813,4 +814,14 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
public void endVisit(MySqlShowMasterStatusStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlShowOpenTablesStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlShowOpenTablesStatement x) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStateme
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterStatusStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowOpenTablesStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
@@ -706,55 +707,53 @@ public class DALParserTest extends TestCase {
|
||||
Assert.assertEquals("SHOW MASTER STATUS", output);
|
||||
}
|
||||
|
||||
public void test_open_tables() throws Exception {
|
||||
String sql = "SHOW OPEN TABLES";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
MySqlShowOpenTablesStatement show = (MySqlShowOpenTablesStatement) parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(show);
|
||||
Assert.assertEquals("SHOW OPEN TABLES", output);
|
||||
}
|
||||
|
||||
public void test_open_tables_1() throws Exception {
|
||||
String sql = "SHOW OPEN TABLES FROM mysql";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
MySqlShowOpenTablesStatement show = (MySqlShowOpenTablesStatement) parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(show);
|
||||
Assert.assertEquals("SHOW OPEN TABLES FROM mysql", output);
|
||||
}
|
||||
|
||||
public void test_open_tables_2() throws Exception {
|
||||
String sql = "SHOW OPEN TABLES in mysql";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
MySqlShowOpenTablesStatement show = (MySqlShowOpenTablesStatement) parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(show);
|
||||
Assert.assertEquals("SHOW OPEN TABLES FROM mysql", output);
|
||||
}
|
||||
|
||||
public void test_open_tables_3() throws Exception {
|
||||
String sql = "SHOW OPEN TABLES in mysql like '%'";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
MySqlShowOpenTablesStatement show = (MySqlShowOpenTablesStatement) parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(show);
|
||||
Assert.assertEquals("SHOW OPEN TABLES FROM mysql LIKE '%'", output);
|
||||
}
|
||||
|
||||
public void test_open_tables_4() throws Exception {
|
||||
String sql = "SHOW OPEN TABLES in mysql where 1 = 1";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
MySqlShowOpenTablesStatement show = (MySqlShowOpenTablesStatement) parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(show);
|
||||
Assert.assertEquals("SHOW OPEN TABLES FROM mysql WHERE 1 = 1", output);
|
||||
}
|
||||
|
||||
//
|
||||
// public void testShow() throws Exception {
|
||||
// sql = "SHOW master status";
|
||||
// lexer = new SQLLexer(sql);
|
||||
// parser = new DALParser(lexer, new SQLExprParser(lexer));
|
||||
// show = (DALShowStatement) parser.show();
|
||||
// parser.match(Token.EOF);
|
||||
// output = output2MySQL(show, sql);
|
||||
// Assert.assertEquals("SHOW MASTER STATUS", output);
|
||||
//
|
||||
// sql = "SHOW open tables from db like 'expr'";
|
||||
// lexer = new SQLLexer(sql);
|
||||
// parser = new DALParser(lexer, new SQLExprParser(lexer));
|
||||
// show = (DALShowStatement) parser.show();
|
||||
// parser.match(Token.EOF);
|
||||
// output = output2MySQL(show, sql);
|
||||
// Assert.assertEquals("SHOW OPEN TABLES FROM db LIKE 'expr'", output);
|
||||
//
|
||||
// sql = "SHOW open tables from db where tb is not null";
|
||||
// lexer = new SQLLexer(sql);
|
||||
// parser = new DALParser(lexer, new SQLExprParser(lexer));
|
||||
// show = (DALShowStatement) parser.show();
|
||||
// parser.match(Token.EOF);
|
||||
// output = output2MySQL(show, sql);
|
||||
// Assert.assertEquals("SHOW OPEN TABLES FROM db WHERE tb IS NOT NULL", output);
|
||||
//
|
||||
// sql = "SHOW open tables from db";
|
||||
// lexer = new SQLLexer(sql);
|
||||
// parser = new DALParser(lexer, new SQLExprParser(lexer));
|
||||
// show = (DALShowStatement) parser.show();
|
||||
// parser.match(Token.EOF);
|
||||
// output = output2MySQL(show, sql);
|
||||
// Assert.assertEquals("SHOW OPEN TABLES FROM db", output);
|
||||
//
|
||||
// sql = "SHOW open tables in db";
|
||||
// lexer = new SQLLexer(sql);
|
||||
// parser = new DALParser(lexer, new SQLExprParser(lexer));
|
||||
// show = (DALShowStatement) parser.show();
|
||||
// parser.match(Token.EOF);
|
||||
// output = output2MySQL(show, sql);
|
||||
// Assert.assertEquals("SHOW OPEN TABLES FROM db", output);
|
||||
//
|
||||
// sql = "SHOW open tables";
|
||||
// lexer = new SQLLexer(sql);
|
||||
// parser = new DALParser(lexer, new SQLExprParser(lexer));
|
||||
// show = (DALShowStatement) parser.show();
|
||||
// parser.match(Token.EOF);
|
||||
// output = output2MySQL(show, sql);
|
||||
// Assert.assertEquals("SHOW OPEN TABLES", output);
|
||||
//
|
||||
// sql = "SHOW plugins";
|
||||
// lexer = new SQLLexer(sql);
|
||||
|
||||
Reference in New Issue
Block a user