Search in sources :

Example 41 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest14 method test_0.

public void test_0() throws Exception {
    String sql = //
    " CREATE TABLE `OptionList` ( " + //
    "`ID` int(11) NOT NULL AUTO_INCREMENT, " + //
    "`OptionID` int(11) DEFAULT NULL COMMENT '选项ID', " + //
    "`QuizID` int(11) DEFAULT NULL COMMENT '竞猜题目ID', " + //
    "`OptionName` varchar(500) DEFAULT NULL COMMENT '选项名称', " + //
    "`OptionCount` int(11) DEFAULT NULL COMMENT '选择的人数', " + //
    "PRIMARY KEY (`ID`), KEY `quizId` (`QuizID`) USING BTREE, " + //
    "KEY `optionId` (`OptionID`) USING BTREE" + ") ENGINE=InnoDB DEFAULT CHARSET=gbk COMMENT=''";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals("CREATE TABLE `OptionList` (" + "\n\t`ID` int(11) NOT NULL AUTO_INCREMENT, " + "\n\t`OptionID` int(11) DEFAULT NULL COMMENT '选项ID', " + "\n\t`QuizID` int(11) DEFAULT NULL COMMENT '竞猜题目ID', " + "\n\t`OptionName` varchar(500) DEFAULT NULL COMMENT '选项名称', " + "\n\t`OptionCount` int(11) DEFAULT NULL COMMENT '选择的人数', " + "\n\tPRIMARY KEY (`ID`), " + "\n\tKEY `quizId` USING BTREE (`QuizID`), " + "\n\tKEY `optionId` USING BTREE (`OptionID`)" + //
    "\n) ENGINE = InnoDB CHARSET = gbk COMMENT = ''", SQLUtils.toMySqlString(stmt));
    Assert.assertEquals("create table `OptionList` (" + "\n\t`ID` int(11) not null auto_increment, " + "\n\t`OptionID` int(11) default null comment '选项ID', " + "\n\t`QuizID` int(11) default null comment '竞猜题目ID', " + "\n\t`OptionName` varchar(500) default null comment '选项名称', " + "\n\t`OptionCount` int(11) default null comment '选择的人数', " + "\n\tprimary key (`ID`), " + "\n\tkey `quizId` using BTREE (`QuizID`), " + "\n\tkey `optionId` using BTREE (`OptionID`)" + //
    "\n) engine = InnoDB charset = gbk comment = ''", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    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(5, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("OptionList")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("OptionList", "ID")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("OptionList", "OptionCount")));
}
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 42 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest15 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE `xxx` (" + //
    "                `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID'," + //
    "                `create_date` datetime DEFAULT NULL," + //
    "                `update_date` datetime DEFAULT NULL," + //
    "                `product_id` int(11) NOT NULL COMMENT '产品ID'," + //
    "                `memeber_id` int(11) NOT NULL COMMENT '用户ID'," + //
    "                `name` varchar(50) DEFAULT NULL COMMENT '姓名'," + //
    "                `address` varchar(500) DEFAULT NULL COMMENT '地址'," + //
    "                `mobile` varchar(50) DEFAULT NULL COMMENT '手机'," + //
    "                `amount` int(11) DEFAULT NULL COMMENT '兑换数量'," + //
    "                PRIMARY KEY (`id`)" + //
    "              ) ENGINE=InnoDB DEFAULT CHARSET=gbk;" + "";
    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(9, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("xxx")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("xxx", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("xxx", "amount")));
}
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 43 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest36 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE lookup" + //
    "  (id INT, INDEX USING BTREE (id))" + //
    "  STATS_AUTO_RECALC = 1;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = 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(1, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("lookup")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "CREATE TABLE lookup (" + //
    "\n\tid INT, " + //
    "\n\tINDEX USING BTREE(id)" + "\n) STATS_AUTO_RECALC = 1", 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 44 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest39 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE lookup" + //
    "  (id INT, INDEX USING BTREE (id))" + //
    "  STATS_PERSISTENT 1;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = 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(1, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("lookup")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "CREATE TABLE lookup (" + //
    "\n\tid INT, " + //
    "\n\tINDEX USING BTREE(id)" + "\n) STATS_PERSISTENT = 1", 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 45 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest4 method test_0.

public void test_0() throws Exception {
    String sql = //
    "create table t_temp_11593685fc3244cf81468209484078a0 (" + //
    "id int not null auto_increment, " + //
    "score float not null, " + //
    "student bigint not null, " + //
    "primary key(id)," + //
    "key score(score), " + "key student(student))";
    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(3, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_temp_11593685fc3244cf81468209484078a0")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("t_temp_11593685fc3244cf81468209484078a0", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("t_temp_11593685fc3244cf81468209484078a0", "score")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("t_temp_11593685fc3244cf81468209484078a0", "student")));
}
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)

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