Search in sources :

Example 26 with DbType

use of com.alibaba.druid.DbType 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 27 with DbType

use of com.alibaba.druid.DbType 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 28 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SchemaStatTest6 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "select count(1), name from tg_rpc_user where id < 5 group by name order by id desc";
    DbType dbType = JdbcConstants.MYSQL;
    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());
    // group by
    System.out.println(statVisitor.getGroupByColumns());
    Assert.assertEquals(2, statVisitor.getColumns().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 29 with DbType

use of com.alibaba.druid.DbType 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'";
    DbType 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(8, statVisitor.getColumns().size());
    Assert.assertEquals(6, statVisitor.getConditions().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 30 with DbType

use of com.alibaba.druid.DbType in project druid by alibaba.

the class SchemaStatTest2 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "select " + " create_time_dd          as  来电日期" + " from alisec_app.adl_tb_wing_rubbish_laidian_new_reason_realname_fdt  " + " order by   来电日期  desc  limit  30;";
    DbType dbType = JdbcConstants.MYSQL;
    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());
    Assert.assertEquals(1, statVisitor.getColumns().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)

Aggregations

DbType (com.alibaba.druid.DbType)114 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)76 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)70 ArrayList (java.util.ArrayList)50 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)45 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)21 TableStat (com.alibaba.druid.stat.TableStat)10 JSONArray (com.alibaba.fastjson.JSONArray)10 MySqlExportParameterVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlExportParameterVisitor)5 ExportParameterVisitor (com.alibaba.druid.sql.visitor.ExportParameterVisitor)5 ParserException (com.alibaba.druid.sql.parser.ParserException)4 Map (java.util.Map)4 JdbcParameter (com.alibaba.druid.proxy.jdbc.JdbcParameter)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)2 OracleSQLObject (com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject)2