-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathInFragment.cs
132 lines (117 loc) · 3.11 KB
/
InFragment.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Util;
namespace NHibernate.SqlCommand
{
/// <summary>
/// Represents an <c>... in (...)</c> expression
/// </summary>
public class InFragment
{
public static readonly string NotNull = "not null";
public static readonly string Null = "null";
private readonly List<object> values = new List<object>();
private string columnName;
/// <summary>
/// Add a value to the value list. Value may be a string,
/// a <see cref="Parameter" />, or one of special values
/// <see cref="Null" /> or <see cref="NotNull" />.
/// </summary>
public InFragment AddValue(object value)
{
values.Add(value);
return this;
}
public InFragment SetColumn(string colName)
{
columnName = colName;
return this;
}
public InFragment SetColumn(string alias, string colName)
{
columnName = !string.IsNullOrEmpty(alias)
? alias + StringHelper.Dot + colName
: colName;
return this;
}
public InFragment SetFormula(string alias, string formulaTemplate)
{
columnName = Template.ReplacePlaceholder(formulaTemplate, alias);
return this;
}
public SqlString ToFragmentString()
{
var buf = new SqlStringBuilder(values.Count * 5);
buf.Add(columnName);
if (values.Count > 1)
{
// is a comma needed before the value that's about to be added - it
// defaults to false because we don't need a comma right away.
bool commaNeeded = false;
// if a "null" is in the list of values then we need to manipulate
// the SqlString a little bit more at the end.
bool allowNull = false;
buf.Add(" in (");
for (int i = 0; i < values.Count; i++)
{
object value = values[i];
if (Null.Equals(value))
{
allowNull = true;
}
else if (NotNull.Equals(value))
{
throw new NotSupportedException(string.Format("not null makes no sense for in expression (column:{0})",columnName));
}
else
{
if (commaNeeded)
{
buf.Add(StringHelper.CommaSpace);
}
if (value is Parameter)
{
buf.Add((Parameter) value);
}
else
{
buf.Add((string) value);
}
// a value has been added into the IN clause so the next
// one needs a comma before it
commaNeeded = true;
}
}
buf.Add(StringHelper.ClosedParen);
// if "null" is in the list of values then add to the beginning of the
// SqlString "is null or [column] (" + [rest of sqlstring here] + ")"
if (allowNull)
{
buf.Insert(0, " is null or ").Insert(0, columnName).Insert(0, StringHelper.OpenParen).Add(StringHelper.ClosedParen);
}
}
else
{
if (values.Count == 0)
{
throw new NotSupportedException(string.Format("Attempting to parse a null value into an sql string (column:{0}).", columnName));
}
object value = values[0];
if (Null.Equals(value))
{
buf.Add(" is null");
}
else if (NotNull.Equals(value))
{
buf.Add(" is not null ");
}
else
{
buf.Add("=").AddObject(values[0]);
}
}
return buf.ToSqlString();
}
}
}