Search in sources :

Example 1 with MiddlerQueryResultHandler

use of io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler in project Mycat_plus by coderczp.

the class ShareRowOutPutDataHandler method onRowData.

@Override
public boolean onRowData(String dataNode, byte[] rowData) {
    RowDataPacket rowDataPkgold = ResultSetUtil.parseRowData(rowData, bfields);
    // 拷贝一份batchRows
    Map<String, byte[]> batchRowsCopy = new ConcurrentHashMap<String, byte[]>();
    batchRowsCopy.putAll(arows);
    // 获取Id字段,
    String id = ByteUtil.getString(rowDataPkgold.fieldValues.get(joinR));
    // 查找ID对应的A表的记录
    // arows.remove(id);
    byte[] arow = getRow(batchRowsCopy, id, joinL);
    // byte[] arow = getRow(id,joinL);//arows.remove(id);
    while (arow != null) {
        // ctx.getAllFields());
        RowDataPacket rowDataPkg = ResultSetUtil.parseRowData(arow, afields);
        for (int i = 1; i < rowDataPkgold.fieldCount; i++) {
            // 设置b.name 字段
            byte[] bname = rowDataPkgold.fieldValues.get(i);
            rowDataPkg.add(bname);
            rowDataPkg.addFieldCount(1);
        }
        // RowData(rowDataPkg);
        // huangyiming add
        MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
        if (null == middlerResultHandler) {
            ctx.writeRow(rowDataPkg);
        } else {
            if (middlerResultHandler instanceof MiddlerQueryResultHandler) {
                // if(middlerResultHandler.getDataType().equalsIgnoreCase("string")){
                byte[] columnData = rowDataPkg.fieldValues.get(0);
                if (columnData != null && columnData.length > 0) {
                    String rowValue = new String(columnData);
                    middlerResultHandler.add(rowValue);
                }
            // }
            }
        }
        arow = getRow(batchRowsCopy, id, joinL);
    // arow = getRow(id,joinL);
    }
    return false;
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) MiddlerResultHandler(io.mycat.backend.mysql.nio.handler.MiddlerResultHandler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MiddlerQueryResultHandler(io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler)

Example 2 with MiddlerQueryResultHandler

use of io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler in project Mycat_plus by coderczp.

the class DruidMycatRouteStrategy method middlerResultRoute.

private RouteResultset middlerResultRoute(final SchemaConfig schema, final String charset, final SQLSelect sqlselect, final int sqlType, final SQLStatement statement, final ServerConnection sc) {
    final String middlesql = SQLUtils.toMySqlString(sqlselect);
    MiddlerResultHandler<String> middlerResultHandler = new MiddlerQueryResultHandler<>(new SecondHandler() {

        @Override
        public void doExecute(List param) {
            sc.getSession2().setMiddlerResultHandler(null);
            String sqls = null;
            // 路由计算
            RouteResultset rrs = null;
            try {
                sqls = buildSql(statement, sqlselect, param);
                rrs = MycatServer.getInstance().getRouterservice().route(MycatServer.getInstance().getConfig().getSystem(), schema, sqlType, sqls.toLowerCase(), charset, sc);
            } catch (Exception e) {
                StringBuilder s = new StringBuilder();
                LOGGER.warn(s.append(this).append(sqls).toString() + " err:" + e.toString(), e);
                String msg = e.getMessage();
                sc.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
                return;
            }
            NonBlockingSession noBlockSession = new NonBlockingSession(sc.getSession2().getSource());
            noBlockSession.setMiddlerResultHandler(null);
            // session的预编译标示传递
            noBlockSession.setPrepared(sc.getSession2().isPrepared());
            if (rrs != null) {
                noBlockSession.setCanClose(false);
                noBlockSession.execute(rrs, ServerParse.SELECT);
            }
        }
    });
    sc.getSession2().setMiddlerResultHandler(middlerResultHandler);
    sc.getSession2().setCanClose(false);
    // 路由计算
    RouteResultset rrs = null;
    try {
        rrs = MycatServer.getInstance().getRouterservice().route(MycatServer.getInstance().getConfig().getSystem(), schema, ServerParse.SELECT, middlesql, charset, sc);
    } catch (Exception e) {
        StringBuilder s = new StringBuilder();
        LOGGER.warn(s.append(this).append(middlesql).toString() + " err:" + e.toString(), e);
        String msg = e.getMessage();
        sc.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
        return null;
    }
    if (rrs != null) {
        rrs.setCacheAble(false);
    }
    return rrs;
}
Also used : NonBlockingSession(io.mycat.server.NonBlockingSession) List(java.util.List) MiddlerQueryResultHandler(io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler) SecondHandler(io.mycat.backend.mysql.nio.handler.SecondHandler) SQLNonTransientException(java.sql.SQLNonTransientException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) RouteResultset(io.mycat.route.RouteResultset)

Example 3 with MiddlerQueryResultHandler

use of io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler in project Mycat-Server by MyCATApache.

the class ShareRowOutPutDataHandler method onRowData.

@Override
public boolean onRowData(String dataNode, byte[] rowData) {
    RowDataPacket rowDataPkgold = ResultSetUtil.parseRowData(rowData, bfields);
    // 拷贝一份batchRows
    Map<String, byte[]> batchRowsCopy = new ConcurrentHashMap<String, byte[]>();
    batchRowsCopy.putAll(arows);
    // 获取Id字段,
    String id = ByteUtil.getString(rowDataPkgold.fieldValues.get(joinR));
    // 查找ID对应的A表的记录
    // arows.remove(id);
    byte[] arow = getRow(batchRowsCopy, id, joinL);
    // byte[] arow = getRow(id,joinL);//arows.remove(id);
    while (arow != null) {
        // ctx.getAllFields());
        RowDataPacket rowDataPkg = ResultSetUtil.parseRowData(arow, afields);
        for (int i = 1; i < rowDataPkgold.fieldCount; i++) {
            // 设置b.name 字段
            byte[] bname = rowDataPkgold.fieldValues.get(i);
            rowDataPkg.add(bname);
            rowDataPkg.addFieldCount(1);
        }
        // RowData(rowDataPkg);
        // huangyiming add
        MiddlerResultHandler middlerResultHandler = session.getMiddlerResultHandler();
        if (null == middlerResultHandler) {
            ctx.writeRow(rowDataPkg);
        } else {
            if (middlerResultHandler instanceof MiddlerQueryResultHandler) {
                // if(middlerResultHandler.getDataType().equalsIgnoreCase("string")){
                byte[] columnData = rowDataPkg.fieldValues.get(0);
                if (columnData != null && columnData.length > 0) {
                    String rowValue = new String(columnData);
                    middlerResultHandler.add(rowValue);
                }
            // }
            }
        }
        arow = getRow(batchRowsCopy, id, joinL);
    // arow = getRow(id,joinL);
    }
    return false;
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) MiddlerResultHandler(io.mycat.backend.mysql.nio.handler.MiddlerResultHandler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MiddlerQueryResultHandler(io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler)

Example 4 with MiddlerQueryResultHandler

use of io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler in project Mycat-Server by MyCATApache.

the class DruidMycatRouteStrategy method middlerResultRoute.

private RouteResultset middlerResultRoute(final SchemaConfig schema, final String charset, final SQLSelect sqlselect, final int sqlType, final SQLStatement statement, final ServerConnection sc) {
    final String middlesql = SQLUtils.toMySqlString(sqlselect);
    MiddlerResultHandler<String> middlerResultHandler = new MiddlerQueryResultHandler<>(new SecondHandler() {

        @Override
        public void doExecute(List param) {
            sc.getSession2().setMiddlerResultHandler(null);
            String sqls = null;
            // 路由计算
            RouteResultset rrs = null;
            try {
                sqls = buildSql(statement, sqlselect, param);
                rrs = MycatServer.getInstance().getRouterservice().route(MycatServer.getInstance().getConfig().getSystem(), schema, sqlType, sqls.toLowerCase(), charset, sc);
            } catch (Exception e) {
                StringBuilder s = new StringBuilder();
                LOGGER.warn(s.append(this).append(sqls).toString() + " err:" + e.toString(), e);
                String msg = e.getMessage();
                sc.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
                return;
            }
            NonBlockingSession noBlockSession = new NonBlockingSession(sc.getSession2().getSource());
            noBlockSession.setMiddlerResultHandler(null);
            // session的预编译标示传递
            noBlockSession.setPrepared(sc.getSession2().isPrepared());
            if (rrs != null) {
                noBlockSession.setCanClose(false);
                noBlockSession.execute(rrs, ServerParse.SELECT);
            }
        }
    });
    sc.getSession2().setMiddlerResultHandler(middlerResultHandler);
    sc.getSession2().setCanClose(false);
    // 路由计算
    RouteResultset rrs = null;
    try {
        rrs = MycatServer.getInstance().getRouterservice().route(MycatServer.getInstance().getConfig().getSystem(), schema, ServerParse.SELECT, middlesql, charset, sc);
    } catch (Exception e) {
        StringBuilder s = new StringBuilder();
        LOGGER.warn(s.append(this).append(middlesql).toString() + " err:" + e.toString(), e);
        String msg = e.getMessage();
        sc.writeErrMessage(ErrorCode.ER_PARSE_ERROR, msg == null ? e.getClass().getSimpleName() : msg);
        return null;
    }
    if (rrs != null) {
        rrs.setCacheAble(false);
    }
    return rrs;
}
Also used : NonBlockingSession(io.mycat.server.NonBlockingSession) List(java.util.List) MiddlerQueryResultHandler(io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler) SecondHandler(io.mycat.backend.mysql.nio.handler.SecondHandler) SQLNonTransientException(java.sql.SQLNonTransientException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

MiddlerQueryResultHandler (io.mycat.backend.mysql.nio.handler.MiddlerQueryResultHandler)4 MiddlerResultHandler (io.mycat.backend.mysql.nio.handler.MiddlerResultHandler)2 SecondHandler (io.mycat.backend.mysql.nio.handler.SecondHandler)2 RowDataPacket (io.mycat.net.mysql.RowDataPacket)2 RouteResultset (io.mycat.route.RouteResultset)2 NonBlockingSession (io.mycat.server.NonBlockingSession)2 SQLNonTransientException (java.sql.SQLNonTransientException)2 SQLSyntaxErrorException (java.sql.SQLSyntaxErrorException)2 List (java.util.List)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2