Add db.Fastest

This commit is contained in:
sunkaixuna 2021-11-17 16:07:14 +08:00
parent 390563bab2
commit d0d7ed4a3f
9 changed files with 186 additions and 1 deletions

View File

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class FastestProvider<T>:IFastest<T>
{
private SqlSugarProvider context;
private ISugarQueryable<T> queryable;
public FastestProvider(SqlSugarProvider sqlSugarProvider)
{
this.context = sqlSugarProvider;
this.queryable = this.context.Queryable<T>();
}
public int BulkCopy(List<T> datas)
{
return BulkCopyAsync(datas).GetAwaiter().GetResult();
}
public async Task<int> BulkCopyAsync(List<T> datas)
{
DataTable tempDataTable = ReflectionInoCore<DataTable>.GetInstance().GetOrCreate("BulkCopyAsync" + typeof(T).FullName,()=> queryable.Where(it=>false).ToDataTable());
var dt = new DataTable();
foreach (DataColumn item in tempDataTable.Columns)
{
dt.Columns.Add(item.ColumnName,item.DataType);
}
var entityInfo = this.context.EntityMaintenance.GetEntityInfo<T>();
dt.TableName =queryable.SqlBuilder.GetTranslationTableName(entityInfo.DbTableName);
var columns = entityInfo.Columns;
foreach (var item in datas)
{
var dr = dt.NewRow();
foreach (var column in columns)
{
if (column.IsIgnore || column.IsOnlyIgnoreInsert)
{
continue;
}
var name = column.DbColumnName;
if (name == null)
{
name = column.PropertyName;
}
var value = ValueConverter(column, PropertyCallAdapterProvider<T>.GetInstance(column.PropertyName).InvokeGet(item));
dr[name] = value;
}
dt.Rows.Add(dr);
}
IFastBuilder buider = new SqlServerFastBuilder();
buider.Context = context;
var result= await buider.ExecuteBulkCopyAsync(dt);
return result;
}
private object ValueConverter(EntityColumnInfo columnInfo,object value)
{
if (value == null)
return value;
if (value is DateTime&&(DateTime)value == DateTime.MinValue)
{
value = Convert.ToDateTime("1900-01-01");
}
return value;
}
}
}

View File

@ -1175,7 +1175,13 @@ namespace SqlSugar
Check.Exception(true,ErrorMessage.GetThrowMessage("Child objects do not support tenant methods, var childDb= Db.GetConnection(confid) ,Db is master ", "Db子对象不支持租户方法请使用主对象,例如var childDb= Db.GetConnection(confid) Db是主对象childDb是子对象 ")); Check.Exception(true,ErrorMessage.GetThrowMessage("Child objects do not support tenant methods, var childDb= Db.GetConnection(confid) ,Db is master ", "Db子对象不支持租户方法请使用主对象,例如var childDb= Db.GetConnection(confid) Db是主对象childDb是子对象 "));
return null; return null;
} }
#endregion
#endregion
#region
public IFastest<T> Fastest<T>()
{
return new FastestProvider<T>(this);
}
#endregion
} }
} }

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public interface IFastBuilder
{
SqlSugarProvider Context { get; set; }
Task<int> ExecuteBulkCopyAsync(DataTable dt);
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public interface IFastest<T>
{
int BulkCopy(List<T> datas);
Task<int> BulkCopyAsync(List<T> datas);
}
}

View File

@ -176,5 +176,10 @@ namespace SqlSugar
#region Cache #region Cache
SugarCacheProvider DataCache { get; } SugarCacheProvider DataCache { get; }
#endregion #endregion
#region Fastest
IFastest<T> Fastest<T>();
#endregion
} }
} }

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SqlServerFastBuilder: IFastBuilder
{
public SqlSugarProvider Context { get; set; }
public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
{
SqlBulkCopy bulkCopy = GetBulkCopyInstance();
bulkCopy.DestinationTableName = dt.TableName;
try
{
await bulkCopy.WriteToServerAsync(dt);
}
catch (Exception ex)
{
CloseDb();
throw ex;
}
CloseDb();
return dt.Rows.Count;
}
public SqlBulkCopy GetBulkCopyInstance()
{
SqlBulkCopy copy;
if (this.Context.Ado.Transaction == null)
{
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection);
}
else
{
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.CheckConstraints, (SqlTransaction)this.Context.Ado.Transaction);
}
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
{
this.Context.Ado.Connection.Open();
}
return copy;
}
public void CloseDb()
{
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
{
this.Context.Ado.Connection.Close();
}
}
}
}

View File

@ -88,7 +88,11 @@
<Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" /> <Compile Include="Abstract\DeleteProvider\SplitTableDeleteProvider.cs" />
<Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" /> <Compile Include="Abstract\EntityMaintenance\EntityMaintenance.cs" />
<Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" /> <Compile Include="Abstract\ExpressionableProvider\Expressionable.cs" />
<Compile Include="Abstract\FastestProvider\FastestProvider.cs" />
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" /> <Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
<Compile Include="Interface\IFastBuilder.cs" />
<Compile Include="Interface\IFastest.cs" />
<Compile Include="Realization\SqlServer\SqlBuilder\SqlServerFastBuilder.cs" />
<Compile Include="SpliteTable\SplitTableAttribute.cs" /> <Compile Include="SpliteTable\SplitTableAttribute.cs" />
<Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" /> <Compile Include="Abstract\InsertableProvider\InsertableProvider.cs" />
<Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" /> <Compile Include="Abstract\DeleteProvider\DeleteableProvider.cs" />

View File

@ -565,6 +565,13 @@ namespace SqlSugar
#endregion #endregion
#region
public IFastest<T> Fastest<T>()
{
return this.Context.Fastest<T>();
}
#endregion
#region More api #region More api
public IContextMethods Utilities { get { return this.Context.Utilities; } set { this.Context.Utilities = value; } } public IContextMethods Utilities { get { return this.Context.Utilities; } set { this.Context.Utilities = value; } }
public AopProvider Aop => this.Context.Aop; public AopProvider Aop => this.Context.Aop;

View File

@ -641,6 +641,10 @@ namespace SqlSugar
return ScopedContext.IsAnyConnection(configId); return ScopedContext.IsAnyConnection(configId);
} }
public IFastest<T> Fastest<T>()
{
return ScopedContext.Fastest<T>();
}
private SqlSugarClient GetContext() private SqlSugarClient GetContext()
{ {
SqlSugarClient result = null; SqlSugarClient result = null;