Skip to content

Commit

Permalink
more views
Browse files Browse the repository at this point in the history
  • Loading branch information
pgallik committed Dec 7, 2023
1 parent 3971b8a commit 0435748
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 16 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,30 @@ 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_ParcelsLinkedToMultipleAddresses);

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

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_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_StreetnameWithoutLinkedRoadSegments)));

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,7 +18,7 @@ public sealed class ActiveAddressWithoutLinkedParcelOrBuildingUnitsConfiguration
public void Configure(EntityTypeBuilder<ActiveAddressWithoutLinkedParcelOrBuildingUnits> builder)
{
builder
.ToView("ActiveAddressWithoutLinkedParcelOrBuildings", IntegrationContext.Schema)
.ToView(nameof(ActiveAddressWithoutLinkedParcelOrBuildingUnits), IntegrationContext.Schema)
.HasNoKey()
.ToSqlQuery(@$"select
""AddressPersistentLocalId"",
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
Expand Up @@ -3,20 +3,20 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
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
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 StreetnameWithoutLinkedRoadSegments
{
public int StreetNamePersistentLocalId { get; set; }
public int NisCode { get; set; }
public DateTime Timestamp { get; set; }


public StreetnameWithoutLinkedRoadSegments() { }
}

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

builder.HasIndex(x => x.StreetNamePersistentLocalId);
builder.HasIndex(x => x.NisCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ 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
Expand Down Expand Up @@ -117,5 +134,22 @@ AND address.""Status"" ILIKE 'inGebruik'
)
ORDER BY a.""PersistentLocalId""
";

public const string VIEW_StreetnameWithoutLinkedRoadSegments = $@"
CREATE MATERIALIZED VIEW IF NOT EXISTS ""Integration"".""{nameof(VIEW_StreetnameWithoutLinkedRoadSegments)}"" 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
";
}
}

0 comments on commit 0435748

Please sign in to comment.