Search in sources :

Example 1 with HintSQLHandler

use of io.mycat.route.handler.HintSQLHandler in project Mycat_plus by coderczp.

the class RouteService method route.

public RouteResultset route(SystemConfig sysconf, SchemaConfig schema, int sqlType, String stmt, String charset, ServerConnection sc) throws SQLNonTransientException {
    RouteResultset rrs = null;
    String cacheKey = null;
    /**
     *  SELECT 类型的SQL, 检测
     */
    if (sqlType == ServerParse.SELECT) {
        cacheKey = schema.getName() + stmt;
        rrs = (RouteResultset) sqlRouteCache.get(cacheKey);
        if (rrs != null) {
            checkMigrateRule(schema.getName(), rrs, sqlType);
            return rrs;
        }
    }
    /*!mycat: sql = select name from aa */
    /*!mycat: schema = test */
    // boolean isMatchOldHint = stmt.startsWith(OLD_MYCAT_HINT);
    // boolean isMatchNewHint = stmt.startsWith(NEW_MYCAT_HINT);
    // if (isMatchOldHint || isMatchNewHint ) {
    int hintLength = RouteService.isHintSql(stmt);
    if (hintLength != -1) {
        int endPos = stmt.indexOf("*/");
        if (endPos > 0) {
            // 用!mycat:内部的语句来做路由分析
            // int hintLength = isMatchOldHint ? OLD_MYCAT_HINT.length() : NEW_MYCAT_HINT.length();
            String hint = stmt.substring(hintLength, endPos).trim();
            int firstSplitPos = hint.indexOf(HINT_SPLIT);
            if (firstSplitPos > 0) {
                Map hintMap = parseHint(hint);
                String hintType = (String) hintMap.get(MYCAT_HINT_TYPE);
                String hintSql = (String) hintMap.get(hintType);
                if (hintSql.length() == 0) {
                    LOGGER.warn("comment int sql must meet :/*!mycat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
                    throw new SQLSyntaxErrorException("comment int sql must meet :/*!mycat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
                }
                String realSQL = stmt.substring(endPos + "*/".length()).trim();
                HintHandler hintHandler = HintHandlerFactory.getHintHandler(hintType);
                if (hintHandler != null) {
                    if (hintHandler instanceof HintSQLHandler) {
                        /**
                         * 修复 注解SQL的 sqlType 与 实际SQL的 sqlType 不一致问题, 如: hint=SELECT,real=INSERT
                         * fixed by zhuam
                         */
                        int hintSqlType = ServerParse.parse(hintSql) & 0xff;
                        rrs = hintHandler.route(sysconf, schema, sqlType, realSQL, charset, sc, tableId2DataNodeCache, hintSql, hintSqlType, hintMap);
                    } else {
                        rrs = hintHandler.route(sysconf, schema, sqlType, realSQL, charset, sc, tableId2DataNodeCache, hintSql, sqlType, hintMap);
                    }
                } else {
                    LOGGER.warn("TODO , support hint sql type : " + hintType);
                }
            } else {
                // fixed by runfriends@126.com
                LOGGER.warn("comment in sql must meet :/*!mycat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
                throw new SQLSyntaxErrorException("comment in sql must meet :/*!mcat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
            }
        }
    } else {
        stmt = stmt.trim();
        rrs = RouteStrategyFactory.getRouteStrategy().route(sysconf, schema, sqlType, stmt, charset, sc, tableId2DataNodeCache);
    }
    if (rrs != null && sqlType == ServerParse.SELECT && rrs.isCacheAble()) {
        sqlRouteCache.putIfAbsent(cacheKey, rrs);
    }
    checkMigrateRule(schema.getName(), rrs, sqlType);
    return rrs;
}
Also used : HintHandler(io.mycat.route.handler.HintHandler) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) HintSQLHandler(io.mycat.route.handler.HintSQLHandler) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 2 with HintSQLHandler

use of io.mycat.route.handler.HintSQLHandler in project Mycat-Server by MyCATApache.

the class RouteService method route.

public RouteResultset route(SystemConfig sysconf, SchemaConfig schema, int sqlType, String stmt, String charset, ServerConnection sc) throws SQLNonTransientException {
    RouteResultset rrs = null;
    String cacheKey = null;
    /**
     *  SELECT 类型的SQL, 检测
     */
    if (sqlType == ServerParse.SELECT) {
        cacheKey = schema.getName() + stmt;
        rrs = (RouteResultset) sqlRouteCache.get(cacheKey);
        if (rrs != null) {
            checkMigrateRule(schema.getName(), rrs, sqlType);
            return rrs;
        }
    }
    /*!mycat: sql = select name from aa */
    /*!mycat: schema = test */
    // boolean isMatchOldHint = stmt.startsWith(OLD_MYCAT_HINT);
    // boolean isMatchNewHint = stmt.startsWith(NEW_MYCAT_HINT);
    // if (isMatchOldHint || isMatchNewHint ) {
    int hintLength = RouteService.isHintSql(stmt);
    if (hintLength != -1) {
        int endPos = stmt.indexOf("*/");
        if (endPos > 0) {
            // 用!mycat:内部的语句来做路由分析
            // int hintLength = isMatchOldHint ? OLD_MYCAT_HINT.length() : NEW_MYCAT_HINT.length();
            String hint = stmt.substring(hintLength, endPos).trim();
            int firstSplitPos = hint.indexOf(HINT_SPLIT);
            if (firstSplitPos > 0) {
                Map hintMap = parseHint(hint);
                String hintType = (String) hintMap.get(MYCAT_HINT_TYPE);
                String hintSql = (String) hintMap.get(hintType);
                if (hintSql.length() == 0) {
                    LOGGER.warn("comment int sql must meet :/*!mycat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
                    throw new SQLSyntaxErrorException("comment int sql must meet :/*!mycat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
                }
                String realSQL = stmt.substring(endPos + "*/".length()).trim();
                HintHandler hintHandler = HintHandlerFactory.getHintHandler(hintType);
                if (hintHandler != null) {
                    if (hintHandler instanceof HintSQLHandler) {
                        /**
                         * 修复 注解SQL的 sqlType 与 实际SQL的 sqlType 不一致问题, 如: hint=SELECT,real=INSERT
                         * fixed by zhuam
                         */
                        int hintSqlType = ServerParse.parse(hintSql) & 0xff;
                        rrs = hintHandler.route(sysconf, schema, sqlType, realSQL, charset, sc, tableId2DataNodeCache, hintSql, hintSqlType, hintMap);
                    } else {
                        rrs = hintHandler.route(sysconf, schema, sqlType, realSQL, charset, sc, tableId2DataNodeCache, hintSql, sqlType, hintMap);
                    }
                } else {
                    LOGGER.warn("TODO , support hint sql type : " + hintType);
                }
            } else {
                // fixed by runfriends@126.com
                LOGGER.warn("comment in sql must meet :/*!mycat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
                throw new SQLSyntaxErrorException("comment in sql must meet :/*!mcat:type=value*/ or /*#mycat:type=value*/ or /*mycat:type=value*/: " + stmt);
            }
        }
    } else {
        stmt = stmt.trim();
        rrs = RouteStrategyFactory.getRouteStrategy().route(sysconf, schema, sqlType, stmt, charset, sc, tableId2DataNodeCache);
    }
    if (rrs != null && sqlType == ServerParse.SELECT && rrs.isCacheAble()) {
        sqlRouteCache.putIfAbsent(cacheKey, rrs);
    }
    checkMigrateRule(schema.getName(), rrs, sqlType);
    return rrs;
}
Also used : HintHandler(io.mycat.route.handler.HintHandler) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) HintSQLHandler(io.mycat.route.handler.HintSQLHandler) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Aggregations

HintHandler (io.mycat.route.handler.HintHandler)2 HintSQLHandler (io.mycat.route.handler.HintSQLHandler)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2