Skip to content

Commit 03d9a13

Browse files
committed
Implement GetUInt16, GetUInt32, GetUInt64. Fixes #439
1 parent 3dd9ebb commit 03d9a13

File tree

3 files changed

+137
-5
lines changed

3 files changed

+137
-5
lines changed

src/MySqlConnector/Core/Row.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,81 @@ public long GetInt64(int ordinal)
219219
return (long) value;
220220
}
221221

222+
public ushort GetUInt16(int ordinal)
223+
{
224+
var value = GetValue(ordinal);
225+
if (value is ushort)
226+
return (ushort) value;
227+
228+
if (value is sbyte)
229+
return checked((ushort) (sbyte) value);
230+
if (value is byte)
231+
return (byte) value;
232+
if (value is short)
233+
return checked((ushort) (short) value);
234+
if (value is int)
235+
return checked((ushort) (int) value);
236+
if (value is uint)
237+
return checked((ushort) (uint) value);
238+
if (value is long)
239+
return checked((ushort) (long) value);
240+
if (value is ulong)
241+
return checked((ushort) (ulong) value);
242+
if (value is decimal)
243+
return (ushort) (decimal) value;
244+
return (ushort) value;
245+
}
246+
247+
public uint GetUInt32(int ordinal)
248+
{
249+
var value = GetValue(ordinal);
250+
if (value is uint)
251+
return (uint) value;
252+
253+
if (value is sbyte)
254+
return checked((uint) (sbyte) value);
255+
if (value is byte)
256+
return (byte) value;
257+
if (value is short)
258+
return checked((uint) (short) value);
259+
if (value is ushort)
260+
return (ushort) value;
261+
if (value is int)
262+
return checked((uint) (int) value);
263+
if (value is long)
264+
return checked((uint) (long) value);
265+
if (value is ulong)
266+
return checked((uint) (ulong) value);
267+
if (value is decimal)
268+
return (uint) (decimal) value;
269+
return (uint) value;
270+
}
271+
272+
public ulong GetUInt64(int ordinal)
273+
{
274+
var value = GetValue(ordinal);
275+
if (value is ulong)
276+
return (ulong) value;
277+
278+
if (value is sbyte)
279+
return checked((ulong) (sbyte) value);
280+
if (value is byte)
281+
return (byte) value;
282+
if (value is short)
283+
return checked((ulong) (short) value);
284+
if (value is ushort)
285+
return (ushort) value;
286+
if (value is int)
287+
return checked((ulong) (int) value);
288+
if (value is uint)
289+
return (uint) value;
290+
if (value is long)
291+
return checked((ulong) (long) value);
292+
if (value is decimal)
293+
return (ulong) (decimal) value;
294+
return (ulong) value;
295+
}
296+
222297
public DateTime GetDateTime(int ordinal) => (DateTime) GetValue(ordinal);
223298

224299
public DateTimeOffset GetDateTimeOffset(int ordinal) => new DateTimeOffset(DateTime.SpecifyKind(GetDateTime(ordinal), DateTimeKind.Utc));

src/MySqlConnector/MySql.Data.MySqlClient/MySqlDataReader.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ public override long GetChars(int ordinal, long dataOffset, char[] buffer, int b
238238
public override float GetFloat(int ordinal) => GetResultSet().GetCurrentRow().GetFloat(ordinal);
239239
public float GetFloat(string name) => GetFloat(GetOrdinal(name));
240240

241+
public ushort GetUInt16(int ordinal) => GetResultSet().GetCurrentRow().GetUInt16(ordinal);
242+
public ushort GetUInt16(string name) => GetUInt16(GetOrdinal(name));
243+
244+
public uint GetUInt32(int ordinal) => GetResultSet().GetCurrentRow().GetUInt32(ordinal);
245+
public uint GetUInt32(string name) => GetUInt32(GetOrdinal(name));
246+
247+
public ulong GetUInt64(int ordinal) => GetResultSet().GetCurrentRow().GetUInt64(ordinal);
248+
public ulong GetUInt64(string name) => GetUInt64(GetOrdinal(name));
249+
241250
public override int VisibleFieldCount => FieldCount;
242251

243252
#if !NETSTANDARD1_3

tests/SideBySide/DataTypes.cs

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,55 @@ public async Task GetInt64(string column, int[] flags, long[] values)
9999
await DoGetValue(column, (r, n) => r.GetInt64(n), (r, s) => r.GetInt64(s), flags, values).ConfigureAwait(false);
100100
}
101101

102-
private async Task DoGetValue<T>(string column, Func<DbDataReader, int, T> getInt, Func<MySqlDataReader, string, T> getIntByName, int[] flags, T[] values)
102+
[Theory]
103+
[InlineData("SByte", new[] { 1, 0, 2, 0, 0 }, new ushort[] { 0, 0, 0, 127, 123 })]
104+
[InlineData("Byte", new[] { 1, 0, 0, 0, 0 }, new ushort[] { 0, 0, 0, 255, 123 })]
105+
[InlineData("Int16", new[] { 1, 0, 2, 0, 0 }, new ushort[] { 0, 0, 0, 32767, 12345 })]
106+
[InlineData("UInt16", new[] { 1, 0, 0, 0, 0 }, new ushort[] { 0, 0, 0, 65535, 12345 })]
107+
[InlineData("Int24", new[] { 1, 0, 2, 2, 2 }, new ushort[] { 0, 0, 0, 0, 0 })]
108+
[InlineData("UInt24", new[] { 1, 0, 0, 2, 2 }, new ushort[] { 0, 0, 0, 0, 0 })]
109+
[InlineData("Int32", new[] { 1, 0, 2, 2, 2 }, new ushort[] { 0, 0, 0, 0, 0 })]
110+
[InlineData("UInt32", new[] { 1, 0, 0, 2, 2 }, new ushort[] { 0, 0, 0, 0, 0 })]
111+
[InlineData("Int64", new[] { 1, 0, 2, 2, 2 }, new ushort[] { 0, 0, 0, 0, 0 })]
112+
[InlineData("UInt64", new[] { 1, 0, 0, 2, 2 }, new ushort[] { 0, 0, 0, 0, 0 })]
113+
public async Task GetUInt16(string column, int[] flags, ushort[] values)
114+
{
115+
await DoGetValue(column, (r, n) => r.GetUInt16(n), (r, s) => r.GetUInt16(s), flags, values).ConfigureAwait(false);
116+
}
117+
118+
[Theory]
119+
[InlineData("SByte", new[] { 1, 0, 2, 0, 0 }, new uint[] { 0, 0, 0, 127, 123 })]
120+
[InlineData("Byte", new[] { 1, 0, 0, 0, 0 }, new uint[] { 0, 0, 0, 255, 123 })]
121+
[InlineData("Int16", new[] { 1, 0, 2, 0, 0 }, new uint[] { 0, 0, 0, 32767, 12345 })]
122+
[InlineData("UInt16", new[] { 1, 0, 0, 0, 0 }, new uint[] { 0, 0, 0, 65535, 12345 })]
123+
[InlineData("Int24", new[] { 1, 0, 2, 0, 0 }, new uint[] { 0, 0, 0, 8388607, 1234567 })]
124+
[InlineData("UInt24", new[] { 1, 0, 0, 0, 0 }, new uint[] { 0, 0, 0, 16777215, 1234567 })]
125+
[InlineData("Int32", new[] { 1, 0, 2, 0, 0 }, new uint[] { 0, 0, 0, 2147483647, 123456789 })]
126+
[InlineData("UInt32", new[] { 1, 0, 0, 0, 0 }, new uint[] { 0, 0, 0, 4294967295, 123456789 })]
127+
[InlineData("Int64", new[] { 1, 0, 2, 2, 2 }, new uint[] { 0, 0, 0, 0, 0 })]
128+
[InlineData("UInt64", new[] { 1, 0, 0, 2, 2 }, new uint[] { 0, 0, 0, 0, 0 })]
129+
public async Task GetUInt32(string column, int[] flags, uint[] values)
130+
{
131+
await DoGetValue(column, (r, n) => r.GetUInt32(n), (r, s) => r.GetUInt32(s), flags, values).ConfigureAwait(false);
132+
}
133+
134+
[Theory]
135+
[InlineData("SByte", new[] { 1, 0, 2, 0, 0 }, new ulong[] { 0, 0, 0, 127, 123 })]
136+
[InlineData("Byte", new[] { 1, 0, 0, 0, 0 }, new ulong[] { 0, 0, 0, 255, 123 })]
137+
[InlineData("Int16", new[] { 1, 0, 2, 0, 0 }, new ulong[] { 0, 0, 0, 32767, 12345 })]
138+
[InlineData("UInt16", new[] { 1, 0, 0, 0, 0 }, new ulong[] { 0, 0, 0, 65535, 12345 })]
139+
[InlineData("Int24", new[] { 1, 0, 2, 0, 0 }, new ulong[] { 0, 0, 0, 8388607, 1234567 })]
140+
[InlineData("UInt24", new[] { 1, 0, 0, 0, 0 }, new ulong[] { 0, 0, 0, 16777215, 1234567 })]
141+
[InlineData("Int32", new[] { 1, 0, 2, 0, 0 }, new ulong[] { 0, 0, 0, 2147483647, 123456789 })]
142+
[InlineData("UInt32", new[] { 1, 0, 0, 0, 0 }, new ulong[] { 0, 0, 0, 4294967295, 123456789 })]
143+
[InlineData("Int64", new[] { 1, 0, 2, 0, 0 }, new ulong[] { 0, 0, 0, 9223372036854775807, 1234567890123456789 })]
144+
[InlineData("UInt64", new[] { 1, 0, 0, 0, 0 }, new ulong[] { 0, 0, 0, 18446744073709551615, 1234567890123456789 })]
145+
public async Task GetUInt64(string column, int[] flags, ulong[] values)
146+
{
147+
await DoGetValue(column, (r, n) => r.GetUInt64(n), (r, s) => r.GetUInt64(s), flags, values).ConfigureAwait(false);
148+
}
149+
150+
private async Task DoGetValue<T>(string column, Func<MySqlDataReader, int, T> getInt, Func<MySqlDataReader, string, T> getIntByName, int[] flags, T[] values)
103151
{
104152
using (var cmd = m_database.Connection.CreateCommand())
105153
{
@@ -202,7 +250,7 @@ public void QueryInt16(string column, string dataTypeName, object[] expected)
202250
[InlineData("UInt16", "SMALLINT", new object[] { null, default(ushort), ushort.MinValue, ushort.MaxValue, (ushort) 12345 })]
203251
public void QueryUInt16(string column, string dataTypeName, object[] expected)
204252
{
205-
DoQuery<InvalidCastException>("integers", column, dataTypeName, expected, reader => reader.GetFieldValue<ushort>(0));
253+
DoQuery("integers", column, dataTypeName, expected, reader => reader.GetUInt16(column));
206254
}
207255

208256
[Theory]
@@ -222,7 +270,7 @@ public void QueryUInt32(string column, string dataTypeName, object[] expected)
222270
// mysql-connector-net incorrectly returns "INT" for "MEDIUMINT UNSIGNED"
223271
dataTypeName = "INT";
224272
#endif
225-
DoQuery<InvalidCastException>("integers", column, dataTypeName, expected, reader => reader.GetFieldValue<uint>(0));
273+
DoQuery("integers", column, dataTypeName, expected, reader => reader.GetUInt32(column));
226274
}
227275

228276
[Theory]
@@ -236,7 +284,7 @@ public void QueryInt64(string column, string dataTypeName, object[] expected)
236284
[InlineData("UInt64", "BIGINT", new object[] { null, default(ulong), ulong.MinValue, ulong.MaxValue, 1234567890123456789u })]
237285
public void QueryUInt64(string column, string dataTypeName, object[] expected)
238286
{
239-
DoQuery<InvalidCastException>("integers", column, dataTypeName, expected, reader => reader.GetFieldValue<ulong>(0));
287+
DoQuery("integers", column, dataTypeName, expected, reader => reader.GetUInt64(0));
240288
}
241289

242290
[Theory]
@@ -245,7 +293,7 @@ public void QueryUInt64(string column, string dataTypeName, object[] expected)
245293
[InlineData("Bit64", new object[] { null, 0UL, 1UL, ulong.MaxValue })]
246294
public void QueryBits(string column, object[] expected)
247295
{
248-
DoQuery<InvalidCastException>("bits", column, "BIT", expected, reader => reader.GetFieldValue<ulong>(0));
296+
DoQuery("bits", column, "BIT", expected, reader => reader.GetUInt64(column));
249297
}
250298

251299
[Theory]

0 commit comments

Comments
 (0)