forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountProjection.cs
55 lines (48 loc) · 1.27 KB
/
CountProjection.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
using System;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Criterion
{
/// <summary>
/// A Count
/// </summary>
[Serializable]
public class CountProjection : AggregateProjection
{
private bool distinct;
protected internal CountProjection(String prop) : base("count", prop) {}
protected internal CountProjection(IProjection projection) : base("count", projection) {}
public override IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
return new IType[] {NHibernateUtil.Int32};
}
public override string ToString()
{
return (distinct) ? "distinct " + base.ToString() : base.ToString();
}
public override SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery)
{
var buf = new SqlStringBuilder().Add("count(");
if (distinct)
{
buf.Add("distinct ");
}
if (projection != null)
{
buf.AddObject(CriterionUtil.GetColumnNameAsSqlStringPart(projection, criteriaQuery, criteria));
}
else
{
buf.Add(criteriaQuery.GetColumn(criteria, propertyName));
}
buf.Add(") as y").Add(position.ToString()).Add("_");
return buf.ToSqlString();
}
public CountProjection SetDistinct()
{
distinct = true;
return this;
}
}
}