From 0e26fad1ed28bf51eca9d427afd33d440c50089a Mon Sep 17 00:00:00 2001 From: Paul Batum Date: Sat, 31 Oct 2009 16:18:48 +1100 Subject: [PATCH] =?UTF-8?q?Applied=20patch=20from=20Toloma=C3=BCs=20adding?= =?UTF-8?q?=20support=20for=20setting=20=20attributes=20on=20a=20one-?= =?UTF-8?q?to-many.=20Added=20appropriate=20tests.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DomainModel/Mapping/OneToManyTester.cs | 8 ++++++ ...ManyMutablePropertyModelGenerationTests.cs | 25 +++++++++++++++++ .../OneToManySubPartModelGenerationTests.cs | 2 +- .../MappingModel/Output/XmlKeyWriterTester.cs | 28 +++++++++++++++++++ src/FluentNHibernate/Mapping/OneToManyPart.cs | 14 ++++++++++ .../MappingModel/KeyMapping.cs | 18 ++++++++++++ .../MappingModel/Output/XmlKeyWriter.cs | 10 +++++++ 7 files changed, 104 insertions(+), 1 deletion(-) diff --git a/src/FluentNHibernate.Testing/DomainModel/Mapping/OneToManyTester.cs b/src/FluentNHibernate.Testing/DomainModel/Mapping/OneToManyTester.cs index 739b1dbc8..82db43e2b 100644 --- a/src/FluentNHibernate.Testing/DomainModel/Mapping/OneToManyTester.cs +++ b/src/FluentNHibernate.Testing/DomainModel/Mapping/OneToManyTester.cs @@ -922,6 +922,14 @@ public void OrderByClauseIgnoredForUnorderableCollections() .Element("class/map").DoesntHaveAttribute("order-by"); } + [Test] + public void CanSpecifyUniqueKey() + { + new MappingTester() + .ForMapping(m => m.HasMany(x => x.MapOfChildren).KeyColumns.Add("key_col", c => c.Unique())) + .Element("class/bag/key/column").HasAttribute("unique", "true"); + } + private class TestO2MConvention : IHasManyConvention { public void Accept(IAcceptanceCriteria acceptance) diff --git a/src/FluentNHibernate.Testing/FluentInterfaceTests/OneToManyMutablePropertyModelGenerationTests.cs b/src/FluentNHibernate.Testing/FluentInterfaceTests/OneToManyMutablePropertyModelGenerationTests.cs index 49f2b584e..419bb8764 100644 --- a/src/FluentNHibernate.Testing/FluentInterfaceTests/OneToManyMutablePropertyModelGenerationTests.cs +++ b/src/FluentNHibernate.Testing/FluentInterfaceTests/OneToManyMutablePropertyModelGenerationTests.cs @@ -220,5 +220,30 @@ public void SubselectShouldSetModelSubselectToValue() .Mapping(m => m.Subselect("whee")) .ModelShouldMatch(x => x.Subselect.ShouldEqual("whee")); } + + [Test] + public void KeyUpdateShouldSetModelValue() + { + OneToMany(x => x.BagOfChildren) + .Mapping(m => m.KeyUpdate()) + .ModelShouldMatch(x => x.Key.Update.ShouldBeTrue()); + } + + [Test] + public void KeyNullableShouldSetModelValue() + { + OneToMany(x => x.BagOfChildren) + .Mapping(m => m.KeyNullable()) + .ModelShouldMatch(x => x.Key.NotNull.ShouldBeFalse()); + } + + [Test] + public void KeyNotNullableShouldSetModelValue() + { + OneToMany(x => x.BagOfChildren) + .Mapping(m => m.Not.KeyNullable()) + .ModelShouldMatch(x => x.Key.NotNull.ShouldBeTrue()); + } + } } \ No newline at end of file diff --git a/src/FluentNHibernate.Testing/FluentInterfaceTests/OneToManySubPartModelGenerationTests.cs b/src/FluentNHibernate.Testing/FluentInterfaceTests/OneToManySubPartModelGenerationTests.cs index 69c1b0514..b495034c7 100644 --- a/src/FluentNHibernate.Testing/FluentInterfaceTests/OneToManySubPartModelGenerationTests.cs +++ b/src/FluentNHibernate.Testing/FluentInterfaceTests/OneToManySubPartModelGenerationTests.cs @@ -76,7 +76,7 @@ public void ElementMappingShouldntHaveOneToMany() public void ShouldPerformKeyColumnMapping() { OneToMany(x => x.ListOfChildren) - .Mapping(m => m.KeyColumns.Add("col1", c => c.Length(50).Not.Nullable())) + .Mapping(m => m.KeyColumns.Add("col1", c => c.Length(50).Not.Nullable())) .ModelShouldMatch(x => { var column = x.Key.Columns.Single(); diff --git a/src/FluentNHibernate.Testing/MappingModel/Output/XmlKeyWriterTester.cs b/src/FluentNHibernate.Testing/MappingModel/Output/XmlKeyWriterTester.cs index 9a8905969..6777917db 100644 --- a/src/FluentNHibernate.Testing/MappingModel/Output/XmlKeyWriterTester.cs +++ b/src/FluentNHibernate.Testing/MappingModel/Output/XmlKeyWriterTester.cs @@ -44,6 +44,34 @@ public void ShouldWriteOnDeleteAttribute() testHelper.VerifyAll(writer); } + [Test] + public void ShouldWriteNotNullAttribute() + { + var testHelper = new XmlWriterTestHelper(); + testHelper.Check(x => x.NotNull, true).MapsToAttribute("not-null"); + + testHelper.VerifyAll(writer); + } + + [Test] + public void ShouldWriteUpdateAttribute() + { + var testHelper = new XmlWriterTestHelper(); + testHelper.Check(x => x.Update, true).MapsToAttribute("update"); + + testHelper.VerifyAll(writer); + } + + [Test] + public void ShouldWriteUniqueAttribute() + { + var testHelper = new XmlWriterTestHelper(); + testHelper.Check(x => x.Unique, true).MapsToAttribute("unique"); + + testHelper.VerifyAll(writer); + } + + [Test] public void ShouldWriteColumns() { diff --git a/src/FluentNHibernate/Mapping/OneToManyPart.cs b/src/FluentNHibernate/Mapping/OneToManyPart.cs index 1387a51f4..d578f61b7 100644 --- a/src/FluentNHibernate/Mapping/OneToManyPart.cs +++ b/src/FluentNHibernate/Mapping/OneToManyPart.cs @@ -163,5 +163,19 @@ public OneToManyPart Subselect(string subselect) collectionAttributes.Set(x => x.Subselect, subselect); return this; } + + public OneToManyPart KeyUpdate() + { + keyMapping.Update = nextBool; + nextBool = true; + return this; + } + + public OneToManyPart KeyNullable() + { + keyMapping.NotNull = !nextBool; + nextBool = true; + return this; + } } } diff --git a/src/FluentNHibernate/MappingModel/KeyMapping.cs b/src/FluentNHibernate/MappingModel/KeyMapping.cs index 3e495c39a..7c3384535 100644 --- a/src/FluentNHibernate/MappingModel/KeyMapping.cs +++ b/src/FluentNHibernate/MappingModel/KeyMapping.cs @@ -45,6 +45,24 @@ public string OnDelete set { attributes.Set(x => x.OnDelete, value); } } + public bool NotNull + { + get { return attributes.Get(x => x.NotNull); } + set { attributes.Set(x => x.NotNull, value); } + } + + public bool Update + { + get { return attributes.Get(x => x.Update); } + set { attributes.Set(x => x.Update, value); } + } + + public bool Unique + { + get { return attributes.Get(x => x.Unique); } + set { attributes.Set(x => x.Unique, value); } + } + public IDefaultableEnumerable Columns { get { return columns; } diff --git a/src/FluentNHibernate/MappingModel/Output/XmlKeyWriter.cs b/src/FluentNHibernate/MappingModel/Output/XmlKeyWriter.cs index f30f22d4b..8d60993aa 100644 --- a/src/FluentNHibernate/MappingModel/Output/XmlKeyWriter.cs +++ b/src/FluentNHibernate/MappingModel/Output/XmlKeyWriter.cs @@ -34,6 +34,16 @@ public override void ProcessKey(KeyMapping mapping) if (mapping.HasValue(x => x.PropertyRef)) element.WithAtt("property-ref", mapping.PropertyRef); + + if (mapping.HasValue(x => x.NotNull)) + element.WithAtt("not-null", mapping.NotNull); + + if (mapping.HasValue(x => x.Update)) + element.WithAtt("update", mapping.Update); + + if (mapping.HasValue(x => x.Unique)) + element.WithAtt("unique", mapping.Unique); + } public override void Visit(ColumnMapping mapping)