Home  >  Article  >  Database  >  ResultsetRow package and analysis of mysql protocol

ResultsetRow package and analysis of mysql protocol

黄舟
黄舟Original
2017-03-07 13:19:591824browse

git

https://github.com/sea-boat/mysql-protocol

Overview

The ResultsetRow package is part of the package when the server returns the ResultSet and is used to describe the row records of the result set.

mysql communication message structure

##string payloadMessage body, the length is the previously specified payload length
Type Name Description
int5bdf4c78156c7953567bb5a0aef2fc53 payload length Stored according to the least significant byte first, 3-byte payload and 1-byte sequence number combination Into the message header
intf35d6e602fd7d0f0edfa6f7d103c1b57 sequence number
ResultsetRow package

Payload

if(NULL){  0xfb}else{
  Protocol::LengthEncodedString}

More details: http://dev.mysql.com/doc/internals/en/com-query-response.html#text-resultset

ResultsetRow package class

/**
 * 
 * <pre class="brush:php;toolbar:false"><b>resultset row packet.</b>
* @author *
seaboat
*
<b>email: </b>849586227@qq.com
*
<b>blog: </b>http://www.php.cn/;/pre>
 * @version 1.0
 * @see http://www.php.cn/
 */public class ResultsetRowPacket extends MySQLPacket {
    private static final byte NULL_MARK = (byte) 251;    
    public int columnCount;    
    public List<byte[]> columnValues;    
    public ResultsetRowPacket() {

    }    
    public ResultsetRowPacket(int columnCount) {        
    this.columnCount = columnCount;
    }    
    @Override
    public void read(byte[] data) {
        MySQLMessage mm = new MySQLMessage(data);
        packetLength = mm.readUB3();
        packetId = mm.read();        
        for (int i = 0; i < columnCount; i++) {
            columnValues.add(mm.readBytesWithLength());
        }
    }    @Override
    public void write(ByteBuffer buffer) {
        BufferUtil.writeUB3(buffer, calcPacketSize());
        buffer.put(packetId);        
        for (int i = 0; i < columnCount; i++) {            
        byte[] fv = columnValues.get(i);            
        if (fv == null) {
                buffer.put(NULL_MARK);
            } else {
                BufferUtil.writeLength(buffer, fv.length);
                buffer.put(fv);
            }
        }
    }    @Override
    public int calcPacketSize() {        
    int size = 0;        
    for (int i = 0; i < columnCount; i++) {            
    byte[] v = columnValues.get(i);
            size += (v == null || v.length == 0) ? 1 : BufferUtil.getLength(v);
        }        return size;
    }    @Override
    protected String getPacketInfo() {        
    return "MySQL Resultset Row Packet";
    }

}

The above is the ResultsetRow package and analysis content of the mysql protocol. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn