Browse Source

-

pull/12/MERGE
sunkaixuan 8 years ago
parent
commit
6b07a03c02
  1. 18
      OrmTest/UnitTest/Query/SelectQuery.cs
  2. 47
      SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  3. 20
      SqlSugar/Abstract/SqlBuilderProvider/DMLBuilder/QueryBuilder.cs
  4. 4
      SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs
  5. 6
      SqlSugar/ExpressionsToSql/ResolveItems/MemberInitExpressionResolve.cs
  6. 9
      SqlSugar/Interface/IQueryable.cs
  7. 1
      SqlSugar/Interface/ISqlBuilder/ISqlBuilder.cs

18
OrmTest/UnitTest/Query/SelectQuery.cs

@ -38,20 +38,24 @@ namespace OrmTest.UnitTest
#region dr ot entity #region dr ot entity
db.IgnoreComumns.Add("TestId", "Student"); db.IgnoreComumns.Add("TestId", "Student");
var s1 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Name=it.Name,Student=it}).ToList(); var s1 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Name = it.Name, Student = it }).ToList();
var s2 = db.Queryable<Student>().Select(it => new { id=it.Id,w=new { x=it } }).ToList(); var s2 = db.Queryable<Student>().Select(it => new { id = it.Id, w = new { x = it } }).ToList();
var s3 = db.Queryable<Student>().Select(it => new { newid = it.Id }).ToList(); var s3 = db.Queryable<Student>().Select(it => new { newid = it.Id }).ToList();
var s4 = db.Queryable<Student>().Select(it => new { newid = it.Id, obj = it }).ToList(); var s4 = db.Queryable<Student>().Select(it => new { newid = it.Id, obj = it }).ToList();
var s5 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Student = it, Name =it.Name }).ToList(); var s5 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Student = it, Name = it.Name }).ToList();
#endregion #endregion
#region sql and parameters validate #region sql and parameters validate
var ss0 = db.Queryable<Student, School>((st,sc)=>new object[] { var ss0 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Inner,st.Id==sc.Id JoinType.Inner,st.Id==sc.Id
}).GroupBy(st => st.Id).Select(st => new { avgId=NBORM.AggregateAvg(st.Id) }).ToSql(); }).GroupBy(st => st.Id).Having(sc => NBORM.AggregateAvg(sc.Id) == 1).Select(st => new { avgId = NBORM.AggregateAvg(st.Id) }).ToSql();
base.Check(" SELECT AVG([st].[Id]) AS [avgId] FROM [Student] st Inner JOIN School sc ON ( [st].[Id] = [sc].[Id] ) GROUP BY [st].[Id] ", null, base.Check("SELECT AVG([st].[Id]) AS [avgId] FROM [Student] st Inner JOIN School sc ON ( [st].[Id] = [sc].[Id] ) GROUP BY [st].[Id] HAVING (AVG([sc].[Id]) = @Const0 ) ",
ss0.Key, null," ss0 Error"); new List<SugarParameter>() {
new SugarParameter("@Const0",1)
}
,
ss0.Key, ss0.Value, " ss0 Error");
var ss1 = db.Queryable<School, School>((st, st2) => new object[] { var ss1 = db.Queryable<School, School>((st, st2) => new object[] {

47
SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs

@ -99,6 +99,45 @@ namespace SqlSugar
this._Where(expression); this._Where(expression);
return this; return this;
} }
public ISugarQueryable<T> Having(Expression<Func<T, bool>> expression) {
this._Having(expression);
return this;
}
public ISugarQueryable<T> Having(string whereString, object whereObj = null)
{
QueryBuilder.HavingInfos = SqlBuilder.AppendHaving(whereString);
if (whereObj != null)
QueryBuilder.QueryPars.AddRange(Context.Database.GetParameters(whereObj));
return this;
}
public ISugarQueryable<T> Having<T2>(Expression<Func<T2, bool>> expression)
{
this._Having(expression);
return this;
}
public ISugarQueryable<T> Having<T2, T3>(Expression<Func<T2, T3, bool>> expression)
{
this._Having(expression);
return this;
}
public ISugarQueryable<T> Having<T2, T3, T4>(Expression<Func<T2, T3, T4, bool>> expression)
{
this._Having(expression);
return this;
}
public ISugarQueryable<T> Having<T2, T3, T4, T5>(Expression<Func<T2, T3, T4, T5, bool>> expression)
{
this._Having(expression);
return this;
}
public ISugarQueryable<T> Having<T2, T3, T4, T5, T6>(Expression<Func<T2, T3, T4, T5, T6, bool>> expression)
{
this._Having(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; if (!isWhere) return this;
@ -539,7 +578,13 @@ namespace SqlSugar
GroupBy(lamResult.GetResultString()); GroupBy(lamResult.GetResultString());
return this; return this;
} }
protected ISugarQueryable<T> _Having(Expression expression)
{
var isSingle = QueryBuilder.IsSingle();
var lamResult = QueryBuilder.GetExpressionValue(expression, isSingle ? ResolveExpressType.WhereSingle : ResolveExpressType.WhereMultiple);
Having(lamResult.GetResultString());
return this;
}
private List<TResult> _ToList<TResult>() private List<TResult> _ToList<TResult>()
{ {
var sqlObj = this.ToSql(); var sqlObj = this.ToSql();

20
SqlSugar/Abstract/SqlBuilderProvider/DMLBuilder/QueryBuilder.cs

@ -19,6 +19,7 @@ namespace SqlSugar
#region Private Fileds #region Private Fileds
private List<JoinQueryInfo> _JoinQueryInfos; private List<JoinQueryInfo> _JoinQueryInfos;
private List<string> _WhereInfos; private List<string> _WhereInfos;
private string _HavingInfos;
private string _TableNameString; private string _TableNameString;
#endregion #endregion
@ -61,6 +62,17 @@ namespace SqlSugar
} }
set { _WhereInfos = value; } set { _WhereInfos = value; }
} }
public virtual string HavingInfos
{
get
{
return _HavingInfos;
}
set
{
_HavingInfos = value;
}
}
#endregion #endregion
#region Lambada Type #region Lambada Type
@ -155,8 +167,10 @@ namespace SqlSugar
return "AVG({0})"; return "AVG({0})";
} }
} }
public virtual string InTemplate { public virtual string InTemplate
get { {
get
{
return "{0} IN ({1}) "; return "{0} IN ({1}) ";
} }
} }
@ -185,7 +199,7 @@ namespace SqlSugar
public virtual string ToSqlString() public virtual string ToSqlString()
{ {
sql = new StringBuilder(); sql = new StringBuilder();
sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString,(Skip!=null||Take!=null)?null:GetOrderByString); sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString+HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString);
if (IsCount) { return sql.ToString(); } if (IsCount) { return sql.ToString(); }
if (Skip != null && Take == null) if (Skip != null && Take == null)
{ {

4
SqlSugar/Abstract/SqlBuilderProvider/SqlBuilderProvider.cs

@ -22,6 +22,10 @@ namespace SqlSugar
{ {
return isWhere ? (" WHERE " + sqlString ):( " AND " + sqlString); return isWhere ? (" WHERE " + sqlString ):( " AND " + sqlString);
} }
public string AppendHaving(string sqlString)
{
return " HAVING " + sqlString;
}
public DeleteBuilder DeleteBuilder public DeleteBuilder DeleteBuilder
{ {

6
SqlSugar/ExpressionsToSql/ResolveItems/MemberInitExpressionResolve.cs

@ -54,6 +54,12 @@ namespace SqlSugar
parameter.Context.Result.Append(base.Context.GetAsString(memberName, parameterName)); parameter.Context.Result.Append(base.Context.GetAsString(memberName, parameterName));
this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData)); this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
} }
else if (item is MethodCallExpression)
{
base.Expression = item;
base.Start();
parameter.Context.Result.Append(base.Context.GetAsString(memberName, parameter.CommonTempData.ObjToString()));
}
else if (item is MemberExpression) else if (item is MemberExpression)
{ {
if (base.Context.Result.IsLockCurrentParameter == false) if (base.Context.Result.IsLockCurrentParameter == false)

9
SqlSugar/Interface/IQueryable.cs

@ -24,6 +24,15 @@ namespace SqlSugar
ISugarQueryable<T> Where<T2, T3, T4>(Expression<Func<T2, T3, T4, bool>> expression); ISugarQueryable<T> Where<T2, T3, T4>(Expression<Func<T2, T3, T4, bool>> expression);
ISugarQueryable<T> Where<T2, T3, T4, T5>(Expression<Func<T2, T3, T4, T5, bool>> expression) ; ISugarQueryable<T> Where<T2, T3, T4, T5>(Expression<Func<T2, T3, T4, T5, bool>> expression) ;
ISugarQueryable<T> Where<T2, T3, T4, T5, T6>(Expression<Func<T2, T3, T4, T5, T6, bool>> expression) ; ISugarQueryable<T> Where<T2, T3, T4, T5, T6>(Expression<Func<T2, T3, T4, T5, T6, bool>> expression) ;
ISugarQueryable<T> Having(Expression<Func<T, bool>> expression);
ISugarQueryable<T> Having(string whereString, object whereObj = null);
ISugarQueryable<T> Having<T2>(Expression<Func<T2, bool>> expression);
ISugarQueryable<T> Having<T2, T3>(Expression<Func<T2, T3, bool>> expression);
ISugarQueryable<T> Having<T2, T3, T4>(Expression<Func<T2, T3, T4, bool>> expression);
ISugarQueryable<T> Having<T2, T3, T4, T5>(Expression<Func<T2, T3, T4, T5, bool>> expression);
ISugarQueryable<T> Having<T2, T3, T4, T5, T6>(Expression<Func<T2, T3, T4, T5, T6, bool>> expression);
ISugarQueryable<T> WhereIF(bool isWhere, Expression<Func<T, bool>> expression); ISugarQueryable<T> WhereIF(bool isWhere, Expression<Func<T, bool>> expression);
ISugarQueryable<T> WhereIF(bool isWhere,string whereString, object whereObj = null); ISugarQueryable<T> WhereIF(bool isWhere,string whereString, object whereObj = null);
ISugarQueryable<T> WhereIF<T2>(bool isWhere, Expression<Func<T2, bool>> expression); ISugarQueryable<T> WhereIF<T2>(bool isWhere, Expression<Func<T2, bool>> expression);

1
SqlSugar/Interface/ISqlBuilder/ISqlBuilder.cs

@ -11,6 +11,7 @@ namespace SqlSugar
SqlSugarClient Context { get; set; } SqlSugarClient Context { get; set; }
CommandType CommandType { get; set; } CommandType CommandType { get; set; }
String AppendWhereOrAnd(bool isWhere, string sqlString); String AppendWhereOrAnd(bool isWhere, string sqlString);
string AppendHaving(string sqlString);
SqlQueryBuilder SqlQueryBuilder { get; set; } SqlQueryBuilder SqlQueryBuilder { get; set; }
QueryBuilder QueryBuilder { get; set; } QueryBuilder QueryBuilder { get; set; }
InsertBuilder InsertBuilder { get; set; } InsertBuilder InsertBuilder { get; set; }

Loading…
Cancel
Save