mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-17 21:25:39 -04:00
added ConverterContext to marr Converters.
This commit is contained in:
@@ -20,14 +20,14 @@ namespace Marr.Data.Converters
|
||||
{
|
||||
public class BooleanIntConverter : IConverter
|
||||
{
|
||||
public object FromDB(ColumnMap map, object dbValue)
|
||||
public object FromDB(ConverterContext context)
|
||||
{
|
||||
if (dbValue == DBNull.Value)
|
||||
if (context.DbValue == DBNull.Value)
|
||||
{
|
||||
return DBNull.Value;
|
||||
}
|
||||
|
||||
int val = (int)dbValue;
|
||||
int val = (int)context.DbValue;
|
||||
|
||||
if (val == 1)
|
||||
{
|
||||
@@ -40,7 +40,12 @@ namespace Marr.Data.Converters
|
||||
throw new ConversionException(
|
||||
string.Format(
|
||||
"The BooleanCharConverter could not convert the value '{0}' to a boolean.",
|
||||
dbValue));
|
||||
context.DbValue));
|
||||
}
|
||||
|
||||
public object FromDB(ColumnMap map, object dbValue)
|
||||
{
|
||||
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
|
||||
}
|
||||
|
||||
public object ToDB(object clrValue)
|
||||
|
||||
@@ -20,14 +20,14 @@ namespace Marr.Data.Converters
|
||||
{
|
||||
public class BooleanYNConverter : IConverter
|
||||
{
|
||||
public object FromDB(ColumnMap map, object dbValue)
|
||||
public object FromDB(ConverterContext context)
|
||||
{
|
||||
if (dbValue == DBNull.Value)
|
||||
if (context.DbValue == DBNull.Value)
|
||||
{
|
||||
return DBNull.Value;
|
||||
}
|
||||
|
||||
string val = dbValue.ToString();
|
||||
string val = context.DbValue.ToString();
|
||||
|
||||
if (val == "Y")
|
||||
{
|
||||
@@ -40,7 +40,12 @@ namespace Marr.Data.Converters
|
||||
throw new ConversionException(
|
||||
string.Format(
|
||||
"The BooleanYNConverter could not convert the value '{0}' to a boolean.",
|
||||
dbValue));
|
||||
context.DbValue));
|
||||
}
|
||||
|
||||
public object FromDB(ColumnMap map, object dbValue)
|
||||
{
|
||||
return FromDB(new ConverterContext {ColumnMap = map, DbValue = dbValue});
|
||||
}
|
||||
|
||||
public object ToDB(object clrValue)
|
||||
|
||||
@@ -30,10 +30,15 @@ namespace Marr.Data.Converters
|
||||
get { return typeof(TDb); }
|
||||
}
|
||||
|
||||
public object FromDB(ConverterContext context)
|
||||
{
|
||||
TDb val = (TDb)context.DbValue;
|
||||
return val.ToType(typeof(TClr), CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public object FromDB(ColumnMap map, object dbValue)
|
||||
{
|
||||
TDb val = (TDb)dbValue;
|
||||
return val.ToType(typeof(TClr), CultureInfo.InvariantCulture);
|
||||
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
|
||||
}
|
||||
|
||||
public object ToDB(object clrValue)
|
||||
|
||||
13
Marr.Data/Converters/ConverterContext.cs
Normal file
13
Marr.Data/Converters/ConverterContext.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Data;
|
||||
using Marr.Data.Mapping;
|
||||
|
||||
namespace Marr.Data.Converters
|
||||
{
|
||||
public class ConverterContext
|
||||
{
|
||||
public ColumnMap ColumnMap { get; set; }
|
||||
public object DbValue { get; set; }
|
||||
public ColumnMapCollection MapCollection { get; set; }
|
||||
public IDataRecord DataRecord { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -20,11 +20,16 @@ namespace Marr.Data.Converters
|
||||
{
|
||||
public class EnumIntConverter : IConverter
|
||||
{
|
||||
public object FromDB(ConverterContext context)
|
||||
{
|
||||
if (context.DbValue == null || context.DbValue == DBNull.Value)
|
||||
return null;
|
||||
return Enum.ToObject(context.ColumnMap.FieldType, (int)context.DbValue);
|
||||
}
|
||||
|
||||
public object FromDB(ColumnMap map, object dbValue)
|
||||
{
|
||||
if (dbValue == null || dbValue == DBNull.Value)
|
||||
return null;
|
||||
return Enum.ToObject(map.FieldType, (int)dbValue);
|
||||
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
|
||||
}
|
||||
|
||||
public object ToDB(object clrValue)
|
||||
|
||||
@@ -20,11 +20,16 @@ namespace Marr.Data.Converters
|
||||
{
|
||||
public class EnumStringConverter : IConverter
|
||||
{
|
||||
public object FromDB(ConverterContext context)
|
||||
{
|
||||
if (context.DbValue == null || context.DbValue == DBNull.Value)
|
||||
return null;
|
||||
return Enum.Parse(context.ColumnMap.FieldType, (string)context.DbValue);
|
||||
}
|
||||
|
||||
public object FromDB(ColumnMap map, object dbValue)
|
||||
{
|
||||
if (dbValue == null || dbValue == DBNull.Value)
|
||||
return null;
|
||||
return Enum.Parse(map.FieldType, (string)dbValue);
|
||||
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
|
||||
}
|
||||
|
||||
public object ToDB(object clrValue)
|
||||
|
||||
@@ -20,6 +20,9 @@ namespace Marr.Data.Converters
|
||||
{
|
||||
public interface IConverter
|
||||
{
|
||||
object FromDB(ConverterContext context);
|
||||
|
||||
[Obsolete("use FromDB(ConverterContext context) instead")]
|
||||
object FromDB(ColumnMap map, object dbValue);
|
||||
object ToDB(object clrValue);
|
||||
Type DbType { get; }
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Data.Common;
|
||||
using Marr.Data.Converters;
|
||||
|
||||
namespace Marr.Data.Mapping
|
||||
{
|
||||
@@ -53,7 +54,15 @@ namespace Marr.Data.Mapping
|
||||
// Handle conversions
|
||||
if (dataMap.Converter != null)
|
||||
{
|
||||
dbValue = dataMap.Converter.FromDB(dataMap, dbValue);
|
||||
var convertContext = new ConverterContext
|
||||
{
|
||||
DbValue = dbValue,
|
||||
ColumnMap = dataMap,
|
||||
MapCollection = mappings,
|
||||
DataRecord = reader
|
||||
};
|
||||
|
||||
dbValue = dataMap.Converter.FromDB(convertContext);
|
||||
}
|
||||
|
||||
if (dbValue != DBNull.Value && dbValue != null)
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
<Compile Include="Converters\BooleanYNConverter.cs" />
|
||||
<Compile Include="Converters\CastConverter.cs" />
|
||||
<Compile Include="Converters\ConversionException.cs" />
|
||||
<Compile Include="Converters\ConverterContext.cs" />
|
||||
<Compile Include="Converters\EnumIntConverter.cs" />
|
||||
<Compile Include="Converters\EnumStringConverter.cs" />
|
||||
<Compile Include="Converters\IConverter.cs" />
|
||||
|
||||
Reference in New Issue
Block a user