|
From: <pa...@us...> - 2011-03-13 03:09:32
|
Revision: 5461
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5461&view=rev
Author: patearl
Date: 2011-03-13 03:09:26 +0000 (Sun, 13 Mar 2011)
Log Message:
-----------
SQLite: Improved cast function.
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Dialect/Function/CastFunction.cs
trunk/nhibernate/src/NHibernate/Dialect/SQLiteDialect.cs
Modified: trunk/nhibernate/src/NHibernate/Dialect/Function/CastFunction.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Dialect/Function/CastFunction.cs 2011-03-12 22:52:19 UTC (rev 5460)
+++ trunk/nhibernate/src/NHibernate/Dialect/Function/CastFunction.cs 2011-03-13 03:09:26 UTC (rev 5461)
@@ -71,17 +71,34 @@
{
throw new QueryException(string.Format("invalid Hibernate type for cast(): type {0} not found", typeName));
}
- return new SqlStringBuilder()
- .Add("cast(")
- .AddObject(args[0])
- .Add(" as ")
- .Add(sqlType)
- .Add(")")
- .ToSqlString();
+
+ if (CastingIsRequired(sqlType))
+ {
+ return new SqlStringBuilder()
+ .Add("cast(")
+ .AddObject(args[0])
+ .Add(" as ")
+ .Add(sqlType)
+ .Add(")")
+ .ToSqlString();
+ }
+ else
+ {
+ return new SqlStringBuilder()
+ .Add("(")
+ .AddObject(args[0])
+ .Add(")")
+ .ToSqlString();
+ }
}
#endregion
+ protected virtual bool CastingIsRequired(string sqlType)
+ {
+ return true;
+ }
+
#region IFunctionGrammar Members
bool IFunctionGrammar.IsSeparator(string token)
Modified: trunk/nhibernate/src/NHibernate/Dialect/SQLiteDialect.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Dialect/SQLiteDialect.cs 2011-03-12 22:52:19 UTC (rev 5460)
+++ trunk/nhibernate/src/NHibernate/Dialect/SQLiteDialect.cs 2011-03-13 03:09:26 UTC (rev 5461)
@@ -67,6 +67,8 @@
RegisterFunction("mod", new SQLFunctionTemplate(NHibernateUtil.Int32, "((?1) % (?2))"));
RegisterFunction("iif", new SQLFunctionTemplate(null, "case when ?1 then ?2 else ?3 end"));
+
+ RegisterFunction("cast", new SQLiteCastFunction());
}
public override Schema.IDataBaseSchema GetDataBaseSchema(DbConnection connection)
@@ -252,5 +254,16 @@
{
get { return "select randomblob(16)"; }
}
+
+ protected class SQLiteCastFunction : CastFunction
+ {
+ protected override bool CastingIsRequired(string sqlType)
+ {
+ // SQLite doesn't support casting to datetime types. It assumes you want an integer and destroys the date string.
+ if (sqlType.ToLowerInvariant().Contains("date") || sqlType.ToLowerInvariant().Contains("time"))
+ return false;
+ return true;
+ }
+ }
}
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|