Skip to content

Commit

Permalink
Applied patch from Tolomaüs adding support for setting <key> attribut…
Browse files Browse the repository at this point in the history
…es on a one-to-many. Added appropriate tests.
  • Loading branch information
paulbatum committed Oct 31, 2009
1 parent 57457eb commit 0e26fad
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 1 deletion.
Expand Up @@ -922,6 +922,14 @@ public void OrderByClauseIgnoredForUnorderableCollections()
.Element("class/map").DoesntHaveAttribute("order-by");
}

[Test]
public void CanSpecifyUniqueKey()
{
new MappingTester<OneToManyTarget>()
.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<IOneToManyCollectionInspector> acceptance)
Expand Down
Expand Up @@ -220,5 +220,30 @@ public void SubselectShouldSetModelSubselectToValue()
.Mapping(m => m.Subselect("whee"))
.ModelShouldMatch(x => x.Subselect.ShouldEqual("whee"));
}

[Test]
public void KeyUpdateShouldSetModelValue()
{
OneToMany<ChildObject>(x => x.BagOfChildren)
.Mapping(m => m.KeyUpdate())
.ModelShouldMatch(x => x.Key.Update.ShouldBeTrue());
}

[Test]
public void KeyNullableShouldSetModelValue()
{
OneToMany<ChildObject>(x => x.BagOfChildren)
.Mapping(m => m.KeyNullable())
.ModelShouldMatch(x => x.Key.NotNull.ShouldBeFalse());
}

[Test]
public void KeyNotNullableShouldSetModelValue()
{
OneToMany<ChildObject>(x => x.BagOfChildren)
.Mapping(m => m.Not.KeyNullable())
.ModelShouldMatch(x => x.Key.NotNull.ShouldBeTrue());
}

}
}
Expand Up @@ -76,7 +76,7 @@ public void ElementMappingShouldntHaveOneToMany()
public void ShouldPerformKeyColumnMapping()
{
OneToMany<ChildObject>(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();
Expand Down
Expand Up @@ -44,6 +44,34 @@ public void ShouldWriteOnDeleteAttribute()
testHelper.VerifyAll(writer);
}

[Test]
public void ShouldWriteNotNullAttribute()
{
var testHelper = new XmlWriterTestHelper<KeyMapping>();
testHelper.Check(x => x.NotNull, true).MapsToAttribute("not-null");

testHelper.VerifyAll(writer);
}

[Test]
public void ShouldWriteUpdateAttribute()
{
var testHelper = new XmlWriterTestHelper<KeyMapping>();
testHelper.Check(x => x.Update, true).MapsToAttribute("update");

testHelper.VerifyAll(writer);
}

[Test]
public void ShouldWriteUniqueAttribute()
{
var testHelper = new XmlWriterTestHelper<KeyMapping>();
testHelper.Check(x => x.Unique, true).MapsToAttribute("unique");

testHelper.VerifyAll(writer);
}


[Test]
public void ShouldWriteColumns()
{
Expand Down
14 changes: 14 additions & 0 deletions src/FluentNHibernate/Mapping/OneToManyPart.cs
Expand Up @@ -163,5 +163,19 @@ public OneToManyPart<TChild> Subselect(string subselect)
collectionAttributes.Set(x => x.Subselect, subselect);
return this;
}

public OneToManyPart<TChild> KeyUpdate()
{
keyMapping.Update = nextBool;
nextBool = true;
return this;
}

public OneToManyPart<TChild> KeyNullable()
{
keyMapping.NotNull = !nextBool;
nextBool = true;
return this;
}
}
}
18 changes: 18 additions & 0 deletions src/FluentNHibernate/MappingModel/KeyMapping.cs
Expand Up @@ -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<ColumnMapping> Columns
{
get { return columns; }
Expand Down
10 changes: 10 additions & 0 deletions src/FluentNHibernate/MappingModel/Output/XmlKeyWriter.cs
Expand Up @@ -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)
Expand Down

0 comments on commit 0e26fad

Please sign in to comment.