Search in sources :

Example 1 with SchemaStatVisitor

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;
    }
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Map(java.util.Map) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 2 with SchemaStatVisitor

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());
}
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 3 with SchemaStatVisitor

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());
}
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 4 with SchemaStatVisitor

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());
}
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 5 with SchemaStatVisitor

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());
}
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)

Aggregations

SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)437 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)434 TableStat (com.alibaba.druid.stat.TableStat)68 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)61 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)51 SQLUtils (com.alibaba.druid.sql.SQLUtils)35 DbType (com.alibaba.druid.DbType)21 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)12 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)12 Test (org.junit.Test)12 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)11 SchemaRepository (com.alibaba.druid.sql.repository.SchemaRepository)11 Column (com.alibaba.druid.stat.TableStat.Column)10 InputStream (java.io.InputStream)9 InputStreamReader (java.io.InputStreamReader)9 Reader (java.io.Reader)9 OdpsStatementParser (com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser)6 OdpsSchemaStatVisitor (com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor)6 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)5 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4