forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInExpression.cs
182 lines (155 loc) · 5.43 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
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
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>
/// An <see cref="ICriterion"/> that constrains the property
/// to a specified list of values.
/// </summary>
[Serializable]
public class InExpression : AbstractCriterion
{
private readonly IProjection _projection;
private readonly string _propertyName;
private object[] _values;
/// <summary>
/// Initializes a new instance of the <see cref="InExpression"/> class.
/// </summary>
/// <param name="projection">The projection.</param>
/// <param name="values">The _values.</param>
public InExpression(IProjection projection, object[] values)
{
_projection = projection;
_values = values;
}
public InExpression(string propertyName, object[] values)
{
_propertyName = propertyName;
_values = values;
}
public override IProjection[] GetProjections()
{
if (_projection != null)
{
return new IProjection[] { _projection };
}
return null;
}
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
if (_projection == null)
{
AssertPropertyIsNotCollection(criteriaQuery, criteria);
}
if (_values.Length == 0)
{
// "something in ()" is always false
return new SqlString("1=0");
}
SqlString[] columns = CriterionUtil.GetColumnNames(_propertyName, _projection, criteriaQuery, criteria);
var list = new List<Parameter>(columns.Length * Values.Length);
foreach (var typedValue in GetParameterTypedValues(criteria, criteriaQuery))
{
//Must be executed after CriterionUtil.GetColumnNames (as it might add _projection parameters to criteria)
list.AddRange(criteriaQuery.NewQueryParameter(typedValue));
}
return GetSqlString(columns, Values.Length, list, criteriaQuery.Factory.Dialect);
}
internal static SqlString GetSqlString(object[] columns, int paramsCount, IReadOnlyList<Parameter> parameters, Dialect.Dialect dialect)
{
var bogusParam = Parameter.Placeholder;
var sqlString = GetSqlString(columns, paramsCount, bogusParam, dialect);
sqlString.SubstituteBogusParameters(parameters, bogusParam);
return sqlString;
}
private static SqlString GetSqlString(object[] columns, int paramsCount, Parameter bogusParam, Dialect.Dialect dialect)
{
if (columns.Length <= 1 || dialect.SupportsRowValueConstructorSyntaxInInList)
{
var wrapInParens = columns.Length > 1;
const string comaSeparator = ", ";
var singleValueParam = SqlStringHelper.Repeat(new SqlString(bogusParam), columns.Length, comaSeparator, wrapInParens);
var parameters = SqlStringHelper.Repeat(singleValueParam, paramsCount, comaSeparator, wrapInParens: false);
//single column: col1 in (?, ?)
//multi column: (col1, col2) in ((?, ?), (?, ?))
return new SqlString(
wrapInParens ? StringHelper.OpenParen : string.Empty,
SqlStringHelper.JoinParts(comaSeparator, columns),
wrapInParens ? StringHelper.ClosedParen : string.Empty,
" in (",
parameters,
")");
}
//((col1 = ? and col2 = ?) or (col1 = ? and col2 = ?))
var cols = new SqlString(
" ( ",
SqlStringHelper.Join(new SqlString(" = ", bogusParam, " and "), columns),
"= ",
bogusParam,
" ) ");
cols = SqlStringHelper.Repeat(cols, paramsCount, " or ", wrapInParens: paramsCount > 1);
return cols;
}
private void AssertPropertyIsNotCollection(ICriteriaQuery criteriaQuery, ICriteria criteria)
{
IType type = criteriaQuery.GetTypeUsingProjection(criteria, _propertyName);
if (type.IsCollectionType)
{
throw new QueryException("Cannot use collections with InExpression");
}
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var list = GetParameterTypedValues(criteria, criteriaQuery);
if (_projection != null)
list.InsertRange(0, _projection.GetTypedValues(criteria, criteriaQuery));
return list.ToArray();
}
private List<TypedValue> GetParameterTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
IType type = GetElementType(criteria, criteriaQuery);
if (!type.IsComponentType)
{
return _values.ToList(v => new TypedValue(type, v, false));
}
List<TypedValue> list = new List<TypedValue>();
IAbstractComponentType actype = (IAbstractComponentType) type;
var types = actype.Subtypes;
foreach (var value in _values)
{
var propertyValues = value != null ? actype.GetPropertyValues(value) : null;
for (int ti = 0; ti < types.Length; ti++)
{
list.Add(new TypedValue(types[ti], propertyValues?[ti], false));
}
}
return list;
}
/// <summary>
/// Determine the type of the elements in the IN clause.
/// </summary>
private IType GetElementType(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
if (_projection == null)
return criteriaQuery.GetTypeUsingProjection(criteria, _propertyName);
IType[] types = _projection.GetTypes(criteria, criteriaQuery);
if (types.Length != 1)
throw new QueryException("Cannot use projections that return more than a single column with InExpression");
return types[0];
}
public object[] Values
{
get { return _values; }
protected set { _values = value; }
}
public override string ToString()
{
return (_projection ?? (object)_propertyName) + " in (" + StringHelper.ToString(_values) + ')';
}
}
}