Search in sources :

Example 86 with SchemaStatVisitor

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

the class PrestoSelect_0 method test_unnest_0.

@Test
public void test_unnest_0() throws Exception {
    String sql = "SELECT student, score\n" + "FROM tests\n" + "CROSS JOIN UNNEST(scores) AS t (score);";
    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 student, score\n" + "FROM tests\n" + "\tCROSS JOIN UNNEST(scores) AS t (score)", 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 87 with SchemaStatVisitor

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

the class PrestoSelect_0 method test_with.

@Test
public void test_with() throws Exception {
    String sql = "WITH RECURSIVE t(n) AS (\n" + "    VALUES (1)\n" + "    UNION ALL\n" + "    SELECT n + 1 FROM t WHERE n < 4\n" + ")\n" + "SELECT sum(n) FROM t;";
    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("WITH RECURSIVE t (n) AS (\n" + "\t\tVALUES (1)\n" + "\t\tUNION ALL\n" + "\t\tSELECT n + 1\n" + "\t\tFROM t\n" + "\t\tWHERE n < 4\n" + "\t)\n" + "SELECT sum(n)\n" + "FROM t", 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 88 with SchemaStatVisitor

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

the class SchemaStatTest10 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "select a.id, b.name from table1 a inner join table2 b on a.id = b.id";
    DbType dbType = JdbcConstants.ORACLE;
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    SQLStatement stmt = parser.parseStatementList().get(0);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(statVisitor);
    Set<TableStat.Relationship> relationships = statVisitor.getRelationships();
    for (TableStat.Relationship relationship : relationships) {
        // table1.id = table2.id
        System.out.println(relationship);
    }
    System.out.println(statVisitor.getColumns());
    // System.out.println(statVisitor.getGroupByColumns()); // group by
    // group by
    System.out.println("relationships : " + statVisitor.getRelationships());
    // System.out.println(statVisitor.getConditions());
    Assert.assertEquals(3, statVisitor.getColumns().size());
    Assert.assertEquals(2, statVisitor.getConditions().size());
    assertEquals(0, statVisitor.getFunctions().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 89 with SchemaStatVisitor

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

the class SchemaStatTest13 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "select a.id, b.name from (select t1.* from table1 t1 left join table3 t3 on cast(t1.id as bigint) = t3.id) a inner join table2 b on a.id = b.id";
    DbType dbType = JdbcConstants.ORACLE;
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    SQLStatement stmt = parser.parseStatementList().get(0);
    System.out.println(stmt);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(statVisitor);
    Set<TableStat.Relationship> relationships = statVisitor.getRelationships();
    System.out.println("columns : " + statVisitor.getColumns());
    // group by
    System.out.println("groups : " + statVisitor.getGroupByColumns());
    // group by
    System.out.println("relationships : " + statVisitor.getRelationships());
    System.out.println("conditions : " + statVisitor.getConditions());
    assertEquals(2, relationships.size());
    Assert.assertEquals(5, statVisitor.getColumns().size());
    Assert.assertEquals(4, statVisitor.getConditions().size());
    assertEquals(0, statVisitor.getFunctions().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) DbType(com.alibaba.druid.DbType) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 90 with SchemaStatVisitor

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

the class PGSelectTest49 method test_0.

public void test_0() throws Exception {
    String sql = "WITH sel AS (SELECT\n" + "       unnest(ARRAY [4275]) AS gdid,\n" + "       unnest(ARRAY [1])    AS gdnumber)\n" + "UPDATE goods_detail\n" + "SET gd_number = gd_number - gdnumber FROM sel\n" + "WHERE gd_id = gdid;";
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    SQLStatement stmt = stmtList.get(0);
    System.out.println(stmt);
    assertEquals("WITH sel AS (\n" + "\t\tSELECT unnest(ARRAY[4275]) AS gdid, unnest(ARRAY[1]) AS gdnumber\n" + "\t)\n" + "UPDATE goods_detail\n" + "SET gd_number = gd_number - gdnumber\n" + "FROM sel\n" + "WHERE gd_id = gdid;", SQLUtils.toPGString(stmt));
    assertEquals("with sel as (\n" + "\t\tselect unnest(ARRAY[4275]) as gdid, unnest(ARRAY[1]) as gdnumber\n" + "\t)\n" + "update goods_detail\n" + "set gd_number = gd_number - gdnumber\n" + "from sel\n" + "where gd_id = gdid;", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    assertEquals(1, stmtList.size());
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(visitor);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    assertEquals(4, visitor.getColumns().size());
    assertEquals(1, visitor.getTables().size());
}
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