-
Notifications
You must be signed in to change notification settings - Fork 331
/
Row.cs
498 lines (437 loc) · 15.8 KB
/
Row.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
using System;
using System.Buffers.Text;
using System.IO;
using System.Runtime.InteropServices;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using MySqlConnector.Protocol;
using MySqlConnector.Protocol.Payloads;
using MySqlConnector.Utilities;
namespace MySqlConnector.Core
{
internal abstract class Row
{
public void SetData(ReadOnlyMemory<byte> data)
{
m_data = data;
GetDataOffsets(m_data.Span, m_dataOffsets, m_dataLengths!);
}
public Row Clone()
{
var clonedRow = CloneCore();
var clonedData = new byte[m_data.Length];
m_data.CopyTo(clonedData);
clonedRow.SetData(clonedData);
return clonedRow;
}
public object GetValue(int ordinal)
{
if (ordinal < 0 || ordinal > ResultSet.ColumnDefinitions!.Length)
throw new ArgumentOutOfRangeException(nameof(ordinal), "value must be between 0 and {0}.".FormatInvariant(ResultSet.ColumnDefinitions!.Length));
if (m_dataOffsets[ordinal] == -1)
return DBNull.Value;
var data = m_data.Slice(m_dataOffsets[ordinal], m_dataLengths[ordinal]).Span;
var columnDefinition = ResultSet.ColumnDefinitions[ordinal];
return GetValueCore(data, columnDefinition);
}
public bool GetBoolean(int ordinal)
{
var value = GetValue(ordinal);
if (value is bool)
return (bool) value;
if (value is sbyte)
return (sbyte) value != 0;
if (value is byte)
return (byte) value != 0;
if (value is short)
return (short) value != 0;
if (value is ushort)
return (ushort) value != 0;
if (value is int)
return (int) value != 0;
if (value is uint)
return (uint) value != 0;
if (value is long)
return (long) value != 0;
if (value is ulong)
return (ulong) value != 0;
if (value is decimal)
return (decimal) value != 0;
return (bool) value;
}
public sbyte GetSByte(int ordinal)
{
var value = GetValue(ordinal);
if (value is sbyte sbyteValue)
return sbyteValue;
if (value is byte byteValue)
return checked((sbyte) byteValue);
if (value is short shortValue)
return checked((sbyte) shortValue);
if (value is ushort ushortValue)
return checked((sbyte) ushortValue);
if (value is int intValue)
return checked((sbyte) intValue);
if (value is uint uintValue)
return checked((sbyte) uintValue);
if (value is long longValue)
return checked((sbyte) longValue);
if (value is ulong ulongValue)
return checked((sbyte) ulongValue);
if (value is decimal decimalValue)
return (sbyte) decimalValue;
return (sbyte) value;
}
public byte GetByte(int ordinal)
{
var value = GetValue(ordinal);
if (value is byte byteValue)
return byteValue;
if (value is sbyte sbyteValue)
return checked((byte) sbyteValue);
if (value is short shortValue)
return checked((byte) shortValue);
if (value is ushort ushortValue)
return checked((byte) ushortValue);
if (value is int intValue)
return checked((byte) intValue);
if (value is uint uintValue)
return checked((byte) uintValue);
if (value is long longValue)
return checked((byte) longValue);
if (value is ulong ulongValue)
return checked((byte) ulongValue);
if (value is decimal decimalValue)
return (byte) decimalValue;
return (byte) value;
}
public long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length)
{
CheckBinaryColumn(ordinal);
if (buffer is null)
{
// this isn't required by the DbDataReader.GetBytes API documentation, but is what mysql-connector-net does
// (as does SqlDataReader: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getbytes.aspx)
return m_dataLengths[ordinal];
}
CheckBufferArguments(dataOffset, buffer, bufferOffset, length);
var offset = (int) dataOffset;
var lengthToCopy = Math.Max(0, Math.Min(m_dataLengths[ordinal] - offset, length));
if (lengthToCopy > 0)
m_data.Slice(m_dataOffsets[ordinal] + offset, lengthToCopy).Span.CopyTo(buffer.AsSpan().Slice(bufferOffset));
return lengthToCopy;
}
public char GetChar(int ordinal)
{
var stringValue = (string) GetValue(ordinal);
return stringValue.Length > 0 ? stringValue[0] : throw new InvalidCastException();
}
public long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length)
{
var value = GetString(ordinal);
if (buffer is null)
return value.Length;
CheckBufferArguments(dataOffset, buffer, bufferOffset, length);
var offset = (int) dataOffset;
var lengthToCopy = Math.Max(0, Math.Min(value.Length - offset, length));
if (lengthToCopy > 0)
value.CopyTo(offset, buffer, bufferOffset, lengthToCopy);
return lengthToCopy;
}
public Guid GetGuid(int ordinal)
{
var value = GetValue(ordinal);
if (value is Guid guid)
return guid;
if (value is string stringValue && Guid.TryParse(stringValue, out guid))
return guid;
if (value is byte[] bytes && bytes.Length == 16)
return CreateGuidFromBytes(Connection.GuidFormat, bytes);
throw new InvalidCastException("The value could not be converted to a GUID: {0}".FormatInvariant(value));
}
public short GetInt16(int ordinal)
{
var value = GetValue(ordinal);
if (value is short)
return (short) value;
if (value is sbyte)
return (sbyte) value;
if (value is byte)
return (byte) value;
if (value is ushort)
return checked((short) (ushort) value);
if (value is int)
return checked((short) (int) value);
if (value is uint)
return checked((short) (uint) value);
if (value is long)
return checked((short) (long) value);
if (value is ulong)
return checked((short) (ulong) value);
if (value is decimal)
return (short) (decimal) value;
return (short) value;
}
public int GetInt32(int ordinal)
{
var value = GetValue(ordinal);
if (value is int)
return (int) value;
if (value is sbyte)
return (sbyte) value;
if (value is byte)
return (byte) value;
if (value is short)
return (short) value;
if (value is ushort)
return (ushort) value;
if (value is uint)
return checked((int) (uint) value);
if (value is long)
return checked((int) (long) value);
if (value is ulong)
return checked((int) (ulong) value);
if (value is decimal)
return (int) (decimal) value;
return (int) value;
}
public long GetInt64(int ordinal)
{
var value = GetValue(ordinal);
if (value is long)
return (long) value;
if (value is sbyte)
return (sbyte) value;
if (value is byte)
return (byte) value;
if (value is short)
return (short) value;
if (value is ushort)
return (ushort) value;
if (value is int)
return (int) value;
if (value is uint)
return (uint) value;
if (value is ulong)
return checked((long) (ulong) value);
if (value is decimal)
return (long) (decimal) value;
return (long) value;
}
public ushort GetUInt16(int ordinal)
{
var value = GetValue(ordinal);
if (value is ushort)
return (ushort) value;
if (value is sbyte)
return checked((ushort) (sbyte) value);
if (value is byte)
return (byte) value;
if (value is short)
return checked((ushort) (short) value);
if (value is int)
return checked((ushort) (int) value);
if (value is uint)
return checked((ushort) (uint) value);
if (value is long)
return checked((ushort) (long) value);
if (value is ulong)
return checked((ushort) (ulong) value);
if (value is decimal)
return (ushort) (decimal) value;
return (ushort) value;
}
public uint GetUInt32(int ordinal)
{
var value = GetValue(ordinal);
if (value is uint)
return (uint) value;
if (value is sbyte)
return checked((uint) (sbyte) value);
if (value is byte)
return (byte) value;
if (value is short)
return checked((uint) (short) value);
if (value is ushort)
return (ushort) value;
if (value is int)
return checked((uint) (int) value);
if (value is long)
return checked((uint) (long) value);
if (value is ulong)
return checked((uint) (ulong) value);
if (value is decimal)
return (uint) (decimal) value;
return (uint) value;
}
public ulong GetUInt64(int ordinal)
{
var value = GetValue(ordinal);
if (value is ulong)
return (ulong) value;
if (value is sbyte)
return checked((ulong) (sbyte) value);
if (value is byte)
return (byte) value;
if (value is short)
return checked((ulong) (short) value);
if (value is ushort)
return (ushort) value;
if (value is int)
return checked((ulong) (int) value);
if (value is uint)
return (uint) value;
if (value is long)
return checked((ulong) (long) value);
if (value is decimal)
return (ulong) (decimal) value;
return (ulong) value;
}
public DateTime GetDateTime(int ordinal)
{
var value = GetValue(ordinal);
if (value is MySqlDateTime mySqlDateTime)
return mySqlDateTime.GetDateTime();
return (DateTime) value;
}
public DateTimeOffset GetDateTimeOffset(int ordinal) => new DateTimeOffset(DateTime.SpecifyKind(GetDateTime(ordinal), DateTimeKind.Utc));
public Stream GetStream(int ordinal)
{
CheckBinaryColumn(ordinal);
return (MemoryMarshal.TryGetArray(m_data, out var arraySegment)) ?
new MemoryStream(arraySegment.Array!, arraySegment.Offset + m_dataOffsets[ordinal], m_dataLengths[ordinal], writable: false) :
throw new InvalidOperationException("Can't get underlying array.");
}
public string GetString(int ordinal) => (string) GetValue(ordinal);
public decimal GetDecimal(int ordinal) => (decimal) GetValue(ordinal);
public double GetDouble(int ordinal)
{
var value = GetValue(ordinal);
return value is float floatValue ? floatValue :
value is decimal decimalValue ? (double) decimalValue :
(double) value;
}
public float GetFloat(int ordinal)
{
var value = GetValue(ordinal);
// Loss of precision is expected, significant loss of information is not.
// Use explicit range checks to guard against that.
return value switch
{
double doubleValue => (doubleValue >= float.MinValue && doubleValue <= float.MaxValue ? (float) doubleValue : throw new InvalidCastException("The value cannot be safely cast to Single.")),
decimal decimalValue => (float) decimalValue,
_ => (float) value
};
}
public MySqlDateTime GetMySqlDateTime(int ordinal)
{
var value = GetValue(ordinal);
if (value is DateTime dateTime)
return new MySqlDateTime(dateTime);
return (MySqlDateTime) value;
}
public MySqlGeometry GetMySqlGeometry(int ordinal)
{
var value = GetValue(ordinal);
if (value is byte[] bytes && ResultSet.ColumnDefinitions![ordinal].ColumnType == ColumnType.Geometry)
return new MySqlGeometry(bytes);
throw new InvalidCastException("Can't convert {0} to MySqlGeometry.".FormatInvariant(ResultSet.ColumnDefinitions![ordinal].ColumnType));
}
public int GetValues(object[] values)
{
int count = Math.Min((values ?? throw new ArgumentNullException(nameof(values))).Length, ResultSet.ColumnDefinitions!.Length);
for (int i = 0; i < count; i++)
values[i] = GetValue(i);
return count;
}
public bool IsDBNull(int ordinal) => m_dataOffsets[ordinal] == -1;
public object this[int ordinal] => GetValue(ordinal);
public object this[string name] => GetValue(ResultSet.GetOrdinal(name));
protected Row(ResultSet resultSet)
{
ResultSet = resultSet;
m_dataOffsets = new int[ResultSet.ColumnDefinitions!.Length];
m_dataLengths = new int[ResultSet.ColumnDefinitions.Length];
}
protected abstract Row CloneCore();
protected abstract object GetValueCore(ReadOnlySpan<byte> data, ColumnDefinitionPayload columnDefinition);
protected abstract void GetDataOffsets(ReadOnlySpan<byte> data, int[] dataOffsets, int[] dataLengths);
protected ResultSet ResultSet { get; }
protected MySqlConnection Connection => ResultSet.Connection;
protected static Guid CreateGuidFromBytes(MySqlGuidFormat guidFormat, ReadOnlySpan<byte> bytes)
{
#if NET45 || NET461 || NET471 || NETSTANDARD1_3 || NETSTANDARD2_0
if (guidFormat == MySqlGuidFormat.Binary16)
return new Guid(new[] { bytes[3], bytes[2], bytes[1], bytes[0], bytes[5], bytes[4], bytes[7], bytes[6], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15] });
if (guidFormat == MySqlGuidFormat.TimeSwapBinary16)
return new Guid(new[] { bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15] });
return new Guid(bytes.ToArray());
#else
unsafe
{
if (guidFormat == MySqlGuidFormat.Binary16)
return new Guid(stackalloc byte[16] { bytes[3], bytes[2], bytes[1], bytes[0], bytes[5], bytes[4], bytes[7], bytes[6], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15] });
if (guidFormat == MySqlGuidFormat.TimeSwapBinary16)
return new Guid(stackalloc byte[16] { bytes[7], bytes[6], bytes[5], bytes[4], bytes[3], bytes[2], bytes[1], bytes[0], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15] });
return new Guid(bytes);
}
#endif
}
protected static object ReadBit(ReadOnlySpan<byte> data, ColumnDefinitionPayload columnDefinition)
{
if ((columnDefinition.ColumnFlags & ColumnFlags.Binary) == 0)
{
// when the Binary flag IS NOT set, the BIT column is transmitted as MSB byte array
ulong bitValue = 0;
for (int i = 0; i < data.Length; i++)
bitValue = bitValue * 256 + data[i];
return bitValue;
}
else if (columnDefinition.ColumnLength <= 5 && data.Length == 1 && data[0] < (byte) (1 << (int) columnDefinition.ColumnLength))
{
// a server bug may return the data as binary even when we expect text: https://github.com/mysql-net/MySqlConnector/issues/713
// in this case, the data can't possibly be an ASCII digit, so assume it's the binary serialisation of BIT(n) where n <= 5
return (ulong) data[0];
}
else
{
// when the Binary flag IS set, the BIT column is transmitted as text
return ParseUInt64(data);
}
}
protected static ulong ParseUInt64(ReadOnlySpan<byte> data) =>
!Utf8Parser.TryParse(data, out ulong value, out var bytesConsumed) || bytesConsumed != data.Length ? throw new FormatException() : value;
private void CheckBinaryColumn(int ordinal)
{
if (m_dataOffsets[ordinal] == -1)
throw new InvalidCastException("Column is NULL.");
var column = ResultSet.ColumnDefinitions![ordinal];
var columnType = column.ColumnType;
if ((column.ColumnFlags & ColumnFlags.Binary) == 0 ||
(columnType != ColumnType.String && columnType != ColumnType.VarString && columnType != ColumnType.TinyBlob &&
columnType != ColumnType.Blob && columnType != ColumnType.MediumBlob && columnType != ColumnType.LongBlob &&
columnType != ColumnType.Geometry))
{
throw new InvalidCastException("Can't convert {0} to bytes.".FormatInvariant(columnType));
}
}
private static void CheckBufferArguments<T>(long dataOffset, T[] buffer, int bufferOffset, int length)
{
if (dataOffset < 0)
throw new ArgumentOutOfRangeException(nameof(dataOffset), dataOffset, "dataOffset must be non-negative");
if (dataOffset > int.MaxValue)
throw new ArgumentOutOfRangeException(nameof(dataOffset), dataOffset, "dataOffset must be a 32-bit integer");
if (length < 0)
throw new ArgumentOutOfRangeException(nameof(length), length, "length must be non-negative");
if (bufferOffset < 0)
throw new ArgumentOutOfRangeException(nameof(bufferOffset), bufferOffset, "bufferOffset must be non-negative");
if (bufferOffset > buffer.Length)
throw new ArgumentOutOfRangeException(nameof(bufferOffset), bufferOffset, "bufferOffset must be within the buffer");
if (checked(bufferOffset + length) > buffer.Length)
throw new ArgumentException("bufferOffset + length cannot exceed buffer.Length", nameof(length));
}
ReadOnlyMemory<byte> m_data;
int[] m_dataOffsets;
int[] m_dataLengths;
}
}