Skip to content

NH-2824, NH-3452 - Add ability to specify column options for IIdMapper #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/NHibernate.Test/MappingByCode/MappersTests/IdMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,35 @@ public void CanSetLength()
mapper.Length(10);
Assert.That(hbmId.length, Is.EqualTo("10"));
}

[Test]
public void CanSetPrecision()
{
//NH-2824
var hbmId = new HbmId();
var mapper = new IdMapper(null, hbmId);
mapper.Column(x => x.Precision(10));
Assert.That(hbmId.column[0].precision, Is.EqualTo("10"));
}

[Test]
public void CanSetScale()
{
//NH-2824
var hbmId = new HbmId();
var mapper = new IdMapper(null, hbmId);
mapper.Column(x => x.Scale(10));
Assert.That(hbmId.column[0].scale, Is.EqualTo("10"));
}

[Test]
public void CanSqlType()
{
//NH-3452
var hbmId = new HbmId();
var mapper = new IdMapper(null, hbmId);
mapper.Column(x => x.SqlType("CHAR(10)"));
Assert.That(hbmId.column[0].sqltype, Is.EqualTo("CHAR(10)"));
}
}
}
3 changes: 1 addition & 2 deletions src/NHibernate/Mapping/ByCode/IIdMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NHibernate.Mapping.ByCode
{
public interface IIdMapper : IAccessorPropertyMapper
public interface IIdMapper : IAccessorPropertyMapper, IColumnsMapper
{
void Generator(IGeneratorDef generator);
void Generator(IGeneratorDef generator, Action<IGeneratorMapper> generatorMapping);
Expand All @@ -15,7 +15,6 @@ public interface IIdMapper : IAccessorPropertyMapper
//void Column(Action<IColumnMapper> columnMapper);
//void Columns(params Action<IColumnMapper>[] columnMapper);
void UnsavedValue(object value);
void Column(string name);
void Length(int length);
}
}
61 changes: 59 additions & 2 deletions src/NHibernate/Mapping/ByCode/Impl/IdMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NHibernate.Cfg.MappingSchema;
Expand All @@ -9,13 +10,15 @@ namespace NHibernate.Mapping.ByCode.Impl
public class IdMapper : IIdMapper
{
private readonly IAccessorPropertyMapper accessorMapper;
private readonly MemberInfo member;
private readonly HbmId hbmId;

public IdMapper(HbmId hbmId)
: this(null, hbmId) {}

public IdMapper(MemberInfo member, HbmId hbmId)
{
this.member = member;
this.hbmId = hbmId;
if (member != null)
{
Expand Down Expand Up @@ -68,14 +71,68 @@ public void UnsavedValue(object value)
hbmId.unsavedvalue = value != null ? value.ToString() : "null";
}

public void Column(Action<IColumnMapper> columnMapper)
{
if (hbmId.Columns.Count() > 1)
throw new MappingException("Multi-columns property can't be mapped through singlr-column API.");

HbmColumn hbm = hbmId.Columns.SingleOrDefault() ?? new HbmColumn
{
name = hbmId.column1,
length = hbmId.length
};

string defaultColumnName = member != null ? member.Name : null;
columnMapper(new ColumnMapper(hbm, member != null ? defaultColumnName : "unnamedcolumn"));
if (hbm.sqltype != null ||
hbm.@default != null ||
hbm.check != null ||
hbm.precision != null ||
hbm.scale != null ||
hbm.notnullSpecified ||
hbm.uniqueSpecified ||
hbm.uniquekey != null ||
hbm.index != null)
{
hbmId.column = new[] {hbm};
ResetIdPlainValues();
}
else
{
hbmId.column1 = defaultColumnName == null || defaultColumnName != hbm.name ? hbm.name : null;
hbmId.length = hbm.length;
}
}

public void Columns(params Action<IColumnMapper>[] columnMapper)
{
ResetIdPlainValues();
int i = 1;
var columns = new List<HbmColumn>(columnMapper.Length);
foreach (var action in columnMapper)
{
var hbm = new HbmColumn();
string defaultColumnName = (member != null ? member.Name : "unnamedcolumn") + i++;
action(new ColumnMapper(hbm, defaultColumnName));
columns.Add(hbm);
}
hbmId.column = columns.ToArray();
}

public void Column(string name)
{
hbmId.column1 = name;
Column(x => x.Name(name));
}

public void Length(int length)
{
hbmId.length = length.ToString();
Column(x => x.Length(length));
}

private void ResetIdPlainValues()
{
hbmId.column1 = null;
hbmId.length = null;
}

private void ApplyGenerator(IGeneratorDef generator)
Expand Down