Skip to content

Commit 16271ea

Browse files
fix: re-organize type mappings in DB drivers
1 parent e2426e4 commit 16271ea

2 files changed

Lines changed: 126 additions & 102 deletions

File tree

Drivers/MySqlConnectorDriver.cs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public partial class MySqlConnectorDriver(
2626
public override Dictionary<string, ColumnMapping> ColumnMappings { get; } =
2727
new()
2828
{
29+
/* Numeric data types */
2930
["bool"] = new(
3031
new()
3132
{
@@ -42,6 +43,16 @@ public partial class MySqlConnectorDriver(
4243
},
4344
ordinal => $"reader.GetInt16({ordinal})"
4445
),
46+
["int"] = new(
47+
new()
48+
{
49+
{ "int", new() },
50+
{ "integer", new() },
51+
{ "mediumint", new() }
52+
},
53+
ordinal => $"reader.GetInt32({ordinal})",
54+
convertFunc: x => $"Convert.ToInt32{x}"
55+
),
4556
["long"] = new(
4657
new()
4758
{
@@ -50,7 +61,24 @@ public partial class MySqlConnectorDriver(
5061
ordinal => $"reader.GetInt64({ordinal})",
5162
convertFunc: x => $"Convert.ToInt64{x}"
5263
),
53-
["byte"] = new ColumnMapping(
64+
["double"] = new(
65+
new()
66+
{
67+
{ "double", new() },
68+
{ "float", new() }
69+
},
70+
ordinal => $"reader.GetDouble({ordinal})"
71+
),
72+
["decimal"] = new(
73+
new()
74+
{
75+
{ "decimal", new() }
76+
},
77+
ordinal => $"reader.GetDecimal({ordinal})"
78+
),
79+
80+
/* Binary data types */
81+
["byte"] = new(
5482
new()
5583
{
5684
{ "bit", new() }
@@ -69,6 +97,8 @@ public partial class MySqlConnectorDriver(
6997
},
7098
ordinal => $"reader.GetFieldValue<byte[]>({ordinal})"
7199
),
100+
101+
/* String data types */
72102
["string"] = new(
73103
new()
74104
{
@@ -83,6 +113,8 @@ public partial class MySqlConnectorDriver(
83113
},
84114
ordinal => $"reader.GetString({ordinal})"
85115
),
116+
117+
/* Date and time data types */
86118
["DateTime"] = new(
87119
new()
88120
{
@@ -92,31 +124,8 @@ public partial class MySqlConnectorDriver(
92124
},
93125
ordinal => $"reader.GetDateTime({ordinal})"
94126
),
95-
["int"] = new(
96-
new()
97-
{
98-
{ "int", new() },
99-
{ "integer", new() },
100-
{ "mediumint", new() }
101-
},
102-
ordinal => $"reader.GetInt32({ordinal})",
103-
convertFunc: x => $"Convert.ToInt32{x}"
104-
),
105-
["double"] = new(
106-
new()
107-
{
108-
{ "double", new() },
109-
{ "float", new() }
110-
},
111-
ordinal => $"reader.GetDouble({ordinal})"
112-
),
113-
["decimal"] = new(
114-
new()
115-
{
116-
{ "decimal", new() }
117-
},
118-
ordinal => $"reader.GetDecimal({ordinal})"
119-
),
127+
128+
/* Unstructured data types */
120129
["JsonElement"] = new(
121130
new()
122131
{
@@ -134,6 +143,8 @@ public partial class MySqlConnectorDriver(
134143
sqlMapper: "SqlMapper.AddTypeHandler(typeof(JsonElement), new JsonElementTypeHandler());",
135144
sqlMapperImpl: JsonElementTypeHandler
136145
),
146+
147+
/* Other data types */
137148
["object"] = new(
138149
new()
139150
{

Drivers/NpgsqlDriver.cs

Lines changed: 89 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,37 @@ public NpgsqlDriver(
3434
public sealed override Dictionary<string, ColumnMapping> ColumnMappings { get; } =
3535
new()
3636
{
37+
/* Numeric data types */
38+
["bool"] = new(
39+
new()
40+
{
41+
{ "bool", new() },
42+
{ "boolean", new() }
43+
},
44+
readerFn: ordinal => $"reader.GetBoolean({ordinal})",
45+
readerArrayFn: ordinal => $"reader.GetFieldValue<bool[]>({ordinal})"
46+
),
47+
["short"] = new(
48+
new()
49+
{
50+
{ "int2", new() }
51+
},
52+
readerFn: ordinal => $"reader.GetInt16({ordinal})",
53+
readerArrayFn: ordinal => $"reader.GetFieldValue<short[]>({ordinal})",
54+
convertFunc: x => $"Convert.ToInt16({x})"
55+
),
56+
["int"] = new(
57+
new()
58+
{
59+
{ "integer", new() },
60+
{ "int", new() },
61+
{ "int4", new() },
62+
{ "serial", new() }
63+
},
64+
readerFn: ordinal => $"reader.GetInt32({ordinal})",
65+
readerArrayFn: ordinal => $"reader.GetFieldValue<int[]>({ordinal})",
66+
convertFunc: x => $"Convert.ToInt32({x})"
67+
),
3768
["long"] = new(
3869
new()
3970
{
@@ -45,21 +76,34 @@ public NpgsqlDriver(
4576
readerArrayFn: ordinal => $"reader.GetFieldValue<long[]>({ordinal})",
4677
convertFunc: x => $"Convert.ToInt64({x})"
4778
),
48-
["byte[]"] = new(
79+
["float"] = new(
4980
new()
5081
{
51-
{ "binary", new() },
52-
{ "bit", new() },
53-
{ "bytea", new() },
54-
{ "blob", new() },
55-
{ "longblob", new() },
56-
{ "mediumblob", new() },
57-
{ "tinyblob", new() },
58-
{ "varbinary", new() }
82+
{ "float4", new() }
5983
},
60-
readerFn: ordinal => $"reader.GetFieldValue<byte[]>({ordinal})",
61-
readerArrayFn: ordinal => $"reader.GetFieldValue<byte[][]>({ordinal})"
84+
readerFn: ordinal => $"reader.GetFloat({ordinal})",
85+
readerArrayFn: ordinal => $"reader.GetFieldValue<float[]>({ordinal})"
86+
),
87+
["decimal"] = new(
88+
new()
89+
{
90+
{ "numeric", new() },
91+
{ "decimal", new() },
92+
{ "money", new(NpgsqlTypeOverride: "NpgsqlDbType.Money") }
93+
},
94+
readerFn: ordinal => $"reader.GetDecimal({ordinal})",
95+
readerArrayFn: ordinal => $"reader.GetFieldValue<decimal[]>({ordinal})"
6296
),
97+
["double"] = new(
98+
new()
99+
{
100+
{ "float8", new() }
101+
},
102+
readerFn: ordinal => $"reader.GetDouble({ordinal})",
103+
readerArrayFn: ordinal => $"reader.GetFieldValue<double[]>({ordinal})"
104+
),
105+
106+
/* String data types */
63107
["string"] = new(
64108
new()
65109
{
@@ -75,15 +119,8 @@ public NpgsqlDriver(
75119
readerFn: ordinal => $"reader.GetString({ordinal})",
76120
readerArrayFn: ordinal => $"reader.GetFieldValue<string[]>({ordinal})"
77121
),
78-
["Guid"] = new(
79-
new()
80-
{
81-
{ "uuid", new() }
82-
},
83-
readerFn: ordinal => $"reader.GetFieldValue<Guid>({ordinal})",
84-
readerArrayFn: ordinal => $"reader.GetFieldValue<Guid[]>({ordinal})",
85-
convertFunc: x => $"Guid.Parse({x}?.ToString())"
86-
),
122+
123+
/* Date and time data types */
87124
["TimeSpan"] = new(
88125
new()
89126
{
@@ -103,6 +140,8 @@ public NpgsqlDriver(
103140
readerFn: ordinal => $"reader.GetDateTime({ordinal})",
104141
readerArrayFn: ordinal => $"reader.GetFieldValue<DateTime[]>({ordinal})"
105142
),
143+
144+
/* Unstructured data types */
106145
["JsonElement"] = new(
107146
new()
108147
{
@@ -121,62 +160,6 @@ public NpgsqlDriver(
121160
sqlMapper: "SqlMapper.AddTypeHandler(typeof(JsonElement), new JsonElementTypeHandler());",
122161
sqlMapperImpl: JsonElementTypeHandler
123162
),
124-
["short"] = new(
125-
new()
126-
{
127-
{ "int2", new() }
128-
},
129-
readerFn: ordinal => $"reader.GetInt16({ordinal})",
130-
readerArrayFn: ordinal => $"reader.GetFieldValue<short[]>({ordinal})",
131-
convertFunc: x => $"Convert.ToInt16({x})"
132-
),
133-
["int"] = new(
134-
new()
135-
{
136-
{ "integer", new() },
137-
{ "int", new() },
138-
{ "int4", new() },
139-
{ "serial", new() }
140-
},
141-
readerFn: ordinal => $"reader.GetInt32({ordinal})",
142-
readerArrayFn: ordinal => $"reader.GetFieldValue<int[]>({ordinal})",
143-
convertFunc: x => $"Convert.ToInt32({x})"
144-
),
145-
["float"] = new(
146-
new()
147-
{
148-
{ "float4", new() }
149-
},
150-
readerFn: ordinal => $"reader.GetFloat({ordinal})",
151-
readerArrayFn: ordinal => $"reader.GetFieldValue<float[]>({ordinal})"
152-
),
153-
["decimal"] = new(
154-
new()
155-
{
156-
{ "numeric", new() },
157-
{ "decimal", new() },
158-
{ "money", new(NpgsqlTypeOverride: "NpgsqlDbType.Money") }
159-
},
160-
readerFn: ordinal => $"reader.GetDecimal({ordinal})",
161-
readerArrayFn: ordinal => $"reader.GetFieldValue<decimal[]>({ordinal})"
162-
),
163-
["double"] = new(
164-
new()
165-
{
166-
{ "float8", new() }
167-
},
168-
readerFn: ordinal => $"reader.GetDouble({ordinal})",
169-
readerArrayFn: ordinal => $"reader.GetFieldValue<double[]>({ordinal})"
170-
),
171-
["bool"] = new(
172-
new()
173-
{
174-
{ "bool", new() },
175-
{ "boolean", new() }
176-
},
177-
readerFn: ordinal => $"reader.GetBoolean({ordinal})",
178-
readerArrayFn: ordinal => $"reader.GetFieldValue<bool[]>({ordinal})"
179-
),
180163
["XmlDocument"] = new(
181164
new()
182165
{
@@ -201,6 +184,8 @@ public NpgsqlDriver(
201184
sqlMapper: "SqlMapper.AddTypeHandler(typeof(XmlDocument), new XmlDocumentTypeHandler());",
202185
sqlMapperImpl: XmlDocumentTypeHandler
203186
),
187+
188+
/* Geometric data types */
204189
["NpgsqlPoint"] = new(
205190
new()
206191
{
@@ -271,6 +256,8 @@ public NpgsqlDriver(
271256
usingDirective: "NpgsqlTypes",
272257
sqlMapper: "RegisterNpgsqlTypeHandler<NpgsqlCircle>();"
273258
),
259+
260+
/* Network data types */
274261
["NpgsqlCidr"] = new(
275262
new()
276263
{
@@ -301,6 +288,32 @@ public NpgsqlDriver(
301288
usingDirective: "System.Net.NetworkInformation",
302289
sqlMapper: "RegisterNpgsqlTypeHandler<PhysicalAddress>();"
303290
),
291+
292+
/* Other data types */
293+
["Guid"] = new(
294+
new()
295+
{
296+
{ "uuid", new() }
297+
},
298+
readerFn: ordinal => $"reader.GetFieldValue<Guid>({ordinal})",
299+
readerArrayFn: ordinal => $"reader.GetFieldValue<Guid[]>({ordinal})",
300+
convertFunc: x => $"Guid.Parse({x}?.ToString())"
301+
),
302+
["byte[]"] = new(
303+
new()
304+
{
305+
{ "binary", new() },
306+
{ "bit", new() },
307+
{ "bytea", new() },
308+
{ "blob", new() },
309+
{ "longblob", new() },
310+
{ "mediumblob", new() },
311+
{ "tinyblob", new() },
312+
{ "varbinary", new() }
313+
},
314+
readerFn: ordinal => $"reader.GetFieldValue<byte[]>({ordinal})",
315+
readerArrayFn: ordinal => $"reader.GetFieldValue<byte[][]>({ordinal})"
316+
),
304317
["object"] = new(
305318
new()
306319
{

0 commit comments

Comments
 (0)