Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string and byte[] : add UseGetFieldValue #2050

Merged
merged 6 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Dapper.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
global.json = global.json
docs\index.md = docs\index.md
License.txt = License.txt
.github\workflows\main.yml = .github\workflows\main.yml
nuget.config = nuget.config
Readme.md = Readme.md
version.json = version.json
Expand Down
14 changes: 10 additions & 4 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ public TypeMapEntry(DbType dbType, TypeMapEntryFlags flags)
public bool Equals(TypeMapEntry other) => other.DbType == DbType && other.Flags == Flags;
public static readonly TypeMapEntry
DoNotSet = new((DbType)(-2), TypeMapEntryFlags.None),
DecimalFieldValue = new(DbType.Decimal, TypeMapEntryFlags.SetType | TypeMapEntryFlags.UseGetFieldValue);
DecimalFieldValue = new(DbType.Decimal, TypeMapEntryFlags.SetType | TypeMapEntryFlags.UseGetFieldValue),
StringFieldValue = new(DbType.String, TypeMapEntryFlags.SetType | TypeMapEntryFlags.UseGetFieldValue),
BinaryFieldValue = new(DbType.Binary, TypeMapEntryFlags.SetType | TypeMapEntryFlags.UseGetFieldValue);

public static implicit operator TypeMapEntry(DbType dbType)
=> new(dbType, TypeMapEntryFlags.SetType);
Expand All @@ -214,13 +216,13 @@ static SqlMapper()
[typeof(double)] = DbType.Double,
[typeof(decimal)] = DbType.Decimal,
[typeof(bool)] = DbType.Boolean,
[typeof(string)] = DbType.String,
[typeof(string)] = TypeMapEntry.StringFieldValue,
[typeof(char)] = DbType.StringFixedLength,
[typeof(Guid)] = DbType.Guid,
[typeof(DateTime)] = TypeMapEntry.DoNotSet,
[typeof(DateTimeOffset)] = DbType.DateTimeOffset,
[typeof(TimeSpan)] = TypeMapEntry.DoNotSet,
[typeof(byte[])] = DbType.Binary,
[typeof(byte[])] = TypeMapEntry.BinaryFieldValue,
[typeof(byte?)] = DbType.Byte,
[typeof(sbyte?)] = DbType.SByte,
[typeof(short?)] = DbType.Int16,
Expand Down Expand Up @@ -3905,7 +3907,11 @@ public static void ThrowDataException(Exception ex, int index, IDataReader reade
}
try
{
if (value is null || value is DBNull)
if (value is null && ex is InvalidCastException)
{
formattedValue = "n/a - " + ex.Message; // provide some context
}
else if (value is null || value is DBNull)
{
formattedValue = "<null>";
}
Expand Down
3 changes: 2 additions & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"sdk": {
"version": "8.0.100"
"version": "8.0.100",
"rollForward": "latestMajor"
}
}
2 changes: 1 addition & 1 deletion tests/Dapper.Tests/MiscTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static async Task TestExceptionsAsync<T>(DbConnection connection, string sql, st
await TestExceptionsAsync<int>(
connection,
"Select null as Foo",
"Error parsing column 0 (Foo=<null>)");
"Error parsing column 0 (Foo=n/a - Null object cannot be converted to a value type.)");
// Incompatible value throws (testing unnamed column bits here too)
await TestExceptionsAsync<int>(
connection,
Expand Down
10 changes: 5 additions & 5 deletions tests/Dapper.Tests/TypeHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public void TestChangingDefaultStringTypeMappingToAnsiString()

SqlMapper.PurgeQueryCache();

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

SqlMapper.PurgeQueryCache();
SqlMapper.AddTypeMap(typeof(string), DbType.String); // Restore Default to Unicode String
SqlMapper.AddTypeMap(typeof(string), DbType.String, true); // Restore Default to Unicode String
}

[Fact]
Expand All @@ -47,12 +47,12 @@ public void TestChangingDefaultStringTypeMappingToAnsiStringFirstOrDefault()

SqlMapper.PurgeQueryCache();

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

SqlMapper.PurgeQueryCache();
SqlMapper.AddTypeMap(typeof(string), DbType.String); // Restore Default to Unicode String
SqlMapper.AddTypeMap(typeof(string), DbType.String, true); // Restore Default to Unicode String
}

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

public class Issue149_Person { public string? Id { get; set; } }
Expand Down