Skip to content
Browse files

Add support for index/unique-key attributes to property, many-to-one …

…and any (related to NH-897)

SVN: trunk@2637
  • Loading branch information...
1 parent 98b467c commit b67fe6851a3d09a4aefed73c0abceb0570cf9f5a Sergey Koshcheyev committed Feb 20, 2007
View
16 src/NHibernate.DomainModel/A.cs
@@ -22,6 +22,11 @@ public class A
private String _name;
/// <summary>
+ /// Holder for anotherName
+ /// </summary>
+ private String _anotherName;
+
+ /// <summary>
/// Holder for forward
/// </summary>
private E _forward;
@@ -65,6 +70,15 @@ public virtual String Name
set { _name = value; }
}
+ /// <summary>
+ /// Get/set for anotherName
+ /// </summary>
+ public virtual String AnotherName
+ {
+ get { return _anotherName; }
+ set { _anotherName = value; }
+ }
+
public virtual E Forward
{
get { return _forward; }
@@ -73,4 +87,4 @@ public virtual E Forward
#endregion
}
-}
+}
View
6 src/NHibernate.DomainModel/ABC.hbm.xml
@@ -19,9 +19,11 @@
/>
<property name="Name" >
- <column name="name" index="indx_a_name2" />
+ <column name="name" index="indx_a_name" />
</property>
+ <property name="AnotherName" index="indx_a_anothername" />
+
<subclass
name="NHibernate.DomainModel.B, NHibernate.DomainModel"
discriminator-value="-1"
@@ -80,4 +82,4 @@
type="Double"
/>
</class>
-</hibernate-mapping>
+</hibernate-mapping>
View
38 src/NHibernate.Test/Legacy/ABCTest.cs
@@ -23,20 +23,21 @@ protected override IList Mappings
}
[Test]
- public void HigherLevelIndexDefinition()
+ public void HigherLevelIndexDefinitionInColumnTag()
{
string[] commands = cfg.GenerateSchemaCreationScript( dialect );
- int max = commands.Length;
- bool found = false;
-
- foreach( string command in commands )
- {
- System.Console.WriteLine("Checking command : " + command);
- found = command.IndexOf("create index indx_a_name") >= 0;
- if (found)
- break;
- }
- Assert.IsTrue( found, "Unable to locate indx_a_name index creation" );
+
+ Assert.IsTrue(ContainsCommandWithSubstring(commands, "create index indx_a_name"),
+ "Unable to locate indx_a_name index creation");
+ }
+
+ [Test]
+ public void HigherLevelIndexDefinitionInPropertyTag()
+ {
+ string[] commands = cfg.GenerateSchemaCreationScript( dialect );
+
+ Assert.IsTrue(ContainsCommandWithSubstring(commands, "create index indx_a_anothername"),
+ "Unable to locate indx_a_anothername index creation");
}
[Test]
@@ -165,5 +166,18 @@ public void Subclassing()
t.Commit();
s.Close();
}
+
+ private static bool ContainsCommandWithSubstring(string[] commands, string subString)
+ {
+ foreach (string command in commands)
+ {
+ System.Console.WriteLine("Checking command : " + command);
+ if (command.IndexOf(subString) >= 0)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
}
}
View
7 src/NHibernate.Test/Legacy/SQLLoaderTest.cs
@@ -337,8 +337,8 @@ public void DoubleAliasing()
session = OpenSession();
- 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} " +
- ", b.identifier_column as {a2.id}, b.clazz_discriminata as {a2.class}, b.count_ as {a2.Count}, b.name as {a2.Name} " +
+ 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} " +
+ ", 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} " +
" from A a, A b" +
" where a.identifier_column = b.identifier_column", new String[] {"a1", "a2" }, new System.Type[] {typeof( A ), typeof( A )});
IList list = query.List();
@@ -561,7 +561,8 @@ public void FindBySQLDiscriminatedDiffSessions()
session = OpenSession();
- 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 ));
+ IQuery query = session.CreateSQLQuery(
+ "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 ));
IList list = query.List();
Assert.IsNotNull(list);
View
58 src/NHibernate/Cfg/HbmBinder.cs
@@ -474,12 +474,12 @@ public static void BindRootClass(XmlNode node, RootClass model, Mappings mapping
public static void BindColumns(XmlNode node, SimpleValue model, bool isNullable, bool autoColumn, string propertyPath,
Mappings mappings)
{
+ Table table = model.Table;
//COLUMN(S)
XmlAttribute columnAttribute = node.Attributes["column"];
if (columnAttribute == null)
{
int count = 0;
- Table table = model.Table;
foreach (XmlNode columnElement in node.SelectNodes(HbmConstants.nsColumn, nsmgr))
{
@@ -496,42 +496,27 @@ public static void BindRootClass(XmlNode node, RootClass model, Mappings mapping
model.AddColumn(col);
//column index
- XmlAttribute indexNode = columnElement.Attributes["index"];
- if (indexNode != null && table != null)
- {
- table.GetIndex(indexNode.Value).AddColumn(col);
- }
-
+ BindIndex(columnElement.Attributes["index"], table, col, mappings);
//column group index (although it can serve as a separate column index)
- XmlAttribute parentElementIndexAttr = node.Attributes["index"];
- if (parentElementIndexAttr != null && table != null)
- {
- table.GetIndex(parentElementIndexAttr.Value).AddColumn(col);
- }
- XmlAttribute uniqueNode = columnElement.Attributes["unique-key"];
- if (uniqueNode != null && table != null)
- {
- table.GetUniqueKey(uniqueNode.Value).AddColumn(col);
- }
+ BindIndex(node.Attributes["index"], table, col, mappings);
+
+ BindUniqueKey(columnElement.Attributes["unique-key"], table, col, mappings);
+ BindUniqueKey(node.Attributes["unique-key"], table, col, mappings);
}
}
else
{
Column col = new Column(model.Type, 0);
BindColumn(node, col, isNullable);
col.Name = mappings.NamingStrategy.ColumnName(columnAttribute.Value);
- Table table = model.Table;
if (table != null)
{
table.AddColumn(col);
} //table=null -> an association - fill it in later
model.AddColumn(col);
//column group index (although can serve as a separate column index)
- XmlAttribute indexAttr = node.Attributes["index"];
- if (indexAttr != null && table != null)
- {
- table.GetIndex(indexAttr.Value).AddColumn(col);
- }
+ BindIndex(node.Attributes["index"], table, col, mappings);
+ BindUniqueKey(node.Attributes["unique-key"], table, col, mappings);
}
if (autoColumn && model.ColumnSpan == 0)
@@ -541,6 +526,33 @@ public static void BindRootClass(XmlNode node, RootClass model, Mappings mapping
col.Name = mappings.NamingStrategy.PropertyToColumnName(propertyPath);
model.Table.AddColumn(col);
model.AddColumn(col);
+ //column group index (although can serve as a separate column index)
+ BindIndex(node.Attributes["index"], table, col, mappings);
+ BindUniqueKey(node.Attributes["unique-key"], table, col, mappings);
+ }
+ }
+
+ private static void BindIndex(XmlAttribute indexAttribute, Table table, Column column, Mappings mappings)
+ {
+ if (indexAttribute != null && table != null)
+ {
+ StringTokenizer tokens = new StringTokenizer(indexAttribute.Value, ", ");
+ foreach (string token in tokens)
+ {
+ table.GetIndex(token).AddColumn(column);
+ }
+ }
+ }
+
+ private static void BindUniqueKey(XmlAttribute uniqueKeyAttribute, Table table, Column column, Mappings mappings)
+ {
+ if (uniqueKeyAttribute != null && table != null)
+ {
+ StringTokenizer tokens = new StringTokenizer(uniqueKeyAttribute.Value, ", ");
+ foreach (string token in tokens)
+ {
+ table.GetUniqueKey(token).AddColumn(column);
+ }
}
}
View
4 src/NHibernate/nhibernate-mapping.xsd
@@ -580,6 +580,7 @@
<xs:documentation>only supported for properties of a class (not component)</xs:documentation>
</xs:annotation>
</xs:attribute>
+ <xs:attribute name="unique-key" type="xs:string" />
<xs:attribute name="index" type="xs:string" />
</xs:complexType>
</xs:element>
@@ -604,6 +605,7 @@
<xs:attribute name="update" type="xs:boolean" default="true" />
<xs:attribute name="cascade" type="cascadeStyle" default="none" />
<xs:attribute name="index" type="xs:string" use="optional" />
+ <xs:attribute name="unique-key" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="array">
@@ -885,6 +887,8 @@
<xs:attribute name="column" type="xs:string" />
<xs:attribute name="not-null" type="xs:boolean" default="false" />
<xs:attribute name="unique" type="xs:boolean" default="false" />
+ <xs:attribute name="unique-key" type="xs:string" />
+ <xs:attribute name="index" type="xs:string" />
<xs:attribute name="cascade" type="cascadeStyle" />
<xs:attribute name="outer-join" type="outerJoinStrategy" use="optional" />
<xs:attribute name="fetch" type="fetchMode" use="optional" />

0 comments on commit b67fe68

Please sign in to comment.
Something went wrong with that request. Please try again.