Search in sources :

Example 21 with TableStat

use of com.alibaba.druid.stat.TableStat 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());
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 22 with TableStat

use of com.alibaba.druid.stat.TableStat 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());
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLUtils(com.alibaba.druid.sql.SQLUtils) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 23 with TableStat

use of com.alibaba.druid.stat.TableStat 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());
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLUtils(com.alibaba.druid.sql.SQLUtils) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 24 with TableStat

use of com.alibaba.druid.stat.TableStat 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());
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLUtils(com.alibaba.druid.sql.SQLUtils) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 25 with TableStat

use of com.alibaba.druid.stat.TableStat in project druid by alibaba.

the class MySqlAlterTableTest_addFk method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = //
    "alter table Test2 add index FK4CF5DC0F5DD7C31 (test1_name), " + "add constraint FK4CF5DC0F5DD7C31 foreign key (test1_name) references Test1 (name)";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
    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(//
    "ALTER TABLE Test2" + //
    "\n\tADD INDEX FK4CF5DC0F5DD7C31 (test1_name)," + "\n\tADD CONSTRAINT FK4CF5DC0F5DD7C31 FOREIGN KEY (test1_name) REFERENCES Test1 (name)", SQLUtils.toMySqlString(stmt));
    Assert.assertEquals(//
    "alter table Test2" + //
    "\n\tadd index FK4CF5DC0F5DD7C31 (test1_name)," + "\n\tadd constraint FK4CF5DC0F5DD7C31 foreign key (test1_name) references Test1 (name)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    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());
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Aggregations

TableStat (com.alibaba.druid.stat.TableStat)45 SQLName (com.alibaba.druid.sql.ast.SQLName)21 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)10 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)9 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)9 SQLUtils (com.alibaba.druid.sql.SQLUtils)6 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)3 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)3 Mode (com.alibaba.druid.stat.TableStat.Mode)2 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLAlterTableItem (com.alibaba.druid.sql.ast.statement.SQLAlterTableItem)1 SQLDropIndexStatement (com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement)1 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)1 Name (com.alibaba.druid.stat.TableStat.Name)1 Map (java.util.Map)1