Skip to content

Commit

Permalink
revert #2050 - see #2049 for more details (#2070)
Browse files Browse the repository at this point in the history
* revert #2050 - see #2049 for more details
  • Loading branch information
mgravell authored Apr 12, 2024
1 parent ad16263 commit fa9c96a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
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

0 comments on commit fa9c96a

Please sign in to comment.