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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations