Search in sources :

Example 11 with DB2StatementParser

use of com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser in project druid by alibaba.

the class DB2SelectTest_0 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT * FROM DSN81010.EMP;";
    DB2StatementParser parser = new DB2StatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
    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("DSN81010.EMP")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "last_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.DB2);
    Assert.assertEquals(//
    "SELECT *" + //
    "\nFROM DSN81010.EMP", output);
}
Also used : DB2StatementParser(com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser) DB2SchemaStatVisitor(com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 12 with DB2StatementParser

use of com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser in project druid by alibaba.

the class DB2SelectTest_1 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT * FROM DSN81010.EMP FETCH FIRST 5 ROWS ONLY;";
    DB2StatementParser parser = new DB2StatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
    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("DSN81010.EMP")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "last_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.DB2);
    Assert.assertEquals(//
    "SELECT *" + "\nFROM DSN81010.EMP" + //
    "\nFETCH FIRST 5 ROWS ONLY", output);
}
Also used : DB2StatementParser(com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser) DB2SchemaStatVisitor(com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 13 with DB2StatementParser

use of com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser in project druid by alibaba.

the class DB2SelectTest_11 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT A.F_0201, A.F_0301, A.F_0802, A.F_2100" + "\nFROM A, B" + "\nWHERE B.F_2211 = '5'" + "\n    AND A.F_0301 = B.F_0301" + "    AND Substr(B.F_0815, 1, 4) concat  Substr ( B.F_0815,5,2 ) > A.F_0802" + "    AND A.F_2100 > 0" + "\nUNION" + "\nSELECT A.F_0201, A.F_0301, A.F_0802, A.F_2100" + "\nFROM A" + "\nWHERE A.F_0301 NOT IN (SELECT F_0301" + "\n        FROM B)" + "\n    AND A.F_2100 > 0";
    DB2StatementParser parser = new DB2StatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
    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(2, visitor.getTables().size());
    Assert.assertEquals(7, visitor.getColumns().size());
    Assert.assertEquals(6, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("A")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("A", "F_0201")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    Assert.assertEquals("SELECT A.F_0201, A.F_0301, A.F_0802, A.F_2100" + "\nFROM A, B" + "\nWHERE B.F_2211 = '5'" + "\n\tAND A.F_0301 = B.F_0301" + "\n\tAND (Substr(B.F_0815, 1, 4) CONCAT Substr(B.F_0815, 5, 2)) > A.F_0802" + "\n\tAND A.F_2100 > 0" + "\nUNION" + "\nSELECT A.F_0201, A.F_0301, A.F_0802, A.F_2100" + "\nFROM A" + "\nWHERE A.F_0301 NOT IN (SELECT F_0301" + "\n\t\tFROM B)" + //
    "\n\tAND A.F_2100 > 0", SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
    Assert.assertEquals("select A.F_0201, A.F_0301, A.F_0802, A.F_2100" + "\nfrom A, B" + "\nwhere B.F_2211 = '5'" + "\n\tand A.F_0301 = B.F_0301" + "\n\tand (Substr(B.F_0815, 1, 4) concat Substr(B.F_0815, 5, 2)) > A.F_0802" + "\n\tand A.F_2100 > 0" + "\nunion" + "\nselect A.F_0201, A.F_0301, A.F_0802, A.F_2100" + "\nfrom A" + "\nwhere A.F_0301 not in (select F_0301" + "\n\t\tfrom B)" + //
    "\n\tand A.F_2100 > 0", SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
Also used : Column(com.alibaba.druid.stat.TableStat.Column) DB2StatementParser(com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser) DB2SchemaStatVisitor(com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 14 with DB2StatementParser

use of com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser in project druid by alibaba.

the class DB2SelectTest_15 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT (PREVVAL FOR TEST_SEQ) FROM SYSIBM.SYSDUMMY1";
    DB2StatementParser parser = new DB2StatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
    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(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("SYSIBM.SYSDUMMY1")));
    //         Assert.assertTrue(visitor.getColumns().contains(new Column("DSN8B10.EMP", "WORKDEPT")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    Assert.assertEquals("SELECT TEST_SEQ.PREVVAL" + //
    "\nFROM SYSIBM.SYSDUMMY1", SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
    Assert.assertEquals("select TEST_SEQ.prevval" + //
    "\nfrom SYSIBM.SYSDUMMY1", SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
Also used : DB2StatementParser(com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser) DB2SchemaStatVisitor(com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 15 with DB2StatementParser

use of com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser in project druid by alibaba.

the class DB2SelectTest_18 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT (PREVIOUS VALUE FOR EMPNO) FROM SYSIBM.SYSDUMMY1";
    DB2StatementParser parser = new DB2StatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
    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(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("SYSIBM.SYSDUMMY1")));
    //         Assert.assertTrue(visitor.getColumns().contains(new Column("DSN8B10.EMP", "WORKDEPT")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    Assert.assertEquals("SELECT EMPNO.PREVVAL" + //
    "\nFROM SYSIBM.SYSDUMMY1", SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
    Assert.assertEquals("select EMPNO.prevval" + //
    "\nfrom SYSIBM.SYSDUMMY1", SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
Also used : DB2StatementParser(com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser) DB2SchemaStatVisitor(com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)34 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)33 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)33 Column (com.alibaba.druid.stat.TableStat.Column)8 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)1 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)1 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)1 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)1 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)1 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)1 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)1