Skip to content

Commit

Permalink
Add NpgsqlPgLsnTypeMapping to map the pg_lsn internal type (#2165)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brar authored Dec 18, 2021
1 parent 324d9fa commit 88b0d7c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/EFCore.PG/Storage/Internal/Mapping/NpgsqlPgLsnTypeMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using NpgsqlTypes;
using System.Text;

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;

/// <summary>
/// The type mapping for the PostgreSQL pg_lsn type.
/// </summary>
/// <remarks>
/// See: https://www.postgresql.org/docs/current/datatype-pg-lsn.html
/// </remarks>
public class NpgsqlPgLsnTypeMapping : NpgsqlTypeMapping
{
/// <summary>
/// Initializes a new instance of the <see cref="NpgsqlPgLsnTypeMapping"/> class.
/// </summary>
public NpgsqlPgLsnTypeMapping()
: base("pg_lsn", typeof(NpgsqlLogSequenceNumber), NpgsqlDbType.PgLsn) {}

protected NpgsqlPgLsnTypeMapping(RelationalTypeMappingParameters parameters)
: base(parameters, NpgsqlDbType.PgLsn) {}

protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new NpgsqlPgLsnTypeMapping(parameters);

protected override string GenerateNonNullSqlLiteral(object value)
{
var lsn = (NpgsqlLogSequenceNumber)value;
var builder = new StringBuilder("PG_LSN '")
.Append(lsn.ToString())
.Append('\'');
return builder.ToString();
}

public override Expression GenerateCodeLiteral(object value)
{
var lsn = (NpgsqlLogSequenceNumber)value;
return Expression.New(Constructor, Expression.Constant((ulong)lsn));
}

private static readonly ConstructorInfo Constructor =
typeof(NpgsqlLogSequenceNumber).GetConstructor(new[] { typeof(ulong) })!;
}
3 changes: 3 additions & 0 deletions src/EFCore.PG/Storage/Internal/NpgsqlTypeMappingSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ static NpgsqlTypeMappingSource()
private readonly NpgsqlHstoreTypeMapping _hstore = new(typeof(Dictionary<string, string>));
private readonly NpgsqlHstoreTypeMapping _immutableHstore = new(typeof(ImmutableDictionary<string, string>));
private readonly NpgsqlTidTypeMapping _tid = new();
private readonly NpgsqlPgLsnTypeMapping _pgLsn = new();

private readonly NpgsqlLTreeTypeMapping _ltree = new();
private readonly NpgsqlStringTypeMapping _ltreeString = new("ltree", NpgsqlDbType.LTree);
Expand Down Expand Up @@ -296,6 +297,7 @@ public NpgsqlTypeMappingSource(TypeMappingSourceDependencies dependencies,
{ "regtype", new[] { _regtype } },
{ "lo", new[] { _lo } },
{ "tid", new[] { _tid } },
{ "pg_lsn", new[] { _pgLsn } },

{ "int4range", new[] { _int4range } },
{ "int8range", new[] { _int8range } },
Expand Down Expand Up @@ -363,6 +365,7 @@ public NpgsqlTypeMappingSource(TypeMappingSourceDependencies dependencies,
{ typeof(ImmutableDictionary<string, string>), _immutableHstore },
{ typeof(Dictionary<string, string>), _hstore },
{ typeof(NpgsqlTid), _tid },
{ typeof(NpgsqlLogSequenceNumber), _pgLsn },

{ typeof(NpgsqlPoint), _point },
{ typeof(NpgsqlBox), _box },
Expand Down
4 changes: 4 additions & 0 deletions test/EFCore.PG.Tests/Storage/NpgsqlTypeMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ private enum DummyEnum
public void GenerateSqlLiteral_returns_tid_literal()
=> Assert.Equal(@"TID '(0,1)'", GetMapping("tid").GenerateSqlLiteral(new NpgsqlTid(0, 1)));

[Fact]
public void GenerateSqlLiteral_returns_pg_lsn_literal()
=> Assert.Equal(@"PG_LSN '12345/67890'", GetMapping("pg_lsn").GenerateSqlLiteral(NpgsqlLogSequenceNumber.Parse("12345/67890")));

#endregion Misc

#region Array
Expand Down

0 comments on commit 88b0d7c

Please sign in to comment.