From 3e95182a0ffe492e45d440363a814805d4394e5f Mon Sep 17 00:00:00 2001 From: Matthias Sebastian Sort <mss@dynamicweb.dk> Date: Wed, 28 Aug 2024 09:49:53 +0200 Subject: [PATCH] added function to check and replace if the auto-id column exists in the column mappings. Bump version to 10.7.0 --- ...taIntegration.Providers.SqlProvider.csproj | 4 ++-- src/SQLDestinationWriter.cs | 24 ++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Dynamicweb.DataIntegration.Providers.SqlProvider.csproj b/src/Dynamicweb.DataIntegration.Providers.SqlProvider.csproj index 3369803..988b8bc 100644 --- a/src/Dynamicweb.DataIntegration.Providers.SqlProvider.csproj +++ b/src/Dynamicweb.DataIntegration.Providers.SqlProvider.csproj @@ -1,6 +1,6 @@ <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> - <Version>10.6.0</Version> + <Version>10.7.0</Version> <AssemblyVersion>1.0.0.0</AssemblyVersion> <Title>SQL Provider</Title> <Description>SQL Provider</Description> @@ -23,7 +23,7 @@ <SymbolPackageFormat>snupkg</SymbolPackageFormat> </PropertyGroup> <ItemGroup> - <PackageReference Include="Dynamicweb.DataIntegration" Version="10.6.3" /> + <PackageReference Include="Dynamicweb.DataIntegration" Version="10.7.0" /> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" /> </ItemGroup> </Project> diff --git a/src/SQLDestinationWriter.cs b/src/SQLDestinationWriter.cs index 176abfb..d83c7df 100644 --- a/src/SQLDestinationWriter.cs +++ b/src/SQLDestinationWriter.cs @@ -91,7 +91,7 @@ public SqlDestinationWriter(Mapping mapping, SqlConnection connection, bool remo public SqlDestinationWriter(Mapping mapping, SqlConnection connection, bool removeMissingAfterImport, ILogger logger, string tempTablePrefix, bool discardDuplicates) { Mapping = mapping; - _columnMappings = Mapping.GetColumnMappings(); + _columnMappings = new ColumnMappingCollection(ReplaceKeyColumnsWithAutoIdIfExists(Mapping)); SqlCommand = connection.CreateCommand(); SqlCommand.CommandTimeout = 1200; this.removeMissingAfterImport = removeMissingAfterImport; @@ -118,7 +118,7 @@ public SqlDestinationWriter(Mapping mapping, SqlConnection connection, bool remo public SqlDestinationWriter(Mapping mapping, SqlConnection connection, bool removeMissingAfterImport, ILogger logger, bool discardDuplicates) { Mapping = mapping; - _columnMappings = Mapping.GetColumnMappings(); + _columnMappings = new ColumnMappingCollection(ReplaceKeyColumnsWithAutoIdIfExists(Mapping)); SqlCommand = connection.CreateCommand(); SqlCommand.CommandTimeout = 1200; this.removeMissingAfterImport = removeMissingAfterImport; @@ -145,7 +145,7 @@ public SqlDestinationWriter(Mapping mapping, SqlConnection connection, bool remo public SqlDestinationWriter(Mapping mapping, SqlCommand mockSqlCommand, bool removeMissingAfterImport, ILogger logger, string tempTablePrefix, bool discardDuplicates) { Mapping = mapping; - _columnMappings = Mapping.GetColumnMappings(); + _columnMappings = new ColumnMappingCollection(ReplaceKeyColumnsWithAutoIdIfExists(Mapping)); SqlCommand = mockSqlCommand; this.removeMissingAfterImport = removeMissingAfterImport; this.logger = logger; @@ -179,6 +179,24 @@ public SqlDestinationWriter(Mapping mapping, SqlCommand mockSqlCommand, bool rem } } + private static IEnumerable<ColumnMapping> ReplaceKeyColumnsWithAutoIdIfExists(Mapping mapping) + { + //will move this to MappingExtensions - US https://dev.azure.com/dynamicwebsoftware/Dynamicweb/_workitems/edit/20900 + if (mapping == null) return []; + + var autoIdDestinationColumnName = MappingExtensions.GetAutoIdColumnName(mapping.DestinationTable?.Name ?? ""); + if (string.IsNullOrEmpty(autoIdDestinationColumnName)) return mapping.GetColumnMappings(); + + var columnMappings = mapping.GetColumnMappings().ToList(); + var autoIdColumnMapping = columnMappings.Where(obj => obj.DestinationColumn.Name.Equals(autoIdDestinationColumnName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); + if (autoIdColumnMapping != null) + { + columnMappings.ForEach(obj => obj.IsKey = false); + autoIdColumnMapping.IsKey = true; + } + return columnMappings; + } + /// <summary> /// Writes the specified row. /// </summary>