forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJunction.cs
116 lines (98 loc) · 2.98 KB
/
Junction.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using NHibernate.Engine;
using NHibernate.Impl;
using NHibernate.SqlCommand;
using NHibernate.Util;
namespace NHibernate.Criterion
{
/// <summary>
/// A sequence of logical <see cref="ICriterion"/>s combined by some associative
/// logical operator.
/// </summary>
[Serializable]
public abstract class Junction : AbstractCriterion
{
private readonly List<ICriterion> criteria = new List<ICriterion>();
/// <summary>
/// Adds an <see cref="ICriterion"/> to the list of <see cref="ICriterion"/>s
/// to junction together.
/// </summary>
/// <param name="criterion">The <see cref="ICriterion"/> to add.</param>
/// <returns>
/// This <see cref="Junction"/> instance.
/// </returns>
public Junction Add(ICriterion criterion)
{
criteria.Add(criterion);
return this;
}
/// <summary>
/// Adds an <see cref="ICriterion"/> to the list of <see cref="ICriterion"/>s
/// to junction together.
/// </summary>
public Junction Add<T>(Expression<Func<T, bool>> expression)
{
ICriterion criterion = ExpressionProcessor.ProcessExpression<T>(expression);
criteria.Add(criterion);
return this;
}
/// <summary>
/// Adds an <see cref="ICriterion"/> to the list of <see cref="ICriterion"/>s
/// to junction together.
/// </summary>
public Junction Add(Expression<Func<bool>> expression)
{
ICriterion criterion = ExpressionProcessor.ProcessExpression(expression);
criteria.Add(criterion);
return this;
}
/// <summary>
/// Get the Sql operator to put between multiple <see cref="ICriterion"/>s.
/// </summary>
protected abstract String Op { get; }
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var typedValues = new List<TypedValue>();
foreach (ICriterion criterion in this.criteria)
{
TypedValue[] subvalues = criterion.GetTypedValues(criteria, criteriaQuery);
typedValues.AddRange(subvalues);
}
return typedValues.ToArray();
}
/// <summary>
/// The <see cref="SqlString" /> corresponding to an instance with no added
/// subcriteria.
/// </summary>
protected abstract SqlString EmptyExpression { get; }
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
if (this.criteria.Count == 0)
{
return EmptyExpression;
}
//TODO: add default capacity
SqlStringBuilder sqlBuilder = new SqlStringBuilder();
sqlBuilder.Add("(");
for (int i = 0; i < this.criteria.Count - 1; i++)
{
sqlBuilder.Add(this.criteria[i].ToSqlString(criteria, criteriaQuery));
sqlBuilder.Add(Op);
}
sqlBuilder.Add(this.criteria[this.criteria.Count - 1].ToSqlString(criteria, criteriaQuery));
sqlBuilder.Add(")");
return sqlBuilder.ToSqlString();
}
public override string ToString()
{
return '(' + string.Join(Op, criteria) + ')';
}
public override IProjection[] GetProjections()
{
return null;
}
}
}