Skip to content

Commit 7ac0d62

Browse files
2881028810
authored andcommitted
增加 IncludeMany 变异多级单元测试
1 parent fe016c0 commit 7ac0d62

File tree

7 files changed

+218
-24
lines changed

7 files changed

+218
-24
lines changed

FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,9 +901,48 @@ public void AsTable() {
901901
Assert.Equal("SELECT a.`Id`, a.`Clicks`, a.`TypeGuid`, a.`Title`, a.`CreateTime` FROM `tb_topicAsTable1` a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = ?bname", sql);
902902
}
903903

904+
public class TestInclude_OneToManyModel1 {
905+
[Column(IsIdentity = true)]
906+
public int id { get; set; }
907+
public virtual TestInclude_OneToManyModel2 model2 { get; set; }
908+
909+
public string m1name { get; set; }
910+
}
911+
public class TestInclude_OneToManyModel2 {
912+
[Column(IsPrimary = true)]
913+
public int model2id { get; set; }
914+
public virtual TestInclude_OneToManyModel1 model1 { get; set; }
915+
916+
public string m2setting { get; set; }
917+
918+
public List<TestInclude_OneToManyModel3> childs { get; set; }
919+
}
920+
public class TestInclude_OneToManyModel3 {
921+
[Column(IsIdentity = true)]
922+
public int id { get; set; }
923+
924+
public int model2111Idaaa { get; set; }
925+
public string title { get; set; }
926+
}
927+
904928
[Fact]
905929
public void Include_OneToMany() {
930+
var model1 = new TestInclude_OneToManyModel1 { m1name = DateTime.Now.Second.ToString() };
931+
model1.id = (int)g.mysql.Insert(model1).ExecuteIdentity();
932+
var model2 = new TestInclude_OneToManyModel2 { model2id = model1.id, m2setting = DateTime.Now.Second.ToString() };
933+
g.mysql.Insert(model2).ExecuteAffrows();
934+
935+
var model3s = new[] {
936+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__111" },
937+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__222" },
938+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__333" }
939+
};
940+
Assert.Equal(3, g.mysql.Insert(model3s).ExecuteAffrows());
906941

942+
var t1 = g.mysql.Select<TestInclude_OneToManyModel1>()
943+
.IncludeMany(a => a.model2.childs.Where(m3 => m3.model2111Idaaa == a.model2.model2id))
944+
.Where(a => a.id <= model1.id)
945+
.ToList();
907946
}
908947
[Fact]
909948
public void Include_OneToChilds() {

FreeSql.Tests/Oracle/Curd/OracleSelectTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,9 +794,48 @@ public void AsTable() {
794794
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", sql);
795795
}
796796

797+
public class TiOtmModel1 {
798+
[Column(IsIdentity = true)]
799+
public int id { get; set; }
800+
public virtual TiOtmModel2 model2 { get; set; }
801+
802+
public string m1name { get; set; }
803+
}
804+
public class TiOtmModel2 {
805+
[Column(IsPrimary = true)]
806+
public int model2id { get; set; }
807+
public virtual TiOtmModel1 model1 { get; set; }
808+
809+
public string m2setting { get; set; }
810+
811+
public List<TiOtmModel3> childs { get; set; }
812+
}
813+
public class TiOtmModel3 {
814+
[Column(IsIdentity = true)]
815+
public int id { get; set; }
816+
817+
public int model2111Idaaa { get; set; }
818+
public string title { get; set; }
819+
}
820+
797821
[Fact]
798822
public void Include_OneToMany() {
823+
var model1 = new TiOtmModel1 { m1name = DateTime.Now.Second.ToString() };
824+
model1.id = (int)g.oracle.Insert(model1).ExecuteIdentity();
825+
var model2 = new TiOtmModel2 { model2id = model1.id, m2setting = DateTime.Now.Second.ToString() };
826+
g.oracle.Insert(model2).ExecuteAffrows();
827+
828+
var model3s = new[] {
829+
new TiOtmModel3{ model2111Idaaa = model1.id, title = "testmodel3__111" },
830+
new TiOtmModel3{ model2111Idaaa = model1.id, title = "testmodel3__222" },
831+
new TiOtmModel3{ model2111Idaaa = model1.id, title = "testmodel3__333" }
832+
};
833+
Assert.Equal(3, g.oracle.Insert(model3s).ExecuteAffrows());
799834

835+
var t1 = g.oracle.Select<TiOtmModel1>()
836+
.IncludeMany(a => a.model2.childs.Where(m3 => m3.model2111Idaaa == a.model2.model2id))
837+
.Where(a => a.id <= model1.id)
838+
.ToList();
800839
}
801840
[Fact]
802841
public void Include_OneToChilds() {

FreeSql.Tests/PostgreSQL/Curd/PostgreSQLSelectTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,9 +860,48 @@ public void AsTable() {
860860
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" a LEFT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\" and b.\"name\" = @bname", sql);
861861
}
862862

863+
public class TestInclude_OneToManyModel1 {
864+
[Column(IsIdentity = true)]
865+
public int id { get; set; }
866+
public virtual TestInclude_OneToManyModel2 model2 { get; set; }
867+
868+
public string m1name { get; set; }
869+
}
870+
public class TestInclude_OneToManyModel2 {
871+
[Column(IsPrimary = true)]
872+
public int model2id { get; set; }
873+
public virtual TestInclude_OneToManyModel1 model1 { get; set; }
874+
875+
public string m2setting { get; set; }
876+
877+
public List<TestInclude_OneToManyModel3> childs { get; set; }
878+
}
879+
public class TestInclude_OneToManyModel3 {
880+
[Column(IsIdentity = true)]
881+
public int id { get; set; }
882+
883+
public int model2111Idaaa { get; set; }
884+
public string title { get; set; }
885+
}
886+
863887
[Fact]
864888
public void Include_OneToMany() {
889+
var model1 = new TestInclude_OneToManyModel1 { m1name = DateTime.Now.Second.ToString() };
890+
model1.id = (int)g.pgsql.Insert(model1).ExecuteIdentity();
891+
var model2 = new TestInclude_OneToManyModel2 { model2id = model1.id, m2setting = DateTime.Now.Second.ToString() };
892+
g.pgsql.Insert(model2).ExecuteAffrows();
893+
894+
var model3s = new[] {
895+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__111" },
896+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__222" },
897+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__333" }
898+
};
899+
Assert.Equal(3, g.pgsql.Insert(model3s).ExecuteAffrows());
865900

901+
var t1 = g.pgsql.Select<TestInclude_OneToManyModel1>()
902+
.IncludeMany(a => a.model2.childs.Where(m3 => m3.model2111Idaaa == a.model2.model2id))
903+
.Where(a => a.id <= model1.id)
904+
.ToList();
866905
}
867906
[Fact]
868907
public void Include_OneToChilds() {

FreeSql.Tests/SqlServer/Curd/SqlServerSelectTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,48 @@ public void AsTable() {
791791
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22AsTable1] a LEFT JOIN TestTypeInfo b on b.Guid = a.TypeGuid and b.Name = @bname", sql);
792792
}
793793

794+
public class TestInclude_OneToManyModel1 {
795+
[Column(IsIdentity = true)]
796+
public int id { get; set; }
797+
public virtual TestInclude_OneToManyModel2 model2 { get; set; }
798+
799+
public string m1name { get; set; }
800+
}
801+
public class TestInclude_OneToManyModel2 {
802+
[Column(IsPrimary = true)]
803+
public int model2id { get; set; }
804+
public virtual TestInclude_OneToManyModel1 model1 { get; set; }
805+
806+
public string m2setting { get; set; }
807+
808+
public List<TestInclude_OneToManyModel3> childs { get; set; }
809+
}
810+
public class TestInclude_OneToManyModel3 {
811+
[Column(IsIdentity = true)]
812+
public int id { get; set; }
813+
814+
public int model2111Idaaa { get; set; }
815+
public string title { get; set; }
816+
}
817+
794818
[Fact]
795819
public void Include_OneToMany() {
820+
var model1 = new TestInclude_OneToManyModel1 { m1name = DateTime.Now.Second.ToString() };
821+
model1.id = (int)_sqlserverFixture.SqlServer.Insert(model1).ExecuteIdentity();
822+
var model2 = new TestInclude_OneToManyModel2 { model2id = model1.id, m2setting = DateTime.Now.Second.ToString() };
823+
_sqlserverFixture.SqlServer.Insert(model2).ExecuteAffrows();
824+
825+
var model3s = new[] {
826+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__111" },
827+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__222" },
828+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__333" }
829+
};
830+
Assert.Equal(3, _sqlserverFixture.SqlServer.Insert(model3s).ExecuteAffrows());
796831

832+
var t1 = _sqlserverFixture.SqlServer.Select<TestInclude_OneToManyModel1>()
833+
.IncludeMany(a => a.model2.childs.Where(m3 => m3.model2111Idaaa == a.model2.model2id))
834+
.Where(a => a.id <= model1.id)
835+
.ToList();
797836
}
798837
[Fact]
799838
public void Include_OneToChilds() {

FreeSql.Tests/Sqlite/Curd/SqliteSelectTest.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -757,29 +757,48 @@ public void AsTable() {
757757
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22AsTable1\" a LEFT JOIN \"TestTypeInfo\" b on b.\"Guid\" = a.\"TypeGuid\" and b.\"Name\" = @bname", sql);
758758
}
759759

760-
class TestInclude_OneToManyModel1 {
760+
public class TestInclude_OneToManyModel1 {
761761
[Column(IsIdentity = true)]
762762
public int id { get; set; }
763763
public virtual TestInclude_OneToManyModel2 model2 { get; set; }
764+
765+
public string m1name { get; set; }
764766
}
765-
class TestInclude_OneToManyModel2 {
767+
public class TestInclude_OneToManyModel2 {
766768
[Column(IsPrimary = true)]
767769
public int model2id { get; set; }
768-
public virtual TestInclude_OneToManyModel2 model1 { get; set; }
770+
public virtual TestInclude_OneToManyModel1 model1 { get; set; }
771+
772+
public string m2setting { get; set; }
769773

770-
public virtual List<TestInclude_OneToManyModel3> childs { get; set; }
774+
public List<TestInclude_OneToManyModel3> childs { get; set; }
771775
}
772-
class TestInclude_OneToManyModel3 {
776+
public class TestInclude_OneToManyModel3 {
773777
[Column(IsIdentity = true)]
774778
public int id { get; set; }
775779

780+
public int model2111Idaaa { get; set; }
776781
public string title { get; set; }
777782
}
778783

779784
[Fact]
780785
public void Include_OneToMany() {
781-
var model1 = new TestInclude_OneToManyModel1 { };
786+
var model1 = new TestInclude_OneToManyModel1 { m1name = DateTime.Now.Second.ToString() };
782787
model1.id = (int)g.sqlite.Insert(model1).ExecuteIdentity();
788+
var model2 = new TestInclude_OneToManyModel2 { model2id = model1.id, m2setting = DateTime.Now.Second.ToString() };
789+
g.sqlite.Insert(model2).ExecuteAffrows();
790+
791+
var model3s = new [] {
792+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__111" },
793+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__222" },
794+
new TestInclude_OneToManyModel3{ model2111Idaaa = model1.id, title = "testmodel3__333" }
795+
};
796+
Assert.Equal(3, g.sqlite.Insert(model3s).ExecuteAffrows());
797+
798+
var t1 = g.sqlite.Select<TestInclude_OneToManyModel1>()
799+
.IncludeMany(a => a.model2.childs.Where(m3 => m3.model2111Idaaa == a.model2.model2id && m3.title == a.model2.m2setting))
800+
.Where(a => a.id <= model1.id)
801+
.ToList();
783802
}
784803
[Fact]
785804
public void Include_OneToChilds() {

FreeSql/FreeSql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>0.5.17</Version>
5+
<Version>0.5.18</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>

FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -467,20 +467,39 @@ public ISelect<T1> IncludeMany<TNavigate>(Expression<Func<T1, IEnumerable<TNavig
467467
subSelect.Where(Expression.Lambda<Func<TNavigate, bool>>(
468468
Expression.Call(null, containsMethod, arrExp, refCol), otmExpParm1));
469469
} else {
470-
var otmExpParm1 = Expression.Parameter(typeof(TNavigate), "a");
471-
Expression expOr = null;
472-
foreach (var item in list) {
473-
Expression expAnd = null;
470+
var subSelectT1Alias = (subSelect as Select1Provider<TNavigate>)._tables[0].Alias;
471+
Dictionary<string, bool> sbDic = new Dictionary<string, bool>();
472+
for (var y = 0; y < list.Count; y++) {
473+
var sbWhereOne = new StringBuilder();
474+
sbWhereOne.Append("(");
474475
for (var z = 0; z < tbref.Columns.Count; z++) {
475-
var colVal = getListValue(item, tbref.Columns[z].CsName);
476-
var expTmp = Expression.Equal(Expression.MakeMemberAccess(otmExpParm1, tbref2.Properties[tbref.RefColumns[0].CsName]), Expression.Constant(colVal));
477-
if (z == 0) expAnd = expTmp;
478-
else expAnd = Expression.AndAlso(expAnd, expTmp);
476+
if (z > 0) sbWhereOne.Append(" AND ");
477+
sbWhereOne.Append(_commonUtils.FormatSql($"{subSelectT1Alias}.{_commonUtils.QuoteSqlName(tbref.RefColumns[z].Attribute.Name)}={{0}}", getListValue(list[y], tbref.Columns[z].CsName)));
479478
}
480-
if (expOr == null) expOr = expAnd;
481-
else expOr = Expression.OrElse(expOr, expAnd);
479+
sbWhereOne.Append(")");
480+
var whereOne = sbWhereOne.ToString();
481+
sbWhereOne.Clear();
482+
if (sbDic.ContainsKey(whereOne) == false) sbDic.Add(whereOne, true);
482483
}
483-
subSelect.Where(Expression.Lambda<Func<TNavigate, bool>>(expOr, otmExpParm1));
484+
var sbWhere = new StringBuilder();
485+
foreach (var sbd in sbDic)
486+
sbWhere.Append(" OR ").Append(sbd.Key);
487+
subSelect.Where(sbWhere.Remove(0, 4).ToString());
488+
sbWhere.Clear();
489+
//var otmExpParm1 = Expression.Parameter(typeof(TNavigate), "a");
490+
//Expression expOr = null;
491+
//foreach (var item in list) {
492+
// Expression expAnd = null;
493+
// for (var z = 0; z < tbref.Columns.Count; z++) {
494+
// var colVal = getListValue(item, tbref.Columns[z].CsName);
495+
// var expTmp = Expression.Equal(Expression.MakeMemberAccess(otmExpParm1, tbref2.Properties[tbref.RefColumns[z].CsName]), Expression.Constant(colVal));
496+
// if (z == 0) expAnd = expTmp;
497+
// else expAnd = Expression.AndAlso(expAnd, expTmp);
498+
// }
499+
// if (expOr == null) expOr = expAnd;
500+
// else expOr = Expression.OrElse(expOr, expAnd);
501+
//}
502+
//subSelect.Where(Expression.Lambda<Func<TNavigate, bool>>(expOr, otmExpParm1));
484503
}
485504
then?.Invoke(subSelect);
486505
var subList = subSelect.ToList(true);
@@ -543,10 +562,10 @@ public ISelect<T1> IncludeMany<TNavigate>(Expression<Func<T1, IEnumerable<TNavig
543562
if (true) {
544563
var tbref2 = _commonUtils.GetTableByEntity(tbref.RefEntityType);
545564
var tbrefMid = _commonUtils.GetTableByEntity(tbref.RefMiddleEntityType);
546-
var sbJoin = new StringBuilder().Append($"{_commonUtils.QuoteSqlName(tbrefMid.DbName)} midtb ON");
565+
var sbJoin = new StringBuilder().Append($"{_commonUtils.QuoteSqlName(tbrefMid.DbName)} midtb ON ");
547566
for (var z = 0; z < tbref.RefColumns.Count; z++) {
548-
if (z > 0) sbJoin.Append(" AND");
549-
sbJoin.Append($" midtb.{_commonUtils.QuoteSqlName(tbref.MiddleColumns[tbref.Columns.Count + z].Attribute.Name)} = a.{_commonUtils.QuoteSqlName(tbref.RefColumns[z].Attribute.Name)}");
567+
if (z > 0) sbJoin.Append(" AND ");
568+
sbJoin.Append($"midtb.{_commonUtils.QuoteSqlName(tbref.MiddleColumns[tbref.Columns.Count + z].Attribute.Name)} = a.{_commonUtils.QuoteSqlName(tbref.RefColumns[z].Attribute.Name)}");
550569
}
551570
subSelect.InnerJoin(sbJoin.ToString());
552571
sbJoin.Clear();
@@ -556,9 +575,9 @@ public ISelect<T1> IncludeMany<TNavigate>(Expression<Func<T1, IEnumerable<TNavig
556575
Dictionary<string, bool> sbDic = new Dictionary<string, bool>();
557576
for (var y = 0; y < list.Count; y++) {
558577
var sbWhereOne = new StringBuilder();
559-
sbWhereOne.Append(" (");
578+
sbWhereOne.Append("(");
560579
for (var z = 0; z < tbref.Columns.Count; z++) {
561-
if (z > 0) sbWhereOne.Append(" AND");
580+
if (z > 0) sbWhereOne.Append(" AND ");
562581
sbWhereOne.Append(_commonUtils.FormatSql($" midtb.{_commonUtils.QuoteSqlName(tbref.MiddleColumns[z].Attribute.Name)}={{0}}", getListValue(list[y], tbref.Columns[z].CsName)));
563582
}
564583
sbWhereOne.Append(")");
@@ -568,7 +587,7 @@ public ISelect<T1> IncludeMany<TNavigate>(Expression<Func<T1, IEnumerable<TNavig
568587
}
569588
var sbWhere = new StringBuilder();
570589
foreach (var sbd in sbDic)
571-
sbWhere.Append(" OR").Append(sbd.Key);
590+
sbWhere.Append(" OR ").Append(sbd.Key);
572591
subSelect.Where(sbWhere.Remove(0, 3).ToString());
573592
sbWhere.Clear();
574593
}

0 commit comments

Comments
 (0)