Search in sources :

Example 1 with ServerSchemaStatVisitor

use of com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor in project dble by actiontech.

the class DQLRouteTest method test.

@Test
public void test() throws Exception {
    String stmt = "select * from `offer` where id = 100";
    SchemaConfig schema = schemaMap.get("mysqldb");
    RouteResultset rrs = new RouteResultset(stmt, 7);
    SQLStatementParser parser = null;
    parser = new MySqlStatementParser(stmt);
    SQLStatement statement;
    ServerSchemaStatVisitor visitor = null;
    try {
        statement = parser.parseStatement();
        visitor = new ServerSchemaStatVisitor();
    } catch (Exception t) {
        throw new SQLSyntaxErrorException(t);
    }
    ctx = new DruidShardingParseInfo();
    List<RouteCalculateUnit> taskList = visitorParse(rrs, statement, visitor);
    Assert.assertEquals(true, !taskList.get(0).getTablesAndConditions().isEmpty());
}
Also used : DruidShardingParseInfo(com.actiontech.dble.route.parser.druid.DruidShardingParseInfo) RouteCalculateUnit(com.actiontech.dble.route.parser.druid.RouteCalculateUnit) ServerSchemaStatVisitor(com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) Test(org.junit.Test)

Example 2 with ServerSchemaStatVisitor

use of com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor in project dble by actiontech.

the class ServerSchemaStatVisitorTest method getConditionList.

private List<List<Condition>> getConditionList(String sql) {
    SQLStatementParser parser = null;
    parser = new MySqlStatementParser(sql);
    ServerSchemaStatVisitor visitor = null;
    SQLStatement statement = null;
    // throw exception
    try {
        statement = parser.parseStatement();
        visitor = new ServerSchemaStatVisitor();
    } catch (Exception e) {
        e.printStackTrace();
    }
    statement.accept(visitor);
    List<List<Condition>> mergedConditionList = new ArrayList<List<Condition>>();
    if (visitor.hasOrCondition()) {
        // contains OR
        mergedConditionList = visitor.splitConditions();
    } else {
        mergedConditionList.add(visitor.getConditions());
    }
    return mergedConditionList;
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) ServerSchemaStatVisitor(com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 3 with ServerSchemaStatVisitor

use of com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor in project dble by actiontech.

the class DefaultRouteStrategy method routeNormalSqlWithAST.

@Override
public RouteResultset routeNormalSqlWithAST(SchemaConfig schema, String originSql, RouteResultset rrs, LayerCachePool cachePool, ServerConnection sc) throws SQLException {
    SQLStatement statement = parserSQL(originSql);
    sc.getSession2().endParse();
    DruidParser druidParser = DruidParserFactory.create(statement, rrs.getSqlType());
    return RouterUtil.routeFromParser(druidParser, schema, rrs, statement, originSql, cachePool, new ServerSchemaStatVisitor(), sc);
}
Also used : ServerSchemaStatVisitor(com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor) DruidParser(com.actiontech.dble.route.parser.druid.DruidParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 4 with ServerSchemaStatVisitor

use of com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor in project dble by actiontech.

the class MergeBuilder method constructByStatement.

public RouteResultset constructByStatement(String sql, SQLSelectStatement select) throws SQLException {
    ServerSchemaStatVisitor visitor = new ServerSchemaStatVisitor();
    DruidParser druidParser = new DruidSingleUnitSelectParser();
    RouteResultset rrs = new RouteResultset(sql, ServerParse.SELECT);
    LayerCachePool pool = DbleServer.getInstance().getRouterService().getTableId2DataNodeCache();
    SchemaConfig schemaConfig = schemaConfigMap.get(node.getReferedTableNodes().get(0).getSchema());
    return RouterUtil.routeFromParser(druidParser, schemaConfig, rrs, select, sql, pool, visitor, session.getSource());
}
Also used : DruidSingleUnitSelectParser(com.actiontech.dble.route.parser.druid.impl.DruidSingleUnitSelectParser) ServerSchemaStatVisitor(com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) DruidParser(com.actiontech.dble.route.parser.druid.DruidParser) LayerCachePool(com.actiontech.dble.cache.LayerCachePool) RouteResultset(com.actiontech.dble.route.RouteResultset)

Aggregations

ServerSchemaStatVisitor (com.actiontech.dble.route.parser.druid.ServerSchemaStatVisitor)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)2 DruidParser (com.actiontech.dble.route.parser.druid.DruidParser)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2 LayerCachePool (com.actiontech.dble.cache.LayerCachePool)1 RouteResultset (com.actiontech.dble.route.RouteResultset)1 DruidShardingParseInfo (com.actiontech.dble.route.parser.druid.DruidShardingParseInfo)1 RouteCalculateUnit (com.actiontech.dble.route.parser.druid.RouteCalculateUnit)1 DruidSingleUnitSelectParser (com.actiontech.dble.route.parser.druid.impl.DruidSingleUnitSelectParser)1 Condition (com.alibaba.druid.stat.TableStat.Condition)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.Test)1