Search in sources :

Example 81 with SchemaStatVisitor

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

the class OracleCreateSynonymTest_2 method test_0.

public void test_0() throws Exception {
    // 
    String sql = "CREATE OR REPLACE SYNONYM CSCLIENT.ECC_CUST_AREA_INFO FOR ECC_CUST.ECC_CUST_AREA_INFO";
    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("CREATE OR REPLACE SYNONYM CSCLIENT.ECC_CUST_AREA_INFO FOR ECC_CUST.ECC_CUST_AREA_INFO", 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)

Example 82 with SchemaStatVisitor

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

the class PrestoSelect_0 method test_unnest_2.

@Test
public void test_unnest_2() throws Exception {
    String sql = "SELECT numbers, n, a\n" + "FROM (\n" + "  VALUES\n" + "    (ARRAY[2, 5]),\n" + "    (ARRAY[7, 8, 9])\n" + ") AS x (numbers)\n" + "CROSS JOIN UNNEST(numbers) WITH ORDINALITY AS t (n, a);";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    SQLStatement stmt = parser.parseStatement();
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(visitor);
    Map<TableStat.Name, TableStat> tableMap = visitor.getTables();
    assertTrue(tableMap.isEmpty());
    assertEquals("SELECT numbers, n, a\n" + "FROM (VALUES (ARRAY[2, 5]), (ARRAY[7, 8, 9])) AS x (numbers)\n" + "\tCROSS JOIN UNNEST(numbers) WITH ORDINALITY AS t (n, a)", stmt.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) Test(org.junit.Test)

Example 83 with SchemaStatVisitor

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

the class PrestoSelect_0 method test_in.

@Test
public void test_in() throws Exception {
    String sql = "SELECT name\n" + "FROM nation\n" + "WHERE regionkey IN (\n" + "     SELECT regionkey\n" + "     FROM region\n" + "     WHERE name = 'AMERICA' OR name = 'AFRICA'\n" + ");";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    SQLStatement stmt = parser.parseStatement();
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(visitor);
    Map<TableStat.Name, TableStat> tableMap = visitor.getTables();
    assertFalse(tableMap.isEmpty());
    assertEquals("SELECT name\n" + "FROM nation\n" + "WHERE regionkey IN (\n" + "\tSELECT regionkey\n" + "\tFROM region\n" + "\tWHERE name = 'AMERICA'\n" + "\t\tOR name = 'AFRICA'\n" + ")", stmt.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) Test(org.junit.Test)

Example 84 with SchemaStatVisitor

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

the class PrestoSelect_0 method test_scalar.

@Test
public void test_scalar() throws Exception {
    String sql = "SELECT name\n" + "FROM nation\n" + "WHERE regionkey = (SELECT max(regionkey) FROM region);";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    SQLStatement stmt = parser.parseStatement();
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(visitor);
    Map<TableStat.Name, TableStat> tableMap = visitor.getTables();
    assertFalse(tableMap.isEmpty());
    assertEquals("SELECT name\n" + "FROM nation\n" + "WHERE regionkey = (\n" + "\tSELECT max(regionkey)\n" + "\tFROM region\n" + ")", stmt.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) Test(org.junit.Test)

Example 85 with SchemaStatVisitor

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

the class PrestoSelect_0 method test_exists.

@Test
public void test_exists() throws Exception {
    String sql = "SELECT name\n" + "FROM nation\n" + "WHERE EXISTS (\n" + "     SELECT *\n" + "     FROM region\n" + "     WHERE region.regionkey = nation.regionkey\n" + ");";
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    SQLStatement stmt = parser.parseStatement();
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(visitor);
    Map<TableStat.Name, TableStat> tableMap = visitor.getTables();
    assertFalse(tableMap.isEmpty());
    assertEquals("SELECT name\n" + "FROM nation\n" + "WHERE EXISTS (\n" + "\tSELECT *\n" + "\tFROM region\n" + "\tWHERE region.regionkey = nation.regionkey\n" + ")", stmt.toString());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) Test(org.junit.Test)

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