mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
StatSchema support DropIndex
This commit is contained in:
@@ -51,6 +51,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
@@ -1156,4 +1157,23 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
public boolean visit(SQLAlterTableDropConstraint x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropIndexStatement x) {
|
||||
setMode(x, Mode.DropIndex);
|
||||
SQLName name = (SQLName) x.getTableName().getExpr();
|
||||
|
||||
String ident = name.toString();
|
||||
setCurrentTable(ident);
|
||||
|
||||
TableStat stat = getTableStat(ident);
|
||||
stat.incrementDropIndexCount();
|
||||
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
if (aliasMap != null) {
|
||||
aliasMap.put(ident, ident);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,15 @@ public class TableStat {
|
||||
int createCount = 0;
|
||||
int alterCount = 0;
|
||||
int createIndexCount = 0;
|
||||
int dropIndexCount = 0;
|
||||
|
||||
public int getDropIndexCount() {
|
||||
return dropIndexCount;
|
||||
}
|
||||
|
||||
public void incrementDropIndexCount() {
|
||||
this.dropIndexCount++;
|
||||
}
|
||||
|
||||
public int getCreateIndexCount() {
|
||||
return createIndexCount;
|
||||
@@ -155,6 +164,9 @@ public class TableStat {
|
||||
if (createIndexCount > 0) {
|
||||
buf.append("CreateIndex");
|
||||
}
|
||||
if (dropIndexCount > 0) {
|
||||
buf.append("DropIndex");
|
||||
}
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
@@ -497,7 +509,7 @@ public class TableStat {
|
||||
Merge(16), //
|
||||
Truncate(32), //
|
||||
Alter(64), //
|
||||
Drop(128);
|
||||
Drop(128), DropIndex(256); //
|
||||
|
||||
public final int mark;
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.DruidDataSourceStatValue;
|
||||
import com.alibaba.druid.stat.DruidStatService;
|
||||
import com.alibaba.druid.stat.JdbcSqlStatValue;
|
||||
import com.alibaba.druid.support.json.JSONUtils;
|
||||
|
||||
public class StatTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:h2:mem:test;");
|
||||
dataSource.setFilters("stat");
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_0() throws Exception {
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement("select * from INFORMATION_SCHEMA.SETTINGS");
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
rs.close();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
DruidDataSourceStatValue dataSourceStatValue = dataSource.getStatValueAndReset();
|
||||
Assert.assertEquals(1, dataSourceStatValue.getSqlList().size());
|
||||
JdbcSqlStatValue sqlStat = dataSourceStatValue.getSqlList().get(0);
|
||||
Assert.assertNotNull(sqlStat.getExecuteLastStartTime());
|
||||
Assert.assertNotNull(sqlStat.getExecuteNanoSpanMaxOccurTime());
|
||||
Assert.assertTrue(sqlStat.getExecuteMillisMax() > 0);
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement("select * from INFORMATION_SCHEMA.SETTINGS");
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
rs.close();
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
String json = DruidStatService.getInstance().service("/sql-" + sqlStat.getId() + ".json");
|
||||
Map map = (Map) JSONUtils.parse(json);
|
||||
System.out.println(json);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.bvt.sql.mysql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
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 MySqlDropIndexTest extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "drop index index_name on table_name ";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLDropIndexStatement stmt = (SQLDropIndexStatement) statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(0, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("table_name")));
|
||||
TableStat tableStat = visitor.getTables().get(new TableStat.Name("table_name"));
|
||||
Assert.assertEquals(1, tableStat.getDropIndexCount());
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user