我正在尝试在 NHibernate 4.x 中实现对 SQL Server 2016 时态表的基本支持。这个想法是改变SQL语句
SELECT * FROM Table t0
至
SELECT * FROM Table FOR SYSTEM_TIME AS OF '2018-01-16 00:00:00' t0
您可以在此处找到有关 SQL Server 2016 中时态表的更多信息
不幸的是,我还没有找到FOR FOR SYSTEM_TIME AS OF '...'
在表名和它的别名之间插入语句的任何方法。我不确定自定义方言是否支持这一点。我现在唯一可行的解决方案是FOR SYSTEM_TIME
在 extra 中附加语句WHERE
,我的输出 SQL 看起来像这样
SELECT * FROM Table t0 WHERE FOR SYSTEM_TIME AS OF '2018-01-16 00:00:00'=1
为此,我实现了生成器和方言,如下所示:
public static class AuditableExtensions
{
public static bool AsOf(this IAuditable entity, DateTime date)
{
return true;
}
public static IQueryable<T> Query<T>(this ISession session, DateTime asOf) where T : IAuditable
{
return session.Query<T>().Where(x => x.AsOf(asOf));
}
}
public class ForSystemTimeGenerator : BaseHqlGeneratorForMethod
{
public static readonly string ForSystemTimeAsOfString = "FOR SYSTEM_TIME AS OF";
public ForSystemTimeGenerator()
{
SupportedMethods = new[]
{
ReflectionHelper.GetMethod(() => AuditableExtensions.AsOf(null, DateTime.MinValue))
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
ReadOnlyCollection<Expression> arguments,
HqlTreeBuilder treeBuilder,
IHqlExpressionVisitor visitor)
{
return treeBuilder.BooleanMethodCall(nameof(AuditableExtensions.AsOf), new[]
{
visitor.Visit(arguments[1]).AsExpression()
});
}
}
public class MsSql2016Dialect : MsSql2012Dialect
{
public MsSql2016Dialect()
{
RegisterFunction(nameof(AuditableExtensions.AsOf), new SQLFunctionTemplate(
NHibernateUtil.Boolean,
$"{ForSystemTimeGenerator.ForSystemTimeAsOfString} ?1?2=1"));
}
}
谁能提供任何更好的方法或示例,我可以用来FOR SYSTEM_TIME AS OF
在表名及其别名之间向前移动和插入语句?目前我能看到的唯一解决方案是在其中更改 SQL,OnPrepareStatement
但SessionInterceptor
我相信有一些更好的方法......