Search in sources :

Example 31 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor 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";
    String 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) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 32 with SchemaStatVisitor

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

the class SchemaStatTest_odps method test_schemaStat.

public void test_schemaStat() throws Exception {
    File file = new File("/Users/wenshao/Downloads/odps_sql_1.txt");
    String sql = FileUtils.readFileToString(file);
    String dbType = JdbcConstants.ODPS;
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    List<SQLStatement> stmtList = parser.parseStatementList();
    System.out.println("stmtList size : " + stmtList.size());
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
    for (SQLStatement stmt : stmtList) {
        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());
//        assertEquals(3, relationships.size());
//
//        Assert.assertEquals(21, statVisitor.getColumns().size());
//        Assert.assertEquals(20, statVisitor.getConditions().size());
//        assertEquals(1, statVisitor.getFunctions().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) TableStat(com.alibaba.druid.stat.TableStat) File(java.io.File) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 33 with SchemaStatVisitor

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

the class ExportConditions method evaluate.

public String evaluate(String sql, String dbType, Boolean compactValues) {
    try {
        List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
        SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
        for (SQLStatement stmt : statementList) {
            stmt.accept(visitor);
        }
        List<List<Object>> rows = new ArrayList<List<Object>>();
        List<Condition> conditions = visitor.getConditions();
        for (int i = 0; i < conditions.size(); ++i) {
            TableStat.Condition condition = conditions.get(i);
            Column column = condition.getColumn();
            String operator = condition.getOperator();
            List<Object> values = condition.getValues();
            List<Object> row = new ArrayList<Object>();
            row.add(column.getTable());
            row.add(column.getName());
            row.add(operator);
            if (values.size() == 0) {
                row.add(null);
            } else if (values.size() == 1) {
                if (compactValues != null && compactValues.booleanValue()) {
                    row.add(values);
                } else {
                    row.add(values.get(0));
                }
            } else {
                row.add(values);
            }
            rows.add(row);
        }
        return JSONUtils.toJSONString(rows);
    } catch (Throwable ex) {
        System.err.println("error sql : " + sql);
        ex.printStackTrace();
        return null;
    }
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) ArrayList(java.util.ArrayList) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) Column(com.alibaba.druid.stat.TableStat.Column) Condition(com.alibaba.druid.stat.TableStat.Condition) List(java.util.List) ArrayList(java.util.ArrayList) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 34 with SchemaStatVisitor

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

the class ExportSelectListColumns method evaluate.

public String evaluate(String sql, String dbType) {
    try {
        List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
        SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
        for (SQLStatement stmt : statementList) {
            stmt.accept(visitor);
        }
        StringBuffer buf = new StringBuffer();
        for (TableStat.Column column : visitor.getColumns()) {
            if (!column.isSelect()) {
                continue;
            }
            if (buf.length() != 0) {
                buf.append(',');
            }
            buf.append(column.toString());
        }
        return buf.toString();
    } catch (Throwable ex) {
        System.err.println("error sql : " + sql);
        ex.printStackTrace();
        return null;
    }
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 35 with SchemaStatVisitor

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

the class MySqlAlterTableDropIndex_0 method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = "ALTER TABLE `test`.`tb1` DROP INDEX `ix` ;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    Assert.assertEquals(//
    "ALTER TABLE `test`.`tb1`" + "\n\tDROP INDEX `ix`", SQLUtils.toMySqlString(stmt));
    Assert.assertEquals(//
    "alter table `test`.`tb1`" + "\n\tdrop index `ix`", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
    stmt.accept(visitor);
    TableStat tableStat = visitor.getTableStat("test.tb1");
    assertNotNull(tableStat);
    assertEquals(1, tableStat.getAlterCount());
    assertEquals(1, tableStat.getDropIndexCount());
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLUtils(com.alibaba.druid.sql.SQLUtils) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)67 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)67 TableStat (com.alibaba.druid.stat.TableStat)18 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)9 Column (com.alibaba.druid.stat.TableStat.Column)8 SQLUtils (com.alibaba.druid.sql.SQLUtils)6 OdpsStatementParser (com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser)6 OdpsSchemaStatVisitor (com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor)6 InputStream (java.io.InputStream)6 InputStreamReader (java.io.InputStreamReader)6 Reader (java.io.Reader)6 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)4 PhoenixSchemaStatVisitor (com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixSchemaStatVisitor)4 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)1 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)1 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)1 MySqlReplaceStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement)1