Search in sources :

Example 31 with Column

use of com.alibaba.druid.stat.TableStat.Column 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)

Example 32 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class MySqlCreateTableTest8 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE Persons\n" + //
    "(\n" + //
    "Id_P int NOT NULL,\n" + //
    "LastName varchar(255) NOT NULL,\n" + //
    "FirstName varchar(255),\n" + //
    "Address varchar(255),\n" + //
    "City varchar(255),\n" + //
    "CHECK (Id_P>0)\n" + ")";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    MySqlCreateTableStatement stmt = (MySqlCreateTableStatement) 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(5, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("Persons")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Persons", "Id_P")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Persons", "LastName")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Persons", "FirstName")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Persons", "Address")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Persons", "City")));
}
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) MySqlCreateTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)

Example 33 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class MySqlCreateTableTest9 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE VIEW v AS\n" + //
    "SELECT ProductID,ProductName\n" + //
    "FROM Products\n" + "WHERE Discontinued=No";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateViewStatement stmt = (SQLCreateViewStatement) 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(2, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("Products")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Products", "ProductID")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Products", "ProductName")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Products", "Discontinued")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Products", "No")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) Column(com.alibaba.druid.stat.TableStat.Column) SQLCreateViewStatement(com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 34 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class MySqlSelectTest_18 method test_0.

public void test_0() throws Exception {
    String sql = //
    "SELECT host.id as id" + //
    ",   host.item_id as itemId" + //
    ",   host.node_id as nodeId" + //
    ",   host.node_type as nodeType" + //
    ",   host.begin_time as beginTime" + //
    ",   host.end_time as endTime" + //
    ",   host.gmt_create as gmtCreate" + //
    ",   host.gmt_modify as gmtModify" + //
    ",   host.reason as reason" + //
    ",   host.creator_id as creatorId" + //
    ",   host.modifier_id as modifierId" + //
    ",   user.name as creator" + //
    ",   user.name as modifier" + //
    ",   user.nick_name as nickName   " + " FROM notice_close_node host left join sys_user user on user.id = host.modifier_id";
    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(2, visitor.getTables().size());
    Assert.assertEquals(15, visitor.getColumns().size());
    Assert.assertEquals(2, visitor.getConditions().size());
    Assert.assertTrue(visitor.getColumns().contains(new Column("sys_user", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("notice_close_node", "modifier_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 35 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class MySqlSelectTest_20 method test_0.

public void test_0() throws Exception {
    String sql = "select bsvariety, max(bsh) as bsh, min(bsl) as bsl " + " from   exchange_market_info " + " where bsdate>date_sub(now(),interval 1 day)" + " group by bsvariety desc;";
    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.assertTrue(visitor.getColumns().contains(new Column("exchange_market_info", "bsvariety")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("exchange_market_info", "bsh")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("exchange_market_info", "bsl")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("exchange_market_info", "bsdate")));
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(4, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals("SELECT bsvariety, MAX(bsh) AS bsh, MIN(bsl) AS bsl" + "\nFROM exchange_market_info" + "\nWHERE bsdate > date_sub(now(), INTERVAL 1 DAY)" + "\nGROUP BY bsvariety DESC", 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)

Aggregations

Column (com.alibaba.druid.stat.TableStat.Column)134 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)122 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)74 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)73 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)19 OracleSchemaStatVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor)19 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)12 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)12 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)10 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)8 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)8 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)8 OracleOutputVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor)6 MysqlTest (com.alibaba.druid.sql.MysqlTest)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)5 Test (org.junit.Test)5 SQLCreateViewStatement (com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement)4 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4