|
1 | 1 | using System.Data.Common; |
2 | 2 | using ImTools; |
3 | 3 | using JasperFx.Core; |
| 4 | +using JasperFx.Core.Reflection; |
4 | 5 | using Weasel.Core.Names; |
5 | 6 |
|
6 | 7 | namespace Weasel.Core; |
@@ -170,6 +171,53 @@ public void RegisterMapping(Type type, string databaseType, TParameterType? para |
170 | 171 | ParameterTypeMemo.Swap(d => d.AddOrUpdate(type, parameterType)); |
171 | 172 | } |
172 | 173 |
|
| 174 | + /// <summary> |
| 175 | + /// Shared memo lookup for the database-type string of a CLR type, with the |
| 176 | + /// standard nullable-promote fallback (if <c>T?</c> is asked but <c>T</c> is |
| 177 | + /// in the memo, copy the entry to <c>T?</c> and return it). Returns null |
| 178 | + /// when neither the type nor its inner-nullable is mapped — subclasses then |
| 179 | + /// apply provider-specific fallbacks (PG queries Npgsql's plugin type map, |
| 180 | + /// SS/Oracle/MySQL throw, SQLite returns null to signal "TEXT/JSON"). |
| 181 | + /// </summary> |
| 182 | + protected string? ResolveDatabaseTypeFromMemo(Type type) |
| 183 | + { |
| 184 | + if (DatabaseTypeMemo.Value.TryFind(type, out var value)) |
| 185 | + { |
| 186 | + return value; |
| 187 | + } |
| 188 | + |
| 189 | + if (type.IsNullable() && DatabaseTypeMemo.Value.TryFind(type.GetInnerTypeFromNullable(), out var inner)) |
| 190 | + { |
| 191 | + DatabaseTypeMemo.Swap(d => d.AddOrUpdate(type, inner)); |
| 192 | + return inner; |
| 193 | + } |
| 194 | + |
| 195 | + return null; |
| 196 | + } |
| 197 | + |
| 198 | + /// <summary> |
| 199 | + /// Shared memo lookup for the provider parameter-type enum of a CLR type, |
| 200 | + /// mirror of <see cref="ResolveDatabaseTypeFromMemo" />. Subclasses apply |
| 201 | + /// their own fallback when this returns null (PG queries Npgsql plugin; |
| 202 | + /// SS returns <c>Variant</c>; Oracle returns <c>Varchar2</c>; MySQL returns |
| 203 | + /// <c>VarChar</c>; SQLite returns <c>Text</c>). |
| 204 | + /// </summary> |
| 205 | + protected TParameterType? ResolveParameterTypeFromMemo(Type type) |
| 206 | + { |
| 207 | + if (ParameterTypeMemo.Value.TryFind(type, out var value)) |
| 208 | + { |
| 209 | + return value; |
| 210 | + } |
| 211 | + |
| 212 | + if (type.IsNullable() && ParameterTypeMemo.Value.TryFind(type.GetInnerTypeFromNullable(), out var inner)) |
| 213 | + { |
| 214 | + ParameterTypeMemo.Swap(d => d.AddOrUpdate(type, inner)); |
| 215 | + return inner; |
| 216 | + } |
| 217 | + |
| 218 | + return null; |
| 219 | + } |
| 220 | + |
173 | 221 | protected abstract Type[] determineClrTypesForParameterType(TParameterType dbType); |
174 | 222 |
|
175 | 223 | public TParameter AddParameter(TCommand command, object? value, TParameterType? dbType = null) |
|
0 commit comments