Skip to content

Commit fe440a0

Browse files
Fix registration of current_date for some dialects
Also fix current_time for ASE 15 which looks like a bad copy-paste, since this database has the exact matching function.
1 parent 32537ba commit fe440a0

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

src/NHibernate.Test/Async/Hql/HQLFunctions.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,53 @@ public async Task Current_TimeStamp_OffsetAsync()
10581058
}
10591059
}
10601060

1061+
[Test]
1062+
public async Task Current_DateAsync()
1063+
{
1064+
AssumeFunctionSupported("current_date");
1065+
using (var s = OpenSession())
1066+
{
1067+
var a1 = new Animal("abcdef", 1.3f);
1068+
await (s.SaveAsync(a1));
1069+
await (s.FlushAsync());
1070+
}
1071+
using (var s = OpenSession())
1072+
{
1073+
var hql = "select current_date() from Animal";
1074+
await (s.CreateQuery(hql).ListAsync());
1075+
}
1076+
}
1077+
1078+
[Test]
1079+
public async Task Current_Date_IsLowestTimeOfDayAsync()
1080+
{
1081+
AssumeFunctionSupported("current_date");
1082+
var now = DateTime.Now;
1083+
if (!TestDialect.SupportsNonDataBoundCondition)
1084+
Assert.Ignore("Test is not supported by the target database");
1085+
if (now.TimeOfDay < TimeSpan.FromMinutes(5) || now.TimeOfDay > TimeSpan.Parse("23:55"))
1086+
Assert.Ignore("Test is unreliable around midnight");
1087+
1088+
var lowTimeDate = now.Date.AddSeconds(1);
1089+
1090+
using (var s = OpenSession())
1091+
{
1092+
var a1 = new Animal("abcdef", 1.3f);
1093+
await (s.SaveAsync(a1));
1094+
await (s.FlushAsync());
1095+
}
1096+
using (var s = OpenSession())
1097+
{
1098+
var hql = "from Animal where current_date() < :lowTimeDate";
1099+
var result =
1100+
await (s
1101+
.CreateQuery(hql)
1102+
.SetDateTime("lowTimeDate", lowTimeDate)
1103+
.ListAsync());
1104+
Assert.That(result, Has.Count.GreaterThan(0));
1105+
}
1106+
}
1107+
10611108
[Test]
10621109
public async Task ExtractAsync()
10631110
{

src/NHibernate.Test/Hql/HQLFunctions.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,53 @@ public void Current_TimeStamp_Offset()
10471047
}
10481048
}
10491049

1050+
[Test]
1051+
public void Current_Date()
1052+
{
1053+
AssumeFunctionSupported("current_date");
1054+
using (var s = OpenSession())
1055+
{
1056+
var a1 = new Animal("abcdef", 1.3f);
1057+
s.Save(a1);
1058+
s.Flush();
1059+
}
1060+
using (var s = OpenSession())
1061+
{
1062+
var hql = "select current_date() from Animal";
1063+
s.CreateQuery(hql).List();
1064+
}
1065+
}
1066+
1067+
[Test]
1068+
public void Current_Date_IsLowestTimeOfDay()
1069+
{
1070+
AssumeFunctionSupported("current_date");
1071+
if (!TestDialect.SupportsNonDataBoundCondition)
1072+
Assert.Ignore("Test is not supported by the target database");
1073+
var now = DateTime.Now;
1074+
if (now.TimeOfDay < TimeSpan.FromMinutes(5) || now.TimeOfDay > TimeSpan.Parse("23:55"))
1075+
Assert.Ignore("Test is unreliable around midnight");
1076+
1077+
var lowTimeDate = now.Date.AddSeconds(1);
1078+
1079+
using (var s = OpenSession())
1080+
{
1081+
var a1 = new Animal("abcdef", 1.3f);
1082+
s.Save(a1);
1083+
s.Flush();
1084+
}
1085+
using (var s = OpenSession())
1086+
{
1087+
var hql = "from Animal where current_date() < :lowTimeDate";
1088+
var result =
1089+
s
1090+
.CreateQuery(hql)
1091+
.SetDateTime("lowTimeDate", lowTimeDate)
1092+
.List();
1093+
Assert.That(result, Has.Count.GreaterThan(0));
1094+
}
1095+
}
1096+
10501097
[Test]
10511098
public void Extract()
10521099
{

src/NHibernate/Dialect/Oracle8iDialect.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ protected virtual void RegisterFunctions()
250250
RegisterFunction("to_char", new StandardSQLFunction("to_char", NHibernateUtil.String));
251251
RegisterFunction("to_date", new StandardSQLFunction("to_date", NHibernateUtil.DateTime));
252252

253-
RegisterFunction("current_date", new NoArgSQLFunction("current_date", NHibernateUtil.Date, false));
253+
// In Oracle, date includes a time, just with fractional seconds dropped. For actually only having
254+
// the date, it must be truncated. Otherwise comparisons may yield unexpected results.
255+
RegisterFunction("current_date", new SQLFunctionTemplate(NHibernateUtil.Date, "trunc(current_date)"));
254256
RegisterFunction("current_time", new NoArgSQLFunction("current_timestamp", NHibernateUtil.Time, false));
255257
RegisterFunction("current_timestamp", new CurrentTimeStamp());
256258

src/NHibernate/Dialect/SybaseASE15Dialect.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public SybaseASE15Dialect()
6868
RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(","+",")"));
6969
RegisterFunction("cos", new StandardSQLFunction("cos", NHibernateUtil.Double));
7070
RegisterFunction("cot", new StandardSQLFunction("cot", NHibernateUtil.Double));
71-
RegisterFunction("current_date", new NoArgSQLFunction("getdate", NHibernateUtil.Date));
72-
RegisterFunction("current_time", new NoArgSQLFunction("getdate", NHibernateUtil.Time));
71+
RegisterFunction("current_date", new NoArgSQLFunction("current_date", NHibernateUtil.Date));
72+
RegisterFunction("current_time", new NoArgSQLFunction("current_time", NHibernateUtil.Time));
7373
RegisterFunction("current_timestamp", new NoArgSQLFunction("getdate", NHibernateUtil.DateTime));
7474
RegisterFunction("datename", new StandardSQLFunction("datename", NHibernateUtil.String));
7575
RegisterFunction("day", new StandardSQLFunction("day", NHibernateUtil.Int32));

src/NHibernate/Dialect/SybaseSQLAnywhere10Dialect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ protected virtual void RegisterDateFunctions()
232232
// compatibility functions
233233
RegisterFunction("current_timestamp", new NoArgSQLFunction("getdate", NHibernateUtil.DateTime, true));
234234
RegisterFunction("current_time", new NoArgSQLFunction("getdate", NHibernateUtil.Time, true));
235-
RegisterFunction("current_date", new NoArgSQLFunction("getdate", NHibernateUtil.Date, true));
235+
RegisterFunction("current_date", new SQLFunctionTemplate(NHibernateUtil.Date, "date(getdate())"));
236236
}
237237

238238
protected virtual void RegisterStringFunctions()

0 commit comments

Comments
 (0)