- 增加 IInsert.ExecuteSqlBulkCopy 扩展方法执行 SqlBulkCopy 批量插入,在 FreeSql.Provider.SqlServer 可用;

This commit is contained in:
28810 2019-12-21 14:43:24 +08:00
parent c268970c71
commit c335eab82c
4 changed files with 113 additions and 2 deletions

View File

@ -910,6 +910,15 @@
</summary>
<returns></returns>
</member>
<member name="M:FreeSql.IInsert`1.ToDataTable">
<summary>
返回 DataTable 以便做 BulkCopy 数据做准备<para></para>
此方法会处理:<para></para>
类型、表名、字段名映射<para></para>
IgnoreColumns、InsertColumns
</summary>
<returns></returns>
</member>
<member name="F:FreeSql.FieldAliasOptions.AsIndex">
<summary>
自动产生 as1, as2, as3 .... 字段别名<para></para>

View File

@ -1,4 +1,5 @@
using FreeSql.Internal.Model;
using FreeSql.Extensions.EntityUtil;
using SafeObjectPool;
using System;
using System.Collections.Generic;
@ -136,8 +137,20 @@ namespace FreeSql.Internal.CommonProvider
foreach (var col in table.Columns.Values)
{
object val = col.GetMapValue(data);
if (col.Attribute.IsPrimary && col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
if (col.Attribute.IsPrimary)
{
if (col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
col.SetMapValue(data, val = FreeUtil.NewMongodbId());
else if (col.CsType.NullableTypeOrThis() == typeof(Guid))
{
var guidVal = orm.GetEntityValueWithPropertyName(table.Type, data, col.CsName);
if (guidVal == null || (Guid)guidVal == Guid.Empty)
{
orm.SetEntityValueWithPropertyName(table.Type, data, col.CsName, FreeUtil.NewMongodbId());
val = col.GetMapValue(data);
}
}
}
if (orm.Aop.AuditValue != null)
{
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Insert, col, table.Properties[col.CsName], val);

View File

@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -18,6 +19,10 @@ namespace FreeSql.SqlServer.Curd
{
}
internal IFreeSql InternalOrm => _orm as IFreeSql;
internal SqlConnection InternalConnection => _connection as SqlConnection;
internal SqlTransaction InternalTransaction => _transaction as SqlTransaction;
public override int ExecuteAffrows() => base.SplitExecuteAffrows(_batchValuesLimit > 0 ? _batchValuesLimit : 1000, _batchParameterLimit > 0 ? _batchParameterLimit : 2100);
public override long ExecuteIdentity() => base.SplitExecuteIdentity(_batchValuesLimit > 0 ? _batchValuesLimit : 1000, _batchParameterLimit > 0 ? _batchParameterLimit : 2100);
public override List<T1> ExecuteInserted() => base.SplitExecuteInserted(_batchValuesLimit > 0 ? _batchValuesLimit : 1000, _batchParameterLimit > 0 ? _batchParameterLimit : 2100);

View File

@ -2,6 +2,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.SqlClient;
public static partial class FreeSqlSqlServerGlobalExtensions
{
@ -40,6 +41,89 @@ public static partial class FreeSqlSqlServerGlobalExtensions
return that;
}
internal static ConcurrentDictionary<IFreeSql, (SqlServerLock, Dictionary<Type, bool>)> _dicSetGlobalSelectWithLock = new ConcurrentDictionary<IFreeSql, (SqlServerLock, Dictionary<Type, bool>)>();
/// <summary>
/// SqlServer SqlCopyBulk 批量插入功能<para></para>
/// 使用 IgnoreColumns/InsertColumns 设置忽略/指定导入的列<para></para>
/// 使用 WithConnection/WithTransaction 传入连接/事务对象<para></para>
/// 提示:若本方法不能满足,请使用 IInsert&lt;T&gt;.ToDataTable 方法得到 DataTable 对象后,自行处理。<para></para>
/// SqlCopyBulk 与 insert into t values(..),(..),(..) 性能测试参考:<para></para>
/// 插入180000行52列21,065ms 与 402,355ms10列4,248ms 与 47,204ms<para></para>
/// 插入10000行52列578ms 与 24,847ms10列127ms 与 2,275ms<para></para>
/// 插入5000行52列326ms 与 11,465ms10列71ms 与 1,108ms<para></para>
/// 插入2000行52列139ms 与 4,971ms10列30ms 与 488ms<para></para>
/// 插入1000行52列105ms 与 2,437ms10列48ms 与 279ms<para></para>
/// 插入500行52列79ms 与 915ms10列14ms 与 123ms<para></para>
/// 插入100行52列60ms 与 138ms10列11ms 与 35ms<para></para>
/// 插入50行52列48ms 与 88ms10列10ms 与 16ms<para></para>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="that"></param>
/// <param name="copyOptions"></param>
/// <param name="batchSize"></param>
/// <param name="bulkCopyTimeout"></param>
public static void ExecuteSqlBulkCopy<T>(this IInsert<T> that, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, int? batchSize = null, int? bulkCopyTimeout = null) where T : class
{
var insert = that as FreeSql.SqlServer.Curd.SqlServerInsert<T>;
if (insert == null) throw new Exception("ExecuteSqlBulkCopy 是 FreeSql.Provider.SqlServer 特有的功能");
var dt = that.ToDataTable();
if (dt.Rows.Count == 0) return;
Action<SqlBulkCopy> writeToServer = bulkCopy =>
{
if (batchSize.HasValue) bulkCopy.BatchSize = batchSize.Value;
if (bulkCopyTimeout.HasValue) bulkCopy.BulkCopyTimeout = bulkCopyTimeout.Value;
bulkCopy.DestinationTableName = dt.TableName;
bulkCopy.WriteToServer(dt);
};
if (insert.InternalConnection == null && insert.InternalTransaction == null)
{
using (var conn = insert.InternalOrm.Ado.MasterPool.Get())
{
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
new SqlBulkCopy(conn.Value as SqlConnection) :
new SqlBulkCopy(conn.Value as SqlConnection, copyOptions, null))
{
writeToServer(bulkCopy);
}
}
}
else if (insert.InternalTransaction != null)
{
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
new SqlBulkCopy(insert.InternalTransaction.Connection) :
new SqlBulkCopy(insert.InternalTransaction.Connection, copyOptions, insert.InternalTransaction))
{
writeToServer(bulkCopy);
}
}
else if (insert.InternalConnection != null)
{
var conn = insert.InternalConnection;
var isNotOpen = false;
if (conn.State != System.Data.ConnectionState.Open)
{
isNotOpen = true;
conn.Open();
}
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
new SqlBulkCopy(insert.InternalConnection) :
new SqlBulkCopy(insert.InternalConnection, copyOptions, null))
{
writeToServer(bulkCopy);
}
if (isNotOpen)
{
conn.Close();
}
}
else
{
throw new NotImplementedException("未实现错误,请反馈给作者");
}
}
}
[Flags]