forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleExpression.cs
147 lines (131 loc) · 4.1 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
using System;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Expression
{
/// <summary>
/// The base class for an <see cref="ICriterion"/> that compares a single Property
/// to a value.
/// </summary>
[Serializable]
public abstract class SimpleExpression : AbstractCriterion
{
private readonly string _propertyName;
private readonly object _value;
private bool _ignoreCase;
/// <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>
public SimpleExpression(string propertyName, object value)
{
_propertyName = propertyName;
_value = value;
}
public SimpleExpression(string propertyName, object value, bool ignoreCase)
{
_propertyName = propertyName;
_value = value;
_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, IDictionary<string, IFilter> enabledFilters)
{
string[] columnNames = criteriaQuery.GetColumnsUsingProjection(criteria, _propertyName);
IType propertyType = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName);
if (_value != null && !(_value is System.Type) && !propertyType.ReturnedClass.IsInstanceOfType(_value))
{
throw new QueryException(string.Format(
"Type mismatch in {0}: {1} expected type {2}, actual type {3}",
GetType(), _propertyName, propertyType.ReturnedClass, _value.GetType()));
}
if (propertyType.IsCollectionType)
{
throw new QueryException(string.Format(
"cannot use collection property ({0}.{1}) directly in a criterion,"
+ " use ICriteria.CreateCriteria instead",
criteriaQuery.GetEntityName(criteria), _propertyName));
}
if (_ignoreCase)
{
if (columnNames.Length != 1)
{
throw new HibernateException(
"case insensitive expression may only be applied to single-column properties: " +
_propertyName);
}
return new SqlStringBuilder(6)
.Add(criteriaQuery.Factory.Dialect.LowercaseFunction)
.Add(StringHelper.OpenParen)
.Add(columnNames[0])
.Add(StringHelper.ClosedParen)
.Add(Op)
.AddParameter()
.ToSqlString();
}
else
{
//TODO: add default capacity
SqlStringBuilder sqlBuilder = new SqlStringBuilder(4 * columnNames.Length);
for (int i = 0; i < columnNames.Length; i++)
{
if (i > 0)
{
sqlBuilder.Add(" and ");
}
sqlBuilder.Add(columnNames[i])
.Add(Op)
.AddParameter();
}
return sqlBuilder.ToSqlString();
}
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
object icvalue = _ignoreCase ? _value.ToString().ToLower() : _value;
return new TypedValue[]
{
criteriaQuery.GetTypedValue(criteria, _propertyName, icvalue)
};
}
/// <summary></summary>
public override string ToString()
{
return _propertyName + Op + _value;
}
/// <summary>
/// Get the Sql operator to use for the specific
/// subclass of <see cref="SimpleExpression"/>.
/// </summary>
protected abstract string Op { get; }
}
}