Browse Source

db.Storageable support DataTable

pull/10/MERGE
sunkaixuna 3 years ago
parent
commit
8c46980cc3
  1. 182
      Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/StorageableDataTable.cs
  2. 12
      Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs
  3. 2
      Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs
  4. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj
  5. 5
      Src/Asp.Net/SqlSugar/SqlSugarClient.cs
  6. 5
      Src/Asp.Net/SqlSugar/SqlSugarScope.cs

182
Src/Asp.Net/SqlSugar/Abstract/SaveableProvider/StorageableDataTable.cs

@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class StorageableDataTable
{
internal DataTable DataTable { get; set; }
internal SqlSugarProvider Context { get; set; }
internal string[] Columns { get; set; } = new string[] { };
internal string SugarGroupId = "SugarGroupId";
internal string SugarUpdateRows = "SugarUpdateRows";
internal string SugarColumns = "SugarColumns";
internal string SugarErrorMessage = "SugarErrorMessage";
internal List<DataRow> dbDataList = new List<DataRow>();
List<KeyValuePair<StorageType, Func<DataRow, bool>,string>> whereFuncs = new List<KeyValuePair<StorageType, Func<DataRow, bool>,string>>();
public StorageableDataTable WhereColumns(string name)
{
return WhereColumns(new string[] { name});
}
public StorageableDataTable WhereColumns(string[] names)
{
this.Columns = names;
var queryable = this.Context.Queryable<object>();
Check.Exception(Columns==null|| Columns.Length==0,"need WhereColums");
var tableName = queryable.SqlBuilder.GetTranslationTableName(DataTable.TableName);
this.Context.Utilities.PageEach(DataTable.Rows.Cast<DataRow>(), 200, itemList =>
{
List<IConditionalModel> conditList = new List<IConditionalModel>();
SetConditList(itemList, Columns, conditList);
var addItem = this.Context.Queryable<object>().AS(tableName).Where(conditList).ToDataTable().Rows.Cast<DataRow>().ToList();
this.dbDataList.AddRange(addItem);
});
return this;
}
public StorageableDataTable WhereColumns(List<string> names)
{
return WhereColumns(names.ToArray());
}
public StorageableDataTable SplitInsert(Func<DataRow, bool> conditions, string message = null)
{
whereFuncs.Add(new KeyValuePair<StorageType, Func<DataRow, bool>, string>(StorageType.Insert, conditions,message));
return this;
}
public StorageableDataTable SplitDelete(Func<DataRow, bool> conditions, string message = null)
{
whereFuncs.Add(new KeyValuePair<StorageType, Func<DataRow, bool>,string>(StorageType.Delete, conditions,message));
return this;
}
public StorageableDataTable SplitUpdate(Func<DataRow, bool> conditions, string message = null)
{
whereFuncs.Add(new KeyValuePair<StorageType, Func<DataRow, bool>,string>(StorageType.Update, conditions,message));
return this;
}
public StorageableDataTable Saveable(string inserMessage = null, string updateMessage = null)
{
SplitUpdate(it => it.Any(), updateMessage);
SplitInsert(it => it.Any(), inserMessage);
return this;
}
public StorageableDataTable SplitError(Func<DataRow, bool> conditions, string message = null)
{
whereFuncs.Add(new KeyValuePair<StorageType, Func<DataRow, bool>, string>(StorageType.Error, conditions, message));
return this;
}
public StorageableDataTable SplitIgnore(Func<DataRow, bool> conditions, string message = null)
{
whereFuncs.Add(new KeyValuePair<StorageType, Func<DataRow, bool>, string>(StorageType.Ignore, conditions, message));
return this;
}
public DataTableResult ToStorage()
{
foreach (DataRow row in DataTable.Rows)
{
foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key))
{
SplitMethod(item.value1,item.key,row,item.value2);
}
}
DataTable.Columns.Remove(SugarUpdateRows);
DataTable.Columns.Remove(SugarColumns);
var Groups=DataTable.Rows.Cast<DataRow>()
.GroupBy(it => ((StorageType)it[SugarGroupId]).ToString()).Select(it=>new DataTableGroups{ Type=it.Key,DataTable= it.CopyToDataTable() })
.ToList();
DataTable.Columns.Remove(SugarGroupId);
DataTable.Columns.Remove(SugarErrorMessage);
DataTableResult result = new DataTableResult()
{
DataTableGroups=Groups,
AsDeleteable=this.Context.Deleteable<object>().AS(DataTable.TableName).Where(""),
AsUpdateable= this.Context.Updateable(new List<Dictionary<string,object>>()).AS(DataTable.TableName).WhereColumns(Columns),
AsInsertable=this.Context.Insertable(new List<Dictionary<string, object>>()).AS(DataTable.TableName)
};
return result;
}
private void SplitMethod(Func<DataRow, bool> conditions, StorageType type,DataRow item,string message)
{
item[SugarColumns] = Columns;
item[SugarUpdateRows] = dbDataList;
if ((item[SugarGroupId]==null|| item[SugarGroupId] == DBNull.Value) && conditions(item))
{
item[SugarGroupId] = type;
item[SugarErrorMessage] = message;
}
}
private void SetConditList(List<DataRow> itemList, string[] whereColumns, List<IConditionalModel> conditList)
{
;
foreach (var dataItem in itemList)
{
var condition = new ConditionalCollections()
{
ConditionalList = new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()
};
conditList.Add(condition);
int i = 0;
foreach (var name in whereColumns)
{
var value = dataItem[name];
if (value != null && value.GetType().IsEnum())
{
value = Convert.ToInt64(value);
}
condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(i == 0 ? WhereType.Or : WhereType.And, new ConditionalModel()
{
FieldName = name,
ConditionalType = ConditionalType.Equal,
FieldValue = value + "",
FieldValueConvertFunc = this.Context.CurrentConnectionConfig.DbType == DbType.PostgreSQL ?
UtilMethods.GetTypeConvert(value) : null
}));
++i;
}
}
}
}
public class DataTableResult
{
public List<DataTableGroups> DataTableGroups { get; set; }
public IUpdateable<Dictionary<string, object>> AsUpdateable { get; set; }
public IDeleteable<object> AsDeleteable { get; set; }
public IInsertable<Dictionary<string, object>> AsInsertable { get; set; }
}
public class DataTableGroups
{
public string Type { get; set; }
public DataTable DataTable { get; set; }
}
public static class StorageableDataTableExtensions
{
public static bool Any(this DataRow row)
{
var list=row["SugarUpdateRows"] as List<DataRow>;
var columns = row["SugarColumns"] as string[];
return list.Any(it =>
{
var result = true;
foreach (var name in columns)
{
if (result)
{
result = row[name].ObjToString() == it[name].ObjToString();
if (result == false)
{
break;
}
}
}
return result;
});
}
}
}

12
Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs

@ -765,6 +765,18 @@ namespace SqlSugar
{
return Storageable(new List<T>() { data });
}
public StorageableDataTable Storageable(DataTable data)
{
var result = new StorageableDataTable();
Check.Exception(data.TableName.IsNullOrEmpty() || data.TableName == "Table",ErrorMessage.GetThrowMessage( "DataTable data.TableName is null", "参数DataTable没有设置TableName ,参数.TableName=表名"));
result.DataTable = data;
result.Context = this;
data.Columns.Add(new DataColumn("SugarGroupId", typeof(StorageType)));
data.Columns.Add(new DataColumn("SugarUpdateRows", typeof(List<DataRow>)));
data.Columns.Add(new DataColumn("SugarErrorMessage", typeof(string)));
data.Columns.Add(new DataColumn("SugarColumns", typeof(string[])));
return result;
}
#endregion
#region Reportable

2
Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq.Expressions;
using System.Threading.Tasks;
@ -122,6 +123,7 @@ namespace SqlSugar
#region Saveable
IStorageable<T> Storageable<T>(List<T> dataList) where T : class, new();
IStorageable<T> Storageable<T>(T data) where T : class, new();
StorageableDataTable Storageable(DataTable data);
[Obsolete("use Storageable")]
ISaveable<T> Saveable<T>(List<T> saveObjects) where T : class, new();
[Obsolete("use Storageable")]

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -94,6 +94,7 @@
<Compile Include="Abstract\FastestProvider\Setting.cs" />
<Compile Include="Abstract\FastestProvider\SplitFastest.cs" />
<Compile Include="Abstract\FilterProvider\FilterProvider.cs" />
<Compile Include="Abstract\SaveableProvider\StorageableDataTable.cs" />
<Compile Include="Abstract\SugarProvider\SqlSugarCoreProvider.cs" />
<Compile Include="Interface\IFastBuilder.cs" />
<Compile Include="Interface\IFastest.cs" />

5
Src/Asp.Net/SqlSugar/SqlSugarClient.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
@ -345,6 +346,10 @@ namespace SqlSugar
#endregion
#region Saveable
public StorageableDataTable Storageable(DataTable data)
{
return this.Context.Storageable(data);
}
public IStorageable<T> Storageable<T>(List<T> dataList) where T : class, new()
{

5
Src/Asp.Net/SqlSugar/SqlSugarScope.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
@ -539,6 +540,10 @@ namespace SqlSugar
{
return ScopedContext.Storageable(data);
}
public StorageableDataTable Storageable(DataTable data)
{
return ScopedContext.Storageable(data);
}
public ISugarQueryable<T> Union<T>(List<ISugarQueryable<T>> queryables) where T : class, new()
{

Loading…
Cancel
Save