forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogicalExpression.cs
109 lines (97 loc) · 3.46 KB
/
LogicalExpression.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
using System;
using NHibernate.Engine;
using NHibernate.SqlCommand;
namespace NHibernate.Criterion
{
/// <summary>
/// An <see cref="ICriterion"/> that combines two <see cref="ICriterion"/>s
/// with a operator (either "<c>and</c>" or "<c>or</c>") between them.
/// </summary>
[Serializable]
public abstract class LogicalExpression : AbstractCriterion
{
private ICriterion _lhs;
private ICriterion _rhs;
/// <summary>
/// Initialize a new instance of the <see cref="LogicalExpression" /> class that
/// combines two other <see cref="ICriterion"/>s.
/// </summary>
/// <param name="lhs">The <see cref="ICriterion"/> to use in the Left Hand Side.</param>
/// <param name="rhs">The <see cref="ICriterion"/> to use in the Right Hand Side.</param>
protected LogicalExpression(ICriterion lhs, ICriterion rhs)
{
_lhs = lhs;
_rhs = rhs;
}
/// <summary>
/// Gets the <see cref="ICriterion"/> that will be on the Left Hand Side of the Op.
/// </summary>
protected ICriterion LeftHandSide
{
get { return _lhs; }
}
/// <summary>
/// Gets the <see cref="ICriterion" /> that will be on the Right Hand Side of the Op.
/// </summary>
protected ICriterion RightHandSide
{
get { return _rhs; }
}
/// <summary>
/// Combines the <see cref="TypedValue"/> for the Left Hand Side and the
/// Right Hand Side of the Expression into one array.
/// </summary>
/// <returns>An array of <see cref="TypedValue"/>s.</returns>
public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
TypedValue[] lhstv = _lhs.GetTypedValues(criteria, criteriaQuery);
TypedValue[] rhstv = _rhs.GetTypedValues(criteria, criteriaQuery);
TypedValue[] result = new TypedValue[lhstv.Length + rhstv.Length];
Array.Copy(lhstv, 0, result, 0, lhstv.Length);
Array.Copy(rhstv, 0, result, lhstv.Length, rhstv.Length);
return result;
}
public override IProjection[] GetProjections()
{
return null;
}
/// <summary>
/// Converts the LogicalExpression to a <see cref="SqlString"/>.
/// </summary>
/// <returns>A well formed SqlString for the Where clause.</returns>
/// <remarks>The SqlString will be enclosed by <c>(</c> and <c>)</c>.</remarks>
public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
//TODO: add default capacity
SqlStringBuilder sqlBuilder = new SqlStringBuilder();
SqlString lhSqlString = _lhs.ToSqlString(criteria, criteriaQuery);
SqlString rhSqlString = _rhs.ToSqlString(criteria, criteriaQuery);
sqlBuilder.Add(new SqlString[] {lhSqlString, rhSqlString},
"(",
Op,
")",
false // not wrapping because the prefix and postfix params already take care of that
);
return sqlBuilder.ToSqlString();
}
/// <summary>
/// Get the Sql operator to put between the two <see cref="Expression"/>s.
/// </summary>
protected abstract string Op { get; } //protected ???
/// <summary>
/// Gets a string representation of the LogicalExpression.
/// </summary>
/// <returns>
/// The String contains the LeftHandSide.ToString() and the RightHandSide.ToString()
/// joined by the Op.
/// </returns>
/// <remarks>
/// This is not a well formed Sql fragment. It is useful for logging what Expressions
/// are being combined.
/// </remarks>
public override string ToString()
{
return '(' + _lhs.ToString() + ' ' + Op + ' ' + _rhs.ToString() + ')';
}
}
}