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

revert #2050 - see #2049 for more details #2070

Merged
merged 2 commits into from
Apr 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ public TypeMapEntry(DbType dbType, TypeMapEntryFlags flags)
public static readonly TypeMapEntry
DoNotSet = new((DbType)(-2), TypeMapEntryFlags.None),
DoNotSetFieldValue = new((DbType)(-2), 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);
DecimalFieldValue = new(DbType.Decimal, TypeMapEntryFlags.SetType | TypeMapEntryFlags.UseGetFieldValue);

public static implicit operator TypeMapEntry(DbType dbType)
=> new(dbType, TypeMapEntryFlags.SetType);
Expand All @@ -221,13 +219,13 @@ static SqlMapper()
[typeof(double)] = DbType.Double,
[typeof(decimal)] = DbType.Decimal,
[typeof(bool)] = DbType.Boolean,
[typeof(string)] = TypeMapEntry.StringFieldValue,
[typeof(string)] = DbType.String,
[typeof(char)] = DbType.StringFixedLength,
[typeof(Guid)] = DbType.Guid,
[typeof(DateTime)] = TypeMapEntry.DoNotSet,
[typeof(DateTimeOffset)] = DbType.DateTimeOffset,
[typeof(TimeSpan)] = TypeMapEntry.DoNotSet,
[typeof(byte[])] = TypeMapEntry.BinaryFieldValue,
[typeof(byte[])] = DbType.Binary,
[typeof(byte?)] = DbType.Byte,
[typeof(sbyte?)] = DbType.SByte,
[typeof(short?)] = DbType.Int16,
Expand Down Expand Up @@ -3928,7 +3926,12 @@ public static void ThrowDataException(Exception ex, int index, IDataReader reade
}
else
{
formattedValue = Convert.ToString(value) + " - " + Type.GetTypeCode(value.GetType());
formattedValue = Convert.ToString(value) + " - " + Identify(value.GetType());
}
static string Identify(Type type)
{
var tc = Type.GetTypeCode(type);
return tc == TypeCode.Object ? type.Name : tc.ToString();
}
}
catch (Exception valEx)
Expand Down
35 changes: 23 additions & 12 deletions tests/Dapper.Tests/TypeHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ public void TestChangingDefaultStringTypeMappingToAnsiString()

SqlMapper.PurgeQueryCache();

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.AddTypeMap(typeof(string), DbType.AnsiString, false); // Change Default String Handling to AnsiString
try
{
var result02 = connection.Query<string>(sql, param).FirstOrDefault();
Assert.Equal("varchar", result02);

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

[Fact]
Expand All @@ -46,13 +52,18 @@ public void TestChangingDefaultStringTypeMappingToAnsiStringFirstOrDefault()
Assert.Equal("nvarchar", result01);

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

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, true); // Restore Default to Unicode String
SqlMapper.PurgeQueryCache();
}
finally
{
SqlMapper.AddTypeMap(typeof(string), DbType.String, false); // Restore Default to Unicode String
}
}

[Fact]
Expand Down Expand Up @@ -643,7 +654,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=n/a - Unable to cast object of type 'System.Guid' to type 'System.String'.)", ex.Message);
Assert.Equal("Error parsing column 0 (Id=cf0ef7ac-b6fe-4e24-aeda-a2b45bb5654e - Guid)", ex.Message);
}

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