Search in sources :

Example 91 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class JDBCConnection method ouputCallStatement.

private void ouputCallStatement(RouteResultsetNode rrn, ServerConnection sc, String sql) throws SQLException {
    CallableStatement stmt = null;
    ResultSet rs = null;
    try {
        Procedure procedure = rrn.getProcedure();
        Collection<ProcedureParameter> paramters = procedure.getParamterMap().values();
        String callSql = procedure.toPreCallSql(null);
        stmt = con.prepareCall(callSql);
        for (ProcedureParameter paramter : paramters) {
            if ((ProcedureParameter.IN.equalsIgnoreCase(paramter.getParameterType()) || ProcedureParameter.INOUT.equalsIgnoreCase(paramter.getParameterType()))) {
                Object value = paramter.getValue() != null ? paramter.getValue() : paramter.getName();
                stmt.setObject(paramter.getIndex(), value);
            }
            if (ProcedureParameter.OUT.equalsIgnoreCase(paramter.getParameterType()) || ProcedureParameter.INOUT.equalsIgnoreCase(paramter.getParameterType())) {
                int jdbcType = "oracle".equalsIgnoreCase(getDbType()) && procedure.getListFields().contains(paramter.getName()) ? oracleCURSORTypeValue : paramter.getJdbcType();
                stmt.registerOutParameter(paramter.getIndex(), jdbcType);
            }
        }
        boolean hadResults = stmt.execute();
        ByteBuffer byteBuf = sc.allocate();
        if (procedure.getSelectColumns().size() > 0) {
            List<FieldPacket> fieldPks = new LinkedList<FieldPacket>();
            for (ProcedureParameter paramter : paramters) {
                if (!procedure.getListFields().contains(paramter.getName()) && (ProcedureParameter.OUT.equalsIgnoreCase(paramter.getParameterType()) || ProcedureParameter.INOUT.equalsIgnoreCase(paramter.getParameterType()))) {
                    FieldPacket packet = PacketUtil.getField(paramter.getName(), MysqlDefs.javaTypeMysql(paramter.getJdbcType()));
                    fieldPks.add(packet);
                }
            }
            int colunmCount = fieldPks.size();
            ResultSetHeaderPacket headerPkg = new ResultSetHeaderPacket();
            headerPkg.fieldCount = fieldPks.size();
            headerPkg.packetId = ++packetId;
            byteBuf = headerPkg.write(byteBuf, sc, true);
            byteBuf.flip();
            byte[] header = new byte[byteBuf.limit()];
            byteBuf.get(header);
            byteBuf.clear();
            List<byte[]> fields = new ArrayList<byte[]>(fieldPks.size());
            Iterator<FieldPacket> itor = fieldPks.iterator();
            while (itor.hasNext()) {
                FieldPacket curField = itor.next();
                curField.packetId = ++packetId;
                byteBuf = curField.write(byteBuf, sc, false);
                byteBuf.flip();
                byte[] field = new byte[byteBuf.limit()];
                byteBuf.get(field);
                byteBuf.clear();
                fields.add(field);
                itor.remove();
            }
            EOFPacket eofPckg = new EOFPacket();
            eofPckg.packetId = ++packetId;
            byteBuf = eofPckg.write(byteBuf, sc, false);
            byteBuf.flip();
            byte[] eof = new byte[byteBuf.limit()];
            byteBuf.get(eof);
            byteBuf.clear();
            this.respHandler.fieldEofResponse(header, fields, eof, this);
            RowDataPacket curRow = new RowDataPacket(colunmCount);
            for (String name : procedure.getSelectColumns()) {
                ProcedureParameter procedureParameter = procedure.getParamterMap().get(name);
                curRow.add(StringUtil.encode(String.valueOf(stmt.getObject(procedureParameter.getIndex())), sc.getCharset()));
            }
            curRow.packetId = ++packetId;
            byteBuf = curRow.write(byteBuf, sc, false);
            byteBuf.flip();
            byte[] row = new byte[byteBuf.limit()];
            byteBuf.get(row);
            byteBuf.clear();
            this.respHandler.rowResponse(row, this);
            eofPckg = new EOFPacket();
            eofPckg.packetId = ++packetId;
            if (procedure.isResultList()) {
                eofPckg.status = 42;
            }
            byteBuf = eofPckg.write(byteBuf, sc, false);
            byteBuf.flip();
            eof = new byte[byteBuf.limit()];
            byteBuf.get(eof);
            byteBuf.clear();
            this.respHandler.rowEofResponse(eof, this);
        }
        if (procedure.isResultList()) {
            List<FieldPacket> fieldPks = new LinkedList<FieldPacket>();
            int listSize = procedure.getListFields().size();
            for (ProcedureParameter paramter : paramters) {
                if (procedure.getListFields().contains(paramter.getName()) && (ProcedureParameter.OUT.equalsIgnoreCase(paramter.getParameterType()) || ProcedureParameter.INOUT.equalsIgnoreCase(paramter.getParameterType()))) {
                    listSize--;
                    Object object = stmt.getObject(paramter.getIndex());
                    rs = (ResultSet) object;
                    if (rs == null) {
                        continue;
                    }
                    ResultSetUtil.resultSetToFieldPacket(sc.getCharset(), fieldPks, rs, this.isSpark);
                    int colunmCount = fieldPks.size();
                    ResultSetHeaderPacket headerPkg = new ResultSetHeaderPacket();
                    headerPkg.fieldCount = fieldPks.size();
                    headerPkg.packetId = ++packetId;
                    byteBuf = headerPkg.write(byteBuf, sc, true);
                    byteBuf.flip();
                    byte[] header = new byte[byteBuf.limit()];
                    byteBuf.get(header);
                    byteBuf.clear();
                    List<byte[]> fields = new ArrayList<byte[]>(fieldPks.size());
                    Iterator<FieldPacket> itor = fieldPks.iterator();
                    while (itor.hasNext()) {
                        FieldPacket curField = itor.next();
                        curField.packetId = ++packetId;
                        byteBuf = curField.write(byteBuf, sc, false);
                        byteBuf.flip();
                        byte[] field = new byte[byteBuf.limit()];
                        byteBuf.get(field);
                        byteBuf.clear();
                        fields.add(field);
                        itor.remove();
                    }
                    EOFPacket eofPckg = new EOFPacket();
                    eofPckg.packetId = ++packetId;
                    byteBuf = eofPckg.write(byteBuf, sc, false);
                    byteBuf.flip();
                    byte[] eof = new byte[byteBuf.limit()];
                    byteBuf.get(eof);
                    byteBuf.clear();
                    this.respHandler.fieldEofResponse(header, fields, eof, this);
                    // output row
                    while (rs.next()) {
                        RowDataPacket curRow = new RowDataPacket(colunmCount);
                        for (int i = 0; i < colunmCount; i++) {
                            int j = i + 1;
                            curRow.add(StringUtil.encode(rs.getString(j), sc.getCharset()));
                        }
                        curRow.packetId = ++packetId;
                        byteBuf = curRow.write(byteBuf, sc, false);
                        byteBuf.flip();
                        byte[] row = new byte[byteBuf.limit()];
                        byteBuf.get(row);
                        byteBuf.clear();
                        this.respHandler.rowResponse(row, this);
                    }
                    eofPckg = new EOFPacket();
                    eofPckg.packetId = ++packetId;
                    if (listSize != 0) {
                        eofPckg.status = 42;
                    }
                    byteBuf = eofPckg.write(byteBuf, sc, false);
                    byteBuf.flip();
                    eof = new byte[byteBuf.limit()];
                    byteBuf.get(eof);
                    byteBuf.clear();
                    this.respHandler.rowEofResponse(eof, this);
                }
            }
        }
        if (!procedure.isResultSimpleValue()) {
            byte[] OK = new byte[] { 7, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0 };
            OK[3] = ++packetId;
            this.respHandler.okResponse(OK, this);
        }
        sc.recycle(byteBuf);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
    }
}
Also used : ProcedureParameter(io.mycat.route.ProcedureParameter) ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) Procedure(io.mycat.route.Procedure) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 92 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ShowVariables method execute.

public static void execute(ServerConnection c, String sql) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    List<String> variableList = parseVariable(sql);
    for (String key : variableList) {
        String value = variables.get(key);
        if (value != null) {
            RowDataPacket row = getRow(key, value, c.getCharset());
            row.packetId = ++packetId;
            buffer = row.write(buffer, c, true);
        }
    }
    // write lastEof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 93 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ShowVariables method justReturnValue.

public static void justReturnValue(ServerConnection c, String value) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    if (value != null) {
        RowDataPacket row = new RowDataPacket(1);
        row.add(StringUtil.encode(value, c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write lastEof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 94 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ShowSysParam method execute.

public static void execute(ManagerConnection c) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    SystemConfig sysConfig = MycatServer.getInstance().getConfig().getSystem();
    List<String> paramValues = new ArrayList<String>();
    paramValues.add(sysConfig.getProcessors() + "");
    paramValues.add(sysConfig.getBufferPoolChunkSize() + "B");
    paramValues.add(sysConfig.getBufferPoolPageSize() + "B");
    paramValues.add(sysConfig.getProcessorBufferLocalPercent() + "");
    paramValues.add(sysConfig.getProcessorExecutor() + "");
    paramValues.add(sysConfig.getSequnceHandlerType() == 1 ? "数据库方式" : "本地文件方式");
    paramValues.add(sysConfig.getPacketHeaderSize() + "B");
    paramValues.add(sysConfig.getMaxPacketSize() / 1024 / 1024 + "M");
    paramValues.add(sysConfig.getIdleTimeout() / 1000 / 60 + "分钟");
    paramValues.add(sysConfig.getCharset() + "");
    paramValues.add(ISOLATIONS[sysConfig.getTxIsolation()]);
    paramValues.add(sysConfig.getSqlExecuteTimeout() + "秒");
    paramValues.add(sysConfig.getProcessorCheckPeriod() / 1000 + "秒");
    paramValues.add(sysConfig.getDataNodeIdleCheckPeriod() / 1000 + "秒");
    paramValues.add(sysConfig.getDataNodeHeartbeatPeriod() / 1000 + "秒");
    paramValues.add(sysConfig.getBindIp() + "");
    paramValues.add(sysConfig.getServerPort() + "");
    paramValues.add(sysConfig.getManagerPort() + "");
    for (int i = 0; i < PARAMNAMES.length; i++) {
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode(PARAMNAMES[i], c.getCharset()));
        row.add(StringUtil.encode(paramValues.get(i), c.getCharset()));
        row.add(StringUtil.encode(PARAM_DESCRIPTION[i], c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) RowDataPacket(io.mycat.net.mysql.RowDataPacket) ArrayList(java.util.ArrayList) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 95 with EOFPacket

use of io.mycat.net.mysql.EOFPacket in project Mycat_plus by coderczp.

the class ShowSysLog method execute.

public static void execute(ManagerConnection c, int numLines) {
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    byte packetId = eof.packetId;
    String filename = SystemConfig.getHomePath() + File.separator + "logs" + File.separator + "mycat.log";
    String[] lines = getLinesByLogFile(filename, numLines);
    boolean linesIsEmpty = true;
    for (int i = 0; i < lines.length; i++) {
        String line = lines[i];
        if (line != null) {
            RowDataPacket row = new RowDataPacket(FIELD_COUNT);
            row.add(StringUtil.encode(line.substring(0, 19), c.getCharset()));
            row.add(StringUtil.encode(line.substring(19, line.length()), c.getCharset()));
            row.packetId = ++packetId;
            buffer = row.write(buffer, c, true);
            linesIsEmpty = false;
        }
    }
    if (linesIsEmpty) {
        RowDataPacket row = new RowDataPacket(FIELD_COUNT);
        row.add(StringUtil.encode("NULL", c.getCharset()));
        row.add(StringUtil.encode("NULL", c.getCharset()));
        row.packetId = ++packetId;
        buffer = row.write(buffer, c, true);
    }
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    buffer = lastEof.write(buffer, c, true);
    // write buffer
    c.write(buffer);
}
Also used : RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Aggregations

EOFPacket (io.mycat.net.mysql.EOFPacket)154 ByteBuffer (java.nio.ByteBuffer)153 FieldPacket (io.mycat.net.mysql.FieldPacket)146 RowDataPacket (io.mycat.net.mysql.RowDataPacket)137 ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)18 NIOProcessor (io.mycat.net.NIOProcessor)14 ServerConnection (io.mycat.server.ServerConnection)12 UserStat (io.mycat.statistic.stat.UserStat)12 Map (java.util.Map)10 SchemaConfig (io.mycat.config.model.SchemaConfig)8 ArrayList (java.util.ArrayList)7 MycatConfig (io.mycat.config.MycatConfig)6 FrontendConnection (io.mycat.net.FrontendConnection)6 BackendConnection (io.mycat.backend.BackendConnection)4 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)4 UserConfig (io.mycat.config.model.UserConfig)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 HashMap (java.util.HashMap)4 List (java.util.List)4 TreeSet (java.util.TreeSet)3