Skip to content

Commit b67fe68

Browse files
author
Sergey Koshcheyev
committed
Add support for index/unique-key attributes to property, many-to-one and any (related to NH-897)
SVN: trunk@2637
1 parent 98b467c commit b67fe68

File tree

6 files changed

+88
-41
lines changed

6 files changed

+88
-41
lines changed

src/NHibernate.DomainModel/A.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class A
2121
/// </summary>
2222
private String _name;
2323

24+
/// <summary>
25+
/// Holder for anotherName
26+
/// </summary>
27+
private String _anotherName;
28+
2429
/// <summary>
2530
/// Holder for forward
2631
/// </summary>
@@ -65,6 +70,15 @@ public virtual String Name
6570
set { _name = value; }
6671
}
6772

73+
/// <summary>
74+
/// Get/set for anotherName
75+
/// </summary>
76+
public virtual String AnotherName
77+
{
78+
get { return _anotherName; }
79+
set { _anotherName = value; }
80+
}
81+
6882
public virtual E Forward
6983
{
7084
get { return _forward; }
@@ -73,4 +87,4 @@ public virtual E Forward
7387

7488
#endregion
7589
}
76-
}
90+
}

src/NHibernate.DomainModel/ABC.hbm.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
/>
2020

2121
<property name="Name" >
22-
<column name="name" index="indx_a_name2" />
22+
<column name="name" index="indx_a_name" />
2323
</property>
2424

25+
<property name="AnotherName" index="indx_a_anothername" />
26+
2527
<subclass
2628
name="NHibernate.DomainModel.B, NHibernate.DomainModel"
2729
discriminator-value="-1"
@@ -80,4 +82,4 @@
8082
type="Double"
8183
/>
8284
</class>
83-
</hibernate-mapping>
85+
</hibernate-mapping>

src/NHibernate.Test/Legacy/ABCTest.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@ protected override IList Mappings
2323
}
2424

2525
[Test]
26-
public void HigherLevelIndexDefinition()
26+
public void HigherLevelIndexDefinitionInColumnTag()
2727
{
2828
string[] commands = cfg.GenerateSchemaCreationScript( dialect );
29-
int max = commands.Length;
30-
bool found = false;
31-
32-
foreach( string command in commands )
33-
{
34-
System.Console.WriteLine("Checking command : " + command);
35-
found = command.IndexOf("create index indx_a_name") >= 0;
36-
if (found)
37-
break;
38-
}
39-
Assert.IsTrue( found, "Unable to locate indx_a_name index creation" );
29+
30+
Assert.IsTrue(ContainsCommandWithSubstring(commands, "create index indx_a_name"),
31+
"Unable to locate indx_a_name index creation");
32+
}
33+
34+
[Test]
35+
public void HigherLevelIndexDefinitionInPropertyTag()
36+
{
37+
string[] commands = cfg.GenerateSchemaCreationScript( dialect );
38+
39+
Assert.IsTrue(ContainsCommandWithSubstring(commands, "create index indx_a_anothername"),
40+
"Unable to locate indx_a_anothername index creation");
4041
}
4142

4243
[Test]
@@ -165,5 +166,18 @@ public void Subclassing()
165166
t.Commit();
166167
s.Close();
167168
}
169+
170+
private static bool ContainsCommandWithSubstring(string[] commands, string subString)
171+
{
172+
foreach (string command in commands)
173+
{
174+
System.Console.WriteLine("Checking command : " + command);
175+
if (command.IndexOf(subString) >= 0)
176+
{
177+
return true;
178+
}
179+
}
180+
return false;
181+
}
168182
}
169183
}

src/NHibernate.Test/Legacy/SQLLoaderTest.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ public void DoubleAliasing()
337337

338338
session = OpenSession();
339339

340-
IQuery query = session.CreateSQLQuery("select a.identifier_column as {a1.id}, a.clazz_discriminata as {a1.class}, a.count_ as {a1.Count}, a.name as {a1.Name} " +
341-
", b.identifier_column as {a2.id}, b.clazz_discriminata as {a2.class}, b.count_ as {a2.Count}, b.name as {a2.Name} " +
340+
IQuery query = session.CreateSQLQuery("select a.identifier_column as {a1.id}, a.clazz_discriminata as {a1.class}, a.count_ as {a1.Count}, a.name as {a1.Name}, a.anothername as {a1.AnotherName} " +
341+
", b.identifier_column as {a2.id}, b.clazz_discriminata as {a2.class}, b.count_ as {a2.Count}, b.name as {a2.Name}, b.anothername as {a2.AnotherName} " +
342342
" from A a, A b" +
343343
" where a.identifier_column = b.identifier_column", new String[] {"a1", "a2" }, new System.Type[] {typeof( A ), typeof( A )});
344344
IList list = query.List();
@@ -561,7 +561,8 @@ public void FindBySQLDiscriminatedDiffSessions()
561561

562562
session = OpenSession();
563563

564-
IQuery query = session.CreateSQLQuery("select identifier_column as {a.id}, clazz_discriminata as {a.class}, count_ as {a.Count}, name as {a.Name} from A", "a", typeof( A ));
564+
IQuery query = session.CreateSQLQuery(
565+
"select identifier_column as {a.id}, clazz_discriminata as {a.class}, count_ as {a.Count}, name as {a.Name}, anothername as {a.AnotherName} from A", "a", typeof( A ));
565566
IList list = query.List();
566567

567568
Assert.IsNotNull(list);

src/NHibernate/Cfg/HbmBinder.cs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,12 @@ public static void BindRootClass(XmlNode node, RootClass model, Mappings mapping
474474
public static void BindColumns(XmlNode node, SimpleValue model, bool isNullable, bool autoColumn, string propertyPath,
475475
Mappings mappings)
476476
{
477+
Table table = model.Table;
477478
//COLUMN(S)
478479
XmlAttribute columnAttribute = node.Attributes["column"];
479480
if (columnAttribute == null)
480481
{
481482
int count = 0;
482-
Table table = model.Table;
483483

484484
foreach (XmlNode columnElement in node.SelectNodes(HbmConstants.nsColumn, nsmgr))
485485
{
@@ -496,42 +496,27 @@ public static void BindColumns(XmlNode node, SimpleValue model, bool isNullable,
496496
model.AddColumn(col);
497497

498498
//column index
499-
XmlAttribute indexNode = columnElement.Attributes["index"];
500-
if (indexNode != null && table != null)
501-
{
502-
table.GetIndex(indexNode.Value).AddColumn(col);
503-
}
504-
499+
BindIndex(columnElement.Attributes["index"], table, col, mappings);
505500
//column group index (although it can serve as a separate column index)
506-
XmlAttribute parentElementIndexAttr = node.Attributes["index"];
507-
if (parentElementIndexAttr != null && table != null)
508-
{
509-
table.GetIndex(parentElementIndexAttr.Value).AddColumn(col);
510-
}
511-
XmlAttribute uniqueNode = columnElement.Attributes["unique-key"];
512-
if (uniqueNode != null && table != null)
513-
{
514-
table.GetUniqueKey(uniqueNode.Value).AddColumn(col);
515-
}
501+
BindIndex(node.Attributes["index"], table, col, mappings);
502+
503+
BindUniqueKey(columnElement.Attributes["unique-key"], table, col, mappings);
504+
BindUniqueKey(node.Attributes["unique-key"], table, col, mappings);
516505
}
517506
}
518507
else
519508
{
520509
Column col = new Column(model.Type, 0);
521510
BindColumn(node, col, isNullable);
522511
col.Name = mappings.NamingStrategy.ColumnName(columnAttribute.Value);
523-
Table table = model.Table;
524512
if (table != null)
525513
{
526514
table.AddColumn(col);
527515
} //table=null -> an association - fill it in later
528516
model.AddColumn(col);
529517
//column group index (although can serve as a separate column index)
530-
XmlAttribute indexAttr = node.Attributes["index"];
531-
if (indexAttr != null && table != null)
532-
{
533-
table.GetIndex(indexAttr.Value).AddColumn(col);
534-
}
518+
BindIndex(node.Attributes["index"], table, col, mappings);
519+
BindUniqueKey(node.Attributes["unique-key"], table, col, mappings);
535520
}
536521

537522
if (autoColumn && model.ColumnSpan == 0)
@@ -541,6 +526,33 @@ public static void BindColumns(XmlNode node, SimpleValue model, bool isNullable,
541526
col.Name = mappings.NamingStrategy.PropertyToColumnName(propertyPath);
542527
model.Table.AddColumn(col);
543528
model.AddColumn(col);
529+
//column group index (although can serve as a separate column index)
530+
BindIndex(node.Attributes["index"], table, col, mappings);
531+
BindUniqueKey(node.Attributes["unique-key"], table, col, mappings);
532+
}
533+
}
534+
535+
private static void BindIndex(XmlAttribute indexAttribute, Table table, Column column, Mappings mappings)
536+
{
537+
if (indexAttribute != null && table != null)
538+
{
539+
StringTokenizer tokens = new StringTokenizer(indexAttribute.Value, ", ");
540+
foreach (string token in tokens)
541+
{
542+
table.GetIndex(token).AddColumn(column);
543+
}
544+
}
545+
}
546+
547+
private static void BindUniqueKey(XmlAttribute uniqueKeyAttribute, Table table, Column column, Mappings mappings)
548+
{
549+
if (uniqueKeyAttribute != null && table != null)
550+
{
551+
StringTokenizer tokens = new StringTokenizer(uniqueKeyAttribute.Value, ", ");
552+
foreach (string token in tokens)
553+
{
554+
table.GetUniqueKey(token).AddColumn(column);
555+
}
544556
}
545557
}
546558

src/NHibernate/nhibernate-mapping.xsd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@
580580
<xs:documentation>only supported for properties of a class (not component)</xs:documentation>
581581
</xs:annotation>
582582
</xs:attribute>
583+
<xs:attribute name="unique-key" type="xs:string" />
583584
<xs:attribute name="index" type="xs:string" />
584585
</xs:complexType>
585586
</xs:element>
@@ -604,6 +605,7 @@
604605
<xs:attribute name="update" type="xs:boolean" default="true" />
605606
<xs:attribute name="cascade" type="cascadeStyle" default="none" />
606607
<xs:attribute name="index" type="xs:string" use="optional" />
608+
<xs:attribute name="unique-key" type="xs:string" use="optional" />
607609
</xs:complexType>
608610
</xs:element>
609611
<xs:element name="array">
@@ -885,6 +887,8 @@
885887
<xs:attribute name="column" type="xs:string" />
886888
<xs:attribute name="not-null" type="xs:boolean" default="false" />
887889
<xs:attribute name="unique" type="xs:boolean" default="false" />
890+
<xs:attribute name="unique-key" type="xs:string" />
891+
<xs:attribute name="index" type="xs:string" />
888892
<xs:attribute name="cascade" type="cascadeStyle" />
889893
<xs:attribute name="outer-join" type="outerJoinStrategy" use="optional" />
890894
<xs:attribute name="fetch" type="fetchMode" use="optional" />

0 commit comments

Comments
 (0)