Search in sources :

Example 76 with MySqlSchemaStatVisitor

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

the class MySqlInsertTest_8 method test_0.

public void test_0() throws Exception {
    String sql = "insert into dd.table1(d,e) select * from bb.table3";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    MySqlInsertStatement insertStmt = (MySqlInsertStatement) stmt;
    Assert.assertEquals(2, insertStmt.getColumns().size());
    Assert.assertEquals(0, insertStmt.getValuesList().size());
    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(//
    "INSERT INTO dd.table1 (d, e)" + //
    "\nSELECT *" + //
    "\nFROM bb.table3", SQLUtils.toMySqlString(insertStmt));
    Assert.assertEquals(//
    "insert into dd.table1 (d, e)" + //
    "\nselect *" + //
    "\nfrom bb.table3", SQLUtils.toMySqlString(insertStmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 77 with MySqlSchemaStatVisitor

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

the class MySqlInsertTest_9 method test_0.

public void test_0() throws Exception {
    String sql = "insert into sequence values('seq_wlb_order_log',268234128+10000000,now());";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLInsertStatement insertStmt = (SQLInsertStatement) stmt;
    Assert.assertEquals(3, insertStmt.getValues().getValues().size());
    Assert.assertEquals(0, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals("INSERT INTO sequence" + "\nVALUES ('seq_wlb_order_log', 268234128 + 10000000, now())", SQLUtils.toMySqlString(insertStmt));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 78 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest71 method test_one.

@Test
public void test_one() throws Exception {
    String sql = //
    "create table xx (id bigint unsigned not null comment 'aa' auto_increment," + //
    "gmt_create datetime not null comment '创建时间'," + //
    "gmt_modified datetime not null comment '修改时间', " + "primary key (id)) comment='re'";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Column column = visitor.getColumn("xx", "id");
    Assert.assertNotNull(column);
    Assert.assertEquals("bigint", column.getDataType());
    {
        String output = SQLUtils.toMySqlString(stmt);
        Assert.assertEquals("CREATE TABLE xx (" + "\n\tid bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'aa', " + "\n\tgmt_create datetime NOT NULL COMMENT '创建时间', " + "\n\tgmt_modified datetime NOT NULL COMMENT '修改时间', " + "\n\tPRIMARY KEY (id)" + "\n) COMMENT = 're'", output);
    }
    {
        String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
        Assert.assertEquals("create table xx (" + "\n\tid bigint unsigned not null auto_increment comment 'aa', " + "\n\tgmt_create datetime not null comment '创建时间', " + "\n\tgmt_modified datetime not null comment '修改时间', " + "\n\tprimary key (id)" + "\n) comment = 're'", output);
    }
}
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) MysqlTest(com.alibaba.druid.sql.MysqlTest) Test(org.junit.Test)

Example 79 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest72 method test_one.

@Test
public void test_one() throws Exception {
    String sql = "CREATE TABLE \"MessageInstance\" (" + "  \"id\" int(11) NOT NULL AUTO_INCREMENT," + "  \"messageId\" int(11) NOT NULL," + "  PRIMARY KEY (\"id\")," + "  KEY \"ix_messageId\" (\"messageId\")" + ")";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Column column = visitor.getColumn("MessageInstance", "id");
    Assert.assertNotNull(column);
    Assert.assertEquals("int", column.getDataType());
    {
        String output = SQLUtils.toMySqlString(stmt);
        Assert.assertEquals("CREATE TABLE \"MessageInstance\" (" + "\n\t\"id\" int(11) NOT NULL AUTO_INCREMENT, " + "\n\t\"messageId\" int(11) NOT NULL, " + "\n\tPRIMARY KEY ('id'), " + "\n\tKEY \"ix_messageId\" ('messageId')" + "\n)", output);
    }
    {
        String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
        Assert.assertEquals("create table \"MessageInstance\" (" + "\n\t\"id\" int(11) not null auto_increment, " + "\n\t\"messageId\" int(11) not null, " + "\n\tprimary key ('id'), " + "\n\tkey \"ix_messageId\" ('messageId')" + "\n)", output);
    }
}
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) MysqlTest(com.alibaba.druid.sql.MysqlTest) Test(org.junit.Test)

Example 80 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest73 method test_one.

@Test
public void test_one() throws Exception {
    String sql = "CREATE TABLE total (" + "    id INT NOT NULL AUTO_INCREMENT," + "    message CHAR(20), INDEX(a))" + "    ENGINE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Column column = visitor.getColumn("total", "id");
    Assert.assertNotNull(column);
    Assert.assertEquals("INT", column.getDataType());
    {
        String output = SQLUtils.toMySqlString(stmt);
        Assert.assertEquals("CREATE TABLE total (" + "\n\tid INT NOT NULL AUTO_INCREMENT, " + "\n\tmessage CHAR(20), " + "\n\tINDEX(a)" + "\n) ENGINE = MERGE UNION = (t1, t2) INSERT_METHOD = LAST", output);
    }
    {
        String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
        Assert.assertEquals("create table total (" + "\n\tid INT not null auto_increment, " + "\n\tmessage CHAR(20), " + "\n\tindex(a)" + "\n) engine = MERGE union = (t1, t2) insert_method = LAST", output);
    }
}
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) 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