Search in sources :

Example 36 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest51 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE `test` (" + //
    "`Id` int(11) NOT NULL," + //
    "`text` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL" + ") ENGINE=InnoDB DEFAULT CHARSET=gbk";
    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());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("test")));
    String output = SQLUtils.toMySqlString(stmt);
    //        System.out.println(output);
    Assert.assertEquals(//
    "CREATE TABLE `test` (" + //
    "\n\t`Id` int(11) NOT NULL, " + //
    "\n\t`text` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL" + //
    "\n) ENGINE = InnoDB CHARSET = gbk", 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)

Example 37 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest52 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE `ins_ebay_auth` (" + //
    "`auth_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id'," + //
    "`usr_id` int(10) NOT NULL COMMENT '外键,用户表'," + //
    "`status` char(1) COLLATE utf8_bin NOT NULL COMMENT '状态 0.有效?1.无效'," + //
    "`ebay_token` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'eBay授权码'," + //
    "`ebay_name` varchar(50)  NOT NULL COMMENT 'eBay唯一名'," + //
    "`create_time` datetime NOT NULL COMMENT '授权时间'," + "`invalid_time` datetime NOT NULL COMMENT '授权失效时间'," + //
    "PRIMARY KEY (`auth_id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='INS_EBAY_AUTH';";
    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());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(7, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ins_ebay_auth")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "CREATE TABLE `ins_ebay_auth` (" + //
    "\n\t`auth_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id', " + //
    "\n\t`usr_id` int(10) NOT NULL COMMENT '外键,用户表', " + //
    "\n\t`status` char(1) COLLATE utf8_bin NOT NULL COMMENT '状态 0.有效?1.无效', " + // 
    "\n\t`ebay_token` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'eBay授权码', " + // 
    "\n\t`ebay_name` varchar(50) NOT NULL COMMENT 'eBay唯一名', " + // 
    "\n\t`create_time` datetime NOT NULL COMMENT '授权时间', " + // 
    "\n\t`invalid_time` datetime NOT NULL COMMENT '授权失效时间', " + "\n\tPRIMARY KEY (`auth_id`)" + "\n) ENGINE = InnoDB CHARSET = utf8 COLLATE utf8_bin COMMENT = 'INS_EBAY_AUTH'", 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)

Example 38 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest53 method test_index_using_btree.

@Test
public void test_index_using_btree() throws Exception {
    String sql = //
    "CREATE TABLE `ins_ebay_auth` (" + //
    "`auth_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id'," + //
    "`usr_id` int(10) NOT NULL COMMENT '外键,用户表'," + //
    "`status` char(1) COLLATE utf8_bin NOT NULL COMMENT '状态 0.有效?1.无效'," + //
    "`ebay_token` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'eBay授权码'," + //
    "`ebay_name` varchar(50)  NOT NULL COMMENT 'eBay唯一名'," + //
    "`create_time` datetime NOT NULL COMMENT '授权时间'," + "`invalid_time` datetime NOT NULL COMMENT '授权失效时间'," + //
    "PRIMARY KEY USING BTREE (`auth_id`), INDEX `ind_usr_id` USING BTREE(`usr_id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='INS_EBAY_AUTH';";
    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());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(7, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ins_ebay_auth")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "CREATE TABLE `ins_ebay_auth` (" + //
    "\n\t`auth_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id', " + //
    "\n\t`usr_id` int(10) NOT NULL COMMENT '外键,用户表', " + //
    "\n\t`status` char(1) COLLATE utf8_bin NOT NULL COMMENT '状态 0.有效?1.无效', " + // 
    "\n\t`ebay_token` varchar(255) COLLATE utf8_bin NOT NULL COMMENT 'eBay授权码', " + // 
    "\n\t`ebay_name` varchar(50) NOT NULL COMMENT 'eBay唯一名', " + // 
    "\n\t`create_time` datetime NOT NULL COMMENT '授权时间', " + // 
    "\n\t`invalid_time` datetime NOT NULL COMMENT '授权失效时间', " + "\n\tPRIMARY KEY USING BTREE (`auth_id`), " + "\n\tINDEX `ind_usr_id` USING BTREE(`usr_id`)" + "\n) ENGINE = InnoDB CHARSET = utf8 COLLATE utf8_bin COMMENT = 'INS_EBAY_AUTH'", 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 39 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest1 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE lookup" + //
    "  (id INT, INDEX USING BTREE (id))" + "  ENGINE = MEMORY;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    statemen.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(1, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("lookup")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("lookup", "id")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) Column(com.alibaba.druid.stat.TableStat.Column) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 40 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest11 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE `tmall_campaign` (" + //
    "`id` int(11) NOT NULL AUTO_INCREMENT," + //
    "`campaign_name` varchar(200) NOT NULL COMMENT '活动名称'," + //
    "`create_date` datetime DEFAULT NULL COMMENT '活动创建时间'," + //
    "`delete_flag` int(11) DEFAULT '0' COMMENT '活动删除标识'," + //
    "PRIMARY KEY (`id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=gbk;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    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());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(4, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("tmall_campaign")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("tmall_campaign", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("tmall_campaign", "campaign_name")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("tmall_campaign", "create_date")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("tmall_campaign", "delete_flag")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) Column(com.alibaba.druid.stat.TableStat.Column) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

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