forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrder.cs
166 lines (144 loc) · 3.95 KB
/
Order.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
using System;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;
namespace NHibernate.Criterion
{
/// <summary>
/// Represents an order imposed upon a <see cref="ICriteria"/>
/// result set.
/// </summary>
/// <remarks>
/// Should Order implement ICriteriaQuery?
/// </remarks>
[Serializable]
public class Order
{
protected bool ascending;
protected string propertyName;
protected IProjection projection;
private bool ignoreCase;
public Order(IProjection projection, bool ascending)
{
this.projection = projection;
this.ascending = ascending;
}
public Order(string propertyName, bool ascending)
{
this.propertyName = propertyName;
this.ascending = ascending;
}
/// <summary>
/// Render the SQL fragment
/// </summary>
public virtual SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var columnsOrAliases = GetColumnsOrAliases(criteria, criteriaQuery);
var sqlTypes = ignoreCase ? SqlTypes(criteria, criteriaQuery) : null;
var fragment = new SqlStringBuilder();
var factory = criteriaQuery.Factory;
for (var i = 0; i < columnsOrAliases.Length; i++)
{
var lower = sqlTypes != null && IsStringType(sqlTypes[i]);
if (lower)
{
fragment
.Add(factory.Dialect.LowercaseFunction)
.Add("(");
}
fragment.AddObject(columnsOrAliases[i]);
if (lower)
{
fragment.Add(")");
}
fragment.Add(ascending ? " asc" : " desc");
if (i < columnsOrAliases.Length - 1)
{
fragment.Add(", ");
}
}
return fragment.ToSqlString();
}
private object[] GetColumnsOrAliases(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var propName = propertyName ?? (projection as IPropertyProjection)?.PropertyName;
return propName != null
? criteriaQuery.GetColumnAliasesUsingProjection(criteria, propName)
: CriterionUtil.GetColumnNamesAsSqlStringParts(projection, criteriaQuery, criteria);
}
private SqlType[] SqlTypes(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
var type = projection == null
? criteriaQuery.GetTypeUsingProjection(criteria, propertyName)
: projection.GetTypes(criteria, criteriaQuery)[0];
return type.SqlTypes(criteriaQuery.Factory);
}
public override string ToString()
{
return (projection != null ? projection.ToString() : propertyName) + (ascending ? " asc" : " desc");
}
/// <summary>
/// Ascending order
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
public static Order Asc(string propertyName)
{
return new Order(propertyName, true);
}
/// <summary>
/// Ascending order
/// </summary>
/// <param name="projection"></param>
/// <returns></returns>
public static Order Asc(IProjection projection)
{
return new Order(projection, true);
}
/// <summary>
/// Descending order
/// </summary>
/// <param name="projection"></param>
/// <returns></returns>
public static Order Desc(IProjection projection)
{
return new Order(projection, false);
}
/// <summary>
/// Descending order
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
public static Order Desc(string propertyName)
{
return new Order(propertyName, false);
}
public TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
{
if (projection != null)
return projection.GetTypedValues(criteria, criteriaQuery);
return Array.Empty<TypedValue>(); // not using parameters for ORDER BY columns
}
public Order IgnoreCase()
{
ignoreCase = true;
return this;
}
private bool IsStringType(SqlTypes.SqlType propertyType)
{
switch (propertyType.DbType)
{
case System.Data.DbType.AnsiString:
return true;
case System.Data.DbType.AnsiStringFixedLength:
return true;
case System.Data.DbType.String:
return true;
case System.Data.DbType.StringFixedLength:
return true;
default:
return false;
}
}
}
}