forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCriterionUtil.cs
177 lines (157 loc) · 5.96 KB
/
CriterionUtil.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
namespace NHibernate.Criterion
{
using System;
using System.Collections.Generic;
using Engine;
using SqlCommand;
using Type;
public static class CriterionUtil
{
public static SqlString[] GetColumnNames(
string propertyName,
IProjection projection,
ICriteriaQuery criteriaQuery,
ICriteria criteria)
{
if (projection == null)
return GetColumnNamesUsingPropertyName(criteriaQuery, criteria, propertyName);
else
return GetColumnNamesUsingProjection(projection, criteriaQuery, criteria);
}
public static SqlString[] GetColumnNamesForSimpleExpression(
string propertyName,
IProjection projection,
ICriteriaQuery criteriaQuery,
ICriteria criteria,
ICriterion criterion,
object value)
{
if (projection == null)
{
return GetColumnNamesUsingPropertyName(
criteriaQuery,
criteria,
propertyName,
value,
criterion);
}
else
{
return GetColumnNamesUsingProjection(projection, criteriaQuery, criteria);
}
}
internal static SqlString[] GetColumnNamesUsingProjection(IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria)
{
if (projection is IPropertyProjection propertyProjection)
{
return GetColumnNamesUsingPropertyName(criteriaQuery, criteria, propertyProjection.PropertyName);
}
return GetProjectionColumns(projection, criteriaQuery, criteria);
}
internal static object[] GetColumnNamesAsSqlStringParts(string propertyName, IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria)
{
if (propertyName != null)
return criteriaQuery.GetColumnsUsingProjection(criteria, propertyName);
return GetColumnNamesAsSqlStringParts(projection, criteriaQuery, criteria);
}
internal static object[] GetColumnNamesAsSqlStringParts(IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria)
{
if (projection is IPropertyProjection propertyProjection)
{
return criteriaQuery.GetColumnsUsingProjection(criteria, propertyProjection.PropertyName);
}
return GetProjectionColumns(projection, criteriaQuery, criteria);
}
internal static object GetColumnNameAsSqlStringPart(string propertyName, IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria)
{
var columnNames = GetColumnNamesAsSqlStringParts(propertyName, projection, criteriaQuery, criteria);
if (columnNames.Length != 1)
{
throw new QueryException("property or projection does not map to a single column: " + (propertyName ?? projection.ToString()));
}
return columnNames[0];
}
internal static object GetColumnNameAsSqlStringPart(IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria)
{
var columnNames = GetColumnNamesAsSqlStringParts(projection, criteriaQuery, criteria);
if (columnNames.Length != 1)
{
throw new QueryException("property or projection does not map to a single column: " + (projection.ToString()));
}
return columnNames[0];
}
private static SqlString[] GetProjectionColumns(IProjection projection, ICriteriaQuery criteriaQuery, ICriteria criteria)
{
var sqlString = projection.ToSqlString(criteria, criteriaQuery.GetIndexForAlias(), criteriaQuery);
return new[] {SqlStringHelper.RemoveAsAliasesFromSql(sqlString)};
}
private static SqlString[] GetColumnNamesUsingPropertyName(ICriteriaQuery criteriaQuery, ICriteria criteria, string propertyName)
{
string[] columnNames = criteriaQuery.GetColumnsUsingProjection(criteria, propertyName);
return Array.ConvertAll<string, SqlString>(columnNames, delegate(string input) { return new SqlString(input); });
}
private static SqlString[] GetColumnNamesUsingPropertyName(
ICriteriaQuery criteriaQuery,
ICriteria criteria,
string propertyName,
object value,
ICriterion critertion)
{
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}",
critertion.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));
}
return Array.ConvertAll<string, SqlString>(columnNames, delegate(string col)
{
return new SqlString(col);
});
}
public static TypedValue[] GetTypedValues(ICriteriaQuery criteriaQuery, ICriteria criteria,
IProjection projection,
string propertyName,
params object[] values)
{
List<TypedValue> types = new List<TypedValue>();
var propertyProjection = projection as IPropertyProjection;
if (projection == null || propertyProjection != null)
{
var pn = propertyProjection != null ? propertyProjection.PropertyName : propertyName;
foreach (object value in values)
{
TypedValue typedValue = criteriaQuery.GetTypedValue(criteria, pn, value);
types.Add(typedValue);
}
}
else
{
foreach (object value in values)
{
types.Add(new TypedValue(NHibernateUtil.GuessType((object)value), value));
}
}
return types.ToArray();
}
public static TypedValue GetTypedValue(
ICriteriaQuery criteriaQuery,
ICriteria criteria,
IProjection projection,
string propertyName,
object value)
{
if (propertyName != null || (propertyName = (projection as IPropertyProjection)?.PropertyName) != null)
return criteriaQuery.GetTypedValue(criteria, propertyName, value);
return new TypedValue(NHibernateUtil.GuessType(value), value);
}
}
}