Browse Source

Update db.QueryableByObject

pull/33/head
sunkaixuan 1 year ago
parent
commit
68dc16a771
  1. 49
      Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryMethodInfo.cs
  2. 4
      Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarProvider.cs
  3. 4
      Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs
  4. 1
      Src/Asp.Net/SqlSugar/Interface/ISqlSugarClient.cs
  5. 4
      Src/Asp.Net/SqlSugar/SqlSugarClient.cs
  6. 4
      Src/Asp.Net/SqlSugar/SqlSugarScope.cs
  7. 9
      Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs

49
Src/Asp.Net/SqlSugar/Abstract/QueryableProvider/QueryMethodInfo.cs

@ -11,23 +11,52 @@ namespace SqlSugar
public SqlSugarProvider Context { get; internal set; }
public Type EntityType { get; set; }
#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(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(string groupBySql)
{
var method = QueryableObj.GetType().GetMyMethod("GroupBy", 1, typeof(string));
this.QueryableObj = method.Invoke(QueryableObj, new object[] { groupBySql });
return this;
}
public QueryMethodInfo Where(string sql, object parameters=null)
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 });
this.QueryableObj = method.Invoke(QueryableObj, new object[] { sql, parameters });
return this;
}
public QueryMethodInfo Having(string sql, object parameters = null)
@ -39,13 +68,13 @@ namespace SqlSugar
public QueryMethodInfo SplitTable(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>> getTableNamesFunc)
{
var method = QueryableObj.GetType().GetMyMethod("SplitTable", 1, typeof(Func<List<SplitTableInfo>, IEnumerable<SplitTableInfo>>));
this.QueryableObj = method.Invoke(QueryableObj, new object[] { getTableNamesFunc});
this.QueryableObj = method.Invoke(QueryableObj, new object[] { getTableNamesFunc });
return this;
}
public QueryMethodInfo SplitTable(DateTime begintTime,DateTime endTime)
public QueryMethodInfo SplitTable(DateTime begintTime, DateTime endTime)
{
var method = QueryableObj.GetType().GetMyMethod("SplitTable", 2, typeof(DateTime), typeof(DateTime));
this.QueryableObj = method.Invoke(QueryableObj, new object[] {begintTime,endTime });
this.QueryableObj = method.Invoke(QueryableObj, new object[] { begintTime, endTime });
return this;
}
public QueryMethodInfo SplitTable()
@ -63,14 +92,16 @@ namespace SqlSugar
return this;
}
public QueryMethodInfo Select(string selectorSql, Type selectType)
public QueryMethodInfo Select(string selectorSql, Type selectType)
{
var method = QueryableObj.GetType().GetMyMethod("Select", 1, typeof(string))
var method = QueryableObj.GetType().GetMyMethod("Select", 1, typeof(string))
.MakeGenericMethod(selectType);
this.QueryableObj = method.Invoke(QueryableObj, new object[] { selectorSql });
return this;
}
#endregion
#region Result
public object ToPageList(int pageNumber, int pageSize)
{

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

@ -93,6 +93,10 @@ namespace SqlSugar
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>

4
Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs

@ -819,6 +819,10 @@ namespace SqlSugar
{
return ScopedContext.QueryableByObject(entityType);
}
public QueryMethodInfo QueryableByObject(Type entityType, string shortName)
{
return ScopedContext.QueryableByObject(entityType, shortName);
}
#endregion
}
}

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

@ -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>();

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

@ -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>();

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

@ -876,5 +876,9 @@ namespace SqlSugar
{
return ScopedContext.QueryableByObject(entityType);
}
public QueryMethodInfo QueryableByObject(Type entityType, string shortName)
{
return ScopedContext.QueryableByObject(entityType, shortName);
}
}
}

9
Src/Asp.Net/SqlSugar/Utilities/CommonExtensions.cs

@ -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 };

Loading…
Cancel
Save