forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectionList.cs
203 lines (181 loc) · 4.54 KB
/
ProjectionList.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using NHibernate.Engine;
using NHibernate.Impl;
using NHibernate.SqlCommand;
using NHibernate.Type;
namespace NHibernate.Criterion
{
[Serializable]
public class ProjectionList : IProjection
{
private List<IProjection> elements = new List<IProjection>();
protected internal ProjectionList()
{
}
public ProjectionList Create()
{
return new ProjectionList();
}
public ProjectionList Add(IProjection proj)
{
elements.Add(proj);
return this;
}
public ProjectionList Add(IProjection projection, String alias)
{
return Add(Projections.Alias(projection, alias));
}
public ProjectionList Add<T>(IProjection projection, Expression<Func<T>> alias)
{
return Add(projection, ExpressionProcessor.FindMemberExpression(alias.Body));
}
public IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var types = new List<IType>(Length);
for (int i = 0; i < Length; i++)
{
IType[] elemTypes = this[i].GetTypes(criteria, criteriaQuery);
foreach (IType elemType in elemTypes)
types.Add(elemType);
}
IType[] result = new IType[types.Count];
types.CopyTo(result, 0);
return result;
}
public SqlString ToSqlString(ICriteria criteria, int loc, ICriteriaQuery criteriaQuery)
{
SqlStringBuilder buf = new SqlStringBuilder();
for (int i = 0; i < Length; i++)
{
IProjection proj = this[i];
buf.Add(proj.ToSqlString(criteria, loc, criteriaQuery));
loc += proj.GetColumnAliases(loc, criteria, criteriaQuery).Length;
if (i < elements.Count - 1)
{
buf.Add(", ");
}
}
return buf.ToSqlString();
}
public SqlString ToGroupSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
SqlStringBuilder buf = new SqlStringBuilder();
for (int i = 0; i < Length; i++)
{
IProjection proj = this[i];
if (proj.IsGrouped)
{
buf.Add(proj.ToGroupSqlString(criteria, criteriaQuery))
.Add(", ");
}
}
if (buf.Count >= 2)
{
buf.RemoveAt(buf.Count - 1);
}
return buf.ToSqlString();
}
public string[] GetColumnAliases(int position, ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var result = new List<string>(Length);
for (var i = 0; i < Length; i++)
{
var colAliases = this[i].GetColumnAliases(position, criteria, criteriaQuery);
result.AddRange(colAliases);
position += colAliases.Length;
}
return result.ToArray();
}
public string[] GetColumnAliases(string alias, int position, ICriteria criteria, ICriteriaQuery criteriaQuery)
{
for (int i = 0; i < Length; i++)
{
string[] result = this[i].GetColumnAliases(alias, position, criteria, criteriaQuery);
if (result != null) return result;
position += this[i].GetColumnAliases(position, criteria, criteriaQuery).Length;
}
return null;
}
public IType[] GetTypes(string alias, ICriteria criteria, ICriteriaQuery criteriaQuery)
{
for (int i = 0; i < Length; i++)
{
IType[] result = this[i].GetTypes(alias, criteria, criteriaQuery);
if (result != null) return result;
}
return null;
}
public String[] Aliases
{
get
{
IList<string> aliasList = new List<string>(Length);
for (int i = 0; i < Length; i++)
{
String[] aliases = this[i].Aliases;
foreach (string alias in aliases)
aliasList.Add(alias);
}
string[] result = new string[aliasList.Count];
aliasList.CopyTo(result, 0);
return result;
}
}
public IProjection this[int index]
{
get
{
return elements[index];
}
}
public int Length
{
get { return elements.Count; }
}
public override String ToString()
{
return elements.ToString();
}
public bool IsGrouped
{
get
{
for (int i = 0; i < Length; i++)
{
if (this[i].IsGrouped)
return true;
}
return false;
}
}
public bool IsAggregate
{
get
{
for(int i = 0; i < Length; i++)
{
if(this[i].IsAggregate)
return true;
}
return false;
}
}
/// <summary>
/// Gets the typed values for parameters in this projection
/// </summary>
/// <param name="criteria">The criteria.</param>
/// <param name="criteriaQuery">The criteria query.</param>
/// <returns></returns>
public TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
List<TypedValue> values = new List<TypedValue>();
foreach (IProjection element in elements)
{
values.AddRange(element.GetTypedValues(criteria, criteriaQuery));
}
return values.ToArray();
}
}
}