forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNvlFunction.cs
79 lines (69 loc) · 1.82 KB
/
NvlFunction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;
namespace NHibernate.Dialect.Function
{
/// <summary>
/// Emulation of coalesce() on Oracle, using multiple nvl() calls
/// </summary>
[Serializable]
public class NvlFunction : ISQLFunction, ISQLFunctionExtended
{
public NvlFunction()
{
}
#region ISQLFunction Members
// Since v5.3
[Obsolete("Use GetReturnType method instead.")]
public IType ReturnType(IType columnType, IMapping mapping)
{
return columnType;
}
/// <inheritdoc />
public IType GetReturnType(IEnumerable<IType> argumentTypes, IMapping mapping, bool throwOnError)
{
#pragma warning disable 618
return ReturnType(argumentTypes.FirstOrDefault(), mapping);
#pragma warning restore 618
}
/// <inheritdoc />
public virtual IType GetEffectiveReturnType(IEnumerable<IType> argumentTypes, IMapping mapping, bool throwOnError)
{
return GetReturnType(argumentTypes, mapping, throwOnError);
}
/// <inheritdoc />
public string Name => "nvl";
public bool HasArguments
{
get { return true; }
}
public bool HasParenthesesIfNoArguments
{
get { return true; }
}
public SqlString Render(IList args, ISessionFactoryImplementor factory)
{
// DONE: QueryException if args.Count==0 (not present in H3.2)
if (args.Count == 0)
{
throw new QueryException("nvl(): Not enough parameters.");
}
int lastIndex = args.Count - 1;
object last = args[lastIndex];
args.RemoveAt(lastIndex);
if (lastIndex == 0)
{
return new SqlString(last);
}
object secondLast = args[lastIndex - 1];
SqlString nvl = new SqlString("nvl(", secondLast, ", ", last, ")");
args[lastIndex - 1] = nvl;
return Render(args, factory);
}
#endregion
}
}