Skip to content

Commit

Permalink
修改SqlBuilderCore,支持简单的单表linq语法查询,具体参考查询单元测试;
Browse files Browse the repository at this point in the history
  • Loading branch information
zqlovejyc committed Oct 20, 2020
1 parent be6258a commit fe718db
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 111 deletions.
19 changes: 19 additions & 0 deletions SQLBuilder.Core.UnitTest/SelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,25 @@ public void Test_Select_82()
Assert.AreEqual("SELECT [A].[Id],[A].[Name] AS [UserName] FROM [Base_UserInfo] AS [A] INNER JOIN [Base_Account] AS [B] ON [A].[Id] = [B].[UserId] AND [A].[Name] LIKE '%' + @Parameter1 + '%' WHERE ([A].[Name] LIKE '%' + @Parameter2 + '%') AND ([A].[Email] = @Parameter3)", builder.Sql);
Assert.AreEqual(3, builder.Parameters.Count);
}

/// <summary>
/// 查询83,linq写法,注意目前仅支持单表查询
/// </summary>
[TestMethod]
public void Test_Select_83()
{
var query = from a in new SqlBuilderCore<UserInfo>(DatabaseType.SqlServer, true)
where a.Id == 1
orderby new { a.Id, a.Email } ascending
select new
{
a.Name,
Address = a.Email
};

Assert.AreEqual("SELECT [A].[Name],[A].[Email] AS [Address] FROM [Base_UserInfo] AS [A] WHERE [A].[Id] = @Parameter1 ORDER BY [A].[Id] ASC,[A].[Email] ASC", query.Sql);
Assert.AreEqual(1, query.Parameters.Count);
}
#endregion

#region Page
Expand Down
189 changes: 78 additions & 111 deletions SQLBuilder.Core/Entry/SqlBuilderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public void Clear()
/// <returns>string</returns>
private string Select(params Type[] array)
{
this.sqlWrapper.Clear();
this.sqlWrapper.IsSingleTable = false;
if (array?.Length > 0)
{
Expand All @@ -178,19 +177,37 @@ private string Select(params Type[] array)
/// Select
/// </summary>
/// <param name="expression">表达式树</param>
/// <param name="sql">sql语句</param>
/// <returns>SqlBuilderCore</returns>
public SqlBuilderCore<T> Select(Expression expression = null)
public SqlBuilderCore<T> Select(Expression expression = null, string sql = null)
{
var sql = this.Select(typeof(T));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
var len = this.sqlWrapper.Sql.Length;

if (sql == null)
sql = this.Select(typeof(T));

var selectFields = "*";
if (expression != null)
{
SqlExpressionProvider.Select(expression, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
selectFields = this.sqlWrapper.SelectFieldsStr;

//移除默认的查询语句
if (len > 0)
{
var sqlReplace = string.Format(this.Select(typeof(T)), "*");
var sqlNew = this.sqlWrapper.Sql.ToString().Replace(sqlReplace, "");
this.sqlWrapper.Sql = new StringBuilder(sqlNew);
}
}

sql = string.Format(sql, selectFields);

if (len == 0)
this.sqlWrapper.Sql.Append(sql);
else
this.sqlWrapper.Sql = new StringBuilder($"{sql}{this.sqlWrapper.Sql}");

return this;
}

Expand All @@ -201,17 +218,7 @@ public SqlBuilderCore<T> Select(Expression expression = null)
/// <returns>SqlBuilderCore</returns>
public SqlBuilderCore<T> Select(Expression<Func<T, object>> expression = null)
{
var sql = this.Select(typeof(T));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
{
SqlExpressionProvider.Select(expression.Body, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
}
return this;
return this.Select(expression?.Body);
}

/// <summary>
Expand All @@ -224,16 +231,7 @@ public SqlBuilderCore<T> Select<T2>(Expression<Func<T, T2, object>> expression =
where T2 : class
{
var sql = this.Select(typeof(T), typeof(T2));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
{
SqlExpressionProvider.Select(expression.Body, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
}
return this;
return this.Select(expression.Body, sql);
}

/// <summary>
Expand All @@ -248,16 +246,7 @@ public SqlBuilderCore<T> Select<T2, T3>(Expression<Func<T, T2, T3, object>> expr
where T3 : class
{
var sql = this.Select(typeof(T), typeof(T2), typeof(T3));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
{
SqlExpressionProvider.Select(expression.Body, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
}
return this;
return this.Select(expression.Body, sql);
}

/// <summary>
Expand All @@ -274,16 +263,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4>(Expression<Func<T, T2, T3, T4, objec
where T4 : class
{
var sql = this.Select(typeof(T), typeof(T2), typeof(T3), typeof(T4));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
{
SqlExpressionProvider.Select(expression.Body, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
}
return this;
return this.Select(expression.Body, sql);
}

/// <summary>
Expand All @@ -302,16 +282,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5>(Expression<Func<T, T2, T3, T4, T
where T5 : class
{
var sql = this.Select(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
{
SqlExpressionProvider.Select(expression.Body, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
}
return this;
return this.Select(expression.Body, sql);
}

/// <summary>
Expand All @@ -332,16 +303,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5, T6>(Expression<Func<T, T2, T3, T
where T6 : class
{
var sql = this.Select(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
{
SqlExpressionProvider.Select(expression.Body, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
}
return this;
return this.Select(expression.Body, sql);
}

/// <summary>
Expand All @@ -364,16 +326,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5, T6, T7>(Expression<Func<T, T2, T
where T7 : class
{
var sql = this.Select(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
{
SqlExpressionProvider.Select(expression.Body, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
}
return this;
return this.Select(expression.Body, sql);
}

/// <summary>
Expand All @@ -398,16 +351,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5, T6, T7, T8>(Expression<Func<T, T
where T8 : class
{
var sql = this.Select(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
{
SqlExpressionProvider.Select(expression.Body, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
}
return this;
return this.Select(expression.Body, sql);
}

/// <summary>
Expand All @@ -434,16 +378,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5, T6, T7, T8, T9>(Expression<Func<
where T9 : class
{
var sql = this.Select(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
{
SqlExpressionProvider.Select(expression.Body, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
}
return this;
return this.Select(expression.Body, sql);
}

/// <summary>
Expand Down Expand Up @@ -472,16 +407,7 @@ public SqlBuilderCore<T> Select<T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression<
where T10 : class
{
var sql = this.Select(typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10));
if (expression == null)
{
this.sqlWrapper.Sql.AppendFormat(sql, "*");
}
else
{
SqlExpressionProvider.Select(expression.Body, this.sqlWrapper);
this.sqlWrapper.Sql.AppendFormat(sql, this.sqlWrapper.SelectFieldsStr);
}
return this;
return this.Select(expression.Body, sql);
}
#endregion

Expand Down Expand Up @@ -787,6 +713,9 @@ public SqlBuilderCore<T> FullJoin<T2, T3>(Expression<Func<T2, T3, bool>> express
/// <returns></returns>
public SqlBuilderCore<T> Where(string sql)
{
if (this.sqlWrapper.Sql.Length == 0)
this.Select(expression: null);

this.sqlWrapper += " WHERE ";
this.sqlWrapper += sql;
return this;
Expand All @@ -800,6 +729,9 @@ public SqlBuilderCore<T> Where(string sql)
/// <returns></returns>
public SqlBuilderCore<T> Where(string sql, ref bool hasWhere)
{
if (this.sqlWrapper.Sql.Length == 0)
this.Select(expression: null);

if (hasWhere)
this.sqlWrapper += " AND ";
else
Expand All @@ -820,6 +752,9 @@ public SqlBuilderCore<T> Where(string sql, ref bool hasWhere)
/// <returns></returns>
public SqlBuilderCore<T> Where(StringBuilder sql)
{
if (this.sqlWrapper.Sql.Length == 0)
this.Select(expression: null);

this.sqlWrapper += " WHERE ";
this.sqlWrapper.Sql.Append(sql);
return this;
Expand All @@ -833,6 +768,9 @@ public SqlBuilderCore<T> Where(StringBuilder sql)
/// <returns></returns>
public SqlBuilderCore<T> Where(StringBuilder sql, ref bool hasWhere)
{
if (this.sqlWrapper.Sql.Length == 0)
this.Select(expression: null);

if (hasWhere)
this.sqlWrapper += " AND ";
else
Expand All @@ -852,6 +790,9 @@ public SqlBuilderCore<T> Where(StringBuilder sql, ref bool hasWhere)
/// <param name="expression">表达式树</param>
public SqlBuilderCore<T> Where(Expression expression)
{
if (this.sqlWrapper.Sql.Length == 0)
this.Select(expression: null);

if (!(expression.NodeType == ExpressionType.Constant && expression.ToObject() is bool b && b))
{
this.sqlWrapper += " WHERE ";
Expand All @@ -869,6 +810,9 @@ public SqlBuilderCore<T> Where(Expression expression)
/// <param name="hasWhere">指定是否已包含where关键字</param>
public SqlBuilderCore<T> Where(Expression expression, ref bool hasWhere)
{
if (this.sqlWrapper.Sql.Length == 0)
this.Select(expression: null);

if (!(expression.NodeType == ExpressionType.Constant && expression.ToObject() is bool b && b))
{
if (hasWhere)
Expand Down Expand Up @@ -3930,6 +3874,29 @@ public SqlBuilderCore<T> OrderBy<T2, T3, T4, T5, T6, T7, T8, T9, T10>(Expression
}
#endregion

#region OrderByDescending
/// <summary>
/// OrderByDescending
/// </summary>
/// <param name="expression">表达式树</param>
/// <returns>SqlBuilderCore</returns>
public SqlBuilderCore<T> OrderByDescending(Expression<Func<T, object>> expression)
{
var orders = new List<OrderType>();
if (expression?.Body is NewExpression newExpression)
{
for (int i = 0; i < newExpression.Arguments.Count; i++)
{
orders.Add(OrderType.Descending);
}
}
else
orders.Add(OrderType.Descending);

return this.OrderBy(expression.Body, orders.ToArray());
}
#endregion

#region Page
/// <summary>
/// Page
Expand Down Expand Up @@ -4039,7 +4006,7 @@ public SqlBuilderCore<T> PageByWith(int pageSize, int pageIndex, string orderFie
if (!sql.IsNullOrEmpty())
{
this.sqlWrapper.DbParameters.Clear();
if (parameters != null)
if (parameters != null)
this.sqlWrapper.DbParameters = parameters;
}
sql = sql.IsNullOrEmpty() ? this.sqlWrapper.Sql.ToString().TrimEnd(';') : sql.TrimEnd(';');
Expand Down

0 comments on commit fe718db

Please sign in to comment.