2020-01-15 13:40:35 +11:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2021-02-12 13:36:50 +01:00
|
|
|
|
using Umbraco.Extensions;
|
2020-01-15 13:40:35 +11:00
|
|
|
|
|
2021-02-12 13:36:50 +01:00
|
|
|
|
namespace Umbraco.Cms.Infrastructure.Persistence
|
2020-01-15 13:40:35 +11:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A provider that just generates insert commands
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class BasicBulkSqlInsertProvider : IBulkSqlInsertProvider
|
|
|
|
|
|
{
|
2021-02-09 10:22:42 +01:00
|
|
|
|
public string ProviderName => Cms.Core.Constants.DatabaseProviders.SqlServer;
|
2020-04-28 07:01:30 +02:00
|
|
|
|
|
2020-01-15 13:40:35 +11:00
|
|
|
|
public int BulkInsertRecords<T>(IUmbracoDatabase database, IEnumerable<T> records)
|
|
|
|
|
|
{
|
2021-08-03 09:48:34 +02:00
|
|
|
|
if (!records.Any()) return 0;
|
2020-01-15 13:40:35 +11:00
|
|
|
|
|
2021-08-03 09:48:34 +02:00
|
|
|
|
return BulkInsertRecordsWithCommands(database, records.ToArray());
|
2020-01-15 13:40:35 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Bulk-insert records using commands.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">The type of the records.</typeparam>
|
|
|
|
|
|
/// <param name="database">The database.</param>
|
|
|
|
|
|
/// <param name="records">The records.</param>
|
|
|
|
|
|
/// <returns>The number of records that were inserted.</returns>
|
|
|
|
|
|
internal static int BulkInsertRecordsWithCommands<T>(IUmbracoDatabase database, T[] records)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var command in database.GenerateBulkInsertCommands(records))
|
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
|
|
|
|
|
|
return records.Length; // what else?
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|