Skip to content

Commit

Permalink
fix PgArrayToMany
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Jun 4, 2022
1 parent 53bea5b commit 7839b42
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion FreeSql.Tests/FreeSql.Tests/Issues/623.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void MySqlGroupBy()
FacilityCount = a.Sum(a.Value.FacilityCount),
FacilityOpenCount = a.Sum(a.Value.FacilityOpenCount)
});
Assert.Equal(@"SELECT a.`Dot`, sum(a.`FacilityCount`) as1, sum(a.`FacilityOpenCount`) as2
Assert.Equal(@"SELECT a.`Dot` as1, sum(a.`FacilityCount`) as2, sum(a.`FacilityOpenCount`) as3
FROM `ts_facility` a
WHERE (a.`Date` = cast(date_format('2020-12-30 00:00:00.000','%Y-%m-%d') as datetime)) AND (((a.`EnterpriseId`) in (5))) AND (a.`FacilityType` = 1)
GROUP BY a.`Dot`", sql);
Expand Down
2 changes: 1 addition & 1 deletion FreeSql/Extensions/FreeSqlGlobalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static partial class FreeSqlGlobalExtensions
});
public static bool IsIntegerType(this Type that) => that == null ? false : (_dicIsNumberType.Value.TryGetValue(that, out var tryval) ? tryval : false);
public static bool IsNumberType(this Type that) => that == null ? false : _dicIsNumberType.Value.ContainsKey(that);
public static bool IsNullableType(this Type that) => that.IsArray == false && that?.FullName.StartsWith("System.Nullable`1[") == true;
public static bool IsNullableType(this Type that) => that == null ? false : (that.IsArray == false && that.FullName.StartsWith("System.Nullable`1[") == true);
public static bool IsAnonymousType(this Type that) => that == null ? false : (that.FullName.StartsWith("<>f__AnonymousType") || that.FullName.StartsWith("VB$AnonymousType"));
public static bool IsArrayOrList(this Type that) => that == null ? false : (that.IsArray || typeof(IList).IsAssignableFrom(that));
public static Type NullableTypeOrThis(this Type that) => that?.IsNullableType() == true ? that.GetGenericArguments().First() : that;
Expand Down
17 changes: 17 additions & 0 deletions FreeSql/FreeSql.xml

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

18 changes: 17 additions & 1 deletion FreeSql/Internal/Model/TableInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ public class TableRef
}
public enum TableRefType
{
OneToOne, ManyToOne, OneToMany, ManyToMany, PgArrayToMany
OneToOne, ManyToOne, OneToMany, ManyToMany,
/// <summary>
/// PostgreSQL 数组类型专属功能<para></para>
/// 方式一:select * from Role where Id in (RoleIds)<para></para>
/// class User {<para></para>
/// ____public int[] RoleIds { get; set; }<para></para>
/// ____[Navigate(nameof(RoleIds))]<para></para>
/// ____public List&lt;Role&gt; Roles { get; set; }<para></para>
/// }<para></para>
/// 方式二:select * from User where RoleIds @&gt; Id<para></para>
/// class Role {<para></para>
/// ____public int Id { get; set; }<para></para>
/// ____[Navigate(nameof(User.RoleIds))]<para></para>
/// ____public List&lt;User&gt; Users { get; set; }<para></para>
/// }<para></para>
/// </summary>
PgArrayToMany
}
}
17 changes: 11 additions & 6 deletions FreeSql/Internal/UtilsExpressionTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,15 +1014,16 @@ public static void AddTableRef(CommonUtils common, TableInfo trytb, PropertyInfo
{
if (trytb.ColumnsByCs.TryGetValue(pnvBind[0], out trycol))
{
if (trycol.CsType.IsArray == true && tbref.Primarys[0].CsType.NullableTypeOrThis() != trycol.CsType.GetElementType().NullableTypeOrThis())
if (trycol.CsType.IsArray == false) trycol = null;
else if (trycol != null && tbref.Primarys[0].CsType.NullableTypeOrThis() != trycol.CsType.GetElementType().NullableTypeOrThis())
{
nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 特性 [Navigate] 解析错误,{trytbTypeName}.{trycol.CsName} 数组元素 与 {tbrefTypeName}.{tbref.Primarys[0].CsName} 类型不符");
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
//if (isLazy) throw nvref.Exception;
}
}
}
if (nvref.Exception == null && trycol == null)
if (pnvBind == null && trycol == null)
{
var findtbrefPkCsName = tbref.Primarys[0].CsName.TrimStart('_');
if (findtbrefPkCsName.StartsWith(trytb.Type.Name, StringComparison.CurrentCultureIgnoreCase)) findtbrefPkCsName = findtbrefPkCsName.Substring(trytb.Type.Name.Length).TrimStart('_');
Expand Down Expand Up @@ -1059,15 +1060,16 @@ public static void AddTableRef(CommonUtils common, TableInfo trytb, PropertyInfo
{
if (tbref.ColumnsByCs.TryGetValue(pnvBind[0], out trycol))
{
if (trycol.CsType.IsArray == true && trytb.Primarys[0].CsType.NullableTypeOrThis() != trycol.CsType.GetElementType().NullableTypeOrThis())
if (trycol.CsType.IsArray == false) trycol = null;
else if (trytb.Primarys[0].CsType.NullableTypeOrThis() != trycol.CsType.GetElementType().NullableTypeOrThis())
{
nvref.Exception = new Exception($"导航属性 {trytbTypeName}.{pnv.Name} 特性 [Navigate] 解析错误,{trytbTypeName}.{trytb.Primarys[0].CsName}{tbrefTypeName}.{trycol.CsName} 数组元素类型不符");
trytb.AddOrUpdateTableRef(pnv.Name, nvref);
//if (isLazy) throw nvref.Exception;
}
}
}
if (nvref.Exception == null && trycol == null)
if (pnvBind != null && trycol == null)
{
var findtrytbPkCsName = trytb.Primarys[0].CsName.TrimStart('_');
if (findtrytbPkCsName.StartsWith(trytb.Type.Name, StringComparison.CurrentCultureIgnoreCase)) findtrytbPkCsName = findtrytbPkCsName.Substring(trytb.Type.Name.Length).TrimStart('_');
Expand All @@ -1086,9 +1088,12 @@ public static void AddTableRef(CommonUtils common, TableInfo trytb, PropertyInfo
isArrayToMany = trycol != null;
if (isArrayToMany)
{
cscodeExtLogic = $" if (this.{trytb.Primarys[0].CsName} == null) return null;\r\n";
lmbdWhere.Append("a.").Append(trycol.CsName).Append(".Contains(this.").Append(trytb.Primarys[0].CsName);
if (trycol.CsType.GetElementType().IsNullableType() == false && trytb.Primarys[0].CsType.IsNullableType()) lmbdWhere.Append(".Value");
if (trycol.CsType.GetElementType().IsNullableType() == false && trytb.Primarys[0].CsType.IsNullableType())
{
lmbdWhere.Append(".Value");
cscodeExtLogic = $" if (this.{trytb.Primarys[0].CsName} == null) return null;\r\n";
}
lmbdWhere.Append(")");
nvref.Columns.Add(tbref.Primarys[0]);
nvref.RefColumns.Add(trycol);
Expand Down

0 comments on commit 7839b42

Please sign in to comment.