Search in sources :

Example 1 with ResultSetHeaderPacket

use of io.mycat.net.mysql.ResultSetHeaderPacket in project Mycat-Server by MyCATApache.

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 2 with ResultSetHeaderPacket

use of io.mycat.net.mysql.ResultSetHeaderPacket in project Mycat-Server by MyCATApache.

the class PacketUtil method getHeader.

public static final ResultSetHeaderPacket getHeader(int fieldCount) {
    ResultSetHeaderPacket packet = new ResultSetHeaderPacket();
    packet.packetId = 1;
    packet.fieldCount = fieldCount;
    return packet;
}
Also used : ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket)

Example 3 with ResultSetHeaderPacket

use of io.mycat.net.mysql.ResultSetHeaderPacket in project Mycat-Server by MyCATApache.

the class MyCATSequnceProcessor method outRawData.

private void outRawData(ServerConnection sc, String value) {
    byte packetId = 0;
    int fieldCount = 1;
    ByteBuffer byteBuf = sc.allocate();
    ResultSetHeaderPacket headerPkg = new ResultSetHeaderPacket();
    headerPkg.fieldCount = fieldCount;
    headerPkg.packetId = ++packetId;
    byteBuf = headerPkg.write(byteBuf, sc, true);
    FieldPacket fieldPkg = new FieldPacket();
    fieldPkg.packetId = ++packetId;
    fieldPkg.name = StringUtil.encode("SEQUNCE", sc.getCharset());
    byteBuf = fieldPkg.write(byteBuf, sc, true);
    EOFPacket eofPckg = new EOFPacket();
    eofPckg.packetId = ++packetId;
    byteBuf = eofPckg.write(byteBuf, sc, true);
    RowDataPacket rowDataPkg = new RowDataPacket(fieldCount);
    rowDataPkg.packetId = ++packetId;
    rowDataPkg.add(StringUtil.encode(value, sc.getCharset()));
    byteBuf = rowDataPkg.write(byteBuf, sc, true);
    // write last eof
    EOFPacket lastEof = new EOFPacket();
    lastEof.packetId = ++packetId;
    byteBuf = lastEof.write(byteBuf, sc, true);
    // write buffer
    sc.write(byteBuf);
}
Also used : ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 4 with ResultSetHeaderPacket

use of io.mycat.net.mysql.ResultSetHeaderPacket in project Mycat-Server by MyCATApache.

the class Explain2Handler method showerror.

private static void showerror(String stmt, ServerConnection c, String msg) {
    ByteBuffer buffer = c.allocate();
    // write header
    ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
    byte packetId = header.packetId;
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        field.packetId = ++packetId;
        buffer = field.write(buffer, c, true);
    }
    // write eof
    EOFPacket eof = new EOFPacket();
    eof.packetId = ++packetId;
    buffer = eof.write(buffer, c, true);
    RowDataPacket row = new RowDataPacket(FIELD_COUNT);
    row.add(StringUtil.encode(stmt, c.getCharset()));
    row.add(StringUtil.encode(msg, 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);
    // post write
    c.write(buffer);
}
Also used : ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) ByteBuffer(java.nio.ByteBuffer) FieldPacket(io.mycat.net.mysql.FieldPacket)

Example 5 with ResultSetHeaderPacket

use of io.mycat.net.mysql.ResultSetHeaderPacket in project Mycat-Server by MyCATApache.

the class SelectVariables method execute.

public static void execute(ServerConnection c, String sql) {
    String subSql = sql.substring(sql.indexOf("SELECT") + 6);
    List<String> splitVar = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(subSql);
    splitVar = convert(splitVar);
    int FIELD_COUNT = splitVar.size();
    ResultSetHeaderPacket header = PacketUtil.getHeader(FIELD_COUNT);
    FieldPacket[] fields = new FieldPacket[FIELD_COUNT];
    int i = 0;
    byte packetId = 0;
    header.packetId = ++packetId;
    for (int i1 = 0, splitVarSize = splitVar.size(); i1 < splitVarSize; i1++) {
        String s = splitVar.get(i1);
        fields[i] = PacketUtil.getField(s, Fields.FIELD_TYPE_VAR_STRING);
        fields[i++].packetId = ++packetId;
    }
    ByteBuffer buffer = c.allocate();
    // write header
    buffer = header.write(buffer, c, true);
    // write fields
    for (FieldPacket field : fields) {
        buffer = field.write(buffer, c, true);
    }
    EOFPacket eof = new EOFPacket();
    eof.packetId = ++packetId;
    // write eof
    buffer = eof.write(buffer, c, true);
    // write rows
    //byte packetId = eof.packetId;
    RowDataPacket row = new RowDataPacket(FIELD_COUNT);
    for (int i1 = 0, splitVarSize = splitVar.size(); i1 < splitVarSize; i1++) {
        String s = splitVar.get(i1);
        String value = variables.get(s) == null ? "" : variables.get(s);
        row.add(value.getBytes());
    }
    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 : ResultSetHeaderPacket(io.mycat.net.mysql.ResultSetHeaderPacket) RowDataPacket(io.mycat.net.mysql.RowDataPacket) EOFPacket(io.mycat.net.mysql.EOFPacket) FieldPacket(io.mycat.net.mysql.FieldPacket) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)21 EOFPacket (io.mycat.net.mysql.EOFPacket)18 ByteBuffer (java.nio.ByteBuffer)18 FieldPacket (io.mycat.net.mysql.FieldPacket)17 RowDataPacket (io.mycat.net.mysql.RowDataPacket)13 ServerConnection (io.mycat.server.ServerConnection)3 Procedure (io.mycat.route.Procedure)2 ProcedureParameter (io.mycat.route.ProcedureParameter)2 RouteResultset (io.mycat.route.RouteResultset)2 RouteResultsetNode (io.mycat.route.RouteResultsetNode)2 DataRow (io.mycat.backend.postgresql.packet.DataRow)1 RowDescription (io.mycat.backend.postgresql.packet.RowDescription)1 BufferArray (io.mycat.buffer.BufferArray)1 ColMeta (io.mycat.sqlengine.mpp.ColMeta)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1