Search in sources :

Example 26 with MySqlSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor in project druid by alibaba.

the class MySqlAlterTableTest12 method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = "ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    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());
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "ALTER TABLE tbl_name" + "\n\tDROP FOREIGN KEY fk_symbol", output);
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 27 with MySqlSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor in project druid by alibaba.

the class MySqlAlterTableTest14 method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = "ALTER TABLE tbl_name IMPORT TABLESPACE;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    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());
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "ALTER TABLE tbl_name" + "\n\tIMPORT TABLESPACE", output);
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 28 with MySqlSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor in project druid by alibaba.

the class MySqlCreateTableTest54 method test_FOREIGN_KEY.

@Test
public void test_FOREIGN_KEY() throws Exception {
    String sql = "CREATE TABLE `t_activity_node` (" + "\n`id` bigint(20) NOT NULL," + "\n`sellerId` bigint(20) DEFAULT NULL," + "\n`canvas_id` bigint(20) NOT NULL COMMENT '画布ID'," + "\n`view_node_id` bigint(20) NOT NULL COMMENT '对应显示的节点id'," + "\n`activity_type` int(11) NOT NULL COMMENT '活动类型'," + "\n`node_type` int(11) NOT NULL COMMENT '节点类型'," + "\n`node_title` varchar(200) NOT NULL COMMENT '节点标题'," + "\n`status` int(11) NOT NULL DEFAULT '0' COMMENT '页面的显示状态'," + "\n`update_status` int(11) DEFAULT NULL COMMENT '节点创建后的修改状态'," + "\n`execute_status` int(11) DEFAULT NULL COMMENT '节点当前的执行状态'," + "\n`start_time` datetime DEFAULT NULL COMMENT '该节点活动的开始时间'," + "\n`end_time` datetime DEFAULT NULL COMMENT '该节点活动的结束时间'," + "\n`activity_start_time` datetime DEFAULT NULL COMMENT '营销活动的开始时间'," + "\n`activity_end_time` datetime DEFAULT NULL COMMENT '营销活动的结束时间'," + "\n`report_start_time` datetime DEFAULT NULL COMMENT '该节点活动效果报告的开始时间'," + "\n`report_end_time` datetime DEFAULT NULL COMMENT '该节点活动效果报告的结束时间'," + "\n`cron_rule` varchar(100) DEFAULT NULL COMMENT '周期性营销的时间表达式'," + "\n`split_rule` varchar(1000) DEFAULT NULL COMMENT '节点拆分规则'," + "\n`search_json` varchar(5000) DEFAULT NULL COMMENT '索引查找的条件字符串'," + "\n`filter_json` varchar(1000) DEFAULT NULL COMMENT '各种过滤条件的设置'," + "\n`buyer_count` int(11) DEFAULT NULL COMMENT '营销的会员数量'," + "\n`gmt_modified` datetime NOT NULL COMMENT '活动最后修改时间'," + "\n`gmt_create` datetime NOT NULL COMMENT '活动创建时间'," + "\nPRIMARY KEY (`id`)," + "\nKEY `canvas_id` (`canvas_id`)," + "\nKEY `sid_ty_time` (`sellerId`,`node_type`,`start_time`)," + "\nCONSTRAINT `t_activity_node_ibfk_1` FOREIGN KEY (`canvas_id`) REFERENCES `t_activity_canvas` (`id`) ON DELETE CASCADE" + "\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();
    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());
    Assert.assertEquals(2, visitor.getTables().size());
    Assert.assertEquals(24, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    {
        String output = SQLUtils.toMySqlString(stmt);
        Assert.assertEquals("CREATE TABLE `t_activity_node` (" + "\n\t`id` bigint(20) NOT NULL, " + "\n\t`sellerId` bigint(20) DEFAULT NULL, " + "\n\t`canvas_id` bigint(20) NOT NULL COMMENT '画布ID', " + "\n\t`view_node_id` bigint(20) NOT NULL COMMENT '对应显示的节点id', " + "\n\t`activity_type` int(11) NOT NULL COMMENT '活动类型', " + "\n\t`node_type` int(11) NOT NULL COMMENT '节点类型', " + "\n\t`node_title` varchar(200) NOT NULL COMMENT '节点标题', " + "\n\t`status` int(11) NOT NULL DEFAULT '0' COMMENT '页面的显示状态', " + "\n\t`update_status` int(11) DEFAULT NULL COMMENT '节点创建后的修改状态', " + "\n\t`execute_status` int(11) DEFAULT NULL COMMENT '节点当前的执行状态', " + "\n\t`start_time` datetime DEFAULT NULL COMMENT '该节点活动的开始时间', " + "\n\t`end_time` datetime DEFAULT NULL COMMENT '该节点活动的结束时间', " + "\n\t`activity_start_time` datetime DEFAULT NULL COMMENT '营销活动的开始时间', " + "\n\t`activity_end_time` datetime DEFAULT NULL COMMENT '营销活动的结束时间', " + "\n\t`report_start_time` datetime DEFAULT NULL COMMENT '该节点活动效果报告的开始时间', " + "\n\t`report_end_time` datetime DEFAULT NULL COMMENT '该节点活动效果报告的结束时间', " + "\n\t`cron_rule` varchar(100) DEFAULT NULL COMMENT '周期性营销的时间表达式', " + "\n\t`split_rule` varchar(1000) DEFAULT NULL COMMENT '节点拆分规则', " + "\n\t`search_json` varchar(5000) DEFAULT NULL COMMENT '索引查找的条件字符串', " + "\n\t`filter_json` varchar(1000) DEFAULT NULL COMMENT '各种过滤条件的设置', " + "\n\t`buyer_count` int(11) DEFAULT NULL COMMENT '营销的会员数量', " + "\n\t`gmt_modified` datetime NOT NULL COMMENT '活动最后修改时间', " + "\n\t`gmt_create` datetime NOT NULL COMMENT '活动创建时间', " + "\n\tPRIMARY KEY (`id`), " + "\n\tKEY `canvas_id` (`canvas_id`), " + "\n\tKEY `sid_ty_time` (`sellerId`, `node_type`, `start_time`), " + "\n\tCONSTRAINT `t_activity_node_ibfk_1` FOREIGN KEY (`canvas_id`) REFERENCES `t_activity_canvas` (`id`) ON DELETE CASCADE ON UPDATE CASCADE" + "\n) ENGINE = InnoDB CHARSET = utf8", output);
    }
    {
        String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
        Assert.assertEquals("create table `t_activity_node` (" + "\n\t`id` bigint(20) not null, " + "\n\t`sellerId` bigint(20) default null, " + "\n\t`canvas_id` bigint(20) not null comment '画布ID', " + "\n\t`view_node_id` bigint(20) not null comment '对应显示的节点id', " + "\n\t`activity_type` int(11) not null comment '活动类型', " + "\n\t`node_type` int(11) not null comment '节点类型', " + "\n\t`node_title` varchar(200) not null comment '节点标题', " + "\n\t`status` int(11) not null default '0' comment '页面的显示状态', " + "\n\t`update_status` int(11) default null comment '节点创建后的修改状态', " + "\n\t`execute_status` int(11) default null comment '节点当前的执行状态', " + "\n\t`start_time` datetime default null comment '该节点活动的开始时间', " + "\n\t`end_time` datetime default null comment '该节点活动的结束时间', " + "\n\t`activity_start_time` datetime default null comment '营销活动的开始时间', " + "\n\t`activity_end_time` datetime default null comment '营销活动的结束时间', " + "\n\t`report_start_time` datetime default null comment '该节点活动效果报告的开始时间', " + "\n\t`report_end_time` datetime default null comment '该节点活动效果报告的结束时间', " + "\n\t`cron_rule` varchar(100) default null comment '周期性营销的时间表达式', " + "\n\t`split_rule` varchar(1000) default null comment '节点拆分规则', " + "\n\t`search_json` varchar(5000) default null comment '索引查找的条件字符串', " + "\n\t`filter_json` varchar(1000) default null comment '各种过滤条件的设置', " + "\n\t`buyer_count` int(11) default null comment '营销的会员数量', " + "\n\t`gmt_modified` datetime not null comment '活动最后修改时间', " + "\n\t`gmt_create` datetime not null comment '活动创建时间', " + "\n\tprimary key (`id`), " + "\n\tkey `canvas_id` (`canvas_id`), " + "\n\tkey `sid_ty_time` (`sellerId`, `node_type`, `start_time`), " + "\n\tconstraint `t_activity_node_ibfk_1` foreign key (`canvas_id`) references `t_activity_canvas` (`id`) on delete cascade on update cascade" + "\n) engine = InnoDB charset = utf8", output);
    }
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MysqlTest(com.alibaba.druid.sql.MysqlTest) Test(org.junit.Test)

Example 29 with MySqlSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor in project druid by alibaba.

the class MySqlCreateTableTest55 method test_union.

@Test
public void test_union() throws Exception {
    String sql = "CREATE TABLE tableA (datasn varchar(100) NOT NULL,PRIMARY KEY(datasn)) ENGINE=MRG_MyISAM DEFAULT CHARSET=utf8 UNION=(tableB)";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();
    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());
    Assert.assertEquals(2, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "CREATE TABLE tableA (" + //
    "\n\tdatasn varchar(100) NOT NULL, " + //
    "\n\tPRIMARY KEY (datasn)" + "\n) ENGINE = MRG_MyISAM CHARSET = utf8 UNION = (tableB)", output);
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MysqlTest(com.alibaba.druid.sql.MysqlTest) Test(org.junit.Test)

Example 30 with MySqlSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor in project druid by alibaba.

the class MySqlCreateTableTest56 method test_union.

@Test
public void test_union() throws Exception {
    String sql = "CREATE TABLE tableA (datasn varchar(100) NOT NULL,PRIMARY KEY(datasn)) ENGINE=MRG_MyISAM DEFAULT CHARSET=utf8 UNION=(tableB,tableC)";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();
    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());
    Assert.assertEquals(3, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    {
        String output = SQLUtils.toMySqlString(stmt);
        Assert.assertEquals(//
        "CREATE TABLE tableA (" + //
        "\n\tdatasn varchar(100) NOT NULL, " + //
        "\n\tPRIMARY KEY (datasn)" + "\n) ENGINE = MRG_MyISAM CHARSET = utf8 UNION = (tableB, tableC)", output);
    }
    {
        String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
        Assert.assertEquals(//
        "create table tableA (" + //
        "\n\tdatasn varchar(100) not null, " + //
        "\n\tprimary key (datasn)" + "\n) engine = MRG_MyISAM charset = utf8 union = (tableB, tableC)", output);
    }
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MysqlTest(com.alibaba.druid.sql.MysqlTest) Test(org.junit.Test)

Aggregations

MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)345 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)344 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)341 Column (com.alibaba.druid.stat.TableStat.Column)74 MysqlTest (com.alibaba.druid.sql.MysqlTest)29 Test (org.junit.Test)29 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)15 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)15 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)15 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)12 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)5 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)3 SQLCreateViewStatement (com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1