forked from NpgsqlRest/NpgsqlRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
34 lines (30 loc) · 878 Bytes
/
Copy pathExtensions.cs
File metadata and controls
34 lines (30 loc) · 878 Bytes
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
using Npgsql;
namespace NpgsqlRest.Extensions;
public static class Ext
{
public static T Get<T>(this NpgsqlDataReader reader, int ordinal)
{
var value = reader[ordinal];
if (value == DBNull.Value)
{
return default!;
}
// strange bug single char representing as string on older pg versions when using functions
if (typeof(T) == typeof(char) && value.GetType() == typeof(string))
{
if (value is null)
{
return default!;
}
object c = ((string)value)[0];
return (T)c;
}
return (T)value;
}
public static T GetEnum<T>(this string? value) where T : struct
{
Enum.TryParse<T>(value, true, out var result);
// return the first enum (Other) when no match
return result;
}
}