Skip to content

Implement support of SqlCheck in mapping by code #2743

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 9 commits into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
42 changes: 42 additions & 0 deletions src/NHibernate.Test/MappingByCode/MappersTests/JoinMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ public void CanSetSqlInsert()
Assert.That(hbmJoin.SqlInsert.Text[0], Is.EqualTo("blah"));
}

[Test]
public void CanSetSqlInsertWithCheck()
{
var mapdoc = new HbmMapping();
var hbmJoin = new HbmJoin();
var mapper = new JoinMapper(typeof(MyClass), "AA", hbmJoin, mapdoc);
mapper.SqlInsert("blah", SqlCheck.RowCount);

Assert.That(hbmJoin.SqlInsert, Is.Not.Null);
Assert.That(hbmJoin.SqlInsert.Text[0], Is.EqualTo("blah"));
Assert.That(hbmJoin.SqlInsert.checkSpecified, Is.True);
Assert.That(hbmJoin.SqlInsert.check, Is.EqualTo(HbmCustomSQLCheck.Rowcount));
}

[Test]
public void SetSqlUpdate()
{
Expand All @@ -124,6 +138,20 @@ public void SetSqlUpdate()
Assert.That(hbmJoin.SqlUpdate.Text[0], Is.EqualTo("blah"));
}

[Test]
public void SetSqlUpdateWithCheck()
{
var mapdoc = new HbmMapping();
var hbmJoin = new HbmJoin();
var mapper = new JoinMapper(typeof(MyClass), "AA", hbmJoin, mapdoc);
mapper.SqlUpdate("blah", SqlCheck.RowCount);

Assert.That(hbmJoin.SqlUpdate, Is.Not.Null);
Assert.That(hbmJoin.SqlUpdate.Text[0], Is.EqualTo("blah"));
Assert.That(hbmJoin.SqlUpdate.checkSpecified, Is.True);
Assert.That(hbmJoin.SqlUpdate.check, Is.EqualTo(HbmCustomSQLCheck.Rowcount));
}

[Test]
public void SetSqlDelete()
{
Expand All @@ -136,6 +164,20 @@ public void SetSqlDelete()
Assert.That(hbmJoin.SqlDelete.Text[0], Is.EqualTo("blah"));
}

[Test]
public void SetSqlDeleteWithCheck()
{
var mapdoc = new HbmMapping();
var hbmJoin = new HbmJoin();
var mapper = new JoinMapper(typeof(MyClass), "AA", hbmJoin, mapdoc);
mapper.SqlDelete("blah", SqlCheck.RowCount);

Assert.That(hbmJoin.SqlDelete, Is.Not.Null);
Assert.That(hbmJoin.SqlDelete.Text[0], Is.EqualTo("blah"));
Assert.That(hbmJoin.SqlDelete.checkSpecified, Is.True);
Assert.That(hbmJoin.SqlDelete.check, Is.EqualTo(HbmCustomSQLCheck.Rowcount));
}

[Test]
public void CanSetSqlSubselect()
{
Expand Down
44 changes: 44 additions & 0 deletions src/NHibernate/Mapping/ByCode/ICollectionSqlsWithCheckMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using NHibernate.Util;

namespace NHibernate.Mapping.ByCode
{
//6.0 TODO: merge into ICollectionSqlsMapper
public interface ICollectionSqlsWithCheckMapper
{
void SqlInsert(string sql, SqlCheck sqlCheck);
void SqlUpdate(string sql, SqlCheck sqlCheck);
void SqlDelete(string sql, SqlCheck sqlCheck);
void SqlDeleteAll(string sql, SqlCheck sqlCheck);
}

public static class CollectionSqlsWithCheckMapperExtensions
{
public static void SqlInsert(this ICollectionSqlsMapper mapper, string sql, SqlCheck sqlCheck)
{
ReflectHelper
.CastOrThrow<ICollectionSqlsWithCheckMapper>(mapper, "SqlInsert with sqlCheck")
.SqlInsert(sql, sqlCheck);
}

public static void SqlUpdate(this ICollectionSqlsMapper mapper, string sql, SqlCheck sqlCheck)
{
ReflectHelper
.CastOrThrow<ICollectionSqlsWithCheckMapper>(mapper, "SqlUpdate with sqlCheck")
.SqlUpdate(sql, sqlCheck);
}

public static void SqlDelete(this ICollectionSqlsMapper mapper, string sql, SqlCheck sqlCheck)
{
ReflectHelper
.CastOrThrow<ICollectionSqlsWithCheckMapper>(mapper, "SqlDelete with sqlCheck")
.SqlDelete(sql, sqlCheck);
}

public static void SqlDeleteAll(this ICollectionSqlsMapper mapper, string sql, SqlCheck sqlCheck)
{
ReflectHelper
.CastOrThrow<ICollectionSqlsWithCheckMapper>(mapper, "SqlDeleteAll with sqlCheck")
.SqlDeleteAll(sql, sqlCheck);
}
}
}
36 changes: 36 additions & 0 deletions src/NHibernate/Mapping/ByCode/IEntitySqlsWithCheckMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using NHibernate.Util;

namespace NHibernate.Mapping.ByCode
{
//6.0 TODO: merge into IEntitySqlsMapper
public interface IEntitySqlsWithCheckMapper
{
void SqlInsert(string sql, SqlCheck sqlCheck);
void SqlUpdate(string sql, SqlCheck sqlCheck);
void SqlDelete(string sql, SqlCheck sqlCheck);
}

public static class EntitySqlsWithCheckMapperExtensions
{
public static void SqlInsert(this IEntitySqlsMapper mapper, string sql, SqlCheck sqlCheck)
{
ReflectHelper
.CastOrThrow<IEntitySqlsWithCheckMapper>(mapper, "SqlInsert with sqlCheck")
.SqlInsert(sql, sqlCheck);
}

public static void SqlUpdate(this IEntitySqlsMapper mapper, string sql, SqlCheck sqlCheck)
{
ReflectHelper
.CastOrThrow<IEntitySqlsWithCheckMapper>(mapper, "SqlUpdate with sqlCheck")
.SqlUpdate(sql, sqlCheck);
}

public static void SqlDelete(this IEntitySqlsMapper mapper, string sql, SqlCheck sqlCheck)
{
ReflectHelper
.CastOrThrow<IEntitySqlsWithCheckMapper>(mapper, "SqlDelete with sqlCheck")
.SqlDelete(sql, sqlCheck);
}
}
}
47 changes: 45 additions & 2 deletions src/NHibernate/Mapping/ByCode/Impl/BagMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace NHibernate.Mapping.ByCode.Impl
{
public class BagMapper : IBagPropertiesMapper
public class BagMapper : IBagPropertiesMapper, ICollectionSqlsWithCheckMapper
{
private readonly IAccessorPropertyMapper entityPropertyMapper;
private readonly KeyMapper keyMapper;
Expand Down Expand Up @@ -249,6 +249,16 @@ public void SqlInsert(string sql)
mapping.sqlinsert.Text = new[] {sql};
}

public void SqlInsert(string sql, SqlCheck sqlCheck)
{
if (mapping.SqlInsert == null)
{
mapping.sqlinsert = new HbmCustomSQL();
}
mapping.sqlinsert.Text = new[] {sql};
mapping.sqlinsert.check = sqlCheck.ToHbmSqlCheck();
}

public void SqlUpdate(string sql)
{
if (mapping.SqlUpdate == null)
Expand All @@ -258,6 +268,17 @@ public void SqlUpdate(string sql)
mapping.sqlupdate.Text = new[] {sql};
}

public void SqlUpdate(string sql, SqlCheck sqlCheck)
{
if (mapping.SqlUpdate == null)
{
mapping.sqlupdate = new HbmCustomSQL();
}
mapping.sqlupdate.Text = new[] {sql};
mapping.sqlupdate.checkSpecified = true;
mapping.sqlupdate.check = sqlCheck.ToHbmSqlCheck();
}

public void SqlDelete(string sql)
{
if (mapping.SqlDelete == null)
Expand All @@ -267,6 +288,17 @@ public void SqlDelete(string sql)
mapping.sqldelete.Text = new[] {sql};
}

public void SqlDelete(string sql, SqlCheck sqlCheck)
{
if (mapping.SqlDelete == null)
{
mapping.sqldelete = new HbmCustomSQL();
}
mapping.sqldelete.Text = new[] {sql};
mapping.sqldelete.checkSpecified = true;
mapping.sqldelete.check = sqlCheck.ToHbmSqlCheck();
}

public void SqlDeleteAll(string sql)
{
if (mapping.SqlDeleteAll == null)
Expand All @@ -276,6 +308,17 @@ public void SqlDeleteAll(string sql)
mapping.sqldeleteall.Text = new[] {sql};
}

public void SqlDeleteAll(string sql, SqlCheck sqlCheck)
{
if (mapping.SqlDeleteAll == null)
{
mapping.sqldeleteall = new HbmCustomSQL();
}
mapping.sqldeleteall.Text = new[] {sql};
mapping.sqldeleteall.checkSpecified = true;
mapping.sqldeleteall.check = sqlCheck.ToHbmSqlCheck();
}

public void Subselect(string sql)
{
if (mapping.Subselect == null)
Expand All @@ -287,4 +330,4 @@ public void Subselect(string sql)

#endregion
}
}
}
35 changes: 34 additions & 1 deletion src/NHibernate/Mapping/ByCode/Impl/ClassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace NHibernate.Mapping.ByCode.Impl
{
public class ClassMapper : AbstractPropertyContainerMapper, IClassMapper
public class ClassMapper : AbstractPropertyContainerMapper, IClassMapper, IEntitySqlsWithCheckMapper
{
private readonly HbmClass classMapping;
private readonly IIdMapper idMapper;
Expand Down Expand Up @@ -369,6 +369,17 @@ public void SqlInsert(string sql)
classMapping.sqlinsert.Text = new[] {sql};
}

public void SqlInsert(string sql, SqlCheck sqlCheck)
{
if (classMapping.SqlInsert == null)
{
classMapping.sqlinsert = new HbmCustomSQL();
}
classMapping.sqlinsert.checkSpecified = true;
classMapping.sqlinsert.check = sqlCheck.ToHbmSqlCheck();
classMapping.sqlinsert.Text = new[] { sql };
}

public void SqlUpdate(string sql)
{
if (classMapping.SqlUpdate == null)
Expand All @@ -378,6 +389,17 @@ public void SqlUpdate(string sql)
classMapping.sqlupdate.Text = new[] {sql};
}

public void SqlUpdate(string sql, SqlCheck sqlCheck)
{
if (classMapping.SqlUpdate == null)
{
classMapping.sqlupdate = new HbmCustomSQL();
}
classMapping.sqlupdate.checkSpecified = true;
classMapping.sqlupdate.check = sqlCheck.ToHbmSqlCheck();
classMapping.sqlupdate.Text = new[] { sql };
}

public void SqlDelete(string sql)
{
if (classMapping.SqlDelete == null)
Expand All @@ -387,6 +409,17 @@ public void SqlDelete(string sql)
classMapping.sqldelete.Text = new[] {sql};
}

public void SqlDelete(string sql, SqlCheck sqlCheck)
{
if (classMapping.SqlDelete == null)
{
classMapping.sqldelete = new HbmCustomSQL();
}
classMapping.sqldelete.checkSpecified = true;
classMapping.sqldelete.check = sqlCheck.ToHbmSqlCheck();
classMapping.sqldelete.Text = new[] { sql };
}

public void Subselect(string sql)
{
if (classMapping.Subselect == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace NHibernate.Mapping.ByCode.Impl.CustomizersImpl
{
public class ClassCustomizer<TEntity> : PropertyContainerCustomizer<TEntity>, IClassMapper<TEntity>, IConformistHoldersProvider where TEntity : class
public class ClassCustomizer<TEntity> : PropertyContainerCustomizer<TEntity>, IClassMapper<TEntity>, IConformistHoldersProvider, IEntitySqlsWithCheckMapper where TEntity : class
{
private Dictionary<string, IJoinMapper<TEntity>> joinCustomizers;

Expand Down Expand Up @@ -264,16 +264,31 @@ public void SqlInsert(string sql)
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.SqlInsert(sql));
}

public void SqlInsert(string sql, SqlCheck sqlCheck)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.SqlInsert(sql, sqlCheck));
}

public void SqlUpdate(string sql)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.SqlUpdate(sql));
}

public void SqlUpdate(string sql, SqlCheck sqlCheck)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.SqlUpdate(sql, sqlCheck));
}

public void SqlDelete(string sql)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.SqlDelete(sql));
}

public void SqlDelete(string sql, SqlCheck sqlCheck)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.SqlDelete(sql, sqlCheck));
}

public void Subselect(string sql)
{
CustomizersHolder.AddCustomizer(typeof(TEntity), (IClassMapper m) => m.Subselect(sql));
Expand All @@ -291,4 +306,4 @@ IModelExplicitDeclarationsHolder IConformistHoldersProvider.ExplicitDeclarations
get { return ExplicitDeclarationsHolder; }
}
}
}
}
Loading