[英]NHibernate: Is it possible for IUserType to convert a value to null and have it output as IS NULL in the resulting query?
We are using NHibernate as our ORM. We have a table that one of the columns has a custom IUserType mapped onto it.我们使用 NHibernate 作为我们的 ORM。我们有一个表,其中一列有一个自定义 IUserType 映射到它。 The purpose of the IUserType is that depending on the type of connection we are using the value might be null or just an empty string. IUserType 的用途是根据我们使用的连接类型,该值可能是 null 或只是一个空字符串。
So when we run this query depending on the underlying data storage the IUserType will convert the empty string to null Session.QueryOver<MyTable>().Where(t=>t.MyColumn == string.Empty).ToList()
Witch creates this query select * from MyTable where MyColumn = NULL因此,当我们根据底层数据存储运行此查询时,IUserType 会将空字符串转换为 null Session.QueryOver<MyTable>().Where(t=>t.MyColumn == string.Empty).ToList()
女巫创建此查询 select * 来自 MyTable,其中 MyColumn = NULL
Looking at the NHibernate code there is this code查看 NHibernate 代码有这段代码
SqlStringBuilder sqlBuilder = new SqlStringBuilder(4 * columnNames.Length);
var columnNullness = typedValue.Type.ToColumnNullness(typedValue.Value, criteriaQuery.Factory);
if (columnNullness.Length != columnNames.Length)
{
throw new AssertionFailure("Column nullness length doesn't match number of columns.");
}
for (int i = 0; i < columnNames.Length; i++)
{
if (i > 0)
{
sqlBuilder.Add(" and ");
}
if (columnNullness[i])
{
sqlBuilder.Add(columnNames[i])
.Add(Op)
.Add(parameters[i]);
}
else
{
sqlBuilder.Add(columnNames[i])
.Add(" is null ");
}
}
return sqlBuilder.ToSqlString();
but column nullness is just based on the current value not the converted value this is from CustomType in NHibernate code但列 nullness 仅基于当前值而不是转换值,这是来自 NHibernate 代码中的 CustomType
public override bool[] ToColumnNullness(object value, IMapping mapping)
{
bool[] result = new bool[GetColumnSpan(mapping)];
if (value != null)
ArrayHelper.Fill(result, true);
return result;
}
Is there anyway to have a IUserType convert a value to null and have the query come out as IS NULL?无论如何,是否有一个 IUserType 将一个值转换为 null 并且查询结果为 IS NULL?
Looks like a bug (missing feature) for IUserType
types.看起来像是IUserType
类型的错误(缺少功能)。
It seems in your case you can implement custom type based on AbstractStringType
.在您的情况下,您似乎可以基于AbstractStringType
实现自定义类型。 Something like:就像是:
[Serializable]
public class EmptyAsDbNullStringType : AbstractStringType
{
public EmptyAsDbNullStringType() : base(new StringSqlType())
{
}
public EmptyAsDbNullStringType(int length) : base(new StringSqlType(length))
{
}
public override string Name => "EmptyAsDbNullString";
public override void NullSafeSet(DbCommand st, object value, int index, bool[] settable,
ISessionImplementor session)
{
base.NullSafeSet(st, ToDbValue(value), index, settable, session);
}
public override object NullSafeGet(DbDataReader rs, string name, ISessionImplementor session)
{
return FromDbValue(base.NullSafeGet(rs, name, session));
}
public override bool[] ToColumnNullness(object value, IMapping mapping)
{
return base.ToColumnNullness(ToDbValue(value), mapping);
}
private static object ToDbValue(object value)
{
return string.Empty.Equals(value) ? null : value;
}
private static object FromDbValue(object value)
{
return value ?? string.Empty;
}
}
You might need to override some more members to make it work properly in all cases.您可能需要覆盖更多成员以使其在所有情况下都能正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.