Search in sources :

Example 76 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class OdpsSelectTest33 method test_select.

public void test_select() throws Exception {
    // 1095288847322
    // 
    String sql = "SELECT * from mytable1 a LEFT ANTI JOIN mytable2 b on a.id=b.id;";
    assertEquals("SELECT *\n" + "FROM mytable1 a\n" + "LEFT ANTI JOIN mytable2 b\n" + "ON a.id = b.id;", SQLUtils.formatOdps(sql));
    assertEquals("select *\n" + "from mytable1 a\n" + "left anti join mytable2 b\n" + "on a.id = b.id;", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
    SQLStatement stmt = statementList.get(0);
    assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ODPS);
    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());
    assertEquals(2, visitor.getTables().size());
    assertEquals(4, visitor.getColumns().size());
    assertEquals(2, visitor.getConditions().size());
// System.out.println(SQLUtils.formatOdps(sql));
// assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 77 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class OdpsSelectTest35 method test_select.

public void test_select() throws Exception {
    // 1095288847322
    String sql = "SELECT a.city, a.car_id, a.rrc_id, a.brand, a.car_series\n" + "  , a.title\n" + "FROM dw_x001_cp_used_car_detail a\n" + "WHERE status = 'PUBLISHED'\n" + "  AND model_id = 4419\n" + // 
    "  AND abs(datediff(licensed_date, '2013-07-01 00:00:00', 'mm')) <= 5;";
    assertEquals("SELECT a.city, a.car_id, a.rrc_id, a.brand, a.car_series\n" + "\t, a.title\n" + "FROM dw_x001_cp_used_car_detail a\n" + "WHERE status = 'PUBLISHED'\n" + "\tAND model_id = 4419\n" + "\tAND abs(datediff(licensed_date, '2013-07-01 00:00:00', 'mm')) <= 5;", SQLUtils.formatOdps(sql));
    assertEquals("select a.city, a.car_id, a.rrc_id, a.brand, a.car_series\n" + "\t, a.title\n" + "from dw_x001_cp_used_car_detail a\n" + "where status = 'PUBLISHED'\n" + "\tand model_id = 4419\n" + "\tand abs(datediff(licensed_date, '2013-07-01 00:00:00', 'mm')) <= 5;", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
    SQLStatement stmt = statementList.get(0);
    assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ODPS);
    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());
    assertEquals(1, visitor.getTables().size());
    assertEquals(9, visitor.getColumns().size());
    assertEquals(2, visitor.getConditions().size());
// System.out.println(SQLUtils.formatOdps(sql));
// assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 78 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class OdpsSelectTest21 method test_select.

public void test_select() throws Exception {
    // 1095288847322
    // 
    String sql = "select wm_concat(',', a) from values('abc', 1), (null, 2) t (a, i);";
    Assert.assertEquals("SELECT wm_concat(',', a)\n" + "FROM (VALUES ('abc', 1), (NULL, 2)) AS t (a, i);", SQLUtils.formatOdps(sql));
    Assert.assertEquals("select wm_concat(',', a)\n" + "from (values ('abc', 1), (null, 2)) as t (a, i);", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ODPS);
    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(0, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
// Assert.assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 79 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class OdpsSelectTest27 method test_select.

public void test_select() throws Exception {
    // 1095288847322
    String sql = "select case when aaa < 0.1 then 2 else 1 end as abc\n" + // 
    "from bigdata_xy.dual;";
    assertEquals("SELECT CASE \n" + "\t\tWHEN aaa < 0.1 THEN 2\n" + "\t\tELSE 1\n" + "\tEND AS abc\n" + "FROM bigdata_xy.dual;", SQLUtils.formatOdps(sql));
    assertEquals("select case \n" + "\t\twhen aaa < 0.1 then 2\n" + "\t\telse 1\n" + "\tend as abc\n" + "from bigdata_xy.dual;", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
    SQLStatement stmt = statementList.get(0);
    assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ODPS);
    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());
    assertEquals(1, visitor.getTables().size());
    assertEquals(1, visitor.getColumns().size());
    assertEquals(1, visitor.getConditions().size());
// System.out.println(SQLUtils.formatOdps(sql));
// assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 80 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class OracleDropTypeTest_0 method test_0.

public void test_0() throws Exception {
    // 
    String sql = "DROP type dmcfck;";
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ORACLE);
    stmt.accept(visitor);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("coditions : " + visitor.getConditions());
    System.out.println("relationships : " + visitor.getRelationships());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    assertEquals("DROP TYPE dmcfck;", SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
    assertEquals(0, visitor.getTables().size());
    // assertTrue(visitor.getTables().containsKey(new TableStat.Name("t1")));
    assertEquals(0, visitor.getColumns().size());
// assertTrue(visitor.getColumns().contains(new TableStat.Column("xwarehouses", "sales_rep_id")));
// assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "YEAR")));
// assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "order_mode")));
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Aggregations

SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)437 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)434 TableStat (com.alibaba.druid.stat.TableStat)68 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)61 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)51 SQLUtils (com.alibaba.druid.sql.SQLUtils)35 DbType (com.alibaba.druid.DbType)21 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)12 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)12 Test (org.junit.Test)12 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)11 SchemaRepository (com.alibaba.druid.sql.repository.SchemaRepository)11 Column (com.alibaba.druid.stat.TableStat.Column)10 InputStream (java.io.InputStream)9 InputStreamReader (java.io.InputStreamReader)9 Reader (java.io.Reader)9 OdpsStatementParser (com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser)6 OdpsSchemaStatVisitor (com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor)6 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)5 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4