mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 增加 IUpdate<T> BulkCopy 批量更新扩展方法(暂时支持 SqlServer/MySql/PostgreSQL);
This commit is contained in:
@ -51,7 +51,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public IDelete<T1> WithTransaction(DbTransaction transaction)
|
||||
{
|
||||
_transaction = transaction;
|
||||
_connection = _transaction?.Connection;
|
||||
if (transaction != null) _connection = transaction.Connection;
|
||||
return this;
|
||||
}
|
||||
public IDelete<T1> WithConnection(DbConnection connection)
|
||||
|
@ -61,7 +61,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public IInsertOrUpdate<T1> WithTransaction(DbTransaction transaction)
|
||||
{
|
||||
_transaction = transaction;
|
||||
_connection = _transaction?.Connection;
|
||||
if (transaction != null) _connection = transaction.Connection;
|
||||
return this;
|
||||
}
|
||||
public IInsertOrUpdate<T1> WithConnection(DbConnection connection)
|
||||
|
@ -78,7 +78,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public IInsert<T1> WithTransaction(DbTransaction transaction)
|
||||
{
|
||||
_transaction = transaction;
|
||||
_connection = _transaction?.Connection;
|
||||
if (transaction != null) _connection = transaction.Connection;
|
||||
return this;
|
||||
}
|
||||
public IInsert<T1> WithConnection(DbConnection connection)
|
||||
|
@ -557,7 +557,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public TSelect WithTransaction(DbTransaction transaction)
|
||||
{
|
||||
_transaction = transaction;
|
||||
_connection = _transaction?.Connection;
|
||||
if (transaction != null) _connection = transaction.Connection;
|
||||
return this as TSelect;
|
||||
}
|
||||
public TSelect WithConnection(DbConnection connection)
|
||||
|
@ -1,5 +1,6 @@
|
||||
using FreeSql.Extensions.EntityUtil;
|
||||
using FreeSql.Internal.Model;
|
||||
using FreeSql.Internal.ObjectPool;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
@ -37,6 +38,87 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public int _commandTimeout = 0;
|
||||
public Action<StringBuilder> _interceptSql;
|
||||
public object _updateVersionValue;
|
||||
|
||||
|
||||
public static int ExecuteBulkUpdate<T1>(UpdateProvider<T1> update, NativeTuple<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;
|
||||
var connection = update._connection;
|
||||
var transaction = update._transaction;
|
||||
|
||||
Object<DbConnection> poolConn = null;
|
||||
if (connection == null)
|
||||
{
|
||||
poolConn = fsql.Ado.MasterPool.Get();
|
||||
connection = poolConn.Value;
|
||||
}
|
||||
try
|
||||
{
|
||||
var droped = false;
|
||||
fsql.Ado.CommandFluent(state.Item1).WithConnection(connection).WithTransaction(transaction).ExecuteNonQuery();
|
||||
try
|
||||
{
|
||||
funcBulkCopy(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();
|
||||
droped = true;
|
||||
return affrows;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (droped == false) fsql.Ado.CommandFluent(state.Item3).WithConnection(connection).WithTransaction(transaction).ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
poolConn?.Dispose();
|
||||
}
|
||||
}
|
||||
#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
|
||||
{
|
||||
if (update._source.Any() != true || update._tempPrimarys.Any() == false) return 0;
|
||||
var fsql = update._orm;
|
||||
var connection = update._connection;
|
||||
var transaction = update._transaction;
|
||||
|
||||
Object<DbConnection> poolConn = null;
|
||||
if (connection == null)
|
||||
{
|
||||
poolConn = await fsql.Ado.MasterPool.GetAsync();
|
||||
connection = poolConn.Value;
|
||||
}
|
||||
try
|
||||
{
|
||||
var droped = false;
|
||||
await fsql.Ado.CommandFluent(state.Item1).WithConnection(connection).WithTransaction(transaction).ExecuteNonQueryAsync();
|
||||
try
|
||||
{
|
||||
await funcBulkCopy(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();
|
||||
droped = true;
|
||||
return affrows;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (droped == false) await fsql.Ado.CommandFluent(state.Item3).WithConnection(connection).WithTransaction(transaction).ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
poolConn?.Dispose();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public abstract partial class UpdateProvider<T1> : UpdateProvider, IUpdate<T1>
|
||||
@ -95,7 +177,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public IUpdate<T1> WithTransaction(DbTransaction transaction)
|
||||
{
|
||||
_transaction = transaction;
|
||||
_connection = _transaction?.Connection;
|
||||
if (transaction != null) _connection = transaction.Connection;
|
||||
return this;
|
||||
}
|
||||
public IUpdate<T1> WithConnection(DbConnection connection)
|
||||
|
Reference in New Issue
Block a user