mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-07-15 14:04:44 +08:00
-
This commit is contained in:
parent
2640561513
commit
6f7a60f639
@ -35,16 +35,29 @@ namespace OrmTest.UnitTest
|
||||
Console.WriteLine(sql + " " + pars);
|
||||
};
|
||||
|
||||
var listx = db.Queryable<School, School>((st, st2) => new object[] {
|
||||
var l1 = db.Queryable<School, School>((st, st2) => new object[] {
|
||||
JoinType.Left,st.Id==st2.Id
|
||||
})
|
||||
.Where(st => st.Id > 0)
|
||||
.Select<School, School, dynamic>((st, st2) => new {stid = st.Id, scId = st2.Id,xx=st }).ToList();
|
||||
return;
|
||||
var list = db.Queryable<School, School>((st, st2) => new object[] {
|
||||
.Select<School, School, dynamic>((st, st2) => new {stid = st.Id, scId = st2.Id,xx=st }).ToSql();
|
||||
|
||||
base.Check("SELECT [st].[Id] AS [stid] , [st2].[Id] AS [scId] , [st].[Id] AS [xx_Id] , [st].[Name] AS [xx_Name] FROM [School] st Left JOIN School st2 ON ( [st].[Id] = [st2].[Id] ) WHERE ( [st].[Id] > @Id0 )"
|
||||
, new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",0)
|
||||
},l1.Key,l1.Value, "l1错误");
|
||||
|
||||
var l2 = db.Queryable<School, School>((st, st2) => new object[] {
|
||||
JoinType.Left,st.Id==st2.Id
|
||||
}).Where<Student, School>((st, sc) => st.Id > 0)
|
||||
.Select(st => new ViewModelStudent { School = st }).ToList();
|
||||
}).Where<Student, School>((st, st2) => st2.Id > 2)
|
||||
.Select(st => new ViewModelStudent { School = st }).ToSql();
|
||||
|
||||
base.Check("SELECT [st].[Id] AS [School_Id] , [st].[Name] AS [School_Name] FROM [School] st Left JOIN School st2 ON ( [st].[Id] = [st2].[Id] ) WHERE ( [st2].[Id] > @Id0 )",
|
||||
new List<SugarParameter>() { new SugarParameter("@Id0", 2) },
|
||||
l2.Key,
|
||||
l2.Value,
|
||||
"l2报错"
|
||||
);
|
||||
|
||||
|
||||
var list2 = db.Queryable<Student>()
|
||||
.Where(st => st.Id > 0)
|
||||
|
@ -10,6 +10,7 @@ namespace SqlSugar
|
||||
public class QueryableAccessory
|
||||
{
|
||||
protected List<SugarParameter> _Pars;
|
||||
protected ILambdaExpressions _LambdaExpressions;
|
||||
protected List<SugarParameter> BasePars
|
||||
{
|
||||
get
|
||||
@ -32,27 +33,6 @@ namespace SqlSugar
|
||||
_Pars = new List<SugarParameter>();
|
||||
_Pars.AddRange(pars);
|
||||
}
|
||||
protected void Where(Expression expression,SqlSugarClient context,ISqlBuilder builder)
|
||||
{
|
||||
ResolveExpressType type = ResolveExpressType.WhereSingle;
|
||||
if (builder.LambadaQueryBuilder.JoinQueryInfos.IsValuable())
|
||||
{
|
||||
type = ResolveExpressType.WhereMultiple;
|
||||
}
|
||||
ILambdaExpressions resolveExpress = context.LambdaExpressions;
|
||||
resolveExpress.Resolve(expression, type);
|
||||
BasePars.AddRange(resolveExpress.Parameters);
|
||||
builder.LambadaQueryBuilder.WhereInfos.Add(builder.AppendWhereOrAnd(builder.LambadaQueryBuilder.WhereInfos.IsNullOrEmpty(),resolveExpress.Result.GetResultString()));
|
||||
resolveExpress.Clear();
|
||||
}
|
||||
|
||||
protected void Where<T>(string whereString, object whereObj, SqlSugarClient context, ISqlBuilder builder)
|
||||
{
|
||||
var SqlBuilder = builder;
|
||||
var whereValue = SqlBuilder.LambadaQueryBuilder.WhereInfos;
|
||||
whereValue.Add(SqlBuilder.AppendWhereOrAnd(whereValue.Count == 0, whereString));
|
||||
this.AddPars(whereObj, context);
|
||||
}
|
||||
|
||||
protected void SetSelectType(SqlSugarClient context,ISqlBuilder builder)
|
||||
{
|
||||
|
@ -9,7 +9,7 @@ using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class QueryableProvider<T> : QueryableAccessory, ISugarQueryable<T>
|
||||
public partial class QueryableProvider<T> : QueryableAccessory, ISugarQueryable<T>
|
||||
{
|
||||
public SqlSugarClient Context { get; set; }
|
||||
public IDb Db { get { return Context.Database; } }
|
||||
@ -56,45 +56,47 @@ namespace SqlSugar
|
||||
|
||||
public virtual ISugarQueryable<T> Where(Expression<Func<T, bool>> expression)
|
||||
{
|
||||
base.Where(expression,this.Context, this.SqlBuilder);
|
||||
this._Where(expression);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Where(string whereString, object whereObj = null)
|
||||
{
|
||||
base.Where<T>(whereString, whereObj, this.Context, this.SqlBuilder);
|
||||
this.Where<T>(whereString, whereObj);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Where<T2>(string whereString, object whereObj = null)
|
||||
{
|
||||
base.Where<T2>(whereString, whereObj, this.Context, this.SqlBuilder);
|
||||
var whereValue = SqlBuilder.LambadaQueryBuilder.WhereInfos;
|
||||
whereValue.Add(SqlBuilder.AppendWhereOrAnd(whereValue.Count == 0, whereString));
|
||||
this.AddPars(whereObj, this.Context);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Where<T2>(Expression<Func<T2, bool>> expression)
|
||||
{
|
||||
base.Where(expression, this.Context, this.SqlBuilder);
|
||||
this._Where(expression);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Where<T2, T3>(Expression<Func<T2, T3, bool>> expression)
|
||||
public ISugarQueryable<T> Where<T2, T3>(Expression<Func<T2, T3, bool>> expression)
|
||||
{
|
||||
base.Where(expression, this.Context, this.SqlBuilder);
|
||||
this._Where(expression);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Where<T2, T3, T4>(Expression<Func<T2, T3, T4, bool>> expression)
|
||||
{
|
||||
base.Where(expression, this.Context, this.SqlBuilder);
|
||||
this._Where(expression);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Where<T2, T3, T4, T5>(Expression<Func<T2, T3, T4, T5, bool>> expression)
|
||||
public ISugarQueryable<T> Where<T2, T3, T4, T5>(Expression<Func<T2, T3, T4, T5, bool>> expression)
|
||||
{
|
||||
base.Where(expression, this.Context, this.SqlBuilder);
|
||||
this._Where(expression);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Where<T2, T3, T4, T5, T6>(Expression<Func<T2, T3, T4, T5, T6, bool>> expression)
|
||||
public ISugarQueryable<T> Where<T2, T3, T4, T5, T6>(Expression<Func<T2, T3, T4, T5, T6, bool>> expression)
|
||||
{
|
||||
base.Where(expression, this.Context, this.SqlBuilder);
|
||||
this._Where(expression);
|
||||
return this;
|
||||
}
|
||||
public virtual ISugarQueryable<T> WhereIF(bool isWhere,Expression<Func<T, bool>> expression)
|
||||
public virtual ISugarQueryable<T> WhereIF(bool isWhere, Expression<Func<T, bool>> expression)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
Where<T>(expression);
|
||||
@ -103,13 +105,13 @@ namespace SqlSugar
|
||||
public ISugarQueryable<T> WhereIF(bool isWhere, string whereString, object whereObj = null)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
base.Where<T>(whereString, whereObj, this.Context, this.SqlBuilder);
|
||||
this.Where<T>(whereString, whereObj);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> WhereIF<T2>(bool isWhere, string whereString, object whereObj = null)
|
||||
public ISugarQueryable<T> WhereIF<T2>(bool isWhere, string whereString, object whereObj = null)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
base.Where<T2>(whereString, whereObj, this.Context, this.SqlBuilder);
|
||||
this.Where<T2>(whereString, whereObj);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> WhereIF<T2>(bool isWhere, Expression<Func<T2, bool>> expression)
|
||||
@ -118,13 +120,13 @@ namespace SqlSugar
|
||||
this.Where(expression);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> WhereIF<T2, T3>(bool isWhere, Expression<Func<T2, T3, bool>> expression)
|
||||
public ISugarQueryable<T> WhereIF<T2, T3>(bool isWhere, Expression<Func<T2, T3, bool>> expression)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
this.Where(expression);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> WhereIF<T2, T3, T4>(bool isWhere, Expression<Func<T2, T3, T4, bool>> expression)
|
||||
public ISugarQueryable<T> WhereIF<T2, T3, T4>(bool isWhere, Expression<Func<T2, T3, T4, bool>> expression)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
this.Where(expression);
|
||||
@ -136,7 +138,7 @@ namespace SqlSugar
|
||||
this.Where(expression);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> WhereIF<T2, T3, T4, T5, T6>(bool isWhere, Expression<Func<T2, T3, T4, T5, T6, bool>> expression)
|
||||
public ISugarQueryable<T> WhereIF<T2, T3, T4, T5, T6>(bool isWhere, Expression<Func<T2, T3, T4, T5, T6, bool>> expression)
|
||||
{
|
||||
if (!isWhere) return this;
|
||||
this.Where(expression);
|
||||
@ -258,35 +260,35 @@ namespace SqlSugar
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<T2, TResult>(Expression<Func<T2, TResult>> expression)
|
||||
public ISugarQueryable<TResult> Select<T2, TResult>(Expression<Func<T2, TResult>> expression)
|
||||
{
|
||||
return SelectMehtod<TResult>(expression);
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<T2, T3, TResult>(Expression<Func<T2, T3, TResult>> expression)
|
||||
public ISugarQueryable<TResult> Select<T2, T3, TResult>(Expression<Func<T2, T3, TResult>> expression)
|
||||
{
|
||||
return SelectMehtod<TResult>(expression);
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<T2, T3, T4, TResult>(Expression<Func<T2, T3, T4, TResult>> expression)
|
||||
public ISugarQueryable<TResult> Select<T2, T3, T4, TResult>(Expression<Func<T2, T3, T4, TResult>> expression)
|
||||
{
|
||||
return SelectMehtod<TResult>(expression);
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<T2, T3, T4, T5, TResult>(Expression<Func<T2, T3, T4, T5, TResult>> expression)
|
||||
public ISugarQueryable<TResult> Select<T2, T3, T4, T5, TResult>(Expression<Func<T2, T3, T4, T5, TResult>> expression)
|
||||
{
|
||||
return SelectMehtod<TResult>(expression);
|
||||
}
|
||||
public ISugarQueryable<TResult> Select<T2, T3, T4, T5, T6, TResult>(Expression<Func<T2, T3, T4, T5, T6, TResult>> expression)
|
||||
public ISugarQueryable<TResult> Select<T2, T3, T4, T5, T6, TResult>(Expression<Func<T2, T3, T4, T5, T6, TResult>> expression)
|
||||
{
|
||||
return SelectMehtod<TResult>(expression);
|
||||
}
|
||||
public ISugarQueryable<TResult> Select<T2, T3, T4, T5, T6, T7, TResult>(Expression<Func<T2, T3, T4, T5, T6, T7, TResult>> expression)
|
||||
public ISugarQueryable<TResult> Select<T2, T3, T4, T5, T6, T7, TResult>(Expression<Func<T2, T3, T4, T5, T6, T7, TResult>> expression)
|
||||
{
|
||||
return SelectMehtod<TResult>(expression);
|
||||
}
|
||||
|
||||
public ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression)
|
||||
public ISugarQueryable<TResult> Select<TResult>(Expression<Func<T, TResult>> expression)
|
||||
{
|
||||
return SelectMehtod<TResult>(expression);
|
||||
}
|
||||
@ -346,8 +348,8 @@ namespace SqlSugar
|
||||
|
||||
public List<T> ToList()
|
||||
{
|
||||
string sql = SqlBuilder.LambadaQueryBuilder.ToSqlString();
|
||||
using (var dataReader = this.Db.GetDataReader(sql, this.Pars.ToArray()))
|
||||
var sqlObj =this.ToSql();
|
||||
using (var dataReader = this.Db.GetDataReader(sqlObj.Key, sqlObj.Value.ToArray()))
|
||||
{
|
||||
var reval = this.Bind.DataReaderToList<T>(typeof(T), dataReader, SqlBuilder.LambadaQueryBuilder.SelectCacheKey);
|
||||
return reval;
|
||||
@ -368,9 +370,10 @@ namespace SqlSugar
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public KeyValuePair<string, Dictionary<string, string>> ToSql()
|
||||
public KeyValuePair<string, List<SugarParameter>> ToSql()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
string sql = SqlBuilder.LambadaQueryBuilder.ToSqlString();
|
||||
return new KeyValuePair<string, List<SugarParameter>>(sql, this.Pars);
|
||||
}
|
||||
|
||||
public DataTable ToDataTable()
|
||||
@ -398,6 +401,23 @@ namespace SqlSugar
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
#region 私有方法
|
||||
protected void _Where(Expression expression)
|
||||
{
|
||||
ResolveExpressType type = ResolveExpressType.WhereSingle;
|
||||
if (SqlBuilder.LambadaQueryBuilder.JoinQueryInfos.IsValuable())
|
||||
{
|
||||
type = ResolveExpressType.WhereMultiple;
|
||||
}
|
||||
ILambdaExpressions resolveExpress = this.SqlBuilder.LambadaQueryBuilder.LambdaExpressions;
|
||||
resolveExpress.Resolve(expression, type);
|
||||
BasePars.AddRange(resolveExpress.Parameters);
|
||||
SqlBuilder.LambadaQueryBuilder.WhereInfos.Add(SqlBuilder.AppendWhereOrAnd(SqlBuilder.LambadaQueryBuilder.WhereInfos.IsNullOrEmpty(), resolveExpress.Result.GetResultString()));
|
||||
resolveExpress.Clear();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -8,11 +8,6 @@ namespace SqlSugar
|
||||
{
|
||||
public abstract class LambadaQueryBuilder : IDMLBuilder
|
||||
{
|
||||
public LambadaQueryBuilder()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private List<SugarParameter> _QueryPars;
|
||||
private List<JoinQueryInfo> _JoinQueryInfos;
|
||||
private List<string> _WhereInfos;
|
||||
@ -20,6 +15,7 @@ namespace SqlSugar
|
||||
|
||||
public StringBuilder Sql { get; set; }
|
||||
public SqlSugarClient Context { get; set; }
|
||||
public ILambdaExpressions LambdaExpressions { get; set; }
|
||||
|
||||
public ISqlBuilder Builder { get; set; }
|
||||
public int? Skip { get; set; }
|
||||
@ -85,7 +81,7 @@ namespace SqlSugar
|
||||
public virtual string GetSelectValueByExpression()
|
||||
{
|
||||
var expression = this.SelectValue as Expression;
|
||||
ILambdaExpressions resolveExpress = this.Context.LambdaExpressions;
|
||||
ILambdaExpressions resolveExpress = this.LambdaExpressions;
|
||||
var isSingle= Builder.LambadaQueryBuilder.JoinQueryInfos.IsValuable();
|
||||
resolveExpress.JoinQueryInfos = Builder.LambadaQueryBuilder.JoinQueryInfos;
|
||||
resolveExpress.MappingColumns = Context.MappingColumns;
|
||||
|
@ -84,7 +84,7 @@ namespace SqlSugar
|
||||
string ToJsonPage(int pageIndex, int pageSize);
|
||||
string ToJsonPage(int pageIndex, int pageSize, ref int pageCount);
|
||||
|
||||
KeyValuePair<string, Dictionary<string, string>> ToSql();
|
||||
KeyValuePair<string, List<SugarParameter>> ToSql();
|
||||
|
||||
|
||||
DataTable ToDataTable();
|
||||
|
@ -90,23 +90,7 @@ namespace SqlSugar
|
||||
return _Ado;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Lambda Expressions operation
|
||||
/// </summary>
|
||||
public virtual ILambdaExpressions LambdaExpressions
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_LambdaExpressions == null)
|
||||
{
|
||||
var reval = InstanceFactory.GetLambdaExpressions(base.CurrentConnectionConfig);
|
||||
reval.Context = this;
|
||||
_LambdaExpressions = reval;
|
||||
return reval;
|
||||
}
|
||||
return _LambdaExpressions;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lambda Query operation
|
||||
/// </summary>
|
||||
@ -120,6 +104,7 @@ namespace SqlSugar
|
||||
reval.SqlBuilder.LambadaQueryBuilder.Builder = sqlBuilder;
|
||||
reval.SqlBuilder.Context = reval.SqlBuilder.LambadaQueryBuilder.Context = this;
|
||||
reval.SqlBuilder.LambadaQueryBuilder.EntityName = typeof(T).Name;
|
||||
reval.SqlBuilder.LambadaQueryBuilder.LambdaExpressions = InstanceFactory.GetLambdaExpressions(base.CurrentConnectionConfig);
|
||||
return reval;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user