Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicrosoftSQL.BulkInsert - Added column mapping feature #67

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions Frends.MicrosoftSQL.BulkInsert/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [3.0.0] - 2025-01-15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually a breaking change? I had a feeling that it should not be, as the default behavior is going to remain exactly the same. However, if this is a breaking change, then please describe how it is a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new parameter will break the automatic updates when trying to import the new version.

### Added
- [Breaking] Added parameters ColumnMapping and ManualColumnMapping.
- Added column mapping feature which allows user to select from JsonPropertyNames, JsonPropertyOrder and ManualColumnMapping options how the column mapping is handled in bulk insert. Default behavior will remain the same JsonPropertyOrder.

## [2.2.0] - 2024-09-10
### Changed
- Updated Options.NotifyAfter property to be set dynamically based on the total row count, with a minimum value of 1, ensuring rowsCopied is updated correctly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,64 @@ public async Task TestBulkInsert_NotifyAfterOne()
}
}

[TestMethod]
public async Task TestBulkInsert_ColumnMapping()
{
var columnMappings = new List<ColumnMapping>()
{
ColumnMapping.JsonPropertyOrder,
ColumnMapping.JsonPropertyNames,
ColumnMapping.ManualColumnMapping
};

var options = new Options()
{
SqlTransactionIsolationLevel = SqlTransactionIsolationLevel.ReadCommitted,
CommandTimeoutSeconds = 60,
FireTriggers = false,
KeepIdentity = true,
NotifyAfter = 0,
ConvertEmptyPropertyValuesToNull = true,
KeepNulls = false,
TableLock = false,
};

var json = @"[
{
""Id"": 1,
""FirstName"": ""Etu"",
""LastName"": ""Suku""
},
{
""Id"": 2,
""LastName"": ""Suku"",
""FirstName"": ""Etu""
},
{
""FirstName"": ""First"",
""LastName"": ""Last"",
""Id"": 3
}
]";
RikuVirtanen marked this conversation as resolved.
Show resolved Hide resolved

var manualMapping = @"{ ""Id"":""Id"", ""FirstName"": ""FirstName"", ""LastName"": ""LastName"" }";
RikuVirtanen marked this conversation as resolved.
Show resolved Hide resolved

foreach (var columnMapping in columnMappings)
{
var input = new Input
{
ConnectionString = _connString,
TableName = _tableName,
InputData = json,
ColumnMapping = columnMapping,
ManualColumnMapping = manualMapping
};

var result = await MicrosoftSQL.BulkInsert(input, options, default);
Assert.IsTrue(result.Success);
}
}
RikuVirtanen marked this conversation as resolved.
Show resolved Hide resolved

private static int GetRowCount()
{
using var connection = new SqlConnection(_connString);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Frends.MicrosoftSQL.BulkInsert.Definitions;
using Microsoft.Data.SqlClient;
using Microsoft.IdentityModel.Abstractions;
using Newtonsoft.Json.Linq;
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -17,15 +16,6 @@ namespace Frends.MicrosoftSQL.BulkInsert;
/// </summary>
public class MicrosoftSQL
{
/// Mem cleanup.
static MicrosoftSQL()
{
var currentAssembly = Assembly.GetExecutingAssembly();
var currentContext = AssemblyLoadContext.GetLoadContext(currentAssembly);
if (currentContext != null)
currentContext.Unloading += OnPluginUnloadingRequested;
}

/// <summary>
/// Execute bulk insert JSON data to Microsoft SQL Server.
/// [Documentation](https://tasks.frends.com/tasks/frends-tasks/Frends.MicrosoftSQL.BulkInsert)
Expand Down Expand Up @@ -57,7 +47,7 @@ public static async Task<Result> BulkInsert([PropertyTab] Input input, [Property
{
try
{
var result = await ExecuteHandler(options, input.TableName, dataSet, new SqlBulkCopy(connection, GetSqlBulkCopyOptions(options), null), cancellationToken);
var result = await ExecuteHandler(options, input, dataSet, new SqlBulkCopy(connection, GetSqlBulkCopyOptions(options), null), cancellationToken);
return new Result(true, result, null);
}
catch (Exception ex)
Expand All @@ -74,7 +64,7 @@ public static async Task<Result> BulkInsert([PropertyTab] Input input, [Property

try
{
var result = await ExecuteHandler(options, input.TableName, dataSet, new SqlBulkCopy(connection, GetSqlBulkCopyOptions(options), transaction), cancellationToken);
var result = await ExecuteHandler(options, input, dataSet, new SqlBulkCopy(connection, GetSqlBulkCopyOptions(options), transaction), cancellationToken);
await transaction.CommitAsync(cancellationToken);
return new Result(true, result, null);
}
Expand Down Expand Up @@ -111,15 +101,29 @@ public static async Task<Result> BulkInsert([PropertyTab] Input input, [Property
}
}

private static async Task<long> ExecuteHandler(Options options, string tableName, DataSet dataSet, SqlBulkCopy sqlBulkCopy, CancellationToken cancellationToken)
private static async Task<long> ExecuteHandler(Options options, Input input, DataSet dataSet, SqlBulkCopy sqlBulkCopy, CancellationToken cancellationToken)
{
var rowsCopied = 0L;

// JsonPropertyOrder is handled implicitly (default behavior) by not adding any column mappings,
// which means the columns will be mapped based on their order in the input JSON.
if (input.ColumnMapping == ColumnMapping.JsonPropertyNames)
{
foreach (var column in dataSet.Tables[0].Columns)
sqlBulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(column.ToString(), column.ToString()));
}
else if (input.ColumnMapping == ColumnMapping.ManualColumnMapping)
{
foreach (var column in JObject.Parse(input.ManualColumnMapping).Properties())
sqlBulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(column.Name, column.Name));
}
RikuVirtanen marked this conversation as resolved.
Show resolved Hide resolved

try
{
using (sqlBulkCopy)
{
sqlBulkCopy.BulkCopyTimeout = options.CommandTimeoutSeconds;
sqlBulkCopy.DestinationTableName = tableName;
sqlBulkCopy.DestinationTableName = input.TableName;
sqlBulkCopy.SqlRowsCopied += (s, e) => rowsCopied = e.RowsCopied;

if (options.NotifyAfter == 0)
Expand Down Expand Up @@ -189,9 +193,4 @@ private static IsolationLevel GetIsolationLevel(Options options)
_ => IsolationLevel.ReadCommitted,
};
}

private static void OnPluginUnloadingRequested(AssemblyLoadContext obj)
{
obj.Unloading -= OnPluginUnloadingRequested;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Frends.MicrosoftSQL.BulkInsert.Definitions;

/// <summary>
/// Selection of column mapping options for bulk insert.
/// </summary>
public enum ColumnMapping
{
/// <summary>
/// Column mapping is disabled and the bulk insert will insert the data based on the order of the properties in input JSON.
/// </summary>
JsonPropertyOrder,

/// <summary>
/// Input JSON property names will be used with bulk insert to create column mapping. Column mapping is case sensitive.
/// </summary>
JsonPropertyNames,

/// <summary>
/// Manual column mapping JSON will be used with bulk insert to create column mapping. Column mapping is case sensitive.
/// </summary>
ManualColumnMapping,
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ public class Input
/// <example>TestTable</example>
public string TableName { get; set; }

/// <summary>
/// Selection for column mapping operation.
/// </summary>
/// <example>ColumnMapping.JsonPropertyNames</example>
[DefaultValue(ColumnMapping.JsonPropertyOrder)]
public ColumnMapping ColumnMapping { get; set; }

/// <summary>
/// Column mapping JSON for manual column mapping.
RikuVirtanen marked this conversation as resolved.
Show resolved Hide resolved
/// JSON property cannot be a nested one. Only top level properties are supported.
/// </summary>
/// <example>
/// {
/// "json_property_1": "db_column_1",
/// "json_property_2": "db_column_2",
/// "json_property_3": "db_column_3"
/// }
/// </example>
[UIHint(nameof(ColumnMapping), "", ColumnMapping.ManualColumnMapping)]
public string ManualColumnMapping { get; set; }

/// <summary>
/// Json Array of objects. All object property names need to match with the destination table column names.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>2.2.0</Version>
<Version>3.0.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down
Loading