Search in sources :

Example 46 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat_plus by coderczp.

the class DefaultDruidParser method visitorParse.

/**
 * 子类可覆盖(如果该方法解析得不到表名、字段等信息的,就覆盖该方法,覆盖成空方法,然后通过statementPparse去解析)
 * 通过visitor解析:有些类型的Statement通过visitor解析得不到表名、
 * @param stmt
 */
@Override
public void visitorParse(RouteResultset rrs, SQLStatement stmt, MycatSchemaStatVisitor visitor) throws SQLNonTransientException {
    stmt.accept(visitor);
    ctx.setVisitor(visitor);
    if (stmt instanceof SQLSelectStatement) {
        SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
        if (query instanceof MySqlSelectQueryBlock) {
            if (((MySqlSelectQueryBlock) query).isForUpdate()) {
                rrs.setSelectForUpdate(true);
            }
        }
    }
    List<List<Condition>> mergedConditionList = new ArrayList<List<Condition>>();
    if (visitor.hasOrCondition()) {
        // 包含or语句
        // TODO
        // 根据or拆分
        mergedConditionList = visitor.splitConditions();
    } else {
        // 不包含OR语句
        mergedConditionList.add(visitor.getConditions());
    }
    if (visitor.isHasChange()) {
        // 在解析的过程中子查询被改写了.需要更新ctx.
        ctx.setSql(stmt.toString());
        rrs.setStatement(ctx.getSql());
    }
    if (visitor.getAliasMap() != null) {
        for (Map.Entry<String, String> entry : visitor.getAliasMap().entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (key != null && key.indexOf("`") >= 0) {
                key = key.replaceAll("`", "");
            }
            if (value != null && value.indexOf("`") >= 0) {
                value = value.replaceAll("`", "");
            }
            // 表名前面带database的,去掉
            if (key != null) {
                int pos = key.indexOf(".");
                if (pos > 0) {
                    key = key.substring(pos + 1);
                }
                tableAliasMap.put(key.toUpperCase(), value);
            }
        // else {
        // tableAliasMap.put(key, value);
        // }
        }
        ctx.addTables(visitor.getTables());
        visitor.getAliasMap().putAll(tableAliasMap);
        ctx.setTableAliasMap(tableAliasMap);
    }
    ctx.setRouteCalculateUnits(this.buildRouteCalculateUnits(visitor, mergedConditionList));
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) ArrayList(java.util.ArrayList) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) ArrayList(java.util.ArrayList) List(java.util.List) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat_plus by coderczp.

the class DruidMycatRouteStrategy method routeNormalSqlWithAST.

@Override
public RouteResultset routeNormalSqlWithAST(SchemaConfig schema, String stmt, RouteResultset rrs, String charset, LayerCachePool cachePool, int sqlType, ServerConnection sc) throws SQLNonTransientException {
    /**
     *  只有mysql时只支持mysql语法
     */
    SQLStatementParser parser = null;
    if (schema.isNeedSupportMultiDBType()) {
        parser = new MycatStatementParser(stmt);
    } else {
        parser = new MySqlStatementParser(stmt);
    }
    MycatSchemaStatVisitor visitor = null;
    SQLStatement statement;
    /**
     * 解析出现问题统一抛SQL语法错误
     */
    try {
        statement = parser.parseStatement();
        visitor = new MycatSchemaStatVisitor();
    } catch (Exception t) {
        LOGGER.error("DruidMycatRouteStrategyError", t);
        throw new SQLSyntaxErrorException(t);
    }
    /**
     * 检验unsupported statement
     */
    checkUnSupportedStatement(statement);
    DruidParser druidParser = DruidParserFactory.create(schema, statement, visitor);
    druidParser.parser(schema, rrs, statement, stmt, cachePool, visitor);
    DruidShardingParseInfo ctx = druidParser.getCtx();
    rrs.setTables(ctx.getTables());
    if (visitor.isSubqueryRelationOr()) {
        String err = "In subQuery,the or condition is not supported.";
        LOGGER.error(err);
        throw new SQLSyntaxErrorException(err);
    }
    /* 按照以下情况路由
			1.2.1 可以直接路由.
       		1.2.2 两个表夸库join的sql.调用calat
       		1.2.3 需要先执行subquery 的sql.把subquery拆分出来.获取结果后,与outerquery
		 */
    // add huangyiming 分片规则不一样的且表中带查询条件的则走Catlet
    List<String> tables = ctx.getTables();
    SchemaConfig schemaConf = MycatServer.getInstance().getConfig().getSchemas().get(schema.getName());
    int index = 0;
    RuleConfig firstRule = null;
    boolean directRoute = true;
    Set<String> firstDataNodes = new HashSet<String>();
    Map<String, TableConfig> tconfigs = schemaConf == null ? null : schemaConf.getTables();
    Map<String, RuleConfig> rulemap = new HashMap<>();
    if (tconfigs != null) {
        for (String tableName : tables) {
            TableConfig tc = tconfigs.get(tableName);
            if (tc == null) {
                // add 别名中取
                Map<String, String> tableAliasMap = ctx.getTableAliasMap();
                if (tableAliasMap != null && tableAliasMap.get(tableName) != null) {
                    tc = schemaConf.getTables().get(tableAliasMap.get(tableName));
                }
            }
            if (index == 0) {
                if (tc != null) {
                    firstRule = tc.getRule();
                    // 没有指定分片规则时,不做处理
                    if (firstRule == null) {
                        continue;
                    }
                    firstDataNodes.addAll(tc.getDataNodes());
                    rulemap.put(tc.getName(), firstRule);
                }
            } else {
                if (tc != null) {
                    // ER关系表的时候是可能存在字表中没有tablerule的情况,所以加上判断
                    RuleConfig ruleCfg = tc.getRule();
                    if (ruleCfg == null) {
                        // 没有指定分片规则时,不做处理
                        continue;
                    }
                    Set<String> dataNodes = new HashSet<String>();
                    dataNodes.addAll(tc.getDataNodes());
                    rulemap.put(tc.getName(), ruleCfg);
                    // 如果匹配规则不相同或者分片的datanode不相同则需要走子查询处理
                    if (firstRule != null && ((ruleCfg != null && !ruleCfg.getRuleAlgorithm().equals(firstRule.getRuleAlgorithm())) || (!dataNodes.equals(firstDataNodes)))) {
                        directRoute = false;
                        break;
                    }
                }
            }
            index++;
        }
    }
    RouteResultset rrsResult = rrs;
    if (directRoute) {
        // 直接路由
        if (!RouterUtil.isAllGlobalTable(ctx, schemaConf)) {
            if (rulemap.size() > 1 && !checkRuleField(rulemap, visitor)) {
                String err = "In case of slice table,there is no rule field in the relationship condition!";
                LOGGER.error(err);
                throw new SQLSyntaxErrorException(err);
            }
        }
        rrsResult = directRoute(rrs, ctx, schema, druidParser, statement, cachePool);
    } else {
        int subQuerySize = visitor.getSubQuerys().size();
        if (subQuerySize == 0 && ctx.getTables().size() == 2) {
            // 两表关联,考虑使用catlet
            if (!visitor.getRelationships().isEmpty()) {
                rrs.setCacheAble(false);
                rrs.setFinishedRoute(true);
                rrsResult = catletRoute(schema, ctx.getSql(), charset, sc);
            } else {
                rrsResult = directRoute(rrs, ctx, schema, druidParser, statement, cachePool);
            }
        } else if (subQuerySize == 1) {
            // 只涉及一张表的子查询,使用  MiddlerResultHandler 获取中间结果后,改写原有 sql 继续执行 TODO 后期可能会考虑多个子查询的情况.
            SQLSelect sqlselect = visitor.getSubQuerys().iterator().next();
            if (!visitor.getRelationships().isEmpty()) {
                // 当 inner query  和 outer  query  有关联条件时,暂不支持
                String err = "In case of slice table,sql have different rules,the relationship condition is not supported.";
                LOGGER.error(err);
                throw new SQLSyntaxErrorException(err);
            } else {
                SQLSelectQuery sqlSelectQuery = sqlselect.getQuery();
                if (((MySqlSelectQueryBlock) sqlSelectQuery).getFrom() instanceof SQLExprTableSource) {
                    rrs.setCacheAble(false);
                    rrs.setFinishedRoute(true);
                    rrsResult = middlerResultRoute(schema, charset, sqlselect, sqlType, statement, sc);
                }
            }
        } else if (subQuerySize >= 2) {
            String err = "In case of slice table,sql has different rules,currently only one subQuery is supported.";
            LOGGER.error(err);
            throw new SQLSyntaxErrorException(err);
        }
    }
    return rrsResult;
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) MycatStatementParser(io.mycat.route.parser.druid.MycatStatementParser) MycatSchemaStatVisitor(io.mycat.route.parser.druid.MycatSchemaStatVisitor) HashMap(java.util.HashMap) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) TableConfig(io.mycat.config.model.TableConfig) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) HashSet(java.util.HashSet) RouteResultset(io.mycat.route.RouteResultset) DruidShardingParseInfo(io.mycat.route.parser.druid.DruidShardingParseInfo) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLNonTransientException(java.sql.SQLNonTransientException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) DruidParser(io.mycat.route.parser.druid.DruidParser) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) RuleConfig(io.mycat.config.model.rule.RuleConfig)

Example 48 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat_plus by coderczp.

the class SQLQueryResultHandler method dohandler.

@Override
public String dohandler(SQLStatement statement, SQLSelect sqlselect, SQLObject parent, List param) {
    if (parent.getParent() instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr pp = (SQLBinaryOpExpr) parent.getParent();
        SQLExprImpl listExpr = null;
        if (null == param || param.isEmpty()) {
            listExpr = new SQLNullExpr();
        } else {
            listExpr = new SQLListExpr();
            ((SQLListExpr) listExpr).getItems().addAll(param);
        }
        if (pp.getLeft().equals(parent)) {
            pp.setLeft(listExpr);
        } else if (pp.getRight().equals(parent)) {
            pp.setRight(listExpr);
        }
    } else if (parent.getParent() instanceof SQLSelectItem) {
        SQLSelectItem pp = (SQLSelectItem) parent.getParent();
        SQLExprImpl listExpr = null;
        if (null == param || param.isEmpty()) {
            listExpr = new SQLNullExpr();
        } else {
            listExpr = new SQLListExpr();
            ((SQLListExpr) listExpr).getItems().addAll(param);
        }
        pp.setExpr(listExpr);
    } else if (parent.getParent() instanceof SQLSelectGroupByClause) {
        SQLSelectGroupByClause pp = (SQLSelectGroupByClause) parent.getParent();
        List<SQLExpr> items = pp.getItems();
        for (int i = 0; i < items.size(); i++) {
            SQLExpr expr = items.get(i);
            if (expr instanceof SQLQueryExpr && ((SQLQueryExpr) expr).getSubQuery().equals(sqlselect)) {
                SQLExprImpl listExpr = null;
                if (null == param || param.isEmpty()) {
                    listExpr = new SQLNullExpr();
                } else {
                    listExpr = new SQLListExpr();
                    ((SQLListExpr) listExpr).getItems().addAll(param);
                }
                items.set(i, listExpr);
            }
        }
    } else if (parent.getParent() instanceof SQLSelectOrderByItem) {
        SQLSelectOrderByItem orderItem = (SQLSelectOrderByItem) parent.getParent();
        SQLExprImpl listExpr = null;
        if (null == param || param.isEmpty()) {
            listExpr = new SQLNullExpr();
        } else {
            listExpr = new SQLListExpr();
            ((SQLListExpr) listExpr).getItems().addAll(param);
        }
        listExpr.setParent(orderItem);
        orderItem.setExpr(listExpr);
    } else if (parent.getParent() instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) parent.getParent();
        // select * from subtest1 a where (select 1 from subtest3); 这种情况会进入到当前分支.
        // 改写为   select * from subtest1 a where (1); 或  select * from subtest1 a where (null);
        SQLExprImpl listExpr = null;
        if (null == param || param.isEmpty()) {
            listExpr = new SQLNullExpr();
        } else {
            listExpr = new SQLListExpr();
            ((SQLListExpr) listExpr).getItems().addAll(param);
        }
        listExpr.setParent(query);
        query.setWhere(listExpr);
    }
    return statement.toString();
}
Also used : SQLSelectGroupByClause(com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExprImpl(com.alibaba.druid.sql.ast.SQLExprImpl) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) SQLListExpr(com.alibaba.druid.sql.ast.expr.SQLListExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 49 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat_plus by coderczp.

the class SQLExistsResultHandler method dohandler.

@Override
public String dohandler(SQLStatement statement, SQLSelect sqlselect, SQLObject parent, List param) {
    SQLExpr se = null;
    if (param == null || param.isEmpty()) {
        se = new SQLNullExpr();
    } else {
        se = (SQLExpr) param.get(0);
    }
    if (parent.getParent() instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock msqb = (MySqlSelectQueryBlock) parent.getParent();
        msqb.setWhere(se);
    } else if (parent.getParent() instanceof SQLBinaryOpExpr) {
        SQLBinaryOpExpr sbqe = (SQLBinaryOpExpr) parent.getParent();
        if (sbqe.getLeft().equals(parent)) {
            sbqe.setLeft(se);
        } else {
            sbqe.setRight(se);
        }
    }
    return statement.toString();
}
Also used : SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 50 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project Mycat-Server by MyCATApache.

the class DruidSelectParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) {
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
        parseOrderAggGroupMysql(schema, stmt, rrs, mysqlSelectQuery);
        // 更改canRunInReadDB属性
        if ((mysqlSelectQuery.isForUpdate() || mysqlSelectQuery.isLockInShareMode()) && rrs.isAutocommit() == false) {
            rrs.setCanRunInReadDB(false);
        }
    } else if (sqlSelectQuery instanceof MySqlUnionQuery) {
    // MySqlUnionQuery unionQuery = (MySqlUnionQuery)sqlSelectQuery;
    // MySqlSelectQueryBlock left = (MySqlSelectQueryBlock)unionQuery.getLeft();
    // MySqlSelectQueryBlock right = (MySqlSelectQueryBlock)unionQuery.getLeft();
    // System.out.println();
    }
}
Also used : MySqlUnionQuery(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUnionQuery) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)

Aggregations

MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)82 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)41 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)34 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)31 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)28 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)25 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)23 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)19 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)13 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)11 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)9 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)8 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)8 HashMap (java.util.HashMap)8 Item (com.actiontech.dble.plan.common.item.Item)7 SQLNonTransientException (java.sql.SQLNonTransientException)7 Test (org.junit.Test)7 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)6 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)6