Skip to content

Commit

Permalink
- 增加 IncludeByPropertyName 按属性名进行 Include/IncludeMany 操作;#278
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Nov 4, 2020
1 parent 856ecf2 commit b701ad8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
25 changes: 25 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/UnitTest3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ public class tcate01
{
[Column(IsIdentity = true)]
public int? Id { get; set; }

public string name { get; set; }
[Navigate(nameof(tshop01.cateId))]
public List<tshop01> tshops { get; set; }
}
public class tshop01
{
Expand All @@ -203,7 +207,28 @@ public class tshop01
[Fact]
public void Test03()
{
g.sqlite.Delete<tcate01>().Where("1=1").ExecuteAffrows();
g.sqlite.Delete<tshop01>().Where("1=1").ExecuteAffrows();
var tshoprepo = g.sqlite.GetRepository<tcate01>();
tshoprepo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
tshoprepo.Insert(new tcate01[]
{
new tcate01 { name = "tcate1", tshops = new List<tshop01>{ new tshop01(), new tshop01(), new tshop01() } },
new tcate01 { name = "tcate1", tshops = new List<tshop01>{ new tshop01(), new tshop01(), new tshop01() } }
});

var tshop01sql = g.sqlite.Select<tshop01>().Include(a => a.cate).ToSql();
var tshop02sql = g.sqlite.Select<tshop01>().IncludeByPropertyName("cate").ToSql();

var tshop03sql = g.sqlite.Select<tshop01>().IncludeMany(a => a.cate.tshops).ToSql();
var tshop04sql = g.sqlite.Select<tshop01>().IncludeByPropertyName("cate.tshops").ToSql();

var tshop01lst = g.sqlite.Select<tshop01>().Include(a => a.cate).ToList();
var tshop02lst = g.sqlite.Select<tshop01>().IncludeByPropertyName("cate").ToList();

var tshop03lst = g.sqlite.Select<tshop01>().IncludeMany(a => a.cate.tshops).ToList();
var tshop04lst = g.sqlite.Select<tshop01>().IncludeByPropertyName("cate.tshops").ToList();



var testisnullsql1 = g.sqlite.Select<t102>().Where(a => SqlExt.IsNull(a.isxx, false).Equals( true)).ToSql();
Expand Down
7 changes: 7 additions & 0 deletions FreeSql/FreeSql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions FreeSql/Interface/Curd/ISelect/ISelect1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ public interface ISelect<T1> : ISelect0<ISelect<T1>, T1>
/// <returns></returns>
ISelect<T1> IncludeMany<TNavigate>(Expression<Func<T1, IEnumerable<TNavigate>>> navigateSelector, Action<ISelect<TNavigate>> then = null) where TNavigate : class;

/// <summary>
/// 按属性名字符串进行 Include/IncludeMany 操作
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
ISelect<T1> IncludeByPropertyName(string property);

/// <summary>
/// 实现 select .. from ( select ... from t ) a 这样的功能<para></para>
/// 使用 AsTable 方法也可以达到效果<para></para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ public static void CopyData(Select0Provider from, Select0Provider to, ReadOnlyCo
to._whereGlobalFilter = new List<GlobalFilter.Item>(from._whereGlobalFilter.ToArray());
}

public Expression ConvertStringPropertyToExpression(string property)
public Expression ConvertStringPropertyToExpression(string property, bool fromFirstTable = false)
{
if (string.IsNullOrEmpty(property)) return null;
var field = property.Split('.').Select(a => a.Trim()).ToArray();
Expression exp = null;

if (field.Length == 1)
if (field.Length == 1 && fromFirstTable == false)
{
foreach (var tb in _tables)
{
Expand Down
30 changes: 30 additions & 0 deletions FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,36 @@ public bool Any(Expression<Func<T1, bool>> exp)

public int InsertInto<TTargetEntity>(string tableName, Expression<Func<T1, TTargetEntity>> select) where TTargetEntity : class => base.InternalInsertInto<TTargetEntity>(tableName, select);

public ISelect<T1> IncludeByPropertyName(string property)
{
var exp = ConvertStringPropertyToExpression(property, true);
if (exp == null) throw new ArgumentException($"{nameof(property)} 无法解析为表达式树");
var memExp = exp as MemberExpression;
if (memExp == null) throw new ArgumentException($"{nameof(property)} 无法解析为表达式树2");
var parTb = _commonUtils.GetTableByEntity(memExp.Expression.Type);
if (parTb == null) throw new ArgumentException($"{nameof(property)} 无法解析为表达式树3");
var parTbref = parTb.GetTableRef(memExp.Member.Name, true);
if (parTbref == null) throw new ArgumentException($"{nameof(property)} 不是有效的导航属性");
switch (parTbref.RefType)
{
case TableRefType.ManyToMany:
case TableRefType.OneToMany:
var funcType = typeof(Func<,>).MakeGenericType(_tables[0].Table.Type, typeof(IEnumerable<>).MakeGenericType(parTbref.RefEntityType));
var navigateSelector = Expression.Lambda(funcType, exp, _tables[0].Parameter);
var incMethod = this.GetType().GetMethod("IncludeMany");
if (incMethod == null) throw new Exception("运行时错误,反射获取 IncludeMany 方法失败");
incMethod.MakeGenericMethod(parTbref.RefEntityType).Invoke(this, new object[] { navigateSelector, null });
break;
case TableRefType.ManyToOne:
case TableRefType.OneToOne:
_isIncluded = true;
var curTb = _commonUtils.GetTableByEntity(exp.Type);
_commonExpression.ExpressionWhereLambda(_tables, Expression.MakeMemberAccess(exp, curTb.Properties[curTb.ColumnsByCs.First().Value.CsName]), null, null, null);
break;
}
return this;
}

bool _isIncluded = false;
public ISelect<T1> Include<TNavigate>(Expression<Func<T1, TNavigate>> navigateSelector) where TNavigate : class
{
Expand Down

0 comments on commit b701ad8

Please sign in to comment.