Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 87eb033

Browse files
authoredMar 6, 2024
string and byte[] : add UseGetFieldValue (#2050)
* string and byte[] : add UseGetFieldValue to prevent problems with npgsql using global deserializer * switch to GetFieldValue<T> changes some messaging in invalid cast scenarios; if anything, new version is clearer, so: fine * test fixes
1 parent 360367c commit 87eb033

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed
 

‎Dapper.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1515
global.json = global.json
1616
docs\index.md = docs\index.md
1717
License.txt = License.txt
18+
.github\workflows\main.yml = .github\workflows\main.yml
1819
nuget.config = nuget.config
1920
Readme.md = Readme.md
2021
version.json = version.json

‎Dapper/SqlMapper.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ public TypeMapEntry(DbType dbType, TypeMapEntryFlags flags)
192192
public bool Equals(TypeMapEntry other) => other.DbType == DbType && other.Flags == Flags;
193193
public static readonly TypeMapEntry
194194
DoNotSet = new((DbType)(-2), TypeMapEntryFlags.None),
195-
DecimalFieldValue = new(DbType.Decimal, TypeMapEntryFlags.SetType | TypeMapEntryFlags.UseGetFieldValue);
195+
DecimalFieldValue = new(DbType.Decimal, TypeMapEntryFlags.SetType | TypeMapEntryFlags.UseGetFieldValue),
196+
StringFieldValue = new(DbType.String, TypeMapEntryFlags.SetType | TypeMapEntryFlags.UseGetFieldValue),
197+
BinaryFieldValue = new(DbType.Binary, TypeMapEntryFlags.SetType | TypeMapEntryFlags.UseGetFieldValue);
196198

197199
public static implicit operator TypeMapEntry(DbType dbType)
198200
=> new(dbType, TypeMapEntryFlags.SetType);
@@ -214,13 +216,13 @@ static SqlMapper()
214216
[typeof(double)] = DbType.Double,
215217
[typeof(decimal)] = DbType.Decimal,
216218
[typeof(bool)] = DbType.Boolean,
217-
[typeof(string)] = DbType.String,
219+
[typeof(string)] = TypeMapEntry.StringFieldValue,
218220
[typeof(char)] = DbType.StringFixedLength,
219221
[typeof(Guid)] = DbType.Guid,
220222
[typeof(DateTime)] = TypeMapEntry.DoNotSet,
221223
[typeof(DateTimeOffset)] = DbType.DateTimeOffset,
222224
[typeof(TimeSpan)] = TypeMapEntry.DoNotSet,
223-
[typeof(byte[])] = DbType.Binary,
225+
[typeof(byte[])] = TypeMapEntry.BinaryFieldValue,
224226
[typeof(byte?)] = DbType.Byte,
225227
[typeof(sbyte?)] = DbType.SByte,
226228
[typeof(short?)] = DbType.Int16,
@@ -3905,7 +3907,11 @@ public static void ThrowDataException(Exception ex, int index, IDataReader reade
39053907
}
39063908
try
39073909
{
3908-
if (value is null || value is DBNull)
3910+
if (value is null && ex is InvalidCastException)
3911+
{
3912+
formattedValue = "n/a - " + ex.Message; // provide some context
3913+
}
3914+
else if (value is null || value is DBNull)
39093915
{
39103916
formattedValue = "<null>";
39113917
}

‎global.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"sdk": {
3-
"version": "8.0.100"
3+
"version": "8.0.100",
4+
"rollForward": "latestMajor"
45
}
56
}

‎tests/Dapper.Tests/MiscTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static async Task TestExceptionsAsync<T>(DbConnection connection, string sql, st
265265
await TestExceptionsAsync<int>(
266266
connection,
267267
"Select null as Foo",
268-
"Error parsing column 0 (Foo=<null>)");
268+
"Error parsing column 0 (Foo=n/a - Null object cannot be converted to a value type.)");
269269
// Incompatible value throws (testing unnamed column bits here too)
270270
await TestExceptionsAsync<int>(
271271
connection,

‎tests/Dapper.Tests/TypeHandlerTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public void TestChangingDefaultStringTypeMappingToAnsiString()
2828

2929
SqlMapper.PurgeQueryCache();
3030

31-
SqlMapper.AddTypeMap(typeof(string), DbType.AnsiString); // Change Default String Handling to AnsiString
31+
SqlMapper.AddTypeMap(typeof(string), DbType.AnsiString, true); // Change Default String Handling to AnsiString
3232
var result02 = connection.Query<string>(sql, param).FirstOrDefault();
3333
Assert.Equal("varchar", result02);
3434

3535
SqlMapper.PurgeQueryCache();
36-
SqlMapper.AddTypeMap(typeof(string), DbType.String); // Restore Default to Unicode String
36+
SqlMapper.AddTypeMap(typeof(string), DbType.String, true); // Restore Default to Unicode String
3737
}
3838

3939
[Fact]
@@ -47,12 +47,12 @@ public void TestChangingDefaultStringTypeMappingToAnsiStringFirstOrDefault()
4747

4848
SqlMapper.PurgeQueryCache();
4949

50-
SqlMapper.AddTypeMap(typeof(string), DbType.AnsiString); // Change Default String Handling to AnsiString
50+
SqlMapper.AddTypeMap(typeof(string), DbType.AnsiString, true); // Change Default String Handling to AnsiString
5151
var result02 = connection.QueryFirstOrDefault<string>(sql, param);
5252
Assert.Equal("varchar", result02);
5353

5454
SqlMapper.PurgeQueryCache();
55-
SqlMapper.AddTypeMap(typeof(string), DbType.String); // Restore Default to Unicode String
55+
SqlMapper.AddTypeMap(typeof(string), DbType.String, true); // Restore Default to Unicode String
5656
}
5757

5858
[Fact]
@@ -643,7 +643,7 @@ public void Issue149_TypeMismatch_SequentialAccess()
643643
{
644644
Guid guid = Guid.Parse("cf0ef7ac-b6fe-4e24-aeda-a2b45bb5654e");
645645
var ex = Assert.ThrowsAny<Exception>(() => connection.Query<Issue149_Person>("select @guid as Id", new { guid }).First());
646-
Assert.Equal("Error parsing column 0 (Id=cf0ef7ac-b6fe-4e24-aeda-a2b45bb5654e - Object)", ex.Message);
646+
Assert.Equal("Error parsing column 0 (Id=n/a - Unable to cast object of type 'System.Guid' to type 'System.String'.)", ex.Message);
647647
}
648648

649649
public class Issue149_Person { public string? Id { get; set; } }

0 commit comments

Comments
 (0)
Please sign in to comment.