Search in sources :

Example 56 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest68 method test_one.

@Test
public void test_one() throws Exception {
    String sql = "CREATE TABLE t1 (" + "\n\tyear_col  INT," + "\n\tsome_data INT" + "\n)" + "\nPARTITION BY RANGE (year_col) (" + "    PARTITION p0 VALUES LESS THAN (1991)," + "    PARTITION p1 VALUES LESS THAN (1995)," + "    PARTITION p2 VALUES LESS THAN (1999)," + "    PARTITION p3 VALUES LESS THAN (2002)," + "    PARTITION p4 VALUES LESS THAN (2006)," + "    PARTITION p5 VALUES LESS THAN MAXVALUE" + ");";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    {
        String output = SQLUtils.toMySqlString(stmt);
        Assert.assertEquals("CREATE TABLE t1 (" + "\n\tyear_col INT, " + "\n\tsome_data INT" + "\n)" + "\nPARTITION BY RANGE (year_col)" + "\n(" + "\n\tPARTITION p0 VALUES LESS THAN (1991)," + "\n\tPARTITION p1 VALUES LESS THAN (1995)," + "\n\tPARTITION p2 VALUES LESS THAN (1999)," + "\n\tPARTITION p3 VALUES LESS THAN (2002)," + "\n\tPARTITION p4 VALUES LESS THAN (2006)," + "\n\tPARTITION p5 VALUES LESS THAN MAXVALUE" + "\n)", output);
    }
    {
        String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
        Assert.assertEquals("create table t1 (" + "\n\tyear_col INT, " + "\n\tsome_data INT" + "\n)" + "\npartition by range (year_col)" + "\n(" + "\n\tpartition p0 values less than (1991)," + "\n\tpartition p1 values less than (1995)," + "\n\tpartition p2 values less than (1999)," + "\n\tpartition p3 values less than (2002)," + "\n\tpartition p4 values less than (2006)," + "\n\tpartition p5 values less than maxvalue" + "\n)", 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 57 with MySqlSchemaStatVisitor

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

the class MySqlCreateTableTest69 method test_one.

@Test
public void test_one() throws Exception {
    String sql = "CREATE TABLE t1 (" + "  s1 INT," + "  s2 INT AS (EXP(s1)) STORED" + ")" + "PARTITION BY LIST (s2) (" + "  PARTITION p1 VALUES IN (1)" + ");";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseCreateTable();
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    {
        String output = SQLUtils.toMySqlString(stmt);
        Assert.assertEquals("CREATE TABLE t1 (" + "\n\ts1 INT, " + "\n\ts2 INT AS (EXP(s1)) SORTED" + "\n)" + "\nPARTITION BY LIST (s2)" + "\n(" + "\n\tPARTITION p1 VALUES IN (1)" + "\n)", output);
    }
    {
        String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
        Assert.assertEquals("create table t1 (" + "\n\ts1 INT, " + "\n\ts2 INT as (EXP(s1)) sorted" + "\n)" + "\npartition by list (s2)" + "\n(" + "\n\tpartition p1 values in (1)" + "\n)", 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 58 with MySqlSchemaStatVisitor

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

the class MySqlCreateProcedureTest2 method test_1.

public void test_1() throws Exception {
    String sql = "create or replace procedure sp_name(level int,age int)" + " begin" + " declare x,y,z int;" + " lable_1: begin" + " insert into test values(id,age);" + " end lable_1;" + " end";
    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(2, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("test")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("test", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("test", "age")));
}
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 59 with MySqlSchemaStatVisitor

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

the class MySqlCreateProcedureTest2 method test_2.

public void test_2() throws Exception {
    String sql = "create or replace procedure sp_name(level int,age int)" + " begin" + " declare x,y,z int;" + " lable_1: loop" + " insert into test values(id,age);" + " end loop lable_1;" + " end";
    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(2, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("test")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("test", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("test", "age")));
}
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 60 with MySqlSchemaStatVisitor

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

the class MySqlCreateProcedureTest3 method test_0.

public void test_0() throws Exception {
    String sql = "create or replace procedure sp_name(level int,age int)" + " begin" + " declare x,y,z int;" + " lable_1: while x do" + " insert into test values(id,age);" + " leave lable_1;" + " end while lable_1;" + " end";
    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(2, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("test")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("test", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("test", "age")));
}
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