- 完善 BulkCopy Update

This commit is contained in:
2881099
2022-12-06 21:57:30 +08:00
parent 768b715e18
commit 09864eaa9f
12 changed files with 269 additions and 136 deletions

View File

@ -25,6 +25,7 @@ namespace FreeSql.Internal.CommonProvider
public DbConnection _connection;
public int _commandTimeout = 0;
public Action<StringBuilder> _interceptSql;
public bool _isAutoSyncStructure;
}
public abstract partial class DeleteProvider<T1> : DeleteProvider, IDelete<T1>
@ -35,8 +36,9 @@ namespace FreeSql.Internal.CommonProvider
_commonUtils = commonUtils;
_commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1));
_isAutoSyncStructure = _orm.CodeFirst.IsAutoSyncStructure;
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
_whereGlobalFilter = _orm.GlobalFilter.GetFilters();
}
@ -145,7 +147,7 @@ namespace FreeSql.Internal.CommonProvider
if (string.IsNullOrEmpty(newname)) return _table.DbName;
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table.Type, newname);
if (_isAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table.Type, newname);
return newname;
}
public IDelete<T1> AsTable(Func<string, string> tableRule)
@ -164,7 +166,7 @@ namespace FreeSql.Internal.CommonProvider
if (entityType == _table.Type) return this;
var newtb = _commonUtils.GetTableByEntity(entityType);
_table = newtb ?? throw new Exception(CoreStrings.Type_AsType_Parameter_Error("IDelete"));
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
if (_isAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
return this;
}

View File

@ -31,6 +31,7 @@ namespace FreeSql.Internal.CommonProvider
public DbTransaction _transaction;
public DbConnection _connection;
public int _commandTimeout = 0;
public bool _isAutoSyncStructure;
}
public abstract partial class InsertProvider<T1> : InsertProvider, IInsert<T1> where T1 : class
@ -46,7 +47,8 @@ namespace FreeSql.Internal.CommonProvider
_commonExpression = commonExpression;
_table = _commonUtils.GetTableByEntity(typeof(T1));
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
_isAutoSyncStructure = _orm.CodeFirst.IsAutoSyncStructure;
if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
IgnoreCanInsert();
_sourceOld = _source;
}
@ -568,7 +570,7 @@ namespace FreeSql.Internal.CommonProvider
if (string.IsNullOrEmpty(newname)) return tbname;
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table?.Type, newname);
if (_isAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table?.Type, newname);
return newname;
}
public IInsert<T1> AsTable(Func<string, string> tableRule)
@ -588,7 +590,7 @@ namespace FreeSql.Internal.CommonProvider
if (entityType == _table.Type) return this;
var newtb = _commonUtils.GetTableByEntity(entityType);
_table = newtb ?? throw new Exception(CoreStrings.Type_AsType_Parameter_Error("IInsert"));
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
if (_isAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
IgnoreCanInsert();
return this;
}

View File

@ -38,9 +38,10 @@ namespace FreeSql.Internal.CommonProvider
public int _commandTimeout = 0;
public Action<StringBuilder> _interceptSql;
public object _updateVersionValue;
public bool _isAutoSyncStructure;
public static int ExecuteBulkUpdate<T1>(UpdateProvider<T1> update, NativeTuple<string, string, string, string> state, Action<IInsert<T1>> funcBulkCopy) where T1 : class
public static int ExecuteBulkUpdate<T1>(UpdateProvider<T1> update, NativeTuple<string, string, string, string, string[]> state, Action<IInsert<T1>> funcBulkCopy) where T1 : class
{
if (update._source.Any() != true || update._tempPrimarys.Any() == false) return 0;
var fsql = update._orm;
@ -59,12 +60,16 @@ namespace FreeSql.Internal.CommonProvider
fsql.Ado.CommandFluent(state.Item1).WithConnection(connection).WithTransaction(transaction).ExecuteNonQuery();
try
{
funcBulkCopy(fsql.Insert<T1>(update._source)
var insert = fsql.Insert<T1>(update._source)
.AsType(update._table.Type)
.WithConnection(connection)
.WithTransaction(transaction)
.InsertIdentity().AsTable(state.Item4));
var affrows = fsql.Ado.CommandFluent(state.Item2 + ";" + state.Item3).WithConnection(connection).WithTransaction(transaction).ExecuteNonQuery();
.InsertIdentity()
.InsertColumns(state.Item5)
.AsTable(state.Item4);
(insert as InsertProvider)._isAutoSyncStructure = false;
funcBulkCopy(insert);
var affrows = fsql.Ado.CommandFluent(state.Item2 + ";\r\n" + state.Item3).WithConnection(connection).WithTransaction(transaction).ExecuteNonQuery();
droped = true;
return affrows;
}
@ -80,7 +85,7 @@ namespace FreeSql.Internal.CommonProvider
}
#if net40
#else
async public static Task<int> ExecuteBulkUpdateAsync<T1>(UpdateProvider<T1> update, NativeTuple<string, string, string, string> state, Func<IInsert<T1>, Task> funcBulkCopy) where T1 : class
async public static Task<int> ExecuteBulkUpdateAsync<T1>(UpdateProvider<T1> update, NativeTuple<string, string, string, string, string[]> state, Func<IInsert<T1>, Task> funcBulkCopy) where T1 : class
{
if (update._source.Any() != true || update._tempPrimarys.Any() == false) return 0;
var fsql = update._orm;
@ -99,12 +104,16 @@ namespace FreeSql.Internal.CommonProvider
await fsql.Ado.CommandFluent(state.Item1).WithConnection(connection).WithTransaction(transaction).ExecuteNonQueryAsync();
try
{
await funcBulkCopy(fsql.Insert<T1>(update._source)
var insert = fsql.Insert<T1>(update._source)
.AsType(update._table.Type)
.WithConnection(connection)
.WithTransaction(transaction)
.InsertIdentity().AsTable(state.Item4));
var affrows = await fsql.Ado.CommandFluent(state.Item2 + ";" + state.Item3).WithConnection(connection).WithTransaction(transaction).ExecuteNonQueryAsync();
.InsertIdentity()
.InsertColumns(state.Item5)
.AsTable(state.Item4);
(insert as InsertProvider)._isAutoSyncStructure = false;
await funcBulkCopy(insert);
var affrows = await fsql.Ado.CommandFluent(state.Item2 + ";\r\n" + state.Item3).WithConnection(connection).WithTransaction(transaction).ExecuteNonQueryAsync();
droped = true;
return affrows;
}
@ -136,8 +145,9 @@ namespace FreeSql.Internal.CommonProvider
_tempPrimarys = _table?.Primarys ?? new ColumnInfo[0];
_versionColumn = _table?.VersionColumn;
_noneParameter = _orm.CodeFirst.IsNoneCommandParameter;
_isAutoSyncStructure = _orm.CodeFirst.IsAutoSyncStructure;
this.Where(_commonUtils.WhereObject(_table, "", dywhere));
if (_orm.CodeFirst.IsAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
if (_isAutoSyncStructure && typeof(T1) != typeof(object)) _orm.CodeFirst.SyncStructure<T1>();
IgnoreCanUpdate();
_whereGlobalFilter = _orm.GlobalFilter.GetFilters();
_sourceOld = _source;
@ -879,7 +889,7 @@ namespace FreeSql.Internal.CommonProvider
if (string.IsNullOrEmpty(newname)) return _table.DbName;
if (_orm.CodeFirst.IsSyncStructureToLower) newname = newname.ToLower();
if (_orm.CodeFirst.IsSyncStructureToUpper) newname = newname.ToUpper();
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table.Type, newname);
if (_isAutoSyncStructure) _orm.CodeFirst.SyncStructure(_table.Type, newname);
return newname;
}
public IUpdate<T1> AsTable(Func<string, string> tableRule)
@ -900,7 +910,7 @@ namespace FreeSql.Internal.CommonProvider
_table = newtb ?? throw new Exception(CoreStrings.Type_AsType_Parameter_Error("IUpdate"));
_tempPrimarys = _table.Primarys;
_versionColumn = _ignoreVersion ? null : _table.VersionColumn;
if (_orm.CodeFirst.IsAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
if (_isAutoSyncStructure) _orm.CodeFirst.SyncStructure(entityType);
IgnoreCanUpdate();
return this;
}