如何在NHibernate的Custom SQLite Dialect中实现AddDays函数

时间:2015-08-04 19:10:49

标签: c# sqlite nhibernate

如果可以,请帮忙。

我正在使用NHibernate连接到Sql Server实例,一切都很顺利。对于我的测试应用程序(集成测试),我需要使用SQLite。除了使用DateTime.AddDays(n)作为查询的一部分派生日期(DateTime)的那些测试之外,我的所有测试都正常工作。

对于Sql Server,我扩展了MsSql2008Dialect并调用

public class CustomMsSql2008Dialect : MsSql2008Dialect
{
    public CustomMsSql2008Dialect()
    {
        RegisterFunction( "AddDays", new SQLFunctionTemplate( NHibernateUtil.DateTime, "DATEADD(day,?2,?1)" ) );
    }
}

并使用

进行配置
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="dialect">«namespace redacted».CustomMsSql2008Dialect, «assembly name redacted»</property>
        ...
    </session-factory>
</hibernate-configuration>

我认为这适用于我手动测试的查询。我还创建了所需的HQLGenerator

public class AddDaysGenerator : BaseHqlGeneratorForMethod
{
    public AddDaysGenerator()
    {
        SupportedMethods = new[] {
            ReflectionHelper.GetMethodDefinition<DateTime>(d => d.AddDays(0)),
        };
    }

    public override HqlTreeNode BuildHql( MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor )
    {
        return treeBuilder.MethodCall( "AddDays",
                                          visitor.Visit( targetObject ).AsExpression(),
                                          visitor.Visit( arguments[0] ).AsExpression()
                                      );
    }

并与

注册相同
public class ExtendedLinqtoHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
{
    public ExtendedLinqtoHqlGeneratorsRegistry() : base()
    {
        this.Merge( new AddDaysGenerator() );
    }
}

并使用

进行配置
configuration.LinqToHqlGeneratorsRegistry<«namespace redacted».ExtendedLinqtoHqlGeneratorsRegistry>();

我尝试将此扩展到SQLite但是使用以下类:

public class CustomSQLiteDialect : NHibernate.Dialect.SQLiteDialect
{
    public CustomSQLiteDialect()
    {
        RegisterFunction(
            "AddDays",
            new SQLFunctionTemplate( NHibernateUtil.DateTime, "date(?1, '+' || ?2 ||' day')"
            )
        );
    }
}

并使用

配置我的测试项目
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
        <property name="dialect">«namespace redacted».CustomSQLiteDialect, «assembly name redacted»</property>
        ...
    </session-factory>
</hibernate-configuration>

每次测试都会产生

System.NotSupportedException: Specified method is not supported.
带有堆栈跟踪的

NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.GetClassName(IASTNode querySource)
NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.Process(IASTNode tree)
NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process()
NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
NHibernate.Engine.Query.QueryExpressionPlan..ctor(IQueryExpression queryExpression, Boolean shallow, IDictionary``2 enabledFilters, ISessionFactoryImplementor factory)
NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary``2 enabledFilters)
NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
Remotion.Linq.QueryableBase``1.GetEnumerator()
System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)

我有很多疑问要测试。

这是SQLite中的错误吗?

这是NHibernate中的错误吗?

这(可能)是我如何实现这个的错误吗?

再次,请帮助。

0 个答案:

没有答案
相关问题