mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-22 20:13:41 +08:00
Synchronization code
This commit is contained in:
@@ -99,7 +99,7 @@ namespace SqlSugar
|
||||
{
|
||||
Check.ExceptionEasy($"{typeof(TChild).Name} need primary key ", $"{typeof(TChild).Name}需要主键");
|
||||
}
|
||||
var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).ToStorage();
|
||||
var x = this._Context.Storageable(children).WhereColumns(new string[] { pkColumn.PropertyName }).GetStorageableResult();
|
||||
var insertData = children = x.InsertList.Select(it => it.Item).ToList();
|
||||
var IsNoExistsNoInsert = _navOptions != null && _navOptions.OneToManyIfExistsNoInsert == true;
|
||||
if (_NavigateType == NavigateType.OneToMany && IsFirst == false && IsNoExistsNoInsert == false)
|
||||
|
@@ -1,23 +1,112 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class QueryMethodInfo
|
||||
{
|
||||
public object QueryableObj { get; internal set; }
|
||||
public SqlSugarProvider Context { get; internal set; }
|
||||
public Type EntityType { get; set; }
|
||||
|
||||
public QueryMethodInfo Where(string sql, object parameters)
|
||||
|
||||
#region Json 2 sql api
|
||||
#endregion
|
||||
|
||||
#region Sql API
|
||||
public QueryMethodInfo AS(string tableName)
|
||||
{
|
||||
string shortName = $"{tableName}_1";
|
||||
var method = QueryableObj.GetType().GetMyMethod("AS", 2, typeof(string), typeof(string));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { tableName, shortName });
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo AS(string tableName, string shortName)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("AS", 2, typeof(string), typeof(string));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { tableName, shortName });
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo OrderBy(List<OrderByModel> models)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("OrderBy", 1, typeof(List<OrderByModel>));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { models });
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo OrderBy(string orderBySql)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("OrderBy", 1, typeof(string));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { orderBySql });
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo AddJoinInfo(string tableName, string shortName,string onWhere, JoinType type = JoinType.Left)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("AddJoinInfo", 4, typeof(string),typeof(string),typeof(string),typeof(JoinType));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { tableName,shortName,onWhere,type });
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo AddJoinInfo(Type joinEntityType, string shortName, string onWhere, JoinType type = JoinType.Left)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("AddJoinInfo", 4, typeof(string), typeof(string), typeof(string), typeof(JoinType));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { this.Context.EntityMaintenance.GetTableName(joinEntityType), shortName, onWhere, type });
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo GroupBy(List<GroupByModel> models)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("GroupBy", 1, typeof(List<GroupByModel>));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { models });
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo GroupBy(string groupBySql)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("GroupBy", 1, typeof(string));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { groupBySql });
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryMethodInfo Where(List<IConditionalModel> conditionalModels)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("Where", 1, typeof(List<IConditionalModel>));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { conditionalModels });
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryMethodInfo Where(IFuncModel model)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("Where", 1, typeof(IFuncModel));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { model });
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryMethodInfo Where(List<IConditionalModel> conditionalModels, bool isWrap)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("Where", 2, typeof(List<IConditionalModel>),typeof(bool));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { conditionalModels,isWrap });
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo Where(string sql, object parameters = null)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("Where", 2, typeof(string), typeof(object));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { sql, parameters });
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryMethodInfo Having(IFuncModel model)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("Having", 1, typeof(IFuncModel));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] {model});
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo Having(string sql, object parameters = null)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("Having", 2, typeof(string), typeof(object));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { sql, parameters });
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("SplitTable", 1, typeof(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>>));
|
||||
@@ -37,6 +126,20 @@ namespace SqlSugar
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryMethodInfo Select(List<SelectModel> models)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("Select", 1, typeof(List<SelectModel>));
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { models });
|
||||
return this;
|
||||
}
|
||||
public QueryMethodInfo Select(string selectorSql)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("Select", 1, typeof(string))
|
||||
.MakeGenericMethod(EntityType);
|
||||
this.QueryableObj = method.Invoke(QueryableObj, new object[] { selectorSql });
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryMethodInfo Select(string selectorSql, Type selectType)
|
||||
{
|
||||
var method = QueryableObj.GetType().GetMyMethod("Select", 1, typeof(string))
|
||||
@@ -45,6 +148,8 @@ namespace SqlSugar
|
||||
return this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Result
|
||||
public object ToPageList(int pageNumber, int pageSize)
|
||||
{
|
||||
|
@@ -211,6 +211,86 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
|
||||
public StorageableResult<T> GetStorageableResult()
|
||||
{
|
||||
if (whereFuncs == null || whereFuncs.Count == 0)
|
||||
{
|
||||
return this.Saveable().GetStorageableResult();
|
||||
}
|
||||
if (this.allDatas.Count == 0)
|
||||
return new StorageableResult<T>()
|
||||
{
|
||||
//AsDeleteable = this.Context.Deleteable<T>().AS(asname).Where(it => false),
|
||||
//AsInsertable = this.Context.Insertable(new List<T>()).AS(asname),
|
||||
//AsUpdateable = this.Context.Updateable(new List<T>()).AS(asname),
|
||||
InsertList = new List<StorageableMessage<T>>(),
|
||||
UpdateList = new List<StorageableMessage<T>>(),
|
||||
DeleteList = new List<StorageableMessage<T>>(),
|
||||
ErrorList = new List<StorageableMessage<T>>(),
|
||||
IgnoreList = new List<StorageableMessage<T>>(),
|
||||
OtherList = new List<StorageableMessage<T>>(),
|
||||
TotalList = new List<StorageableMessage<T>>()
|
||||
};
|
||||
var pkInfos = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsPrimarykey);
|
||||
if (whereExpression == null && !pkInfos.Any())
|
||||
{
|
||||
Check.ExceptionEasy(true, "Need primary key or WhereColumn", "使用Storageable实体需要主键或者使用WhereColumn指定条件列");
|
||||
}
|
||||
if (whereExpression == null && pkInfos.Any())
|
||||
{
|
||||
this.Context.Utilities.PageEach(allDatas, 300, item => {
|
||||
var addItems = this.Context.Queryable<T>().Filter(null, this.isDisableFilters).TranLock(this.lockType).AS(asname).WhereClassByPrimaryKey(item.Select(it => it.Item).ToList()).ToList();
|
||||
dbDataList.AddRange(addItems);
|
||||
});
|
||||
}
|
||||
var pkProperties = GetPkProperties(pkInfos);
|
||||
var messageList = allDatas.Select(it => new StorageableMessage<T>()
|
||||
{
|
||||
Item = it.Item,
|
||||
Database = dbDataList,
|
||||
PkFields = pkProperties
|
||||
}).ToList();
|
||||
foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key))
|
||||
{
|
||||
List<StorageableMessage<T>> whereList = messageList.Where(it => it.StorageType == null).ToList();
|
||||
Func<StorageableMessage<T>, bool> exp = item.value1;
|
||||
var list = whereList.Where(exp).ToList();
|
||||
foreach (var it in list)
|
||||
{
|
||||
it.StorageType = item.key;
|
||||
it.StorageMessage = item.value2;
|
||||
}
|
||||
}
|
||||
var delete = messageList.Where(it => it.StorageType == StorageType.Delete).ToList();
|
||||
var update = messageList.Where(it => it.StorageType == StorageType.Update).ToList();
|
||||
var inset = messageList.Where(it => it.StorageType == StorageType.Insert).ToList();
|
||||
var error = messageList.Where(it => it.StorageType == StorageType.Error).ToList();
|
||||
var ignore = messageList.Where(it => it.StorageType == StorageType.Ignore || it.StorageType == null).ToList();
|
||||
var other = messageList.Where(it => it.StorageType == StorageType.Other).ToList();
|
||||
StorageableResult<T> result = new StorageableResult<T>()
|
||||
{
|
||||
_WhereColumnList = wherecolumnList,
|
||||
_AsName = asname,
|
||||
_Context = this.Context,
|
||||
//AsDeleteable = this.Context.Deleteable<T>().AS(asname),
|
||||
//AsUpdateable = this.Context.Updateable(update.Select(it => it.Item).ToList()).AS(asname),
|
||||
//AsInsertable = this.Context.Insertable(inset.Select(it => it.Item).ToList()).AS(asname),
|
||||
OtherList = other,
|
||||
InsertList = inset,
|
||||
DeleteList = delete,
|
||||
UpdateList = update,
|
||||
ErrorList = error,
|
||||
IgnoreList = ignore,
|
||||
TotalList = messageList
|
||||
};
|
||||
//if (this.whereExpression != null)
|
||||
//{
|
||||
// result.AsUpdateable.WhereColumns(whereExpression);
|
||||
// result.AsDeleteable.WhereColumns(update.Select(it => it.Item).ToList(), whereExpression);
|
||||
//}
|
||||
//result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<StorageableResult<T>> ToStorageAsync()
|
||||
{
|
||||
|
@@ -90,8 +90,13 @@ namespace SqlSugar
|
||||
var queryableObj=methodT.Invoke(this,new object[] {});
|
||||
result.QueryableObj = queryableObj;
|
||||
result.Context = this.Context;
|
||||
result.EntityType = entityType;
|
||||
return result;
|
||||
}
|
||||
public QueryMethodInfo QueryableByObject(Type entityType, string shortName)
|
||||
{
|
||||
return this.QueryableByObject(entityType).AS(this.Context.EntityMaintenance.GetTableName(entityType),shortName);
|
||||
}
|
||||
/// <summary>
|
||||
/// Get datebase time
|
||||
/// </summary>
|
||||
|
@@ -819,6 +819,10 @@ namespace SqlSugar
|
||||
{
|
||||
return ScopedContext.QueryableByObject(entityType);
|
||||
}
|
||||
public QueryMethodInfo QueryableByObject(Type entityType, string shortName)
|
||||
{
|
||||
return ScopedContext.QueryableByObject(entityType, shortName);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -4,7 +4,6 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.DirectoryServices.ActiveDirectory;
|
||||
using System.Dynamic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
@@ -17,5 +17,7 @@ namespace SqlSugar
|
||||
public static Action<object> CompleteUpdateableFunc;
|
||||
public static Action<object> CompleteDeleteableFunc;
|
||||
public static Action<ISqlSugarClient> CompleteDbFunc;
|
||||
|
||||
public static Func<List<SplitTableInfo>> SplitTableGetTablesFunc;
|
||||
}
|
||||
}
|
||||
|
@@ -79,6 +79,7 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region Queryable
|
||||
QueryMethodInfo QueryableByObject(Type entityType, string shortName);
|
||||
QueryMethodInfo QueryableByObject(Type entityType);
|
||||
ISugarQueryable<T> MasterQueryable<T>();
|
||||
ISugarQueryable<T> SlaveQueryable<T>();
|
||||
|
@@ -23,6 +23,7 @@ namespace SqlSugar
|
||||
IStorageable<T> SplitDelete(Func<StorageableInfo<T>, bool> conditions, string message = null);
|
||||
IStorageable<T> SplitOther(Func<StorageableInfo<T>, bool> conditions, string message = null);
|
||||
StorageableResult<T> ToStorage();
|
||||
StorageableResult<T> GetStorageableResult();
|
||||
Task<StorageableResult<T>> ToStorageAsync();
|
||||
IStorageable<T> As(string tableName);
|
||||
int ExecuteCommand();
|
||||
|
@@ -54,6 +54,10 @@ namespace SqlSugar
|
||||
}
|
||||
public List<SplitTableInfo> GetTables()
|
||||
{
|
||||
if (StaticConfig.SplitTableGetTablesFunc != null)
|
||||
{
|
||||
return StaticConfig.SplitTableGetTablesFunc();
|
||||
}
|
||||
var oldIsEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
|
||||
this.Context.Ado.IsEnableLogEvent = false;
|
||||
var tableInfos = this.Context.DbMaintenance.GetTableInfoList(false);
|
||||
|
@@ -288,6 +288,10 @@ namespace SqlSugar
|
||||
{
|
||||
return this.Context.QueryableByObject(entityType);
|
||||
}
|
||||
public QueryMethodInfo QueryableByObject(Type entityType,string shortName)
|
||||
{
|
||||
return this.Context.QueryableByObject(entityType,shortName);
|
||||
}
|
||||
public ISugarQueryable<T> MasterQueryable<T>()
|
||||
{
|
||||
return this.Context.MasterQueryable<T>();
|
||||
|
@@ -876,5 +876,9 @@ namespace SqlSugar
|
||||
{
|
||||
return ScopedContext.QueryableByObject(entityType);
|
||||
}
|
||||
public QueryMethodInfo QueryableByObject(Type entityType, string shortName)
|
||||
{
|
||||
return ScopedContext.QueryableByObject(entityType, shortName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -47,6 +47,15 @@ namespace SqlSugar
|
||||
it.GetParameters()[1].ParameterType == parameterType2&&
|
||||
it.GetParameters()[2].ParameterType == parameterType3);
|
||||
}
|
||||
public static MethodInfo GetMyMethod(this Type type, string name, int argCount, Type parameterType, Type parameterType2, Type parameterType3,Type parameterType4)
|
||||
{
|
||||
return type.GetMethods().Where(it => it.Name == name).FirstOrDefault(it =>
|
||||
it.GetParameters().Length == argCount &&
|
||||
it.GetParameters().First().ParameterType == parameterType &&
|
||||
it.GetParameters()[1].ParameterType == parameterType2 &&
|
||||
it.GetParameters()[2].ParameterType == parameterType3&&
|
||||
it.GetParameters()[3].ParameterType == parameterType4);
|
||||
}
|
||||
public static List<T> ToList<T>(this T thisValue,Func<T,T> action) where T:class,new()
|
||||
{
|
||||
return new List<T> { thisValue };
|
||||
|
Reference in New Issue
Block a user