Search in sources :

Example 1 with ProcedureParameter

use of io.mycat.route.ProcedureParameter 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 ProcedureParameter

use of io.mycat.route.ProcedureParameter 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)

Aggregations

EOFPacket (io.mycat.net.mysql.EOFPacket)2 FieldPacket (io.mycat.net.mysql.FieldPacket)2 ResultSetHeaderPacket (io.mycat.net.mysql.ResultSetHeaderPacket)2 RowDataPacket (io.mycat.net.mysql.RowDataPacket)2 Procedure (io.mycat.route.Procedure)2 ProcedureParameter (io.mycat.route.ProcedureParameter)2 ByteBuffer (java.nio.ByteBuffer)2