mirror of
https://github.com/Radarr/Radarr.git
synced 2026-04-27 22:57:09 -04:00
Swap to dapper and system.text.json for database backend
This commit is contained in:
@@ -1,59 +1,37 @@
|
||||
using System;
|
||||
using Marr.Data.Converters;
|
||||
using Marr.Data.Mapping;
|
||||
using NzbDrone.Core.Qualities;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Dapper;
|
||||
using System.Data;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Converters
|
||||
{
|
||||
public class QualityIntConverter : JsonConverter, IConverter
|
||||
public class QualityIntConverter : JsonConverter<Quality>
|
||||
{
|
||||
public object FromDB(ConverterContext context)
|
||||
public override Quality Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (context.DbValue == DBNull.Value)
|
||||
{
|
||||
return Quality.Unknown;
|
||||
}
|
||||
|
||||
var val = Convert.ToInt32(context.DbValue);
|
||||
|
||||
return (Quality)val;
|
||||
var item = reader.GetInt32();
|
||||
return (Quality)item;
|
||||
}
|
||||
|
||||
public object FromDB(ColumnMap map, object dbValue)
|
||||
public override void Write(Utf8JsonWriter writer, Quality value, JsonSerializerOptions options)
|
||||
{
|
||||
return FromDB(new ConverterContext { ColumnMap = map, DbValue = dbValue });
|
||||
}
|
||||
|
||||
public object ToDB(object clrValue)
|
||||
{
|
||||
if (clrValue == DBNull.Value) return 0;
|
||||
|
||||
if (clrValue as Quality == null)
|
||||
{
|
||||
throw new InvalidOperationException("Attempted to save a quality that isn't really a quality");
|
||||
}
|
||||
|
||||
var quality = clrValue as Quality;
|
||||
return (int)quality;
|
||||
}
|
||||
|
||||
public Type DbType => typeof(int);
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(Quality);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var item = reader.Value;
|
||||
return (Quality)Convert.ToInt32(item);
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
writer.WriteValue(ToDB(value));
|
||||
writer.WriteNumberValue((int) value);
|
||||
}
|
||||
}
|
||||
|
||||
public class DapperQualityIntConverter : SqlMapper.TypeHandler<Quality>
|
||||
{
|
||||
public override void SetValue(IDbDataParameter parameter, Quality value)
|
||||
{
|
||||
parameter.Value = value == null ? 0 : (int) value;
|
||||
}
|
||||
|
||||
public override Quality Parse(object value)
|
||||
{
|
||||
return (Quality) Convert.ToInt32(value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user