use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class OracleListAggTest1 method test_0.
public void test_0() throws Exception {
String sql = "SELECT count(0) " + "FROM WEP_USER T " + "LEFT JOIN (" + " SELECT T.ID, LISTAGG(T2.ROLE_NAME, ',') WITHIN GROUP (ORDER BY T.ID) AS ROLENAMES FROM WEP_USER T " + " LEFT JOIN WEP_USER_ROLE T1 ON T.ID = T1.USER_ID " + " LEFT JOIN WEP_ROLE T2 ON T1.ROLE_ID = T2.ID " + " WHERE t.IS_DELETED = 0 " + " AND T1.IS_DELETED = 0 " + " AND T2.IS_DELETED = 0 " + " GROUP BY T.ID" + ") T1 ON T.ID = T1.ID " + "WHERE t.IS_DELETED = 0";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
SQLStatement stmt = statementList.get(0);
// print(statementList);
Assert.assertEquals(1, statementList.size());
Assert.assertEquals("SELECT COUNT(0)\n" + "FROM WEP_USER T\n" + "\tLEFT JOIN (\n" + "\t\tSELECT T.ID, LISTAGG(T2.ROLE_NAME, ',') WITHIN GROUP (ORDER BY T.ID) AS ROLENAMES\n" + "\t\tFROM WEP_USER T\n" + "\t\tLEFT JOIN WEP_USER_ROLE T1 ON T.ID = T1.USER_ID \n" + "\t\t\tLEFT JOIN WEP_ROLE T2 ON T1.ROLE_ID = T2.ID \n" + "\t\tWHERE t.IS_DELETED = 0\n" + "\t\t\tAND T1.IS_DELETED = 0\n" + "\t\t\tAND T2.IS_DELETED = 0\n" + "\t\tGROUP BY T.ID\n" + "\t) T1 ON T.ID = T1.ID \n" + //
"WHERE t.IS_DELETED = 0", SQLUtils.toSQLString(stmt, dbType));
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());
System.out.println("relationships : " + visitor.getRelationships());
Assert.assertEquals(3, visitor.getTables().size());
Assert.assertEquals(8, visitor.getColumns().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("WEP_USER_ROLE")));
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("WEP_ROLE")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("WEP_USER_ROLE", "IS_DELETED")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("WEP_ROLE", "IS_DELETED")));
WallUtils.isValidateOracle(sql);
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class PhoenixUpsertTest method test_0.
public void test_0() throws Exception {
String sql = "upsert into t_1 (a,b,c) values (?,?,?)";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.PHOENIX);
SQLStatement stmt = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = new PhoenixSchemaStatVisitor();
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(3, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_1")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "a")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "b")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "c")));
// 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.PHOENIX);
Assert.assertEquals("UPSERT INTO t_1 (a, b, c)\n" + //
"VALUES (?, ?, ?)", output);
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class SchemaStatTest8 method test_schemaStat.
public void test_schemaStat() throws Exception {
String sql = "SELECT * FROM AQLQCAT " + "JOIN AQLQCATB " + " ON AQLQCAT.ZZ = AQLQCATB.RR " + " AND trim(AQLQCAT.BB) = AQLQCATB.DD " + "WHERE AQLQCAT.MANDT = 'A0' " + " AND AQLQCATB.NUM = 'A1'";
String dbType = JdbcConstants.ORACLE;
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
SQLStatement stmt = parser.parseStatementList().get(0);
SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
stmt.accept(statVisitor);
// System.out.println(statVisitor.getColumns());
// System.out.println(statVisitor.getGroupByColumns()); // group by
// group by
System.out.println("relationships : " + statVisitor.getRelationships());
Assert.assertEquals(6, statVisitor.getColumns().size());
Assert.assertEquals(6, statVisitor.getConditions().size());
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class SQLite_SelectTest_0 method test_0.
public void test_0() throws Exception {
String sql = "select first_name, last_name from mytable limit 3, 4";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.SQLITE);
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
statemen.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(2, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("mytable")));
Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "last_name")));
Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
}
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";
String 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());
}
Aggregations