Skip to content

Commit

Permalink
UseJsonSerializerOptions for PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Nov 13, 2024
1 parent 0221d3e commit 0c55f74
Show file tree
Hide file tree
Showing 4 changed files with 1,858 additions and 770 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res
}
}

return (T)(object)new EFTableRowsDataReader(tableRows);
return (T)(object)new EFTableRowsDataReader(tableRows
#if NET9_0 || NET8_0 || NET7_0 || NET6_0 || NET5_0
, _cacheSettings
#endif
);
}

if (result is object)
Expand Down Expand Up @@ -258,7 +262,11 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultSuppressed, message, commandText);
}

using var rows = new EFTableRowsDataReader(new EFTableRows());
using var rows = new EFTableRowsDataReader(new EFTableRows()
#if NET9_0 || NET8_0 || NET7_0 || NET6_0 || NET5_0
, _cacheSettings
#endif
);

return (T)Convert.ChangeType(InterceptionResult<DbDataReader>.SuppressWithResult(rows), typeof(T),
CultureInfo.InvariantCulture);
Expand All @@ -273,7 +281,11 @@ public T ProcessExecutingCommands<T>(DbCommand command, DbContext? context, T re
_logger.NotifyCacheableEvent(CacheableLogEventId.QueryResultSuppressed, message, commandText);
}

using var dataRows = new EFTableRowsDataReader(cacheResult.TableRows);
using var dataRows = new EFTableRowsDataReader(cacheResult.TableRows
#if NET9_0 || NET8_0 || NET7_0 || NET6_0 || NET5_0
, _cacheSettings
#endif
);

return (T)Convert.ChangeType(InterceptionResult<DbDataReader>.SuppressWithResult(dataRows), typeof(T),
CultureInfo.InvariantCulture);
Expand Down
23 changes: 19 additions & 4 deletions src/EFCoreSecondLevelCacheInterceptor/EFTableRowsDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Data.Common;
using System.Globalization;
using System.Linq;
#if NET9_0 || NET8_0 || NET7_0 || NET6_0 || NET5_0 || NETCORE3_1
#if NET9_0 || NET8_0 || NET7_0 || NET6_0 || NET5_0
using System.Text.Json;
#endif

Expand All @@ -17,6 +17,11 @@ namespace EFCoreSecondLevelCacheInterceptor;
public class EFTableRowsDataReader : DbDataReader
{
private readonly int _rowsCount;

#if NET9_0 || NET8_0 || NET7_0 || NET6_0 || NET5_0
private readonly EFCoreSecondLevelCacheSettings _settings;
#endif

private readonly EFTableRows _tableRows;
private readonly Dictionary<int, Type> _valueTypes;
private int _currentRow;
Expand All @@ -26,11 +31,19 @@ public class EFTableRowsDataReader : DbDataReader
/// <summary>
/// Converts an EFTableRows to a DbDataReader.
/// </summary>
public EFTableRowsDataReader(EFTableRows tableRows)
public EFTableRowsDataReader(EFTableRows tableRows
#if NET9_0 || NET8_0 || NET7_0 || NET6_0 || NET5_0
,
EFCoreSecondLevelCacheSettings settings
#endif
)
{
_tableRows = tableRows;
_rowsCount = _tableRows.RowsCount;
_valueTypes = new Dictionary<int, Type>(_tableRows.FieldCount);
#if NET9_0 || NET8_0 || NET7_0 || NET6_0 || NET5_0
_settings = settings;
#endif
}

/// <summary>
Expand Down Expand Up @@ -547,13 +560,15 @@ public override T GetFieldValue<T>(int ordinal)
return ProcessPostgresArrayOrList<T>(expectedValueType, enumerable);
}

#if NET9_0 || NET8_0 || NET7_0 || NET6_0 || NET5_0 || NETCORE3_1
#if NET9_0 || NET8_0 || NET7_0 || NET6_0 || NET5_0
var dbTypeName = GetDataTypeName(ordinal);

if (actualValueType == TypeExtensions.StringType &&
string.Equals(dbTypeName, b: "jsonb", StringComparison.OrdinalIgnoreCase))
{
return JsonSerializer.Deserialize<T>((string)value)!;
return _settings.JsonSerializerOptions is null
? JsonSerializer.Deserialize<T>((string)value)!
: JsonSerializer.Deserialize<T>((string)value, _settings.JsonSerializerOptions)!;
}
#endif

Expand Down
Loading

0 comments on commit 0c55f74

Please sign in to comment.