forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractEmptinessExpression.cs
91 lines (78 loc) · 2.81 KB
/
AbstractEmptinessExpression.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
using System;
using System.Text;
using NHibernate.Engine;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
using NHibernate.SqlCommand;
using NHibernate.Type;
namespace NHibernate.Criterion
{
[Serializable]
public abstract class AbstractEmptinessExpression : AbstractCriterion
{
private readonly TypedValue[] NO_VALUES = Array.Empty<TypedValue>();
private readonly string propertyName;
protected abstract bool ExcludeEmpty { get; }
protected AbstractEmptinessExpression(string propertyName)
{
this.propertyName = propertyName;
}
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
return NO_VALUES;
}
public override sealed string ToString()
{
return propertyName + (ExcludeEmpty ? " is not empty" : " is empty");
}
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var entityName = criteriaQuery.GetEntityName(criteria, propertyName);
var actualPropertyName = criteriaQuery.GetPropertyName(propertyName);
var sqlAlias = criteriaQuery.GetSQLAlias(criteria, propertyName);
var factory = criteriaQuery.Factory;
var collectionPersister = GetQueryableCollection(entityName, actualPropertyName, factory);
var collectionKeys = collectionPersister.KeyColumnNames;
var ownerKeys = ((ILoadable)factory.GetEntityPersister(entityName)).IdentifierColumnNames;
var innerSelect = new StringBuilder();
innerSelect.Append("(select 1 from ")
.Append(collectionPersister.TableName)
.Append(" where ")
.Append(
new ConditionalFragment().SetTableAlias(sqlAlias).SetCondition(ownerKeys, collectionKeys).ToSqlStringFragment());
if (collectionPersister.HasWhere)
{
innerSelect.Append(" and (")
.Append(collectionPersister.GetSQLWhereString(collectionPersister.TableName))
.Append(") ");
}
innerSelect.Append(")");
return new SqlString(new object[] {ExcludeEmpty ? "exists" : "not exists", innerSelect.ToString()});
}
protected IQueryableCollection GetQueryableCollection(string entityName, string actualPropertyName,
ISessionFactoryImplementor factory)
{
var ownerMapping = (IPropertyMapping) factory.GetEntityPersister(entityName);
var type = ownerMapping.ToType(actualPropertyName);
if (!type.IsCollectionType)
{
throw new MappingException(
"Property path [" + entityName + "." + actualPropertyName + "] does not reference a collection"
);
}
string role = ((CollectionType) type).Role;
try
{
return (IQueryableCollection) factory.GetCollectionPersister(role);
}
catch (InvalidCastException cce)
{
throw new QueryException("collection role is not queryable: " + role, cce);
}
catch (Exception e)
{
throw new QueryException("collection role not found: " + role, e);
}
}
}
}