mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Update Core
This commit is contained in:
parent
209ffea3ff
commit
642955a948
@ -229,7 +229,11 @@ namespace SqlSugar
|
||||
{
|
||||
method = isNullableType ? getConvertByte : getByte;
|
||||
}
|
||||
else if (bindPropertyType == UtilConstants.StringType&&dbTypeName?.ToLower()== "timestamp")
|
||||
else if (bindPropertyType == UtilConstants.StringType && dbTypeName?.ToLower() == "timestamp")
|
||||
{
|
||||
method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
|
||||
}
|
||||
else if (dbTypeName.EqualCase("STRING"))
|
||||
{
|
||||
method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
|
||||
}
|
||||
|
@ -254,15 +254,44 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
|
||||
public IDeleteable<T> WhereColumns(Expression<Func<T, object>> columns)
|
||||
public IDeleteable<T> WhereColumns(List<T> list,Expression<Func<T, object>> columns)
|
||||
{
|
||||
if (columns != null)
|
||||
if (this.GetPrimaryKeys().IsNullOrEmpty())
|
||||
{
|
||||
tempPrimaryKeys = DeleteBuilder.GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => this.SqlBuilder.GetNoTranslationColumnName(it)).ToList();
|
||||
}
|
||||
this.Where(list);
|
||||
if (columns != null&& tempPrimaryKeys.IsNullOrEmpty())
|
||||
{
|
||||
tempPrimaryKeys = DeleteBuilder.GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => this.SqlBuilder.GetNoTranslationColumnName(it)).ToList();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public IDeleteable<T> WhereColumns(List<Dictionary<string, object>> list)
|
||||
{
|
||||
List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
|
||||
foreach (var model in list)
|
||||
{
|
||||
int i = 0;
|
||||
var clist = new List<KeyValuePair<WhereType, ConditionalModel>>();
|
||||
foreach (var item in model.Keys)
|
||||
{
|
||||
clist.Add(new KeyValuePair<WhereType, ConditionalModel>(i == 0 ? WhereType.Or : WhereType.And, new ConditionalModel()
|
||||
{
|
||||
FieldName =item,
|
||||
ConditionalType = ConditionalType.Equal,
|
||||
FieldValue = model[item].ObjToString(),
|
||||
CSharpTypeName = model[item]==null?null : model[item].GetType().Name
|
||||
}));
|
||||
i++;
|
||||
}
|
||||
conditionalModels.Add(new ConditionalCollections()
|
||||
{
|
||||
ConditionalList = clist
|
||||
});
|
||||
}
|
||||
return this.Where(conditionalModels);
|
||||
}
|
||||
public IDeleteable<T> RemoveDataCache()
|
||||
{
|
||||
this.RemoveCacheFunc = () =>
|
||||
|
@ -24,6 +24,11 @@ namespace SqlSugar
|
||||
{
|
||||
return BulkCopyAsync(tableName,dt).ConfigureAwait(true).GetAwaiter().GetResult();
|
||||
}
|
||||
public int BulkCopy(DataTable dt)
|
||||
{
|
||||
Check.ExceptionEasy(this.AsName.IsNullOrEmpty(), "need .AS(tablaeName) ", "需要 .AS(tablaeName) 设置表名");
|
||||
return BulkCopyAsync(this.AsName, dt).ConfigureAwait(true).GetAwaiter().GetResult();
|
||||
}
|
||||
public async Task<int> BulkCopyAsync(string tableName, DataTable dt)
|
||||
{
|
||||
if (Size > 0)
|
||||
@ -99,6 +104,17 @@ namespace SqlSugar
|
||||
{
|
||||
return BulkUpdateAsync(tableName,dataTable, whereColumns, updateColumns).ConfigureAwait(true).GetAwaiter().GetResult();
|
||||
}
|
||||
public int BulkUpdate(DataTable dataTable, string[] whereColumns, string[] updateColumns)
|
||||
{
|
||||
Check.ExceptionEasy(this.AsName.IsNullOrEmpty(), "need .AS(tablaeName) ", "需要 .AS(tablaeName) 设置表名");
|
||||
return BulkUpdateAsync(this.AsName, dataTable, whereColumns, updateColumns).ConfigureAwait(true).GetAwaiter().GetResult();
|
||||
}
|
||||
public int BulkUpdate(DataTable dataTable, string[] whereColumns)
|
||||
{
|
||||
string[] updateColumns = dataTable.Columns.Cast<DataColumn>().Select(it => it.ColumnName).Where(it => !whereColumns.Any(z => z.EqualCase(it))).ToArray();
|
||||
Check.ExceptionEasy(this.AsName.IsNullOrEmpty(), "need .AS(tablaeName) ", "需要 .AS(tablaeName) 设置表名");
|
||||
return BulkUpdateAsync(this.AsName, dataTable, whereColumns, updateColumns).ConfigureAwait(true).GetAwaiter().GetResult();
|
||||
}
|
||||
public async Task<int> BulkUpdateAsync(string tableName, DataTable dataTable, string[] whereColumns, string[] updateColumns)
|
||||
{
|
||||
|
||||
|
@ -0,0 +1,180 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
internal class MappingFieldsHelper<T>
|
||||
{
|
||||
public SqlSugarProvider Context { get; set; }
|
||||
public EntityInfo NavEntity { get; set; }
|
||||
public EntityInfo RootEntity { get; set; }
|
||||
|
||||
public MappingFieldsInfo GetMappings(Expression thisFiled, Expression mappingFiled)
|
||||
{
|
||||
MappingFieldsInfo mappingFields=new MappingFieldsInfo();
|
||||
var pkName = "";
|
||||
if ((mappingFiled as LambdaExpression).Body is UnaryExpression)
|
||||
{
|
||||
pkName = (((mappingFiled as LambdaExpression).Body as UnaryExpression).Operand as MemberExpression).Member.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
pkName = ((mappingFiled as LambdaExpression).Body as MemberExpression).Member.Name;
|
||||
}
|
||||
return mappingFields;
|
||||
}
|
||||
|
||||
public List<IConditionalModel> GetMppingSql(List<T> list, List<MappingFieldsExpression> mappingFieldsExpressions)
|
||||
{
|
||||
List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
|
||||
foreach (var model in list)
|
||||
{
|
||||
var clist = new List<KeyValuePair<WhereType, ConditionalModel>>();
|
||||
var i = 0;
|
||||
foreach (var item in mappingFieldsExpressions)
|
||||
{
|
||||
InitMappingFieldsExpression(item);
|
||||
clist.Add(new KeyValuePair<WhereType, ConditionalModel>(i==0?WhereType.Or: WhereType.And, new ConditionalModel()
|
||||
{
|
||||
FieldName = item.LeftEntityColumn.DbColumnName,
|
||||
ConditionalType = ConditionalType.Equal,
|
||||
FieldValue = item.RightEntityColumn.PropertyInfo.GetValue(model).ObjToString(),
|
||||
CSharpTypeName = item.RightEntityColumn.PropertyInfo.PropertyType.Name
|
||||
}));
|
||||
i++;
|
||||
}
|
||||
conditionalModels.Add(new ConditionalCollections() {
|
||||
ConditionalList= clist
|
||||
});
|
||||
}
|
||||
return conditionalModels;
|
||||
}
|
||||
|
||||
public void SetChildList(EntityColumnInfo navColumnInfo,object item,List<object> list, List<MappingFieldsExpression> mappingFieldsExpressions)
|
||||
{
|
||||
if (item != null)
|
||||
{
|
||||
//var expable =Expressionable.Create<object>();
|
||||
foreach (var field in mappingFieldsExpressions)
|
||||
{
|
||||
InitMappingFieldsExpression(field);
|
||||
}
|
||||
var setList =new List<object>();
|
||||
var count = mappingFieldsExpressions.Count;
|
||||
if (count == 1)
|
||||
{
|
||||
setList=list.Where(it => GetWhereByIndex(item, mappingFieldsExpressions, it, 0)).ToList();
|
||||
}
|
||||
else if (count == 2)
|
||||
{
|
||||
setList = list.Where(it =>
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 0) &&
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 1)
|
||||
).ToList();
|
||||
}
|
||||
else if (count == 3)
|
||||
{
|
||||
setList = list.Where(it =>
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 0) &&
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 1) &&
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 2)
|
||||
).ToList();
|
||||
}
|
||||
else if (count == 4)
|
||||
{
|
||||
setList = list.Where(it =>
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 0) &&
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 1) &&
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 2) &&
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 3)
|
||||
).ToList();
|
||||
}
|
||||
else if (count == 5)
|
||||
{
|
||||
setList = list.Where(it =>
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 0) &&
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 1) &&
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 2) &&
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 3) &&
|
||||
GetWhereByIndex(item, mappingFieldsExpressions, it, 4)
|
||||
).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
Check.ExceptionEasy("MappingField max value is 5", "MappingField最大数量不能超过5");
|
||||
}
|
||||
//navColumnInfo.PropertyInfo.SetValue();
|
||||
var instance = Activator.CreateInstance(navColumnInfo.PropertyInfo.PropertyType, true);
|
||||
var ilist = instance as IList;
|
||||
foreach (var value in setList)
|
||||
{
|
||||
ilist.Add(value);
|
||||
}
|
||||
navColumnInfo.PropertyInfo.SetValue(item,ilist);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool GetWhereByIndex(object item, List<MappingFieldsExpression> mappingFieldsExpressions, object it,int index)
|
||||
{
|
||||
var left = mappingFieldsExpressions[index].LeftEntityColumn.PropertyInfo.GetValue(it).ObjToString();
|
||||
var right= mappingFieldsExpressions[index].RightEntityColumn.PropertyInfo.GetValue(item).ObjToString(); ;
|
||||
return left == right;
|
||||
}
|
||||
|
||||
private void InitMappingFieldsExpression(MappingFieldsExpression item)
|
||||
{
|
||||
var leftName = item.LeftName;
|
||||
var rightName = item.RightName;
|
||||
if (item.LeftEntityColumn == null)
|
||||
{
|
||||
item.LeftEntityColumn = this.NavEntity.Columns.FirstOrDefault(it => it.PropertyName == leftName);
|
||||
}
|
||||
if (item.RightEntityColumn == null)
|
||||
{
|
||||
item.RightEntityColumn = this.RootEntity.Columns.FirstOrDefault(it => it.PropertyName == rightName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public class MappingFieldsInfo
|
||||
{
|
||||
public DbColumnInfo LeftColumn { get; set; }
|
||||
public DbColumnInfo RightColumn { get; set; }
|
||||
}
|
||||
public class MappingFieldsExpression
|
||||
{
|
||||
public Expression LeftColumnExpression { get; set; }
|
||||
public Expression RightColumnExpression { get; set; }
|
||||
public EntityColumnInfo LeftEntityColumn { get; set; }
|
||||
public EntityColumnInfo RightEntityColumn { get; set; }
|
||||
private string _LeftName;
|
||||
public string LeftName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_LeftName == null)
|
||||
{
|
||||
_LeftName = ExpressionTool.GetMemberName(this.LeftColumnExpression);
|
||||
}
|
||||
return _LeftName;
|
||||
}
|
||||
}
|
||||
private string _RightName;
|
||||
public string RightName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_RightName == null)
|
||||
{
|
||||
_RightName = ExpressionTool.GetMemberName(this.RightColumnExpression);
|
||||
}
|
||||
return _RightName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -165,6 +165,10 @@ namespace SqlSugar
|
||||
{
|
||||
OneToOne(list, selector, listItemEntity, navObjectNamePropety, navObjectNameColumnInfo);
|
||||
}
|
||||
else if (navObjectNameColumnInfo.Navigat.NavigatType == NavigateType.Dynamic)
|
||||
{
|
||||
Dynamic(list, selector, listItemEntity, navObjectNamePropety, navObjectNameColumnInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
ManyToMany(list, selector, listItemEntity, navObjectNamePropety, navObjectNameColumnInfo);
|
||||
@ -236,7 +240,17 @@ namespace SqlSugar
|
||||
ilist.Add(bInfo);
|
||||
}
|
||||
}
|
||||
navObjectNamePropety.SetValue(listItem, instance);
|
||||
if (sql.MappingExpressions.HasValue())
|
||||
{
|
||||
MappingFieldsHelper<T> helper = new MappingFieldsHelper<T>();
|
||||
helper.NavEntity = bEntityInfo;
|
||||
helper.RootEntity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
helper.SetChildList(navObjectNameColumnInfo, listItem, ilist.Cast<object>().ToList(), sql.MappingExpressions);
|
||||
}
|
||||
else
|
||||
{
|
||||
navObjectNamePropety.SetValue(listItem, instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -300,18 +314,54 @@ namespace SqlSugar
|
||||
{
|
||||
var setValue = navList
|
||||
.Where(x => navColumn.PropertyInfo.GetValue(x).ObjToString() == listItemPkColumn.PropertyInfo.GetValue(item).ObjToString()).ToList();
|
||||
var instance = Activator.CreateInstance(navObjectNamePropety.PropertyType, true);
|
||||
var ilist = instance as IList;
|
||||
foreach (var value in setValue)
|
||||
|
||||
if (sqlObj.MappingExpressions.HasValue())
|
||||
{
|
||||
ilist.Add(value);
|
||||
MappingFieldsHelper<T> helper = new MappingFieldsHelper<T>();
|
||||
helper.NavEntity = navEntityInfo;
|
||||
helper.RootEntity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
helper.SetChildList(navObjectNameColumnInfo, item,setValue,sqlObj.MappingExpressions);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
var instance = Activator.CreateInstance(navObjectNamePropety.PropertyType, true);
|
||||
var ilist = instance as IList;
|
||||
foreach (var value in setValue)
|
||||
{
|
||||
ilist.Add(value);
|
||||
}
|
||||
navObjectNamePropety.SetValue(item, instance);
|
||||
}
|
||||
navObjectNamePropety.SetValue(item, instance);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Dynamic(List<object> list, Func<ISugarQueryable<object>, List<object>> selector, EntityInfo listItemEntity, System.Reflection.PropertyInfo navObjectNamePropety, EntityColumnInfo navObjectNameColumnInfo)
|
||||
{
|
||||
var navEntity = navObjectNameColumnInfo.PropertyInfo.PropertyType.GetGenericArguments()[0];
|
||||
var navEntityInfo = this.Context.EntityMaintenance.GetEntityInfo(navEntity);
|
||||
var sqlObj = GetWhereSql(navObjectNameColumnInfo.Navigat.Name);
|
||||
Check.ExceptionEasy(sqlObj.MappingExpressions.IsNullOrEmpty(), $"Dynamic need MappingField ,Demo: Includes(it => it.Books.MappingField(z=>z.studenId,()=>it.StudentId).ToList())", $"自定义映射需要 MappingFields ,例子: Includes(it => it.Books.MappingFields(z=>z.studenId,()=>it.StudentId).ToList())");
|
||||
if (list.Any() && navObjectNamePropety.GetValue(list.First()) == null)
|
||||
{
|
||||
MappingFieldsHelper<T> helper = new MappingFieldsHelper<T>();
|
||||
helper.Context = this.Context;
|
||||
helper.NavEntity = navEntityInfo;
|
||||
helper.RootEntity = this.Context.EntityMaintenance.GetEntityInfo<T>();
|
||||
var whereSql = helper.GetMppingSql(RootList,sqlObj.MappingExpressions);
|
||||
var navList = selector(this.Context.Queryable<object>().AS(navEntityInfo.DbTableName).AddParameters(sqlObj.Parameters).Where(whereSql,true).WhereIF(sqlObj.WhereString.HasValue(), sqlObj.WhereString).Select(sqlObj.SelectString).OrderByIF(sqlObj.OrderByString.HasValue(), sqlObj.OrderByString));
|
||||
if (navList.HasValue())
|
||||
{
|
||||
foreach (var item in list)
|
||||
{
|
||||
helper.SetChildList(navObjectNameColumnInfo, item, navList, sqlObj.MappingExpressions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private SqlInfo GetWhereSql(string properyName=null)
|
||||
{
|
||||
if (_ListCallFunc == null|| _ListCallFunc.Count==0) return new SqlInfo();
|
||||
@ -327,6 +377,7 @@ namespace SqlSugar
|
||||
var queryable = this.Context.Queryable<object>();
|
||||
if (method.Method.Name == "Where")
|
||||
{
|
||||
CheckHasRootShortName(method.Arguments[0], method.Arguments[1]);
|
||||
var exp = method.Arguments[1];
|
||||
where.Add(" " +queryable.QueryBuilder.GetExpressionValue(exp, ResolveExpressType.WhereSingle).GetString());
|
||||
}
|
||||
@ -336,6 +387,7 @@ namespace SqlSugar
|
||||
if (isOk.ObjToBool())
|
||||
{
|
||||
var exp = method.Arguments[2];
|
||||
CheckHasRootShortName(method.Arguments[1], method.Arguments[2]);
|
||||
where.Add(" " + queryable.QueryBuilder.GetExpressionValue(exp, ResolveExpressType.WhereSingle).GetString());
|
||||
}
|
||||
}
|
||||
@ -344,6 +396,16 @@ namespace SqlSugar
|
||||
var exp = method.Arguments[1];
|
||||
oredrBy.Add(" " + queryable.QueryBuilder.GetExpressionValue(exp, ResolveExpressType.WhereSingle).GetString());
|
||||
}
|
||||
else if (method.Method.Name == "MappingField")
|
||||
{
|
||||
if (result.MappingExpressions == null)
|
||||
result.MappingExpressions = new List<MappingFieldsExpression>();
|
||||
result.MappingExpressions.Add(new MappingFieldsExpression()
|
||||
{
|
||||
LeftColumnExpression = method.Arguments[1],
|
||||
RightColumnExpression = method.Arguments[2]
|
||||
});
|
||||
}
|
||||
else if (method.Method.Name == "Select")
|
||||
{
|
||||
var exp = method.Arguments[1];
|
||||
@ -370,7 +432,7 @@ namespace SqlSugar
|
||||
foreach (var nav in entityInfo.Columns.Where(x => x.Navigat != null&&x.Navigat.NavigatType==NavigateType.OneToOne))
|
||||
{
|
||||
var navColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyName == nav.Navigat.Name);
|
||||
if (navColumn != null)
|
||||
if (navColumn != null)
|
||||
{
|
||||
AppColumns(result, queryable, navColumn.DbColumnName);
|
||||
}
|
||||
@ -405,17 +467,17 @@ namespace SqlSugar
|
||||
}
|
||||
if (where.Any())
|
||||
{
|
||||
Check.Exception(isList == false, $"{_ListCallFunc.First()} need is ToList()", $"{_ListCallFunc.First()} 需要ToList");
|
||||
Check.ExceptionEasy(isList == false, $"{_ListCallFunc.First()} need is ToList()", $"{_ListCallFunc.First()} 需要ToList");
|
||||
result.WhereString= String.Join(" AND ", where);
|
||||
}
|
||||
if (oredrBy.Any())
|
||||
{
|
||||
Check.Exception(isList == false, $"{_ListCallFunc.First()} need is ToList()", $"{_ListCallFunc.First()} 需要ToList");
|
||||
Check.ExceptionEasy(isList == false, $"{_ListCallFunc.First()} need is ToList()", $"{_ListCallFunc.First()} 需要ToList");
|
||||
result.OrderByString = String.Join(" , ", oredrBy);
|
||||
}
|
||||
if (result.SelectString.HasValue())
|
||||
{
|
||||
Check.Exception(isList == false, $"{_ListCallFunc.First()} need is ToList()", $"{_ListCallFunc.First()} 需要ToList");
|
||||
Check.ExceptionEasy(isList == false, $"{_ListCallFunc.First()} need is ToList()", $"{_ListCallFunc.First()} 需要ToList");
|
||||
result.OrderByString = String.Join(" , ", oredrBy);
|
||||
}
|
||||
return result;
|
||||
@ -429,6 +491,31 @@ namespace SqlSugar
|
||||
result.SelectString = result.SelectString + "," + selectPkName;
|
||||
}
|
||||
}
|
||||
private void CheckHasRootShortName(Expression rootExpression, Expression childExpression)
|
||||
{
|
||||
var rootShortName = GetShortName(rootExpression);
|
||||
if (rootShortName.HasValue()&& childExpression.ToString().Contains($"{rootShortName}."))
|
||||
{
|
||||
Check.ExceptionEasy($".Where({childExpression}) no support {rootShortName}.Field, Use .MappingField",$".Where({childExpression})禁止出{rootShortName}.字段 , 你可以使用.MappingField(z=>z.字段,()=>{rootShortName}.字段) 与主表字段进行过滤");
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetShortName(Expression expression1)
|
||||
{
|
||||
string shortName = null;
|
||||
if (expression1 is MemberExpression)
|
||||
{
|
||||
var shortNameExpression = (expression1 as MemberExpression).Expression;
|
||||
if (shortNameExpression != null && shortNameExpression.Type == typeof(T))
|
||||
{
|
||||
if (shortNameExpression is ParameterExpression)
|
||||
{
|
||||
shortName = (shortNameExpression as ParameterExpression).Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
return shortName;
|
||||
}
|
||||
|
||||
public class SqlInfo
|
||||
{
|
||||
@ -436,6 +523,7 @@ namespace SqlSugar
|
||||
public string OrderByString { get; set; }
|
||||
public string SelectString { get; set; }
|
||||
public List<SugarParameter> Parameters { get; set; }
|
||||
public List<MappingFieldsExpression> MappingExpressions { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -380,6 +380,31 @@ namespace SqlSugar
|
||||
_WhereClassByPrimaryKey(new List<T>() { data });
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> WhereColumns(List<Dictionary<string, object>> list)
|
||||
{
|
||||
List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
|
||||
foreach (var model in list)
|
||||
{
|
||||
int i = 0;
|
||||
var clist = new List<KeyValuePair<WhereType, ConditionalModel>>();
|
||||
foreach (var item in model.Keys)
|
||||
{
|
||||
clist.Add(new KeyValuePair<WhereType, ConditionalModel>(i == 0 ? WhereType.Or : WhereType.And, new ConditionalModel()
|
||||
{
|
||||
FieldName = item,
|
||||
ConditionalType = ConditionalType.Equal,
|
||||
FieldValue = model[item].ObjToString(),
|
||||
CSharpTypeName = model[item] == null ? null : model[item].GetType().Name
|
||||
}));
|
||||
i++;
|
||||
}
|
||||
conditionalModels.Add(new ConditionalCollections()
|
||||
{
|
||||
ConditionalList = clist
|
||||
});
|
||||
}
|
||||
return this.Where(conditionalModels);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// if a property that is primary key is a condition
|
||||
@ -536,7 +561,19 @@ namespace SqlSugar
|
||||
var sqlObj = this.SqlBuilder.ConditionalModelToSql(conditionalModels,0);
|
||||
return this.Where(sqlObj.Key, sqlObj.Value);
|
||||
}
|
||||
|
||||
public ISugarQueryable<T> Where(List<IConditionalModel> conditionalModels, bool isWrap)
|
||||
{
|
||||
if (conditionalModels.IsNullOrEmpty()) return this;
|
||||
var sqlObj = this.SqlBuilder.ConditionalModelToSql(conditionalModels, 0);
|
||||
if (isWrap)
|
||||
{
|
||||
return this.Where("("+sqlObj.Key+")", sqlObj.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.Where(sqlObj.Key, sqlObj.Value);
|
||||
}
|
||||
}
|
||||
public virtual ISugarQueryable<T> Where<T2>(string whereString, object whereObj = null)
|
||||
{
|
||||
var whereValue = QueryBuilder.WhereInfos;
|
||||
@ -1856,7 +1893,7 @@ namespace SqlSugar
|
||||
public Task<List<T>> ToPageListAsync(int pageNumber, int pageSize, RefAsync<int> totalNumber, RefAsync<int> totalPage)
|
||||
{
|
||||
var result = ToPageListAsync(pageNumber, pageSize, totalNumber);
|
||||
totalPage = (totalNumber + pageSize - 1) / pageSize;
|
||||
totalPage.Value = (totalNumber + pageSize - 1) / pageSize;
|
||||
return result;
|
||||
}
|
||||
public async Task<string> ToJsonAsync()
|
||||
@ -2332,6 +2369,13 @@ namespace SqlSugar
|
||||
_InitNavigat(result);
|
||||
return result;
|
||||
}
|
||||
private async Task _InitNavigatAsync<TResult>(List<TResult> result)
|
||||
{
|
||||
if (this.QueryBuilder.Includes != null)
|
||||
{
|
||||
await Task.Run(() => { _InitNavigat(result); });
|
||||
}
|
||||
}
|
||||
|
||||
private void _InitNavigat<TResult>(List<TResult> result)
|
||||
{
|
||||
@ -2366,7 +2410,7 @@ namespace SqlSugar
|
||||
}
|
||||
RestoreMapping();
|
||||
_Mapper(result);
|
||||
await Task.Run(() => { _InitNavigat(result); });
|
||||
await _InitNavigatAsync(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -158,9 +158,9 @@ namespace SqlSugar
|
||||
if (this.whereExpression != null)
|
||||
{
|
||||
result.AsUpdateable.WhereColumns(whereExpression);
|
||||
result.AsDeleteable.WhereColumns(whereExpression);
|
||||
result.AsDeleteable.WhereColumns(update.Select(it => it.Item).ToList(),whereExpression);
|
||||
}
|
||||
result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
|
||||
//result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -240,9 +240,9 @@ namespace SqlSugar
|
||||
if (this.whereExpression != null)
|
||||
{
|
||||
result.AsUpdateable.WhereColumns(whereExpression);
|
||||
result.AsDeleteable.WhereColumns(whereExpression);
|
||||
result.AsDeleteable.WhereColumns(delete.Select(it => it.Item).ToList(),whereExpression);
|
||||
}
|
||||
result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
|
||||
//result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@ namespace SqlSugar
|
||||
OneToOne=1,
|
||||
OneToMany=2,
|
||||
ManyToOne=3,
|
||||
ManyToMany=4,
|
||||
ManyToMany=4,
|
||||
Dynamic=5
|
||||
}
|
||||
}
|
||||
|
@ -183,6 +183,51 @@ namespace SqlSugar
|
||||
return reval;
|
||||
}
|
||||
|
||||
internal static Expression RemoveConvert(Expression item)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if ((item is UnaryExpression) && (item as UnaryExpression).NodeType == ExpressionType.Convert)
|
||||
{
|
||||
item = (item as UnaryExpression).Operand;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return item;
|
||||
}
|
||||
internal static Expression RemoveConvertThanOne(Expression item)
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if ((item is UnaryExpression)
|
||||
&& (item as UnaryExpression).NodeType == ExpressionType.Convert
|
||||
&& (item as UnaryExpression).Operand is UnaryExpression)
|
||||
{
|
||||
item = (item as UnaryExpression).Operand;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return item;
|
||||
}
|
||||
public static string GetMemberName(Expression expression)
|
||||
{
|
||||
if (expression is LambdaExpression)
|
||||
{
|
||||
expression = (expression as LambdaExpression).Body;
|
||||
}
|
||||
if (expression is UnaryExpression)
|
||||
{
|
||||
expression = ((UnaryExpression)expression).Operand;
|
||||
}
|
||||
var member = (expression as MemberExpression).Member.Name;
|
||||
return member;
|
||||
}
|
||||
internal static object GetExpressionValue(Expression expression)
|
||||
{
|
||||
try
|
||||
|
@ -130,24 +130,12 @@ namespace SqlSugar
|
||||
}
|
||||
else if (IsConst(item))
|
||||
{
|
||||
base.Expression = item;
|
||||
base.Expression =ExpressionTool.RemoveConvertThanOne(item);
|
||||
base.Start();
|
||||
string parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex;
|
||||
parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
|
||||
var addItem = new SugarParameter(parameterName, parameter.CommonTempData);
|
||||
var dataType = UtilMethods.GetUnderType(item.Type);
|
||||
if (addItem.Value == null && dataType == UtilConstants.DateType)
|
||||
{
|
||||
addItem.DbType = System.Data.DbType.Date;
|
||||
}
|
||||
if (addItem.Value == null && dataType.IsIn(UtilConstants.FloatType,UtilConstants.IntType,UtilConstants.LongType,UtilConstants.DecType,UtilConstants.DobType))
|
||||
{
|
||||
addItem.DbType = System.Data.DbType.Int32;
|
||||
}
|
||||
if (addItem.Value == null && dataType == UtilConstants.BoolType)
|
||||
{
|
||||
addItem.DbType = System.Data.DbType.Boolean;
|
||||
}
|
||||
ConvertParameterTypeByType(item, addItem);
|
||||
|
||||
this.Context.Parameters.Add(addItem);
|
||||
this.Context.ParameterIndex++;
|
||||
@ -163,6 +151,16 @@ namespace SqlSugar
|
||||
base.Start();
|
||||
parameter.IsAppendResult();
|
||||
parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString()));
|
||||
|
||||
if (this.Context.Parameters != null)
|
||||
{
|
||||
var memberParameter = this.Context.Parameters?.FirstOrDefault(it =>it.Value==null && it.ParameterName == parameter.CommonTempData.ObjToString());
|
||||
if (memberParameter != null)
|
||||
{
|
||||
ConvertParameterTypeByType(item, memberParameter);
|
||||
}
|
||||
}
|
||||
|
||||
base.Context.Result.CurrentParameter = null;
|
||||
}
|
||||
}
|
||||
@ -208,6 +206,24 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void ConvertParameterTypeByType(Expression item, SugarParameter addItem)
|
||||
{
|
||||
var dataType = UtilMethods.GetUnderType(item.Type);
|
||||
if (addItem.Value == null && dataType == UtilConstants.DateType)
|
||||
{
|
||||
addItem.DbType = System.Data.DbType.Date;
|
||||
}
|
||||
if (addItem.Value == null && dataType.IsIn(UtilConstants.FloatType, UtilConstants.IntType, UtilConstants.LongType, UtilConstants.DecType, UtilConstants.DobType))
|
||||
{
|
||||
addItem.DbType = System.Data.DbType.Int32;
|
||||
}
|
||||
if (addItem.Value == null && dataType == UtilConstants.BoolType)
|
||||
{
|
||||
addItem.DbType = System.Data.DbType.Boolean;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsConst(Expression item)
|
||||
{
|
||||
return item is UnaryExpression || item.NodeType == ExpressionType.Constant || (item is MemberExpression) && ((MemberExpression)item).Expression.NodeType == ExpressionType.Constant;
|
||||
|
@ -543,6 +543,30 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region DataTable
|
||||
public DataTable DictionaryListToDataTable(List<Dictionary<string, object>> list)
|
||||
{
|
||||
DataTable result = new DataTable();
|
||||
if (list.Count == 0)
|
||||
return result;
|
||||
|
||||
var columnNames = list.First();
|
||||
foreach (var item in columnNames)
|
||||
{
|
||||
result.Columns.Add(item.Key,item.Value==null?typeof(object):item.Value.GetType());
|
||||
}
|
||||
foreach (var item in list)
|
||||
{
|
||||
var row = result.NewRow();
|
||||
foreach (var key in item.Keys)
|
||||
{
|
||||
row[key] = item[key];
|
||||
}
|
||||
|
||||
result.Rows.Add(row);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
public dynamic DataTableToDynamic(DataTable table)
|
||||
{
|
||||
List<Dictionary<string, object>> deserializeObject = new List<Dictionary<string, object>>();
|
||||
|
@ -38,5 +38,6 @@ namespace SqlSugar
|
||||
Task PageEachAsync<T>(IEnumerable<T> pageItems, int pageSize, Func<List<T>, Task> action);
|
||||
Task PageEachAsync<T, ResultType>(IEnumerable<T> pageItems, int pageSize, Func<List<T>, Task<ResultType>> action);
|
||||
List<IConditionalModel> JsonToConditionalModels(string json);
|
||||
DataTable DictionaryListToDataTable(List<Dictionary<string, object>> dictionaryList);
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,8 @@ namespace SqlSugar
|
||||
IDeleteable<T> Where(string whereString, SugarParameter parameter);
|
||||
IDeleteable<T> Where(string whereString, SugarParameter[] parameters);
|
||||
IDeleteable<T> Where(string whereString, List<SugarParameter> parameters);
|
||||
IDeleteable<T> WhereColumns(Expression<Func<T, object>> columns);
|
||||
IDeleteable<T> WhereColumns(List<T> list,Expression<Func<T, object>> columns);
|
||||
IDeleteable<T> WhereColumns(List<Dictionary<string,object>> columns);
|
||||
IDeleteable<T> Where(List<IConditionalModel> conditionalModels);
|
||||
IDeleteable<T> EnableDiffLogEvent(object businessData = null);
|
||||
IDeleteable<T> RemoveDataCache();
|
||||
|
@ -16,6 +16,7 @@ namespace SqlSugar
|
||||
int BulkCopy(List<T> datas);
|
||||
Task<int> BulkCopyAsync(List<T> datas);
|
||||
int BulkCopy(string tableName,DataTable dataTable);
|
||||
int BulkCopy(DataTable dataTable);
|
||||
Task<int> BulkCopyAsync(string tableName, DataTable dataTable);
|
||||
|
||||
int BulkUpdate(List<T> datas);
|
||||
@ -23,6 +24,8 @@ namespace SqlSugar
|
||||
int BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns);
|
||||
Task<int> BulkUpdateAsync(List<T> datas, string[] whereColumns, string[] updateColumns);
|
||||
int BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns);
|
||||
int BulkUpdate(DataTable dataTable, string[] whereColumns, string[] updateColumns);
|
||||
int BulkUpdate(DataTable dataTable, string[] whereColumns);
|
||||
Task<int> BulkUpdateAsync(string tableName, DataTable dataTable, string[] whereColumns, string[] updateColumns);
|
||||
SplitFastest<T> SplitTable();
|
||||
}
|
||||
|
@ -52,10 +52,11 @@ namespace SqlSugar
|
||||
ISugarQueryable<T> WhereClass<ClassType>(List<ClassType> whereClassList,bool ignoreDefaultValue = false) where ClassType : class, new();
|
||||
ISugarQueryable<T> WhereClassByPrimaryKey(List<T> list);
|
||||
ISugarQueryable<T> WhereClassByPrimaryKey(T data) ;
|
||||
ISugarQueryable<T> WhereColumns(List<Dictionary<string, object>> columns);
|
||||
ISugarQueryable<T> Where(Expression<Func<T, bool>> expression);
|
||||
ISugarQueryable<T> Where(string whereString, object parameters = null);
|
||||
ISugarQueryable<T> Where(List<IConditionalModel> conditionalModels);
|
||||
|
||||
ISugarQueryable<T> Where(List<IConditionalModel> conditionalModels,bool isWrap);
|
||||
ISugarQueryable<T> Having(Expression<Func<T, bool>> expression);
|
||||
ISugarQueryable<T> Having(string whereString, object parameters = null);
|
||||
|
||||
|
@ -23,6 +23,7 @@ namespace SqlSugar
|
||||
int Count(Expression<Func<T, bool>> whereExpression);
|
||||
bool Delete(Expression<Func<T, bool>> whereExpression);
|
||||
bool Delete(T deleteObj);
|
||||
bool Delete(List<T> deleteObjs);
|
||||
bool DeleteById(dynamic id);
|
||||
bool DeleteByIds(dynamic[] ids);
|
||||
T GetById(dynamic id);
|
||||
@ -55,6 +56,7 @@ namespace SqlSugar
|
||||
Task<int> CountAsync(Expression<Func<T, bool>> whereExpression);
|
||||
Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression);
|
||||
Task<bool> DeleteAsync(T deleteObj);
|
||||
Task<bool> DeleteAsync(List<T> deleteObjs);
|
||||
Task<bool> DeleteByIdAsync(dynamic id);
|
||||
Task<bool> DeleteByIdsAsync(dynamic[] ids);
|
||||
Task<T> GetByIdAsync(dynamic id);
|
||||
|
@ -21,6 +21,10 @@ namespace SqlSugar
|
||||
if (exceptionalCaseInfo != null) {
|
||||
foreach (var item in exceptionalCaseInfo.Cast<Match>())
|
||||
{
|
||||
if (item.Value != null && item.Value.IndexOf(",") == 1&&Regex.IsMatch(item.Value, @"^ \,\@\w+$"))
|
||||
{
|
||||
break;
|
||||
}
|
||||
sql = sql.Replace(item.Value, item.Value.Replace("@", UtilConstants.ReplaceKey));
|
||||
}
|
||||
}
|
||||
|
@ -136,8 +136,10 @@ namespace SqlSugar
|
||||
}
|
||||
else if (type == UtilConstants.ByteArrayType)
|
||||
{
|
||||
string bytesString = "0x" + BitConverter.ToString((byte[])value).Replace("-", "");
|
||||
return bytesString;
|
||||
++i;
|
||||
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
|
||||
this.Parameters.Add(new SugarParameter(parameterName, value,System.Data.DbType.Binary));
|
||||
return parameterName;
|
||||
}
|
||||
else if (type == UtilConstants.BoolType)
|
||||
{
|
||||
@ -150,7 +152,7 @@ namespace SqlSugar
|
||||
}
|
||||
else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
|
||||
{
|
||||
if (value.ToString().Length > 2000)
|
||||
if (value.ToString().Length > 1000)
|
||||
{
|
||||
++i;
|
||||
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
|
||||
|
@ -94,7 +94,7 @@ namespace SqlSugar
|
||||
}
|
||||
else if (type == UtilConstants.StringType || type == UtilConstants.ObjType)
|
||||
{
|
||||
if (value.ToString().Length > 2000)
|
||||
if (value.ToString().Length > 1000)
|
||||
{
|
||||
++i;
|
||||
var parameterName = this.Builder.SqlParameterKeyWord + name + i;
|
||||
|
@ -200,6 +200,10 @@ namespace SqlSugar
|
||||
{
|
||||
return this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommand() > 0;
|
||||
}
|
||||
public virtual bool Delete(List<T> deleteObjs)
|
||||
{
|
||||
return this.Context.Deleteable<T>().Where(deleteObjs).ExecuteCommand() > 0;
|
||||
}
|
||||
public virtual bool Delete(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
|
||||
@ -314,6 +318,10 @@ namespace SqlSugar
|
||||
{
|
||||
return await this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual async Task<bool> DeleteAsync(List<T> deleteObjs)
|
||||
{
|
||||
return await this.Context.Deleteable<T>().Where(deleteObjs).ExecuteCommandAsync() > 0;
|
||||
}
|
||||
public virtual async Task<bool> DeleteAsync(Expression<Func<T, bool>> whereExpression)
|
||||
{
|
||||
return await this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommandAsync() > 0;
|
||||
|
@ -365,6 +365,12 @@ namespace SqlSugar
|
||||
{
|
||||
return this.Context.Storageable(data);
|
||||
}
|
||||
public StorageableDataTable Storageable(List<Dictionary<string,object>> dictionaryList,string tableName)
|
||||
{
|
||||
DataTable dt = this.Context.Utilities.DictionaryListToDataTable(dictionaryList);
|
||||
dt.TableName = tableName;
|
||||
return this.Context.Storageable(dt);
|
||||
}
|
||||
|
||||
public IStorageable<T> Storageable<T>(List<T> dataList) where T : class, new()
|
||||
{
|
||||
|
@ -20,5 +20,9 @@ namespace SqlSugar
|
||||
return thisValue;
|
||||
}
|
||||
}
|
||||
public static IEnumerable<T> MappingField<T>(this IEnumerable<T> thisValue,Func<T, object> leftField, Func<object> rightField)
|
||||
{
|
||||
return thisValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user