add ToTreeList tests #268

This commit is contained in:
28810
2020-04-10 13:09:08 +08:00
parent f9566af7d1
commit 4e4240ff7a
24 changed files with 1291 additions and 283 deletions

View File

@ -148,7 +148,7 @@ namespace FreeSql.Tests
//֧<><D6A7> 1<>Զ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
using (var ctx = new FreeContext(g.sqlite))
using (var ctx = g.sqlite.CreateDbContext())
{
var tags = ctx.Set<Tag>().Select.IncludeMany(a => a.Tags).ToList();
@ -177,7 +177,7 @@ namespace FreeSql.Tests
{
//<2F><>ѯ 1<>Զ࣬<D4B6>ټ<EFBFBD><D9BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
using (var ctx = new FreeContext(g.sqlite))
using (var ctx = g.sqlite.CreateDbContext())
{
var tag = ctx.Set<Tag>().Select.First();

View File

@ -1848,5 +1848,90 @@ WHERE ((b.`IsFinished` OR a.`TaskType` = 3) AND b.`EnabledMark` = 1)", groupsql1
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.mysql;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1698,5 +1698,90 @@ WHERE (((to_char(a.""ID"")) in (SELECT b.""TITLE""
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.dameng;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1510,5 +1510,90 @@ WHERE (((cast(a.[Id] as nvarchar)) in (SELECT b.[Title]
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.odbc;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1859,5 +1859,90 @@ WHERE ((b.`IsFinished` OR a.`TaskType` = 3) AND b.`EnabledMark` = 1)", groupsql1
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.mysql;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1699,5 +1699,90 @@ WHERE (((to_char(a.""ID"")) in (SELECT b.""TITLE""
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.oracle;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1758,5 +1758,90 @@ WHERE ((((a.""id"")::varchar) in (SELECT b.""title""
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.pgsql;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1649,5 +1649,90 @@ WHERE (((cast(a.[Id] as nvarchar)) in (SELECT b.[Title]
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.sqlserver;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1511,5 +1511,90 @@ WHERE (((cstr(a.[Id])) in (SELECT b.[Title]
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.msaccess;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1907,5 +1907,90 @@ WHERE ((b.`IsFinished` OR a.`TaskType` = 3) AND b.`EnabledMark` = 1)", groupsql1
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.mysql;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1699,5 +1699,90 @@ WHERE (((to_char(a.""ID"")) in (SELECT b.""TITLE""
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.oracle;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1775,5 +1775,89 @@ WHERE ((((a.""id"")::varchar) in (SELECT b.""title""
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.pgsql;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1730,5 +1730,90 @@ WHERE (((cast(a.[Id] as nvarchar)) in (SELECT b.[Title]
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.sqlserver;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1846,5 +1846,90 @@ WHERE (((cast(a.""Id"" as character)) in (SELECT b.""Title""
orm.Select<ToUpd1Pk>().ForUpdate(true).Limit(1).ToList();
});
}
[Fact]
public void ToTreeList()
{
var fsql = g.sqlite;
fsql.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = fsql.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = fsql.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = fsql.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}

View File

@ -1,111 +0,0 @@
using FreeSql.DataAnnotations;
using FreeSql;
using System;
using System.Collections.Generic;
using Xunit;
using System.Linq;
using Newtonsoft.Json.Linq;
using NpgsqlTypes;
using Npgsql.LegacyPostgis;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Data.SqlClient;
using kwlib;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace FreeSql.Tests
{
public class UnitTest4
{
[Fact]
public void Test04()
{
g.sqlite.Delete<BaseDistrict>().Where("1=1").ExecuteAffrows();
var repo = g.sqlite.GetRepository<VM_District_Child>();
repo.DbContextOptions.EnableAddOrUpdateNavigateList = true;
repo.DbContextOptions.NoneParameter = true;
repo.Insert(new VM_District_Child
{
Code = "100000",
Name = "中国",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child
{
Code = "110000",
Name = "北京市",
Childs = new List<VM_District_Child>(new[] {
new VM_District_Child{ Code="110100", Name = "北京市" },
new VM_District_Child{ Code="110101", Name = "东城区" },
})
}
})
});
var t1 = g.sqlite.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t1);
Assert.Equal("110101", t1[0].Code);
Assert.NotNull(t1[0].Parent);
Assert.Equal("110000", t1[0].Parent.Code);
var t2 = g.sqlite.Select<VM_District_Parent>()
.InnerJoin(a => a.ParentCode == a.Parent.Code)
.InnerJoin(a => a.Parent.ParentCode == a.Parent.Parent.Code)
.Where(a => a.Code == "110101")
.ToList(true);
Assert.Single(t2);
Assert.Equal("110101", t2[0].Code);
Assert.NotNull(t2[0].Parent);
Assert.Equal("110000", t2[0].Parent.Code);
Assert.NotNull(t2[0].Parent.Parent);
Assert.Equal("100000", t2[0].Parent.Parent.Code);
var t3 = g.sqlite.Select<VM_District_Child>().ToTreeList();
Assert.Single(t3);
Assert.Equal("100000", t3[0].Code);
Assert.Single(t3[0].Childs);
Assert.Equal("110000", t3[0].Childs[0].Code);
Assert.Equal(2, t3[0].Childs[0].Childs.Count);
Assert.Equal("110100", t3[0].Childs[0].Childs[0].Code);
Assert.Equal("110101", t3[0].Childs[0].Childs[1].Code);
}
[Table(Name = "D_District")]
public class BaseDistrict
{
[Column(IsPrimary = true, StringLength = 6)]
public string Code { get; set; }
[Column(StringLength = 20, IsNullable = false)]
public string Name { get; set; }
[Column(StringLength = 6)]
public virtual string ParentCode { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Child : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public List<VM_District_Child> Childs { get; set; }
}
[Table(Name = "D_District", DisableSyncStructure = true)]
public class VM_District_Parent : BaseDistrict
{
public override string ParentCode { get => base.ParentCode; set => base.ParentCode = value; }
[Navigate(nameof(ParentCode))]
public VM_District_Parent Parent { get; set; }
}
}
}