mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser SchemaStatVisitor.
This commit is contained in:
@@ -663,5 +663,11 @@ public class SQLUtils {
|
||||
List<SQLStatement> stmtList = parseStatements(sql, dbType);
|
||||
return SQLUtils.toSQLString(stmtList, dbType, null, null, tableMapping);
|
||||
}
|
||||
|
||||
public static boolean containsIndexDDL(String sql, String dbType) {
|
||||
List<SQLStatement> stmtList = parseStatements(sql, dbType);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 1999-2101 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.ast;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
@@ -122,7 +122,9 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
tableName = handleName(tableName);
|
||||
TableStat stat = tableStats.get(tableName);
|
||||
|
||||
TableStat.Name tableNameObj = new TableStat.Name(tableName);
|
||||
TableStat stat = tableStats.get(tableNameObj);
|
||||
if (stat == null) {
|
||||
stat = new TableStat();
|
||||
tableStats.put(new TableStat.Name(tableName), stat);
|
||||
@@ -1452,6 +1454,10 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
for (SQLSelectOrderByItem item : x.getItems()) {
|
||||
item.accept(this);
|
||||
}
|
||||
|
||||
String table = ((SQLAlterTableStatement) x.getParent()).getName().toString();
|
||||
TableStat tableStat = this.getTableStat(table);
|
||||
tableStat.incrementCreateIndexCount();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1694,4 +1700,48 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
public boolean visit(SQLCreateSequenceStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableAddConstraint x) {
|
||||
SQLConstraint constraint = x.getConstraint();
|
||||
if (constraint instanceof SQLUniqueConstraint) {
|
||||
SQLAlterTableStatement stmt = (SQLAlterTableStatement) x.getParent();
|
||||
String tableName = stmt.getName().toString();
|
||||
|
||||
TableStat tableStat = this.getTableStat(tableName);
|
||||
tableStat.incrementCreateIndexCount();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableDropIndex x) {
|
||||
SQLAlterTableStatement stmt = (SQLAlterTableStatement) x.getParent();
|
||||
String tableName = stmt.getName().toString();
|
||||
|
||||
TableStat tableStat = this.getTableStat(tableName);
|
||||
tableStat.incrementDropIndexCount();
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableDropPrimaryKey x) {
|
||||
SQLAlterTableStatement stmt = (SQLAlterTableStatement) x.getParent();
|
||||
String tableName = stmt.getName().toString();
|
||||
|
||||
TableStat tableStat = this.getTableStat(tableName);
|
||||
tableStat.incrementDropIndexCount();
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableDropKey x) {
|
||||
SQLAlterTableStatement stmt = (SQLAlterTableStatement) x.getParent();
|
||||
String tableName = stmt.getName().toString();
|
||||
|
||||
TableStat tableStat = this.getTableStat(tableName);
|
||||
tableStat.incrementDropIndexCount();
|
||||
return false;
|
||||
}
|
||||
//
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.alter;
|
||||
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
@@ -37,6 +40,14 @@ public class MySqlAlterTableAddIndex_0 extends TestCase {
|
||||
|
||||
Assert.assertEquals("alter table `test`.`tb1`" + //
|
||||
"\n\tadd index `ix` (`f2` asc)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
|
||||
TableStat tableStat = visitor.getTableStat("test.tb1");
|
||||
assertNotNull(tableStat);
|
||||
|
||||
assertEquals(1, tableStat.getCreateIndexCount());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,10 @@
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.alter;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -36,6 +40,13 @@ public class MySqlAlterTableAddIndex_1 extends TestCase {
|
||||
|
||||
Assert.assertEquals("alter table `test`.`tb1`" + //
|
||||
"\n\tadd unique index `ix2` (`fid` asc)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
TableStat tableStat = visitor.getTableStat("test.tb1");
|
||||
assertNotNull(tableStat);
|
||||
assertEquals(1, tableStat.getAlterCount());
|
||||
assertEquals(1, tableStat.getCreateIndexCount());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.alter;
|
||||
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -38,6 +41,13 @@ public class MySqlAlterTableAddPrimaryKey extends TestCase {
|
||||
Assert.assertEquals("alter table `test`.`tb1`" + //
|
||||
"\n\tchange column `fid` `fid` INT(11) not null default null,\n\t" + //
|
||||
"add primary key (`fid`)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
TableStat tableStat = visitor.getTableStat("test.tb1");
|
||||
assertNotNull(tableStat);
|
||||
assertEquals(1, tableStat.getAlterCount());
|
||||
assertEquals(1, tableStat.getCreateIndexCount());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.alter;
|
||||
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
@@ -37,6 +40,13 @@ public class MySqlAlterTableAddPrimaryKey_1 extends TestCase {
|
||||
|
||||
Assert.assertEquals("alter table tabelname" + //
|
||||
"\n\tadd constraint mYconstraint primary key (id)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
TableStat tableStat = visitor.getTableStat("tabelname");
|
||||
assertNotNull(tableStat);
|
||||
assertEquals(1, tableStat.getAlterCount());
|
||||
assertEquals(1, tableStat.getCreateIndexCount());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.alter;
|
||||
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
@@ -37,6 +40,13 @@ public class MySqlAlterTableAddUniqueTest extends TestCase {
|
||||
|
||||
Assert.assertEquals("alter table icp.wx_msg"
|
||||
+ "\n\tadd unique (msgId, msgType, event, eventKey)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
TableStat tableStat = visitor.getTableStat("icp.wx_msg");
|
||||
assertNotNull(tableStat);
|
||||
assertEquals(1, tableStat.getAlterCount());
|
||||
assertEquals(1, tableStat.getCreateIndexCount());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.alter;
|
||||
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -36,6 +39,13 @@ public class MySqlAlterTableDropIndex_0 extends TestCase {
|
||||
|
||||
Assert.assertEquals("alter table `test`.`tb1`" + //
|
||||
"\n\tdrop index `ix`", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
TableStat tableStat = visitor.getTableStat("test.tb1");
|
||||
assertNotNull(tableStat);
|
||||
assertEquals(1, tableStat.getAlterCount());
|
||||
assertEquals(1, tableStat.getDropIndexCount());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.alter;
|
||||
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
@@ -37,6 +40,13 @@ public class MySqlAlterTableDropKey extends TestCase {
|
||||
|
||||
Assert.assertEquals("alter table t6" + //
|
||||
"\n\tdrop key v", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
TableStat tableStat = visitor.getTableStat("t6");
|
||||
assertNotNull(tableStat);
|
||||
assertEquals(1, tableStat.getAlterCount());
|
||||
assertEquals(1, tableStat.getDropIndexCount());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql.alter;
|
||||
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
@@ -34,7 +37,7 @@ public class MySqlAlterTableTest_addFk extends TestCase {
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
stmt.accept(visitor);
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
@@ -54,5 +57,10 @@ public class MySqlAlterTableTest_addFk extends TestCase {
|
||||
|
||||
Assert.assertEquals(2, visitor.getTables().size());
|
||||
Assert.assertEquals(2, visitor.getColumns().size());
|
||||
|
||||
TableStat tableStat = visitor.getTableStat("Test2");
|
||||
assertNotNull(tableStat);
|
||||
assertEquals(1, tableStat.getAlterCount());
|
||||
assertEquals(1, tableStat.getCreateIndexCount());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user