Browse Source

Add 达梦 bulkcopy

SqlSugar5
sunkaixuan 2 years ago
parent
commit
2e7e595886
  1. 4
      Src/Asp.Net/DmTest/DmTest.csproj
  2. 2
      Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs
  3. 154
      Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmBlukCopy.cs
  4. 56
      Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmFastBuilder.cs
  5. 2
      Src/Asp.Net/SqlSugar/SqlSugar.csproj

4
Src/Asp.Net/DmTest/DmTest.csproj

@ -33,6 +33,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="DmProvider, Version=1.1.0.0, Culture=neutral, PublicKeyToken=7a2d44aa446c6d01, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\SqlSugar\References\DmProvider.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />

2
Src/Asp.Net/SqlSugar/Abstract/FastestProvider/Private.cs

@ -30,7 +30,7 @@ namespace SqlSugar
resultConnector.CharacterSet = this.CharacterSet;
return resultConnector;
case DbType.Dm:
break;
return new DmFastBuilder();
case DbType.Kdbndp:
break;
case DbType.Oscar:

154
Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmBlukCopy.cs

@ -0,0 +1,154 @@
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 DmBlukCopy
{
internal List<IGrouping<int, DbColumnInfo>> DbColumnInfoList { get; set; }
internal SqlSugarProvider Context { get; set; }
internal ISqlBuilder Builder { get; set; }
internal InsertBuilder InsertBuilder { get; set; }
internal object[] Inserts { get; set; }
public int ExecuteBulkCopy()
{
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
if (Inserts.First().GetType() == typeof(DataTable))
{
return WriteToServer();
}
DataTable dt = GetCopyData();
SqlBulkCopy bulkCopy = GetBulkCopyInstance();
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
try
{
bulkCopy.WriteToServer(dt);
}
catch (Exception ex)
{
CloseDb();
throw ex;
}
CloseDb();
return DbColumnInfoList.Count;
}
public async Task<int> ExecuteBulkCopyAsync()
{
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
if (Inserts.First().GetType() == typeof(DataTable))
{
return WriteToServer();
}
DataTable dt=GetCopyData();
SqlBulkCopy bulkCopy = GetBulkCopyInstance();
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
try
{
await bulkCopy.WriteToServerAsync(dt);
}
catch (Exception ex)
{
CloseDb();
throw ex;
}
CloseDb();
return DbColumnInfoList.Count;
}
private int WriteToServer()
{
var dt = this.Inserts.First() as DataTable;
if (dt == null)
return 0;
Check.Exception(dt.TableName == "Table", "dt.TableName can't be null ");
dt = GetCopyWriteDataTable(dt);
SqlBulkCopy copy = GetBulkCopyInstance();
copy.DestinationTableName = this.Builder.GetTranslationColumnName(dt.TableName);
copy.WriteToServer(dt);
CloseDb();
return dt.Rows.Count;
}
private DataTable GetCopyWriteDataTable(DataTable dt)
{
var result = this.Context.Ado.GetDataTable("select top 0 * from " + this.Builder.GetTranslationColumnName(dt.TableName));
foreach (DataRow item in dt.Rows)
{
DataRow dr= result.NewRow();
foreach (DataColumn column in result.Columns)
{
if (dt.Columns.Cast<DataColumn>().Select(it => it.ColumnName.ToLower()).Contains(column.ColumnName.ToLower()))
{
dr[column.ColumnName] = item[column.ColumnName];
if (dr[column.ColumnName] == null)
{
dr[column.ColumnName] = DBNull.Value;
}
}
}
result.Rows.Add(dr);
}
result.TableName = dt.TableName;
return result;
}
private 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();
}
copy.BulkCopyTimeout = this.Context.Ado.CommandTimeOut;
return copy;
}
private DataTable GetCopyData()
{
var dt = this.Context.Ado.GetDataTable("select top 0 * from " + InsertBuilder.GetTableNameString);
foreach (var rowInfos in DbColumnInfoList)
{
var dr = dt.NewRow();
foreach (var value in rowInfos)
{
if (value.Value != null && UtilMethods.GetUnderType(value.Value.GetType()) == UtilConstants.DateType)
{
if (value.Value != null && value.Value.ToString() == DateTime.MinValue.ToString())
{
value.Value = Convert.ToDateTime("1753/01/01");
}
}
if (value.Value == null)
{
value.Value = DBNull.Value;
}
dr[value.DbColumnName] = value.Value;
}
dt.Rows.Add(dr);
}
return dt;
}
private void CloseDb()
{
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
{
this.Context.Ado.Connection.Close();
}
}
}
}

56
Src/Asp.Net/SqlSugar/Realization/Dm/SqlBuilder/DmFastBuilder.cs

@ -0,0 +1,56 @@
using Dm;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class DmFastBuilder:FastBuilder,IFastBuilder
{
public override bool IsActionUpdateColumns { get; set; } = true;
public override DbFastestProperties DbFastestProperties { get; set; } = new DbFastestProperties() {
HasOffsetTime=true
};
public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
{
DmBulkCopy bulkCopy = GetBulkCopyInstance();
bulkCopy.DestinationTableName = dt.TableName;
try
{
bulkCopy.WriteToServer(dt);
await Task.Delay(0);//No Support Async
}
catch (Exception ex)
{
CloseDb();
throw ex;
}
CloseDb();
return dt.Rows.Count;
}
public DmBulkCopy GetBulkCopyInstance()
{
DmBulkCopy copy;
if (this.Context.Ado.Transaction == null)
{
copy = new DmBulkCopy((DmConnection)this.Context.Ado.Connection);
}
else
{
copy = new DmBulkCopy((DmConnection)this.Context.Ado.Connection, DmBulkCopyOptions.Default, (DmTransaction)this.Context.Ado.Transaction);
}
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
{
this.Context.Ado.Connection.Open();
}
copy.BulkCopyTimeout = this.Context.Ado.CommandTimeOut;
return copy;
}
}
}

2
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -200,6 +200,8 @@
<Compile Include="Json2Sql\Queryable\QueryableProvider.cs" />
<Compile Include="Json2Sql\Utils\Json2SqlConfig.cs" />
<Compile Include="Json2Sql\Utils\Json2SqlHelper.cs" />
<Compile Include="Realization\Dm\SqlBuilder\DmBlukCopy.cs" />
<Compile Include="Realization\Dm\SqlBuilder\DmFastBuilder.cs" />
<Compile Include="SugarUnitOfWork.cs" />
<Compile Include="Entities\SugarAbMapping.cs" />
<Compile Include="Entities\JoinMapper.cs" />

Loading…
Cancel
Save