2013-03-25 09:13:53 +03:00
|
|
|
using System;
|
|
|
|
using Marr.Data.Converters;
|
|
|
|
using Marr.Data.Mapping;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore.Converters
|
|
|
|
{
|
|
|
|
public class BooleanIntConverter : IConverter
|
|
|
|
{
|
2013-09-21 09:38:27 +03:00
|
|
|
public object FromDB(ConverterContext context)
|
2013-03-25 09:13:53 +03:00
|
|
|
{
|
2013-09-21 09:38:27 +03:00
|
|
|
if (context.DbValue == DBNull.Value)
|
2013-03-25 09:13:53 +03:00
|
|
|
{
|
|
|
|
return DBNull.Value;
|
|
|
|
}
|
|
|
|
|
2013-09-21 09:38:27 +03:00
|
|
|
var val = (Int64)context.DbValue;
|
2013-03-25 09:13:53 +03:00
|
|
|
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
return true;
|
|
|
|
case 0:
|
|
|
|
return false;
|
|
|
|
default:
|
2013-09-21 09:38:27 +03:00
|
|
|
throw new ConversionException(string.Format("The BooleanCharConverter could not convert the value '{0}' to a Boolean.", context.DbValue));
|
2013-03-25 09:13:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-21 09:38:27 +03:00
|
|
|
public object FromDB(ColumnMap map, object dbValue)
|
|
|
|
{
|
|
|
|
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
|
|
|
|
}
|
|
|
|
|
2013-03-25 09:13:53 +03:00
|
|
|
public object ToDB(object clrValue)
|
|
|
|
{
|
|
|
|
var val = (Nullable<bool>)clrValue;
|
|
|
|
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case true:
|
|
|
|
return 1;
|
|
|
|
case false:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return DBNull.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Type DbType
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return typeof(int);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|