Skip to content

Commit

Permalink
more views
Browse files Browse the repository at this point in the history
  • Loading branch information
pgallik authored and ArneD committed Dec 8, 2023
1 parent 39e8749 commit f2c2e77
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Basisregisters.IntegrationDb.Schema/IntegrationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class IntegrationContext : DbContext
public DbSet<Parcel> Parcels { get; set; }
public DbSet<PostInfo> PostInfo { get; set; }
public DbSet<RoadSegment> RoadSegments { get; set; }
public DbSet<ParcelAddressRelation> ParcelAddressRelations { get; set; }
public DbSet<BuildingUnitAddressRelation> BuildingUnitAddressRelations { get; set; }
public DbSet<ParcelAddressRelations> ParcelAddressRelations { get; set; }
public DbSet<BuildingUnitAddressRelations> BuildingUnitAddressRelations { get; set; }
public DbSet<ActiveAddressWithoutLinkedParcelOrBuildingUnits> ActiveAddressWithoutParcelOrBuildingUnitRelations { get; set; }

public IntegrationContext() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,32 @@ protected override void Up(MigrationBuilder migrationBuilder)
migrationBuilder.Sql(ViewQueries.VIEW_ParcelAddressRelations);

migrationBuilder.Sql(ViewQueries.VIEW_AddressesLinkedToMultipleBuildingUnits);
migrationBuilder.Sql(ViewQueries.VIEW_AddressesLinkedToMultipleParcels);
migrationBuilder.Sql(ViewQueries.VIEW_AddressesWithMultipleLinks);
migrationBuilder.Sql(ViewQueries.VIEW_AddressesWithoutPostalCode);

migrationBuilder.Sql(ViewQueries.VIEW_ParcelsLinkedToMultipleAddresses);

migrationBuilder.Sql(ViewQueries.VIEW_ActiveAddressWithoutLinkedParcels);
migrationBuilder.Sql(ViewQueries.VIEW_ActiveAddressWithoutLinkedParcelOrBuildingUnit);
migrationBuilder.Sql(ViewQueries.VIEW_AddressesLinkedToMultipleParcels);
migrationBuilder.Sql(ViewQueries.VIEW_ActiveStreetnameWithoutLinkedRoadSegments);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
string DropView(string viewName) => @$"DROP VIEW ""{IntegrationContext.Schema}"".""{viewName}""";

migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_AddressesWithMultipleLinks)));
migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_AddressesLinkedToMultipleBuildingUnits)));
migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ParcelsLinkedToMultipleAddresses)));
migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_AddressesLinkedToMultipleParcels)));
migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_AddressesWithoutPostalCode)));

migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ActiveAddressWithoutLinkedParcels)));
migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ActiveAddressWithoutLinkedParcelOrBuildingUnit)));
migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_AddressesLinkedToMultipleParcels)));

migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ParcelsLinkedToMultipleAddresses)));

migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ActiveStreetnameWithoutLinkedRoadSegments)));

migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_BuildingUnitAddressRelations)));
migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ParcelAddressRelations)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public sealed class ActiveAddressWithoutLinkedParcelOrBuildingUnitsConfiguration
public void Configure(EntityTypeBuilder<ActiveAddressWithoutLinkedParcelOrBuildingUnits> builder)
{
builder
.ToView("ActiveAddressWithoutLinkedParcelOrBuildings", IntegrationContext.Schema)
.ToView(nameof(ActiveAddressWithoutLinkedParcelOrBuildingUnits), IntegrationContext.Schema)
.HasNoKey()
.ToSqlQuery(@$"select
.ToSqlQuery(@$"SELECT
""AddressPersistentLocalId"",
""NisCode"",
""Timestamp""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public void Configure(EntityTypeBuilder<ActiveAddressWithoutLinkedParcels> build
builder
.ToView(nameof(ActiveAddressWithoutLinkedParcels), IntegrationContext.Schema)
.HasNoKey()
.ToSqlQuery(@$"select
.ToSqlQuery(@$"SELECT
""AddressPersistentLocalId""
,""NisCode""
,""Timestamp""
FROM ""Integration"".""{ViewQueries.VIEW_ActiveAddressWithoutLinkedParcels}"" ");
FROM ""{IntegrationContext.Schema}"".""{ViewQueries.VIEW_ActiveAddressWithoutLinkedParcels}"" ");

builder.HasIndex(x => x.AddressPersistentLocalId);
builder.HasIndex(x => x.NisCode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Basisregisters.IntegrationDb.Schema.Models.Views
{
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class ActiveStreetnameWithoutLinkedRoadSegments
{
public int StreetNamePersistentLocalId { get; set; }
public int NisCode { get; set; }
public DateTime Timestamp { get; set; }


public ActiveStreetnameWithoutLinkedRoadSegments() { }
}

public sealed class ActiveStreetnameWithoutLinkedRoadSegmentsConfiguration : IEntityTypeConfiguration<ActiveStreetnameWithoutLinkedRoadSegments>
{
public void Configure(EntityTypeBuilder<ActiveStreetnameWithoutLinkedRoadSegments> builder)
{
builder
.ToView(nameof(ActiveStreetnameWithoutLinkedRoadSegments), IntegrationContext.Schema)
.HasNoKey()
.ToSqlQuery(@$"
SELECT
""StreetNamePersistentLocalId"",
""NisCode"",
""Timestamp""
FROM ""{IntegrationContext.Schema}"".""{nameof(ViewQueries.VIEW_ActiveStreetnameWithoutLinkedRoadSegments)}"" ");

builder.HasIndex(x => x.StreetNamePersistentLocalId);
builder.HasIndex(x => x.NisCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Configure(EntityTypeBuilder<AddressesLinkedToMultipleBuildingUnits>
.ToView(nameof(AddressesLinkedToMultipleBuildingUnits), IntegrationContext.Schema)
.HasNoKey()
.ToSqlQuery(@$"
select
SELECT
""AddressPersistentLocalId"",
""LinkedBuildingUnitCount"",
""NisCode"",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Basisregisters.IntegrationDb.Schema.Models.Views
{
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class AddressesWithMultipleLinks
{
public int AddressPersistentLocalId { get; set; }
public string Status { get; set; }
public bool IsRemoved { get; set; }
public int LinkedBuildingUnitCount { get; set; }
public int LinkedParcelCount { get; set; }
public int NisCode { get; set; }
public DateTime Timestamp { get; set; }

public AddressesWithMultipleLinks() { }
}

public sealed class AddressesWithMultipleLinksConfiguration : IEntityTypeConfiguration<AddressesWithMultipleLinks>
{
public void Configure(EntityTypeBuilder<AddressesWithMultipleLinks> builder)
{
builder
.ToView(nameof(AddressesWithMultipleLinks), IntegrationContext.Schema)
.HasNoKey()
.ToSqlQuery(@$"
SELECT
""AddressPersistentLocalId"",
""Status"",
""IsRemoved"",
""LinkedBuildingUnitCount"",
""LinkedParcelCount"",
""NisCode"",
""CURRENT_TIMESTAMP AS ""Timestamp""
FROM ""{IntegrationContext.Schema}"".""{nameof(ViewQueries.VIEW_AddressesWithMultipleLinks)}"" ");

builder.HasIndex(x => x.AddressPersistentLocalId);
builder.HasIndex(x => x.NisCode);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Basisregisters.IntegrationDb.Schema.Models.Views
{
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class AddressesWithoutPostalCode
{
public int AddressPersistentLocalId { get; set; }
public int NisCode { get; set; }
public DateTime Timestamp { get; set; }

public AddressesWithoutPostalCode() { }
}

public sealed class AddressesWithoutPostalCodeConfiguration : IEntityTypeConfiguration<AddressesWithoutPostalCode>
{
public void Configure(EntityTypeBuilder<AddressesWithoutPostalCode> builder)
{
builder
.ToView(nameof(AddressesWithoutPostalCode), IntegrationContext.Schema)
.HasNoKey()
.ToSqlQuery(@$"
SELECT
""AddressPersistentLocalId"",
""NisCode"",
""Timestamp""
FROM ""{IntegrationContext.Schema}"".""{nameof(ViewQueries.VIEW_AddressesWithoutPostalCode)}"" ");

builder.HasIndex(x => x.AddressPersistentLocalId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class BuildingUnitAddressRelation
public class BuildingUnitAddressRelations
{
public int BuildingUnitPersistentLocalId { get; set; }
public string AddressPersistentLocalId { get; set; }

public BuildingUnitAddressRelation() { }
public BuildingUnitAddressRelations() { }
}

public sealed class BuildingUnitAddressRelationConfiguration : IEntityTypeConfiguration<BuildingUnitAddressRelation>
public sealed class BuildingUnitAddressRelationConfiguration : IEntityTypeConfiguration<BuildingUnitAddressRelations>
{
public void Configure(EntityTypeBuilder<BuildingUnitAddressRelation> builder)
public void Configure(EntityTypeBuilder<BuildingUnitAddressRelations> builder)
{
builder
.ToView("BuildingUnitAddressRelations", IntegrationContext.Schema)
.ToView(nameof(BuildingUnitAddressRelations), IntegrationContext.Schema)
.HasNoKey()
.ToSqlQuery(@$"
select
SELECT
""BuildingUnitPersistentLocalId"",
""AddressPersistentLocalId""
FROM ""{IntegrationContext.Schema}"".""{nameof(ViewQueries.VIEW_BuildingUnitAddressRelations)}"" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public class ParcelAddressRelation
public class ParcelAddressRelations
{
public string CaPaKey { get; set; }
public int? AddressPersistentLocalId { get; set; }
public DateTime Timestamp { get; set; }

public ParcelAddressRelation() { }
public ParcelAddressRelations() { }
}

public sealed class ParcelAddressRelationConfiguration : IEntityTypeConfiguration<ParcelAddressRelation>
public sealed class ParcelAddressRelationConfiguration : IEntityTypeConfiguration<ParcelAddressRelations>
{
public void Configure(EntityTypeBuilder<ParcelAddressRelation> builder)
public void Configure(EntityTypeBuilder<ParcelAddressRelations> builder)
{
builder
.ToView("ParcelAddressRelations", IntegrationContext.Schema)
.ToView(nameof(ParcelAddressRelations), IntegrationContext.Schema)
.HasNoKey()
.ToSqlQuery(@$"
select
SELECT
""CaPaKey"",
""AddressPersistentLocalId""
FROM ""{IntegrationContext.Schema}"".""{nameof(ViewQueries.VIEW_ParcelAddressRelations)}"" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Configure(EntityTypeBuilder<ParcelsLinkedToMultipleAddresses> builde
.ToView(nameof(ParcelsLinkedToMultipleAddresses), IntegrationContext.Schema)
.HasNoKey()
.ToSqlQuery(@$"
select
SELECT
""CaPaKey"",
""NisCode"",
""LinkedAddressCount""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,28 @@ GROUP BY
COUNT(*) > 1;
";

public const string VIEW_AddressesWithMultipleLinks = $@"
CREATE MATERIALIZED VIEW IF NOT EXISTS ""Integration"".""{nameof(VIEW_AddressesWithMultipleLinks)}"" AS
SELECT
address.""PersistentLocalId"" AS ""AddressPersistentLocalId"",
address.""Status"",
address.""IsRemoved"",
buLinks.""LinkedBuildingUnitCount"",
parcelLinks.""LinkedParcelCount"",
address.""NisCode"",
CURRENT_TIMESTAMP AS ""Timestamp""
FROM ""Integration"".""Addresses"" address
JOIN ""Integration"".""VIEW_AddressesLinkedToMultipleBuildingUnits"" buLinks
on address.""PersistentLocalId"" = buLinks.""AddressPersistentLocalId""
JOIN ""Integration"".""VIEW_AddressesLinkedToMultipleParcels"" parcelLinks
on address.""PersistentLocalId"" = parcelLinks.""AddressPersistentLocalId""
";

public const string VIEW_ParcelsLinkedToMultipleAddresses = $@"
CREATE MATERIALIZED VIEW IF NOT EXISTS ""Integration"".""{nameof(VIEW_ParcelsLinkedToMultipleAddresses)}"" AS
SELECT
p.""CaPaKey"",
p.""Status"",
COUNT(*) AS ""LinkedAddressCount"",
CURRENT_TIMESTAMP AS ""Timestamp""
Expand Down Expand Up @@ -117,5 +135,33 @@ AND address.""Status"" ILIKE 'inGebruik'
)
ORDER BY a.""PersistentLocalId""
";

public const string VIEW_ActiveStreetnameWithoutLinkedRoadSegments = $@"
CREATE MATERIALIZED VIEW IF NOT EXISTS ""Integration"".""{nameof(VIEW_ActiveStreetnameWithoutLinkedRoadSegments)}"" AS
SELECT
streetname.""PersistentLocalId"" AS ""StreetNamePersistentLocalId"",
streetname.""NisCode"",
CURRENT_TIMESTAMP AS ""Timestamp""
FROM ""Integration"".""StreetNames"" streetname
WHERE NOT EXISTS (
SELECT 1
FROM ""Integration"".""RoadSegments"" roadsegment
WHERE roadsegment.""LeftSideStreetNameId"" = streetname.""PersistentLocalId""
AND roadsegment.""RightSideStreetNameId"" = streetname.""PersistentLocalId""
)
AND streetname.""Status"" ILIKE 'ingebruik'
AND streetname.""IsRemoved"" = false
";

public const string VIEW_AddressesWithoutPostalCode = $@"
CREATE MATERIALIZED VIEW IF NOT EXISTS ""Integration"".""{nameof(VIEW_AddressesWithoutPostalCode)}"" AS
SELECT
""PersistentLocalId"" AS ""AddressPersistentLocalId"",
""NisCode""::int,
CURRENT_TIMESTAMP AS ""Timestamp""
FROM ""Integration"".""Addresses""
WHERE ""PostalCode"" = '' OR ""PostalCode"" IS NULL
";
}
}

0 comments on commit f2c2e77

Please sign in to comment.