Skip to content

Commit a97b56c

Browse files
fix: refactor id column type logic
1 parent c64b31d commit a97b56c

5 files changed

Lines changed: 20 additions & 15 deletions

File tree

Drivers/ColumnMapping.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class ColumnMapping(
1111
Func<int, string>? readerArrayFn = null,
1212
string? usingDirective = null,
1313
Func<string, bool, bool, string>? writerFn = null,
14+
string? convertFunc = null,
1415
string? sqlMapper = null,
1516
string? sqlMapperImpl = null)
1617
{
@@ -19,6 +20,7 @@ public class ColumnMapping(
1920
public Func<int, string>? ReaderArrayFn { get; } = readerArrayFn;
2021
public string? UsingDirective { get; } = usingDirective;
2122
public Func<string, bool, bool, string>? WriterFn { get; } = writerFn;
23+
public string? ConvertFunc { get; } = convertFunc;
2224
public string? SqlMapper { get; } = sqlMapper;
2325
public string? SqlMapperImpl { get; } = sqlMapperImpl;
2426
}

Drivers/DbDriver.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public abstract class DbDriver
5151
"JsonElement"
5252
];
5353

54+
protected const string IntTo32 = "Convert.ToInt32";
55+
protected const string IntTo64 = "Convert.ToInt64";
56+
57+
5458
public abstract Dictionary<string, ColumnMapping> ColumnMappings { get; }
5559

5660
protected const string JsonElementTypeHandler =
@@ -330,11 +334,12 @@ public string GetIdColumnType(Query query)
330334

331335
public virtual string[] GetLastIdStatement(Query query)
332336
{
333-
var convertFunc = GetIdColumnType(query) == "int" ? "ToInt32" : "ToInt64"; // TODO refactor
337+
var idColumnType = GetIdColumnType(query);
338+
var convertFunc = ColumnMappings[idColumnType].ConvertFunc ?? throw new InvalidOperationException($"ConvertFunc is missing for id column type {idColumnType}");
334339
return
335340
[
336341
$"var {Variable.Result.AsVarName()} = await {Variable.Command.AsVarName()}.ExecuteScalarAsync();",
337-
$"return Convert.{convertFunc}({Variable.Result.AsVarName()});"
342+
$"return {convertFunc}({Variable.Result.AsVarName()});"
338343
];
339344
}
340345

Drivers/MySqlConnectorDriver.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public partial class MySqlConnectorDriver(
4747
{
4848
{ "bigint", new() }
4949
},
50-
ordinal => $"reader.GetInt64({ordinal})"
50+
ordinal => $"reader.GetInt64({ordinal})",
51+
convertFunc: IntTo64
5152
),
5253
["byte"] = new ColumnMapping(
5354
new()
@@ -97,7 +98,8 @@ public partial class MySqlConnectorDriver(
9798
{ "int", new() },
9899
{ "mediumint", new() }
99100
},
100-
ordinal => $"reader.GetInt32({ordinal})"
101+
ordinal => $"reader.GetInt32({ordinal})",
102+
convertFunc: IntTo32
101103
),
102104
["double"] = new(
103105
new()

Drivers/NpgsqlDriver.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public NpgsqlDriver(
3939
{ "bigint", new() },
4040
{ "bigserial", new() }
4141
},
42-
ordinal => $"reader.GetInt64({ordinal})"
42+
ordinal => $"reader.GetInt64({ordinal})",
43+
convertFunc: IntTo64
4344
),
4445
["byte[]"] = new(
4546
new()
@@ -118,7 +119,8 @@ public NpgsqlDriver(
118119
{ "serial", new(NpgsqlTypeOverride: "NpgsqlDbType.Integer") }
119120
},
120121
ordinal => $"reader.GetInt32({ordinal})",
121-
ordinal => $"reader.GetFieldValue<int[]>({ordinal})"
122+
ordinal => $"reader.GetFieldValue<int[]>({ordinal})",
123+
convertFunc: IntTo32
122124
),
123125
["float"] = new(
124126
new()

Drivers/SqliteDriver.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public partial class SqliteDriver(
3939
{ "integer", new() },
4040
{ "integernotnulldefaultunixepoch", new() }
4141
},
42-
ordinal => $"reader.GetInt32({ordinal})"
42+
ordinal => $"reader.GetInt32({ordinal})",
43+
convertFunc: IntTo32
4344
),
4445
["decimal"] = new(
4546
new()
@@ -54,14 +55,7 @@ public partial class SqliteDriver(
5455
{ "any", new() }
5556
},
5657
ordinal => $"reader.GetValue({ordinal})"
57-
),
58-
["long"] = new(
59-
new()
60-
{
61-
{ "bigint", new() }
62-
},
63-
ordinal => $"reader.GetInt64({ordinal})"
64-
),
58+
)
6559
};
6660

6761
public override string TransactionClassName => "SqliteTransaction";

0 commit comments

Comments
 (0)