How to work around SQLite lack of DateTime2 support?

943 views
Skip to first unread message

Chris F

unread,
Aug 21, 2009, 3:51:49 PM8/21/09
to S#arp Architecture
Hi,

I've used a DateTimeConvention to map all my DateTime properties as
datetime2 in SQL2008. But now my Project.Data tests are broken because
SQLite's dialect does not support DateTime2.

Is there a workaround for this?

Thanks
- Chris

Chris F

unread,
Aug 21, 2009, 4:17:09 PM8/21/09
to S#arp Architecture
I tried modifying my DateTimeConvention using a build condition for
unit testing:

public void Apply(IProperty target)
{
#if UNIT_TEST
target.CustomSqlTypeIs("datetime2(0)");
#else
target.CustomTypeIs("DateTime2");
#endif
}

giving a UNIT_TEST build mapping like:
<property name="CreatedDate" type="DateTime">
<column name="CreatedDate" sql-type="datetime2(0)" />
</property>

SQLite likes that fine, but NH doesn't push to SQL2008 as datetime2
like you'd expect for the UNIT_TEST build, as tested using
"01/01/0001"! Back to the drawing board.

...

Snahider

unread,
Aug 22, 2009, 3:14:59 AM8/22/09
to S#arp Architecture
I faced a similar problem with the substring function. I extended the
dialect for the database

public class CustomSQLiteDialect:SQLiteDialect
{
public CustomSQLiteDialect()
{
RegisterFunction("substr", new StandardSQLFunction
("substr", NHibernateUtil.String));
RegisterFunction("substring", new StandardSQLFunction
("substr", NHibernateUtil.String));
}
}

In the config file use the CustomDialect:
<property
name="dialect">Tests.Northwind.Data.Dialect.CustomSQLiteDialect,
Northwind.Tests</property>

Try to add something like:

RegisterColumnType(System.Data.DbType.DateTime2, "date");

Links about SqlDialect:
http://github.com/gwenn/sqlite-dialect/blob/73d07f0990ecfd9b9a869066b8f7b3e023af39ff/org/hibernate/dialect/SQLiteDialect.java
http://ayende.com/Blog/archive/2006/10/01/UsingSQLFunctionsInNHibernate.aspx


Chris F

unread,
Aug 24, 2009, 8:52:42 AM8/24/09
to S#arp Architecture
Thanks a lot,

I'll try and give this a shot today. I wonder if I'll still have
issues due to the nature of datetime not having the same range as
datetime2!

- Chris

On Aug 22, 3:14 am, Snahider <snahi...@gmail.com> wrote:
> I faced a similar problem with the substring function. I extended the
> dialect for the database
>
>     public class CustomSQLiteDialect:SQLiteDialect
>     {
>         public CustomSQLiteDialect()
>         {
>             RegisterFunction("substr", new StandardSQLFunction
> ("substr", NHibernateUtil.String));
>             RegisterFunction("substring", new StandardSQLFunction
> ("substr", NHibernateUtil.String));
>         }
>     }
>
> In the config file use the CustomDialect:
> <property
> name="dialect">Tests.Northwind.Data.Dialect.CustomSQLiteDialect,
> Northwind.Tests</property>
>
> Try to add something like:
>
> RegisterColumnType(System.Data.DbType.DateTime2, "date");
>
> Links about SqlDialect:http://github.com/gwenn/sqlite-dialect/blob/73d07f0990ecfd9b9a869066b...http://ayende.com/Blog/archive/2006/10/01/UsingSQLFunctionsInNHiberna...
Reply all
Reply to author
Forward
0 new messages