|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace SuperSocket.MySQL.Packets |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Represents a MySQL column definition packet |
| 9 | + /// Contains metadata about a column in the result set |
| 10 | + /// </summary> |
| 11 | + public class ColumnDefinitionPacket : MySQLPacket |
| 12 | + { |
| 13 | + public string Catalog { get; set; } |
| 14 | + public string Schema { get; set; } |
| 15 | + public string Table { get; set; } |
| 16 | + public string OrgTable { get; set; } |
| 17 | + public string Name { get; set; } |
| 18 | + public string OrgName { get; set; } |
| 19 | + public ushort NextLength { get; set; } |
| 20 | + public uint CharacterSet { get; set; } |
| 21 | + public uint ColumnLength { get; set; } |
| 22 | + public byte ColumnType { get; set; } |
| 23 | + public ushort Flags { get; set; } |
| 24 | + public byte Decimals { get; set; } |
| 25 | + |
| 26 | + public ColumnDefinitionPacket() |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + protected internal override MySQLPacket Decode(ref SequenceReader<byte> reader, object context) |
| 31 | + { |
| 32 | + // Read catalog (length-encoded string) |
| 33 | + if (!reader.TryReadLengthEncodedString(out string catalog)) |
| 34 | + throw new InvalidOperationException("Failed to read catalog"); |
| 35 | + Catalog = catalog; |
| 36 | + |
| 37 | + // Read schema (length-encoded string) |
| 38 | + if (!reader.TryReadLengthEncodedString(out string schema)) |
| 39 | + throw new InvalidOperationException("Failed to read schema"); |
| 40 | + Schema = schema; |
| 41 | + |
| 42 | + // Read table (length-encoded string) |
| 43 | + if (!reader.TryReadLengthEncodedString(out string table)) |
| 44 | + throw new InvalidOperationException("Failed to read table"); |
| 45 | + Table = table; |
| 46 | + |
| 47 | + // Read org_table (length-encoded string) |
| 48 | + if (!reader.TryReadLengthEncodedString(out string orgTable)) |
| 49 | + throw new InvalidOperationException("Failed to read org_table"); |
| 50 | + OrgTable = orgTable; |
| 51 | + |
| 52 | + // Read name (length-encoded string) |
| 53 | + if (!reader.TryReadLengthEncodedString(out string name)) |
| 54 | + throw new InvalidOperationException("Failed to read name"); |
| 55 | + Name = name; |
| 56 | + |
| 57 | + // Read org_name (length-encoded string) |
| 58 | + if (!reader.TryReadLengthEncodedString(out string orgName)) |
| 59 | + throw new InvalidOperationException("Failed to read org_name"); |
| 60 | + OrgName = orgName; |
| 61 | + |
| 62 | + // Read next_length (length-encoded integer) |
| 63 | + NextLength = (ushort)reader.ReadLengthEncodedInteger(); |
| 64 | + |
| 65 | + // Read character_set (2 bytes) |
| 66 | + if (!reader.TryReadLittleEndian(out short characterSetShort)) |
| 67 | + throw new InvalidOperationException("Failed to read character_set"); |
| 68 | + CharacterSet = (uint)characterSetShort; |
| 69 | + |
| 70 | + // Read column_length (4 bytes) |
| 71 | + if (!reader.TryReadLittleEndian(out int columnLengthInt)) |
| 72 | + throw new InvalidOperationException("Failed to read column_length"); |
| 73 | + ColumnLength = (uint)columnLengthInt; |
| 74 | + |
| 75 | + // Read column_type (1 byte) |
| 76 | + if (!reader.TryRead(out byte columnType)) |
| 77 | + throw new InvalidOperationException("Failed to read column_type"); |
| 78 | + ColumnType = columnType; |
| 79 | + |
| 80 | + // Read flags (2 bytes) |
| 81 | + if (!reader.TryReadLittleEndian(out short flagsShort)) |
| 82 | + throw new InvalidOperationException("Failed to read flags"); |
| 83 | + Flags = (ushort)flagsShort; |
| 84 | + |
| 85 | + // Read decimals (1 byte) |
| 86 | + if (!reader.TryRead(out byte decimals)) |
| 87 | + throw new InvalidOperationException("Failed to read decimals"); |
| 88 | + Decimals = decimals; |
| 89 | + |
| 90 | + // Skip the two null bytes that follow |
| 91 | + reader.Advance(2); |
| 92 | + |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + protected internal override int Encode(IBufferWriter<byte> writer) |
| 97 | + { |
| 98 | + var bytesWritten = 0; |
| 99 | + |
| 100 | + // Write catalog |
| 101 | + var catalogBytes = Encoding.UTF8.GetBytes(Catalog ?? "def"); |
| 102 | + bytesWritten += writer.WriteLengthEncodedInteger((ulong)catalogBytes.Length); |
| 103 | + writer.Write(catalogBytes); |
| 104 | + bytesWritten += catalogBytes.Length; |
| 105 | + |
| 106 | + // Write schema |
| 107 | + var schemaBytes = Encoding.UTF8.GetBytes(Schema ?? ""); |
| 108 | + bytesWritten += writer.WriteLengthEncodedInteger((ulong)schemaBytes.Length); |
| 109 | + writer.Write(schemaBytes); |
| 110 | + bytesWritten += schemaBytes.Length; |
| 111 | + |
| 112 | + // Write table |
| 113 | + var tableBytes = Encoding.UTF8.GetBytes(Table ?? ""); |
| 114 | + bytesWritten += writer.WriteLengthEncodedInteger((ulong)tableBytes.Length); |
| 115 | + writer.Write(tableBytes); |
| 116 | + bytesWritten += tableBytes.Length; |
| 117 | + |
| 118 | + // Write org_table |
| 119 | + var orgTableBytes = Encoding.UTF8.GetBytes(OrgTable ?? ""); |
| 120 | + bytesWritten += writer.WriteLengthEncodedInteger((ulong)orgTableBytes.Length); |
| 121 | + writer.Write(orgTableBytes); |
| 122 | + bytesWritten += orgTableBytes.Length; |
| 123 | + |
| 124 | + // Write name |
| 125 | + var nameBytes = Encoding.UTF8.GetBytes(Name ?? ""); |
| 126 | + bytesWritten += writer.WriteLengthEncodedInteger((ulong)nameBytes.Length); |
| 127 | + writer.Write(nameBytes); |
| 128 | + bytesWritten += nameBytes.Length; |
| 129 | + |
| 130 | + // Write org_name |
| 131 | + var orgNameBytes = Encoding.UTF8.GetBytes(OrgName ?? ""); |
| 132 | + bytesWritten += writer.WriteLengthEncodedInteger((ulong)orgNameBytes.Length); |
| 133 | + writer.Write(orgNameBytes); |
| 134 | + bytesWritten += orgNameBytes.Length; |
| 135 | + |
| 136 | + // Write next_length |
| 137 | + bytesWritten += writer.WriteLengthEncodedInteger(NextLength); |
| 138 | + |
| 139 | + // Write character_set |
| 140 | + bytesWritten += writer.WriteUInt16((ushort)CharacterSet); |
| 141 | + |
| 142 | + // Write column_length |
| 143 | + bytesWritten += writer.WriteUInt32(ColumnLength); |
| 144 | + |
| 145 | + // Write column_type |
| 146 | + bytesWritten += writer.WriteUInt8(ColumnType); |
| 147 | + |
| 148 | + // Write flags |
| 149 | + bytesWritten += writer.WriteUInt16(Flags); |
| 150 | + |
| 151 | + // Write decimals |
| 152 | + bytesWritten += writer.WriteUInt8(Decimals); |
| 153 | + |
| 154 | + // Write two null bytes |
| 155 | + bytesWritten += writer.WriteUInt16(0); |
| 156 | + |
| 157 | + return bytesWritten; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments