当前位置:Gxlcms > mysql > mysql协议的ColumnDefinition包及解析代码详情

mysql协议的ColumnDefinition包及解析代码详情

时间:2021-07-01 10:21:17 帮助过:40人阅读


git

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

概况

ColumnDefinition包属于服务端返回ResultSet时的其中一部分包,用于描述结果集的字段信息。

mysql通信报文结构

类型名字描述
int<3>payload长度按照the least significant byte first存储,3个字节的payload和1个字节的序列号组合成报文头
int<1>序列号
stringpayload报文体,长度即为前面指定的payload长度

ColumnDefinition包

Payload

  1. lenenc_str catalog
  2. lenenc_str schema
  3. lenenc_str table
  4. lenenc_str org_table
  5. lenenc_str name
  6. lenenc_str org_name
  7. lenenc_int length of fixed-length fields [0c]2
  8. character set4
  9. column length1
  10. type2
  11. flags1
  12. decimals2
  13. filler [00] [00] if command was COM_FIELD_LIST {
  14. lenenc_int
  15. length of default-valuesstring[$len]
  16. default values
  17. }

更多详情 : http://dev.mysql.com/doc/internals/en/com-query-response.html#column-definition

ColumnCount包类

  1. /**
  2. *
  3. * <pre><b>column definition command packet.</b></pre>
  4. * @author
  5. * <pre>seaboat</pre>
  6. * <pre><b>email: </b>849586227@qq.com</pre>
  7. * <pre><b>blog: </b>http://www.gxlcms.com/;/pre>
  8. * @version 1.0
  9. * @see http://www.gxlcms.com/
  10. */public class ColumnDefinitionPacket extends MySQLPacket {
  11. private static final byte[] DEFAULT_CATALOG = "def".getBytes();
  12. private static final byte NEXT_LENGTH = 0x0c;
  13. private static final byte[] FILLER = { 00, 00 };
  14. public byte[] catalog = DEFAULT_CATALOG;// always "def"
  15. public byte[] schema;
  16. public byte[] table;
  17. public byte[] orgTable;
  18. public byte[] name;
  19. public byte[] orgName;
  20. public byte nextLength = NEXT_LENGTH;// always 0x0c
  21. public int charsetSet;
  22. public long length;
  23. public int type;
  24. public int flags;
  25. public byte decimals;
  26. public byte[] filler = FILLER;
  27. public byte[] defaultValues;
  28. public void read(byte[] data) {
  29. MySQLMessage mm = new MySQLMessage(data);
  30. this.packetLength = mm.readUB3();
  31. this.packetId = mm.read();
  32. this.catalog = mm.readBytesWithLength();
  33. this.schema = mm.readBytesWithLength();
  34. this.table = mm.readBytesWithLength();
  35. this.orgTable = mm.readBytesWithLength();
  36. this.name = mm.readBytesWithLength();
  37. this.orgName = mm.readBytesWithLength();
  38. this.nextLength = mm.read();
  39. this.charsetSet = mm.readUB2();
  40. this.length = mm.readUB4();
  41. this.type = mm.read() & 0xff;
  42. this.flags = mm.readUB2();
  43. this.decimals = mm.read();
  44. this.filler = mm.readBytes(2);
  45. if (mm.hasRemaining()) {
  46. this.defaultValues = mm.readBytesWithLength();
  47. }
  48. } @Override
  49. public void write(ByteBuffer buffer) {
  50. int size = calcPacketSize();
  51. BufferUtil.writeUB3(buffer, size);
  52. buffer.put(packetId);
  53. BufferUtil.writeWithLength(buffer, catalog, (byte) 0);
  54. BufferUtil.writeWithLength(buffer, schema, (byte) 0);
  55. BufferUtil.writeWithLength(buffer, table, (byte) 0);
  56. BufferUtil.writeWithLength(buffer, orgTable, (byte) 0);
  57. BufferUtil.writeWithLength(buffer, name, (byte) 0);
  58. BufferUtil.writeWithLength(buffer, orgName, (byte) 0);
  59. buffer.put(NEXT_LENGTH);
  60. BufferUtil.writeUB2(buffer, charsetSet);
  61. BufferUtil.writeUB4(buffer, length);
  62. buffer.put((byte) (type & 0xff));
  63. BufferUtil.writeUB2(buffer, flags);
  64. buffer.put(decimals);
  65. buffer.put(FILLER);
  66. if (defaultValues != null) {
  67. //only use for show columns
  68. BufferUtil.writeWithLength(buffer, defaultValues);
  69. }
  70. } @Override
  71. public int calcPacketSize() {
  72. int size = (catalog == null ? 1 : BufferUtil.getLength(catalog));
  73. size += (schema == null ? 1 : BufferUtil.getLength(schema));
  74. size += (table == null ? 1 : BufferUtil.getLength(table));
  75. size += (orgTable == null ? 1 : BufferUtil.getLength(orgTable));
  76. size += (name == null ? 1 : BufferUtil.getLength(name));
  77. size += (orgName == null ? 1 : BufferUtil.getLength(orgName));
  78. size += 13; if (defaultValues != null) {
  79. size += BufferUtil.getLength(defaultValues);
  80. }
  81. return size;
  82. }
  83. @Override
  84. protected String getPacketInfo() {
  85. return "MySQL Column Definition Packet";
  86. }
  87. }

以上就是mysql 协议的ColumnDefinition包及解析代码详情的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行