forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleExpression.cs
206 lines (182 loc) · 5.09 KB
/
SimpleExpression.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Criterion
{
/// <summary>
/// The base class for an <see cref="ICriterion"/> that compares a single Property
/// to a value.
/// </summary>
[Serializable]
public class SimpleExpression : AbstractCriterion
{
private readonly IProjection _projection;
private readonly string propertyName;
private readonly object value;
private bool ignoreCase;
private readonly string op;
protected internal SimpleExpression(IProjection projection, object value, string op)
{
_projection = projection;
this.value = value;
this.op = op;
}
/// <summary>
/// Initialize a new instance of the <see cref="SimpleExpression" /> class for a named
/// Property and its value.
/// </summary>
/// <param name="propertyName">The name of the Property in the class.</param>
/// <param name="value">The value for the Property.</param>
/// <param name="op">The SQL operation.</param>
public SimpleExpression(string propertyName, object value, string op)
{
this.propertyName = propertyName;
this.value = value;
this.op = op;
}
public SimpleExpression(string propertyName, object value, string op, bool ignoreCase)
: this(propertyName, value, op)
{
this.ignoreCase = ignoreCase;
}
public SimpleExpression IgnoreCase()
{
ignoreCase = true;
return this;
}
/// <summary>
/// Gets the named Property for the Expression.
/// </summary>
/// <value>A string that is the name of the Property.</value>
public string PropertyName
{
get { return propertyName; }
}
/// <summary>
/// Gets the Value for the Expression.
/// </summary>
/// <value>An object that is the value for the Expression.</value>
public object Value
{
get { return value; }
}
/// <summary>
/// Converts the SimpleExpression to a <see cref="SqlString"/>.
/// </summary>
/// <returns>A SqlString that contains a valid Sql fragment.</returns>
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
SqlString[] columnNames =
CriterionUtil.GetColumnNamesForSimpleExpression(
propertyName,
_projection,
criteriaQuery,
criteria,
this,
value);
TypedValue typedValue = GetParameterTypedValue(criteria, criteriaQuery);
Parameter[] parameters = criteriaQuery.NewQueryParameter(typedValue).ToArray();
if (ignoreCase)
{
if (columnNames.Length != 1)
{
throw new HibernateException(
"case insensitive expression may only be applied to single-column properties: " +
propertyName);
}
return new SqlString(
criteriaQuery.Factory.Dialect.LowercaseFunction,
StringHelper.OpenParen,
columnNames[0],
StringHelper.ClosedParen,
Op,
parameters.Single());
}
else
{
SqlStringBuilder sqlBuilder = new SqlStringBuilder(4 * columnNames.Length);
var columnNullness = typedValue.Type.ToColumnNullness(typedValue.Value, criteriaQuery.Factory);
if (columnNullness.Length != columnNames.Length)
{
throw new AssertionFailure("Column nullness length doesn't match number of columns.");
}
for (int i = 0; i < columnNames.Length; i++)
{
if (i > 0)
{
sqlBuilder.Add(" and ");
}
if (columnNullness[i])
{
sqlBuilder.Add(columnNames[i])
.Add(Op)
.Add(parameters[i]);
}
else
{
sqlBuilder.Add(columnNames[i])
.Add(" is null ");
}
}
return sqlBuilder.ToSqlString();
}
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var typedValues = new List<TypedValue>();
if (_projection != null)
{
typedValues.AddRange(_projection.GetTypedValues(criteria, criteriaQuery));
}
typedValues.Add(GetParameterTypedValue(criteria, criteriaQuery));
return typedValues.ToArray();
}
public TypedValue GetParameterTypedValue(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
object icvalue = ignoreCase ? value.ToString().ToLower() : value;
return CriterionUtil.GetTypedValue(criteriaQuery, criteria, _projection, propertyName, icvalue);
}
public override IProjection[] GetProjections()
{
if (_projection != null)
{
return new IProjection[] { _projection };
}
return null;
}
public override string ToString()
{
return (_projection ?? (object)propertyName) + Op + ValueToStrings();
}
/// <summary>
/// Get the Sql operator to use for the specific
/// subclass of <see cref="SimpleExpression"/>.
/// </summary>
protected virtual string Op
{
get { return op; }
}
private static readonly System.Type[] CallToStringTypes = new[]
{
typeof(DateTime),
typeof(string),
};
private string ValueToStrings()
{
if (value == null)
{
return "null";
}
var type = value.GetType();
if (type.IsPrimitive || CallToStringTypes.Any(t => t.IsAssignableFrom(type)))
{
return value.ToString();
}
return ObjectHelpers.IdentityToString(value);
}
}
}