Skip to content

Commit 3d5e32a

Browse files
fixup! Support mixed formulas and columns
Use a more robust checking of sub-elements existence
1 parent bf6fa53 commit 3d5e32a

File tree

7 files changed

+35
-29
lines changed

7 files changed

+35
-29
lines changed

src/NHibernate/Cfg/MappingSchema/HbmElement.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
43
using System.Xml.Serialization;
4+
using NHibernate.Util;
55

66
namespace NHibernate.Cfg.MappingSchema
77
{
@@ -12,7 +12,7 @@ public partial class HbmElement: IColumnsMapping, IFormulasMapping, ITypeMapping
1212
[XmlIgnore]
1313
public IEnumerable<HbmColumn> Columns
1414
{
15-
get { return Items != null ? Items.OfType<HbmColumn>() : AsColumns(); }
15+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmColumn>() : AsColumns(); }
1616
}
1717

1818
#endregion
@@ -45,7 +45,7 @@ private IEnumerable<HbmColumn> AsColumns()
4545
[XmlIgnore]
4646
public IEnumerable<HbmFormula> Formulas
4747
{
48-
get { return Items != null ? Items.OfType<HbmFormula>() : AsFormulas(); }
48+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmFormula>() : AsFormulas(); }
4949
}
5050

5151
private IEnumerable<HbmFormula> AsFormulas()
@@ -80,15 +80,15 @@ public IEnumerable<object> ColumnsAndFormulas
8080
{
8181
get
8282
{
83-
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
83+
if (!ArrayHelper.IsNullOrEmpty(Items) && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
8484
throw new MappingException(
8585
$"On an element: specifying columns or formulas with both attributes and xml sub-elements is " +
8686
$"invalid. Please use only xml sub-elements, or only one of them as attribute");
8787
if (!string.IsNullOrEmpty(column) && !string.IsNullOrEmpty(formula))
8888
throw new MappingException(
8989
$"On an element: specifying both column and formula attributes is invalid. Please " +
9090
$"specify only one of them, or use xml sub-elements");
91-
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
91+
return !ArrayHelper.IsNullOrEmpty(Items) ? Items : AsColumns().Cast<object>().Concat(AsFormulas());
9292
}
9393
}
9494
}

src/NHibernate/Cfg/MappingSchema/HbmManyToMany.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
43
using System.Xml.Serialization;
4+
using NHibernate.Util;
55

66
namespace NHibernate.Cfg.MappingSchema
77
{
@@ -12,7 +12,7 @@ public partial class HbmManyToMany : IColumnsMapping, IFormulasMapping, IRelatio
1212
[XmlIgnore]
1313
public IEnumerable<HbmColumn> Columns
1414
{
15-
get { return Items != null ? Items.OfType<HbmColumn>() : AsColumns(); }
15+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmColumn>() : AsColumns(); }
1616
}
1717

1818
#endregion
@@ -39,7 +39,7 @@ private IEnumerable<HbmColumn> AsColumns()
3939
[XmlIgnore]
4040
public IEnumerable<HbmFormula> Formulas
4141
{
42-
get { return Items != null ? Items.OfType<HbmFormula>() : AsFormulas(); }
42+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmFormula>() : AsFormulas(); }
4343
}
4444

4545
private IEnumerable<HbmFormula> AsFormulas()
@@ -83,7 +83,7 @@ public IEnumerable<object> ColumnsAndFormulas
8383
{
8484
get
8585
{
86-
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
86+
if (!ArrayHelper.IsNullOrEmpty(Items) && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
8787
throw new MappingException(
8888
$"On a many-to-many targeting {EntityName}: specifying columns or formulas with both " +
8989
$"attributes and sub-elements is invalid. Please use only sub-elements, or only one of them " +
@@ -92,7 +92,7 @@ public IEnumerable<object> ColumnsAndFormulas
9292
throw new MappingException(
9393
$"On a many-to-many targeting {EntityName}: specifying both column and formula attributes is " +
9494
$"invalid. Please specify only one of them, or use sub-elements");
95-
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
95+
return !ArrayHelper.IsNullOrEmpty(Items) ? Items : AsColumns().Cast<object>().Concat(AsFormulas());
9696
}
9797
}
9898
}

src/NHibernate/Cfg/MappingSchema/HbmManyToOne.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Xml.Serialization;
5+
using NHibernate.Util;
56

67
namespace NHibernate.Cfg.MappingSchema
78
{
@@ -45,7 +46,7 @@ protected override HbmMeta[] Metadatas
4546
[XmlIgnore]
4647
public IEnumerable<HbmColumn> Columns
4748
{
48-
get { return Items != null ? Items.OfType<HbmColumn>() : AsColumns(); }
49+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmColumn>() : AsColumns(); }
4950
}
5051

5152
#endregion
@@ -76,7 +77,7 @@ private IEnumerable<HbmColumn> AsColumns()
7677
[XmlIgnore]
7778
public IEnumerable<HbmFormula> Formulas
7879
{
79-
get { return Items != null ? Items.OfType<HbmFormula>() : AsFormulas(); }
80+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmFormula>() : AsFormulas(); }
8081
}
8182

8283
private IEnumerable<HbmFormula> AsFormulas()
@@ -120,15 +121,15 @@ public IEnumerable<object> ColumnsAndFormulas
120121
{
121122
get
122123
{
123-
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
124+
if (!ArrayHelper.IsNullOrEmpty(Items) && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
124125
throw new MappingException(
125126
$"On {Name} many-to-one: specifying columns or formulas with both attributes and " +
126127
$"sub-elements is invalid. Please use only sub-elements, or only one of them as attribute");
127128
if (!string.IsNullOrEmpty(column) && !string.IsNullOrEmpty(formula))
128129
throw new MappingException(
129130
$"On {Name} many-to-one: specifying both column and formula attributes is invalid. Please " +
130131
$"specify only one of them, or use sub-elements");
131-
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
132+
return !ArrayHelper.IsNullOrEmpty(Items) ? Items : AsColumns().Cast<object>().Concat(AsFormulas());
132133
}
133134
}
134135

src/NHibernate/Cfg/MappingSchema/HbmMapKey.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
43
using System.Xml.Serialization;
4+
using NHibernate.Util;
55

66
namespace NHibernate.Cfg.MappingSchema
77
{
@@ -13,7 +13,7 @@ public partial class HbmMapKey: IColumnsMapping, ITypeMapping
1313
[XmlIgnore]
1414
public IEnumerable<HbmColumn> Columns
1515
{
16-
get { return Items != null ? Items.OfType<HbmColumn>() : AsColumns(); }
16+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmColumn>() : AsColumns(); }
1717
}
1818

1919
#endregion
@@ -39,7 +39,7 @@ private IEnumerable<HbmColumn> AsColumns()
3939
[XmlIgnore]
4040
public IEnumerable<HbmFormula> Formulas
4141
{
42-
get { return Items != null ? Items.OfType<HbmFormula>() : AsFormulas(); }
42+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmFormula>() : AsFormulas(); }
4343
}
4444

4545
private IEnumerable<HbmFormula> AsFormulas()
@@ -73,15 +73,15 @@ public IEnumerable<object> ColumnsAndFormulas
7373
{
7474
get
7575
{
76-
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
76+
if (!ArrayHelper.IsNullOrEmpty(Items) && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
7777
throw new MappingException(
7878
$"On a map-key: specifying columns or formulas with both attributes and sub-elements is " +
7979
$"invalid. Please use only sub-elements, or only one of them as attribute");
8080
if (!string.IsNullOrEmpty(column) && !string.IsNullOrEmpty(formula))
8181
throw new MappingException(
8282
$"On a map-key: specifying both column and formula attributes is invalid. Please specify " +
8383
$"only one of them, or use sub-elements");
84-
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
84+
return !ArrayHelper.IsNullOrEmpty(Items) ? Items : AsColumns().Cast<object>().Concat(AsFormulas());
8585
}
8686
}
8787
}

src/NHibernate/Cfg/MappingSchema/HbmMapKeyManyToMany.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
43
using System.Xml.Serialization;
4+
using NHibernate.Util;
55

66
namespace NHibernate.Cfg.MappingSchema
77
{
@@ -13,7 +13,7 @@ public partial class HbmMapKeyManyToMany: IColumnsMapping, IFormulasMapping, IRe
1313
[XmlIgnore]
1414
public IEnumerable<HbmColumn> Columns
1515
{
16-
get { return Items != null ? Items.OfType<HbmColumn>() : AsColumns(); }
16+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmColumn>() : AsColumns(); }
1717
}
1818

1919
#endregion
@@ -38,7 +38,7 @@ private IEnumerable<HbmColumn> AsColumns()
3838
[XmlIgnore]
3939
public IEnumerable<HbmFormula> Formulas
4040
{
41-
get { return Items != null ? Items.OfType<HbmFormula>() : AsFormulas(); }
41+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmFormula>() : AsFormulas(); }
4242
}
4343

4444
private IEnumerable<HbmFormula> AsFormulas()
@@ -82,7 +82,7 @@ public IEnumerable<object> ColumnsAndFormulas
8282
{
8383
get
8484
{
85-
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
85+
if (!ArrayHelper.IsNullOrEmpty(Items) && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
8686
throw new MappingException(
8787
$"On a map-key-many-to-many targeting {EntityName}: specifying columns or formulas with both " +
8888
$"attributes and sub-elements is invalid. Please use only sub-elements, or only one of them " +
@@ -91,7 +91,7 @@ public IEnumerable<object> ColumnsAndFormulas
9191
throw new MappingException(
9292
$"On a map-key-many-to-many targeting {EntityName}: specifying both column and formula " +
9393
$"attributes is invalid. Please specify only one of them, or use sub-elements");
94-
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
94+
return !ArrayHelper.IsNullOrEmpty(Items) ? Items : AsColumns().Cast<object>().Concat(AsFormulas());
9595
}
9696
}
9797
}

src/NHibernate/Cfg/MappingSchema/HbmProperty.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Xml.Serialization;
5+
using NHibernate.Util;
56

67
namespace NHibernate.Cfg.MappingSchema
78
{
@@ -45,7 +46,7 @@ protected override HbmMeta[] Metadatas
4546
[XmlIgnore]
4647
public IEnumerable<HbmColumn> Columns
4748
{
48-
get { return Items != null ? Items.OfType<HbmColumn>() : AsColumns(); }
49+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmColumn>() : AsColumns(); }
4950
}
5051

5152
#endregion
@@ -79,7 +80,7 @@ private IEnumerable<HbmColumn> AsColumns()
7980
[XmlIgnore]
8081
public IEnumerable<HbmFormula> Formulas
8182
{
82-
get { return Items != null ? Items.OfType<HbmFormula>() : AsFormulas(); }
83+
get { return !ArrayHelper.IsNullOrEmpty(Items) ? Items.OfType<HbmFormula>() : AsFormulas(); }
8384
}
8485

8586
private IEnumerable<HbmFormula> AsFormulas()
@@ -113,15 +114,15 @@ public IEnumerable<object> ColumnsAndFormulas
113114
{
114115
get
115116
{
116-
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
117+
if (!ArrayHelper.IsNullOrEmpty(Items) && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
117118
throw new MappingException(
118119
$"On {Name} property: specifying columns or formulas with both attributes and " +
119120
$"sub-elements is invalid. Please use only sub-elements, or only one of them as attribute");
120121
if (!string.IsNullOrEmpty(column) && !string.IsNullOrEmpty(formula))
121122
throw new MappingException(
122123
$"On {Name} property: specifying both column and formula attributes is invalid. Please " +
123124
$"specify only one of them, or use sub-elements");
124-
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
125+
return !ArrayHelper.IsNullOrEmpty(Items) ? Items : AsColumns().Cast<object>().Concat(AsFormulas());
125126
}
126127
}
127128
}

src/NHibernate/Util/ArrayHelper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections;
33
using System.Linq;
4-
using System.Reflection;
54
using System.Text;
65
using System.Collections.Generic;
76

@@ -26,6 +25,11 @@ public static class ArrayHelper
2625
public static readonly bool[] True = new bool[] { true };
2726
public static readonly bool[] False = new bool[] { false };
2827

28+
internal static bool IsNullOrEmpty(Array array)
29+
{
30+
return array == null || array.Length == 0;
31+
}
32+
2933
public static bool IsAllNegative(int[] array)
3034
{
3135
return array.All(t => t < 0);

0 commit comments

Comments
 (0)