use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class ExportTables method evaluate.
public String evaluate(String sql, String dbType) {
try {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
for (SQLStatement stmt : statementList) {
stmt.accept(visitor);
}
StringBuffer buf = new StringBuffer();
for (Map.Entry<TableStat.Name, TableStat> entry : visitor.getTables().entrySet()) {
TableStat.Name name = entry.getKey();
if (buf.length() != 0) {
buf.append(',');
}
buf.append(name.toString());
}
return buf.toString();
} catch (Throwable ex) {
System.err.println("error sql : " + sql);
ex.printStackTrace();
return null;
}
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class MySqlAlterTableAddIndex_0 method test_alter_first.
public void test_alter_first() throws Exception {
String sql = "ALTER TABLE `test`.`tb1` ADD INDEX `ix` (`f2` ASC) ;";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
Assert.assertEquals(//
"ALTER TABLE `test`.`tb1`" + "\n\tADD INDEX `ix` (`f2` ASC)", SQLUtils.toMySqlString(stmt));
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());
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class MySqlAlterTableAddIndex_1 method test_alter_first.
public void test_alter_first() throws Exception {
String sql = "ALTER TABLE `test`.`tb1` ADD UNIQUE INDEX `ix2` (`fid` ASC) ;";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
Assert.assertEquals(//
"ALTER TABLE `test`.`tb1`" + "\n\tADD UNIQUE INDEX `ix2` (`fid` ASC)", SQLUtils.toMySqlString(stmt));
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());
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class MySqlAlterTableAddPrimaryKey method test_alter_first.
public void test_alter_first() throws Exception {
String sql = "ALTER TABLE `test`.`tb1` CHANGE COLUMN `fid` `fid` INT(11) NOT NULL DEFAULT NULL, ADD PRIMARY KEY (`fid`) ;";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
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));
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());
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class MySqlAlterTableAddPrimaryKey_1 method test_alter_first.
public void test_alter_first() throws Exception {
String sql = "alter table tabelname add constraint mYconstraint primary key(id)";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
Assert.assertEquals(//
"ALTER TABLE tabelname" + "\n\tADD CONSTRAINT mYconstraint PRIMARY KEY (id)", SQLUtils.toMySqlString(stmt));
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());
}
Aggregations