Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactor DefaultTypeHandlerFactory to remove reflection
As part of this, PostgresType can now be injected into handlers after
construction.

Closes #3815
  • Loading branch information
roji committed Jun 6, 2021
commit 84b5fd7649fc3338b9eb15189373129095163295
2 changes: 1 addition & 1 deletion src/Npgsql.GeoJSON/Internal/GeoJSONHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ sealed partial class GeoJsonHandler : NpgsqlTypeHandler<GeoJSONObject>,
int _lastSrid;

internal GeoJsonHandler(PostgresType postgresType, GeoJSONOptions options, CrsMap crsMap)
: base(postgresType)
{
PostgresType = postgresType;
_options = options;
_crsMap = crsMap;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Npgsql.LegacyPostgis/Internal/LegacyPostgisHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ partial class LegacyPostgisHandler : NpgsqlTypeHandler<PostgisGeometry>,
INpgsqlTypeHandler<PostgisPolygon>, INpgsqlTypeHandler<PostgisMultiPolygon>,
INpgsqlTypeHandler<PostgisGeometryCollection>
{
public LegacyPostgisHandler(PostgresType postgresType) : base(postgresType) {}
public LegacyPostgisHandler(PostgresType postgresType)
=> PostgresType = postgresType;

#region Read

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ partial class NetTopologySuiteHandler : NpgsqlTypeHandler<Geometry>,
readonly LengthStream _lengthStream = new();

internal NetTopologySuiteHandler(PostgresType postgresType, PostGisReader reader, PostGisWriter writer)
: base(postgresType)
{
PostgresType = postgresType;
_reader = reader;
_writer = writer;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql.NodaTime/Internal/DateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ sealed partial class DateHandler : NpgsqlSimpleTypeHandler<LocalDate>, INpgsqlSi
readonly BclDateHandler _bclHandler;

internal DateHandler(PostgresType postgresType, bool convertInfinityDateTime)
: base(postgresType)
{
PostgresType = postgresType;
_convertInfinityDateTime = convertInfinityDateTime;
_bclHandler = new BclDateHandler(postgresType, convertInfinityDateTime);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Npgsql.NodaTime/Internal/IntervalHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ sealed partial class IntervalHandler :
{
readonly BclIntervalHandler _bclHandler;

internal IntervalHandler(PostgresType postgresType) : base(postgresType)
=> _bclHandler = new BclIntervalHandler(postgresType);
internal IntervalHandler(PostgresType postgresType)
{
PostgresType = postgresType;
_bclHandler = new BclIntervalHandler(postgresType);
}

public override Period Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
{
Expand Down
7 changes: 5 additions & 2 deletions src/Npgsql.NodaTime/Internal/TimeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ sealed partial class TimeHandler : NpgsqlSimpleTypeHandler<LocalTime>, INpgsqlSi
{
readonly BclTimeHandler _bclHandler;

internal TimeHandler(PostgresType postgresType) : base(postgresType)
=> _bclHandler = new BclTimeHandler(postgresType);
internal TimeHandler(PostgresType postgresType)
{
PostgresType = postgresType;
_bclHandler = new BclTimeHandler(postgresType);
}

// PostgreSQL time resolution == 1 microsecond == 10 ticks
public override LocalTime Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
Expand Down
7 changes: 5 additions & 2 deletions src/Npgsql.NodaTime/Internal/TimeTzHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ sealed partial class TimeTzHandler : NpgsqlSimpleTypeHandler<OffsetTime>, INpgsq
{
readonly BclTimeTzHandler _bclHandler;

internal TimeTzHandler(PostgresType postgresType) : base(postgresType)
=> _bclHandler = new BclTimeTzHandler(postgresType);
internal TimeTzHandler(PostgresType postgresType)
{
PostgresType = postgresType;
_bclHandler = new BclTimeTzHandler(postgresType);
}

// Adjust from 1 microsecond to 100ns. Time zone (in seconds) is inverted.
public override OffsetTime Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql.NodaTime/Internal/TimestampHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ sealed partial class TimestampHandler : NpgsqlSimpleTypeHandler<Instant>, INpgsq
readonly BclTimestampHandler _bclHandler;

internal TimestampHandler(PostgresType postgresType, bool convertInfinityDateTime)
: base(postgresType)
{
PostgresType = postgresType;
_convertInfinityDateTime = convertInfinityDateTime;
_bclHandler = new BclTimestampHandler(postgresType, convertInfinityDateTime);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql.NodaTime/Internal/TimestampTzHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ sealed partial class TimestampTzHandler : NpgsqlSimpleTypeHandler<Instant>, INpg
readonly bool _convertInfinityDateTime;

public TimestampTzHandler(PostgresType postgresType, bool convertInfinityDateTime)
: base(postgresType)
{
PostgresType = postgresType;
_dateTimeZoneProvider = DateTimeZoneProviders.Tzdb;
_convertInfinityDateTime = convertInfinityDateTime;
_bclHandler = new BclTimestampTzHandler(postgresType, convertInfinityDateTime);
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql/Internal/TypeHandlers/ArrayHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public abstract class ArrayHandler : NpgsqlTypeHandler

/// <inheritdoc />
protected ArrayHandler(PostgresType arrayPostgresType, NpgsqlTypeHandler elementHandler, ArrayNullabilityMode arrayNullabilityMode, int lowerBound = 1)
: base(arrayPostgresType)
{
PostgresType = arrayPostgresType;
LowerBound = lowerBound;
ElementHandler = elementHandler;
ArrayNullabilityMode = arrayNullabilityMode;
Expand Down
5 changes: 0 additions & 5 deletions src/Npgsql/Internal/TypeHandlers/BitStringHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;
using Npgsql.TypeMapping;
using NpgsqlTypes;

namespace Npgsql.Internal.TypeHandlers
{
Expand All @@ -29,9 +27,6 @@ namespace Npgsql.Internal.TypeHandlers
public partial class BitStringHandler : NpgsqlTypeHandler<BitArray>,
INpgsqlTypeHandler<BitVector32>, INpgsqlTypeHandler<bool>, INpgsqlTypeHandler<string>
{
/// <inheritdoc />
public BitStringHandler(PostgresType postgresType) : base(postgresType) {}

internal override Type GetFieldType(FieldDescription? fieldDescription = null)
=> fieldDescription != null && fieldDescription.TypeModifier == 1 ? typeof(bool) : typeof(BitArray);

Expand Down
4 changes: 0 additions & 4 deletions src/Npgsql/Internal/TypeHandlers/BoolHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;

namespace Npgsql.Internal.TypeHandlers
{
Expand All @@ -16,9 +15,6 @@ namespace Npgsql.Internal.TypeHandlers
/// </remarks>
public partial class BoolHandler : NpgsqlSimpleTypeHandler<bool>
{
/// <inheritdoc />
public BoolHandler(PostgresType postgresType) : base(postgresType) {}

/// <inheritdoc />
public override bool Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
=> buf.ReadByte() != 0;
Expand Down
6 changes: 0 additions & 6 deletions src/Npgsql/Internal/TypeHandlers/ByteaHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;

namespace Npgsql.Internal.TypeHandlers
{
Expand All @@ -22,11 +21,6 @@ public partial class ByteaHandler : NpgsqlTypeHandler<byte[]>, INpgsqlTypeHandle
, INpgsqlTypeHandler<ReadOnlyMemory<byte>>, INpgsqlTypeHandler<Memory<byte>>
#endif
{
/// <summary>
/// Constructs a <see cref="ByteaHandler"/>.
/// </summary>
public ByteaHandler(PostgresType postgresType) : base(postgresType) {}

/// <inheritdoc />
public override async ValueTask<byte[]> Read(NpgsqlReadBuffer buf, int len, bool async, FieldDescription? fieldDescription = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ partial class CompositeHandler<T> : NpgsqlTypeHandler<T>, ICompositeHandler
public Type CompositeType => typeof(T);

public CompositeHandler(PostgresCompositeType postgresType, ConnectorTypeMapper typeMapper, INpgsqlNameTranslator nameTranslator)
: base(postgresType)
{
PostgresType = postgresType;
_typeMapper = typeMapper;
_nameTranslator = nameTranslator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ public partial class DateHandler : NpgsqlSimpleTypeHandlerWithPsv<DateTime, Npgs
/// Constructs a <see cref="DateHandler"/>
/// </summary>
public DateHandler(PostgresType postgresType, bool convertInfinityDateTime)
: base(postgresType)
=> _convertInfinityDateTime = convertInfinityDateTime;
{
PostgresType = postgresType;
_convertInfinityDateTime = convertInfinityDateTime;
}

#region Read

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public partial class IntervalHandler : NpgsqlSimpleTypeHandlerWithPsv<TimeSpan,
/// <summary>
/// Constructs an <see cref="IntervalHandler"/>
/// </summary>
public IntervalHandler(PostgresType postgresType) : base(postgresType) {}
public IntervalHandler(PostgresType postgresType)
=> PostgresType = postgresType;

/// <inheritdoc />
public override TimeSpan Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public partial class TimeHandler : NpgsqlSimpleTypeHandler<TimeSpan>
/// <summary>
/// Constructs a <see cref="TimeHandler"/>.
/// </summary>
public TimeHandler(PostgresType postgresType) : base(postgresType) {}
public TimeHandler(PostgresType postgresType)
=> PostgresType = postgresType;

// PostgreSQL time resolution == 1 microsecond == 10 ticks
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public partial class TimeTzHandler : NpgsqlSimpleTypeHandler<DateTimeOffset>, IN
/// <summary>
/// Constructs an <see cref="TimeTzHandler"/>.
/// </summary>
public TimeTzHandler(PostgresType postgresType) : base(postgresType) {}
public TimeTzHandler(PostgresType postgresType)
=> PostgresType = postgresType;

#region Read

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public partial class TimestampHandler : NpgsqlSimpleTypeHandlerWithPsv<DateTime,
/// Constructs a <see cref="TimestampHandler"/>.
/// </summary>
public TimestampHandler(PostgresType postgresType, bool convertInfinityDateTime)
: base(postgresType) => ConvertInfinityDateTime = convertInfinityDateTime;
{
PostgresType = postgresType;
ConvertInfinityDateTime = convertInfinityDateTime;
}

#region Read

Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql/Internal/TypeHandlers/EnumHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ partial class EnumHandler<TEnum> : NpgsqlSimpleTypeHandler<TEnum>, IEnumHandler
#region Construction

internal EnumHandler(PostgresType postgresType, Dictionary<TEnum, string> enumToLabel, Dictionary<string, TEnum> labelToEnum)
: base(postgresType)
{
Debug.Assert(typeof(TEnum).GetTypeInfo().IsEnum, "EnumHandler instantiated for non-enum type");
PostgresType = postgresType;
_enumToLabel = enumToLabel;
_labelToEnum = labelToEnum;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Threading.Tasks;
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;
using Npgsql.TypeMapping;
using NpgsqlTypes;

// TODO: Need to work on the nullability here
Expand Down Expand Up @@ -35,9 +33,6 @@ public partial class TsQueryHandler : NpgsqlTypeHandler<NpgsqlTsQuery>,

readonly Stack<NpgsqlTsQuery> _stack = new();

/// <inheritdoc />
public TsQueryHandler(PostgresType postgresType) : base(postgresType) {}

#region Read

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Threading.Tasks;
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;
using Npgsql.TypeMapping;
using NpgsqlTypes;

namespace Npgsql.Internal.TypeHandlers.FullTextSearchHandlers
Expand All @@ -28,9 +26,6 @@ public partial class TsVectorHandler : NpgsqlTypeHandler<NpgsqlTsVector>
// 2 (num_pos) + sizeof(int16) * 256 (max_num_pos (positions/wegihts))
const int MaxSingleLexemeBytes = 2561;

/// <inheritdoc />
public TsVectorHandler(PostgresType postgresType) : base(postgresType) {}

#region Read

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;
using Npgsql.TypeMapping;
using NpgsqlTypes;

namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
Expand All @@ -18,9 +16,6 @@ namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
/// </remarks>
public partial class BoxHandler : NpgsqlSimpleTypeHandler<NpgsqlBox>
{
/// <inheritdoc />
public BoxHandler(PostgresType postgresType) : base(postgresType) {}

/// <inheritdoc />
public override NpgsqlBox Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
=> new(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;
using Npgsql.TypeMapping;
using NpgsqlTypes;

namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
Expand All @@ -18,9 +16,6 @@ namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
/// </remarks>
public partial class CircleHandler : NpgsqlSimpleTypeHandler<NpgsqlCircle>
{
/// <inheritdoc />
public CircleHandler(PostgresType postgresType) : base(postgresType) {}

/// <inheritdoc />
public override NpgsqlCircle Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
=> new(buf.ReadDouble(), buf.ReadDouble(), buf.ReadDouble());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;
using Npgsql.TypeMapping;
using NpgsqlTypes;

namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
Expand All @@ -18,9 +16,6 @@ namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
/// </remarks>
public partial class LineHandler : NpgsqlSimpleTypeHandler<NpgsqlLine>
{
/// <inheritdoc />
public LineHandler(PostgresType postgresType) : base(postgresType) {}

/// <inheritdoc />
public override NpgsqlLine Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
=> new(buf.ReadDouble(), buf.ReadDouble(), buf.ReadDouble());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;
using Npgsql.TypeMapping;
using NpgsqlTypes;

namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
Expand All @@ -18,9 +16,6 @@ namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
/// </remarks>
public partial class LineSegmentHandler : NpgsqlSimpleTypeHandler<NpgsqlLSeg>
{
/// <inheritdoc />
public LineSegmentHandler(PostgresType postgresType) : base(postgresType) {}

/// <inheritdoc />
public override NpgsqlLSeg Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
=> new(buf.ReadDouble(), buf.ReadDouble(), buf.ReadDouble(), buf.ReadDouble());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.Threading.Tasks;
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;
using Npgsql.TypeMapping;
using NpgsqlTypes;

namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
Expand All @@ -21,9 +19,6 @@ namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
/// </remarks>
public partial class PathHandler : NpgsqlTypeHandler<NpgsqlPath>
{
/// <inheritdoc />
public PathHandler(PostgresType postgresType) : base(postgresType) {}

#region Read

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Npgsql.BackendMessages;
using Npgsql.Internal.TypeHandling;
using Npgsql.PostgresTypes;
using NpgsqlTypes;

namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
Expand All @@ -17,9 +16,6 @@ namespace Npgsql.Internal.TypeHandlers.GeometricHandlers
/// </remarks>
public partial class PointHandler : NpgsqlSimpleTypeHandler<NpgsqlPoint>
{
/// <inheritdoc />
public PointHandler(PostgresType postgresType) : base(postgresType) {}

/// <inheritdoc />
public override NpgsqlPoint Read(NpgsqlReadBuffer buf, int len, FieldDescription? fieldDescription = null)
=> new(buf.ReadDouble(), buf.ReadDouble());
Expand Down
Loading