Search in sources :

Example 1 with HintHandler

use of com.actiontech.dble.route.handler.HintHandler in project dble by actiontech.

the class RouteService method route.

public RouteResultset route(SchemaConfig schema, int sqlType, String stmt, ServerConnection sc) throws SQLException {
    RouteResultset rrs;
    String cacheKey = null;
    /*
         *  SELECT  SQL,  not cached in debug mode
         */
    if (sqlType == ServerParse.SELECT && !LOGGER.isDebugEnabled() && sqlRouteCache != null) {
        cacheKey = (schema == null ? "NULL" : schema.getName()) + "_" + sc.getUser() + "_" + stmt;
        rrs = (RouteResultset) sqlRouteCache.get(cacheKey);
        if (rrs != null) {
            return rrs;
        }
    }
    /*!dble: sql = select name from aa */
    /*!dble: schema = test */
    int hintLength = RouteService.isHintSql(stmt);
    if (hintLength != -1) {
        int endPos = stmt.indexOf("*/");
        if (endPos > 0) {
            // router by hint of !dble:
            String hint = stmt.substring(hintLength, endPos).trim();
            String hintSplit = "=";
            int firstSplitPos = hint.indexOf(hintSplit);
            if (firstSplitPos > 0) {
                Map hintMap = parseHint(hint);
                String hintType = (String) hintMap.get(HINT_TYPE);
                String hintSql = (String) hintMap.get(hintType);
                if (hintSql.length() == 0) {
                    String msg = "comment in sql must meet :/*!" + Versions.ANNOTATION_NAME + "type=value*/ or /*#" + Versions.ANNOTATION_NAME + "type=value*/ or /*" + Versions.ANNOTATION_NAME + "type=value*/: " + stmt;
                    LOGGER.info(msg);
                    throw new SQLSyntaxErrorException(msg);
                }
                String realSQL = stmt.substring(endPos + "*/".length()).trim();
                HintHandler hintHandler = HintHandlerFactory.getHintHandler(hintType);
                if (hintHandler != null) {
                    if (hintHandler instanceof HintSQLHandler) {
                        int hintSqlType = ServerParse.parse(hintSql) & 0xff;
                        rrs = hintHandler.route(schema, sqlType, realSQL, sc, tableId2DataNodeCache, hintSql, hintSqlType, hintMap);
                        // HintSQLHandler will always send to master
                        rrs.setRunOnSlave(false);
                    } else {
                        rrs = hintHandler.route(schema, sqlType, realSQL, sc, tableId2DataNodeCache, hintSql, sqlType, hintMap);
                    }
                } else {
                    String msg = "Not supported hint sql type : " + hintType;
                    LOGGER.info(msg);
                    throw new SQLSyntaxErrorException(msg);
                }
            } else {
                // fixed by runfriends@126.com
                String msg = "comment in sql must meet :/*!" + Versions.ANNOTATION_NAME + "type=value*/ or /*#" + Versions.ANNOTATION_NAME + "type=value*/ or /*" + Versions.ANNOTATION_NAME + "type=value*/: " + stmt;
                LOGGER.info(msg);
                throw new SQLSyntaxErrorException(msg);
            }
        } else {
            stmt = stmt.trim();
            rrs = RouteStrategyFactory.getRouteStrategy().route(schema, sqlType, stmt, sc, tableId2DataNodeCache);
        }
    } else {
        stmt = stmt.trim();
        rrs = RouteStrategyFactory.getRouteStrategy().route(schema, sqlType, stmt, sc, tableId2DataNodeCache);
    }
    if (rrs != null && sqlType == ServerParse.SELECT && rrs.isCacheAble() && !LOGGER.isDebugEnabled() && sqlRouteCache != null) {
        sqlRouteCache.putIfAbsent(cacheKey, rrs);
    }
    return rrs;
}
Also used : HintHandler(com.actiontech.dble.route.handler.HintHandler) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) HintSQLHandler(com.actiontech.dble.route.handler.HintSQLHandler) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HintHandler (com.actiontech.dble.route.handler.HintHandler)1 HintSQLHandler (com.actiontech.dble.route.handler.HintSQLHandler)1 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1