mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 10:42:52 +08:00
- 增加 ExecutePgCopyAsync/ExecuteSqlBulkCopyAsync 异步方法;
This commit is contained in:
parent
834bdea11f
commit
01c0fbf4f2
@ -110,13 +110,6 @@
|
|||||||
清空状态数据
|
清空状态数据
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
|
|
||||||
<summary>
|
|
||||||
根据 lambda 条件删除数据
|
|
||||||
</summary>
|
|
||||||
<param name="predicate"></param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:FreeSql.DbSet`1.Add(`0)">
|
<member name="M:FreeSql.DbSet`1.Add(`0)">
|
||||||
<summary>
|
<summary>
|
||||||
添加
|
添加
|
||||||
|
@ -34,6 +34,10 @@
|
|||||||
<PackageReference Include="Npgsql.LegacyPostgis" Version="4.0.10" />
|
<PackageReference Include="Npgsql.LegacyPostgis" Version="4.0.10" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<PropertyGroup Condition="'$(TargetFramework)' == 'net452' or '$(TargetFramework)' == 'net451' or '$(TargetFramework)' == 'net45'">
|
||||||
|
<DefineConstants>net45</DefineConstants>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -5,6 +5,7 @@ using System;
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public static partial class FreeSqlPostgreSQLGlobalExtensions
|
public static partial class FreeSqlPostgreSQLGlobalExtensions
|
||||||
{
|
{
|
||||||
@ -28,6 +29,7 @@ public static partial class FreeSqlPostgreSQLGlobalExtensions
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static OnConflictDoUpdate<T1> OnConflictDoUpdate<T1>(this IInsert<T1> that, Expression<Func<T1, object>> columns = null) where T1 : class => new FreeSql.PostgreSQL.Curd.OnConflictDoUpdate<T1>(that.InsertIdentity(), columns);
|
public static OnConflictDoUpdate<T1> OnConflictDoUpdate<T1>(this IInsert<T1> that, Expression<Func<T1, object>> columns = null) where T1 : class => new FreeSql.PostgreSQL.Curd.OnConflictDoUpdate<T1>(that.InsertIdentity(), columns);
|
||||||
|
|
||||||
|
#region ExecutePgCopy
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// PostgreSQL COPY 批量导入功能,封装了 NpgsqlConnection.BeginBinaryImport 方法<para></para>
|
/// PostgreSQL COPY 批量导入功能,封装了 NpgsqlConnection.BeginBinaryImport 方法<para></para>
|
||||||
/// 使用 IgnoreColumns/InsertColumns 设置忽略/指定导入的列<para></para>
|
/// 使用 IgnoreColumns/InsertColumns 设置忽略/指定导入的列<para></para>
|
||||||
@ -48,7 +50,7 @@ public static partial class FreeSqlPostgreSQLGlobalExtensions
|
|||||||
public static void ExecutePgCopy<T>(this IInsert<T> that) where T : class
|
public static void ExecutePgCopy<T>(this IInsert<T> that) where T : class
|
||||||
{
|
{
|
||||||
var insert = that as FreeSql.PostgreSQL.Curd.PostgreSQLInsert<T>;
|
var insert = that as FreeSql.PostgreSQL.Curd.PostgreSQLInsert<T>;
|
||||||
if (insert == null) throw new Exception("ExecuteSqlBulkCopy 是 FreeSql.Provider.PostgreSQL 特有的功能");
|
if (insert == null) throw new Exception("ExecutePgCopy 是 FreeSql.Provider.PostgreSQL 特有的功能");
|
||||||
|
|
||||||
var dt = that.ToDataTable();
|
var dt = that.ToDataTable();
|
||||||
if (dt.Rows.Count == 0) return;
|
if (dt.Rows.Count == 0) return;
|
||||||
@ -106,7 +108,7 @@ public static partial class FreeSqlPostgreSQLGlobalExtensions
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new NotImplementedException("ExecuteCopy 未实现错误,请反馈给作者");
|
throw new NotImplementedException("ExecutePgCopy 未实现错误,请反馈给作者");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@ -114,4 +116,77 @@ public static partial class FreeSqlPostgreSQLGlobalExtensions
|
|||||||
dt.Clear();
|
dt.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if net45
|
||||||
|
#else
|
||||||
|
async public static Task ExecutePgCopyAsync<T>(this IInsert<T> that) where T : class
|
||||||
|
{
|
||||||
|
var insert = that as FreeSql.PostgreSQL.Curd.PostgreSQLInsert<T>;
|
||||||
|
if (insert == null) throw new Exception("ExecutePgCopyAsync 是 FreeSql.Provider.PostgreSQL 特有的功能");
|
||||||
|
|
||||||
|
var dt = that.ToDataTable();
|
||||||
|
if (dt.Rows.Count == 0) return;
|
||||||
|
Func<NpgsqlConnection, Task> binaryImportAsync = async conn =>
|
||||||
|
{
|
||||||
|
var copyFromCommand = new StringBuilder().Append("COPY ").Append(insert.InternalCommonUtils.QuoteSqlName(dt.TableName)).Append("(");
|
||||||
|
var colIndex = 0;
|
||||||
|
foreach (DataColumn col in dt.Columns)
|
||||||
|
{
|
||||||
|
if (colIndex++ > 0) copyFromCommand.Append(", ");
|
||||||
|
copyFromCommand.Append(insert.InternalCommonUtils.QuoteSqlName(col.ColumnName));
|
||||||
|
}
|
||||||
|
copyFromCommand.Append(") FROM STDIN BINARY");
|
||||||
|
using (var writer = conn.BeginBinaryImport(copyFromCommand.ToString()))
|
||||||
|
{
|
||||||
|
foreach (DataRow item in dt.Rows)
|
||||||
|
await writer.WriteRowAsync(System.Threading.CancellationToken.None, item.ItemArray);
|
||||||
|
writer.Complete();
|
||||||
|
}
|
||||||
|
copyFromCommand.Clear();
|
||||||
|
};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (insert.InternalConnection == null && insert.InternalTransaction == null)
|
||||||
|
{
|
||||||
|
using (var conn = await insert.InternalOrm.Ado.MasterPool.GetAsync())
|
||||||
|
{
|
||||||
|
await binaryImportAsync(conn.Value as NpgsqlConnection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (insert.InternalTransaction != null)
|
||||||
|
{
|
||||||
|
await binaryImportAsync(insert.InternalTransaction.Connection as NpgsqlConnection);
|
||||||
|
}
|
||||||
|
else if (insert.InternalConnection != null)
|
||||||
|
{
|
||||||
|
var conn = insert.InternalConnection as NpgsqlConnection;
|
||||||
|
var isNotOpen = false;
|
||||||
|
if (conn.State != System.Data.ConnectionState.Open)
|
||||||
|
{
|
||||||
|
isNotOpen = true;
|
||||||
|
await conn.OpenAsync();
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await binaryImportAsync(conn);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (isNotOpen)
|
||||||
|
await conn.CloseAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new NotImplementedException("ExecutePgCopyAsync 未实现错误,请反馈给作者");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
dt.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ using System;
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public static partial class FreeSqlSqlServerGlobalExtensions
|
public static partial class FreeSqlSqlServerGlobalExtensions
|
||||||
{
|
{
|
||||||
@ -42,6 +43,7 @@ public static partial class FreeSqlSqlServerGlobalExtensions
|
|||||||
}
|
}
|
||||||
internal static ConcurrentDictionary<IFreeSql, (SqlServerLock, Dictionary<Type, bool>)> _dicSetGlobalSelectWithLock = new ConcurrentDictionary<IFreeSql, (SqlServerLock, Dictionary<Type, bool>)>();
|
internal static ConcurrentDictionary<IFreeSql, (SqlServerLock, Dictionary<Type, bool>)> _dicSetGlobalSelectWithLock = new ConcurrentDictionary<IFreeSql, (SqlServerLock, Dictionary<Type, bool>)>();
|
||||||
|
|
||||||
|
#region ExecuteSqlBulkCopy
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// SqlServer SqlCopyBulk 批量插入功能<para></para>
|
/// SqlServer SqlCopyBulk 批量插入功能<para></para>
|
||||||
/// 使用 IgnoreColumns/InsertColumns 设置忽略/指定导入的列<para></para>
|
/// 使用 IgnoreColumns/InsertColumns 设置忽略/指定导入的列<para></para>
|
||||||
@ -135,6 +137,83 @@ public static partial class FreeSqlSqlServerGlobalExtensions
|
|||||||
dt.Clear();
|
dt.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#if net40
|
||||||
|
#else
|
||||||
|
async public static Task ExecuteSqlBulkCopyAsync<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("ExecuteSqlBulkCopyAsync 是 FreeSql.Provider.SqlServer 特有的功能");
|
||||||
|
|
||||||
|
var dt = that.ToDataTable();
|
||||||
|
if (dt.Rows.Count == 0) return;
|
||||||
|
|
||||||
|
Func<SqlBulkCopy, Task> writeToServerAsync = bulkCopy =>
|
||||||
|
{
|
||||||
|
if (batchSize.HasValue) bulkCopy.BatchSize = batchSize.Value;
|
||||||
|
if (bulkCopyTimeout.HasValue) bulkCopy.BulkCopyTimeout = bulkCopyTimeout.Value;
|
||||||
|
bulkCopy.DestinationTableName = dt.TableName;
|
||||||
|
return bulkCopy.WriteToServerAsync(dt);
|
||||||
|
};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (insert.InternalConnection == null && insert.InternalTransaction == null)
|
||||||
|
{
|
||||||
|
using (var conn = await insert.InternalOrm.Ado.MasterPool.GetAsync())
|
||||||
|
{
|
||||||
|
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||||
|
new SqlBulkCopy(conn.Value as SqlConnection) :
|
||||||
|
new SqlBulkCopy(conn.Value as SqlConnection, copyOptions, null))
|
||||||
|
{
|
||||||
|
await writeToServerAsync(bulkCopy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (insert.InternalTransaction != null)
|
||||||
|
{
|
||||||
|
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||||
|
new SqlBulkCopy(insert.InternalTransaction.Connection as SqlConnection) :
|
||||||
|
new SqlBulkCopy(insert.InternalTransaction.Connection as SqlConnection, copyOptions, insert.InternalTransaction as SqlTransaction))
|
||||||
|
{
|
||||||
|
await writeToServerAsync(bulkCopy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (insert.InternalConnection != null)
|
||||||
|
{
|
||||||
|
var conn = insert.InternalConnection as SqlConnection;
|
||||||
|
var isNotOpen = false;
|
||||||
|
if (conn.State != System.Data.ConnectionState.Open)
|
||||||
|
{
|
||||||
|
isNotOpen = true;
|
||||||
|
await conn.OpenAsync();
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var bulkCopy = copyOptions == SqlBulkCopyOptions.Default ?
|
||||||
|
new SqlBulkCopy(conn) :
|
||||||
|
new SqlBulkCopy(conn, copyOptions, null))
|
||||||
|
{
|
||||||
|
await writeToServerAsync(bulkCopy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (isNotOpen)
|
||||||
|
conn.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new NotImplementedException("ExecuteSqlBulkCopyAsync 未实现错误,请反馈给作者");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
dt.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
[Flags]
|
[Flags]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user