/*
* Copyright 2015 Liu Huanting.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package fm.liu.timo.mysql.packet;
import java.nio.ByteBuffer;
import fm.liu.timo.mysql.BufferUtil;
import fm.liu.timo.mysql.MySQLMessage;
/**
* From server to client after command, if no error and result set -- that is, if the command was a
* query which returned a result set. The Result Set Header Packet is the first of several, possibly
* many, packets that the server sends for result sets. The order of packets for a result set is:
*
* <pre>
* (Result Set Header Packet) the number of columns
* (Field Packets) column descriptors
* (EOF Packet) marker: end of Field Packets
* (Row Data Packets) row contents
* (EOF Packet) marker: end of Data Packets
*
* Bytes Name
* ----- ----
* 1-9 (Length-Coded-Binary) field_count
* 1-9 (Length-Coded-Binary) extra
*
* @see http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol#Result_Set_Header_Packet
* </pre>
*
*/
public class ResultSetHeaderPacket extends ResultSetPacket {
public int fieldCount;
public long extra;
@Override
protected void readBody(MySQLMessage mm) {
this.fieldCount = (int) mm.readLength();
if (mm.hasRemaining()) {
this.extra = mm.readLength();
}
}
@Override
public int calcPacketSize() {
int size = BufferUtil.getLength(fieldCount);
if (extra > 0) {
size += BufferUtil.getLength(extra);
}
return size;
}
@Override
protected String getPacketInfo() {
return "MySQL ResultSetHeader Packet";
}
@Override
protected void writeBody(ByteBuffer buffer) {
BufferUtil.writeLength(buffer, fieldCount);
if (extra > 0) {
BufferUtil.writeLength(buffer, extra);
}
}
}