forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInExpression.cs
128 lines (110 loc) · 3.07 KB
/
InExpression.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
using System;
using System.Collections;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;
using System.Collections.Generic;
namespace NHibernate.Expression
{
/// <summary>
/// An <see cref="ICriterion"/> that constrains the property
/// to a specified list of values.
/// </summary>
/// <remarks>
/// InExpression - should only be used with a Single Value column - no multicolumn properties...
/// </remarks>
[Serializable]
public class InExpression : AbstractCriterion
{
private readonly string _propertyName;
private object[] _values;
/// <summary>
///
/// </summary>
/// <param name="propertyName"></param>
/// <param name="values"></param>
public InExpression(string propertyName, object[] values)
{
_propertyName = propertyName;
_values = values;
}
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
{
IType type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName);
if (type.IsCollectionType)
{
throw new QueryException("Cannot use collections with InExpression");
}
if (_values.Length == 0)
{
// "something in ()" is always false
return new SqlString("1=0");
}
//TODO: add default capacity
SqlStringBuilder result = new SqlStringBuilder();
string[] columnNames = criteriaQuery.GetColumnsUsingProjection(criteria, _propertyName);
// Generate SqlString of the form:
// columnName1 in (values) and columnName2 in (values) and ...
for (int columnIndex = 0; columnIndex < columnNames.Length; columnIndex++)
{
string columnName = columnNames[columnIndex];
if (columnIndex > 0)
{
result.Add(" and ");
}
result
.Add(columnName)
.Add(" in (");
for (int i = 0; i < _values.Length; i++)
{
if (i > 0)
{
result.Add(StringHelper.CommaSpace);
}
result.AddParameter();
}
result.Add(")");
}
return result.ToSqlString();
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
ArrayList list = new ArrayList();
IType type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName);
if (type.IsComponentType)
{
IAbstractComponentType actype = (IAbstractComponentType) type;
IType[] types = actype.Subtypes;
for (int i = 0; i < types.Length; i++)
{
for (int j = 0; j < _values.Length; j++)
{
object subval = _values[j] == null ?
null :
actype.GetPropertyValues(_values[j])[i];
list.Add(new TypedValue(types[i], subval));
}
}
}
else
{
for (int j = 0; j < _values.Length; j++)
{
list.Add(new TypedValue(type, _values[j]));
}
}
return (TypedValue[]) list.ToArray(typeof(TypedValue));
}
public object[] Values
{
get { return _values; }
protected set { _values = value; }
}
/// <summary></summary>
public override string ToString()
{
return _propertyName + " in (" + StringHelper.ToString(_values) + ')';
}
}
}