-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathSubselectClauseExtractor.cs
211 lines (182 loc) · 5.19 KB
/
SubselectClauseExtractor.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
204
205
206
207
208
209
210
211
using System;
using System.Collections;
using NHibernate.Util;
namespace NHibernate.SqlCommand
{
/// <summary>
/// Given an SQL SELECT statement, parse it to extract clauses starting with
/// <c>FROM</c>, up to and not including <c>ORDER BY</c> (known collectively
/// as a subselect clause).
/// </summary>
public class SubselectClauseExtractor
{
/*
* NH TODO: this implementation will break, for MsSQL2005Dialect, a when the query is an HQL with skip/take because the last "ORDER BY" is there for pagination.
* Because HQL skip/take are new features, we hope nobody will use it in conjuction with subselect fetching at least until MS-SQL will release a more modern
* syntax for pagination.
*/
private const string FromClauseToken = " from ";
private const string OrderByToken = "order by";
private int lastOrderByIndex;
private int lastOrderByPartIndex;
private int parenNestCount;
private SqlString sql;
/// <summary>
/// Contains the subselect clause as it is being built.
/// </summary>
private SqlStringBuilder builder;
/// <summary>
/// Initializes a new instance of the <see cref="SubselectClauseExtractor"/> class.
/// </summary>
/// <param name="sql">The <see cref="SqlString" /> to extract the subselect clause from.</param>
public SubselectClauseExtractor(SqlString sql)
{
builder = new SqlStringBuilder(sql.Count);
this.sql = sql;
lastOrderByIndex = -1;
lastOrderByPartIndex = -1;
}
/// <summary>
/// Looks for a <c>FROM</c> clause in the <paramref name="part"/>
/// and adds the clause to the result if found.
/// </summary>
/// <param name="part">A <see cref="String"/> or a <see cref="Parameter"/>.</param>
/// <returns><see langword="true" /> if the part contained a <c>FROM</c> clause,
/// <see langword="false" /> otherwise.</returns>
private bool ProcessPartBeforeFrom(object part)
{
string partString = part as string;
if (partString == null)
{
return false;
}
int fromStart = FindFromClauseInPart(partString);
if (fromStart >= 0)
{
AddPart(partString.Substring(fromStart));
return true;
}
return false;
}
/// <summary>
/// Returns the subselect clause of the statement
/// being processed.
/// </summary>
/// <returns>An <see cref="SqlString" /> containing
/// the subselect clause of the original <c>SELECT</c>
/// statement.</returns>
public SqlString GetSqlString()
{
using (var partEnumerator = sql.GetEnumerator())
{
parenNestCount = 0;
// Process the parts until FROM is found
while (partEnumerator.MoveNext())
{
var part = partEnumerator.Current;
if (ProcessPartBeforeFrom(part))
{
break;
}
}
// Process the rest
while (partEnumerator.MoveNext())
{
AddPart(partEnumerator.Current);
}
}
RemoveLastOrderByClause();
return builder.ToSqlString();
}
public static bool HasOrderBy(SqlString subselect)
{
var extractor = new SubselectClauseExtractor(subselect);
extractor.GetSqlString();
return extractor.lastOrderByPartIndex >= 0;
}
private int FindFromClauseInPart(string part)
{
int afterLastClosingParenIndex = 0;
int fromIndex = part.IndexOf(FromClauseToken, StringComparison.OrdinalIgnoreCase);
for (int i = 0; i < part.Length; i++)
{
if (parenNestCount == 0 && i > fromIndex)
{
break;
}
if (part[i] == '(')
{
++parenNestCount;
}
else if (part[i] == ')')
{
--parenNestCount;
if (parenNestCount != 0)
{
continue;
}
afterLastClosingParenIndex = i + 1;
fromIndex = part.IndexOf(FromClauseToken, afterLastClosingParenIndex, StringComparison.OrdinalIgnoreCase);
}
}
if (afterLastClosingParenIndex == 0)
{
fromIndex = part.IndexOf(FromClauseToken, StringComparison.OrdinalIgnoreCase);
}
if(parenNestCount > 0)
return -1;
return fromIndex;
}
private void AddPart(object part)
{
builder.AddObject(part);
CheckLastPartForOrderByClause();
}
private void CheckLastPartForOrderByClause()
{
object part = builder[builder.Count - 1];
if (part == Parameter.Placeholder)
{
return;
}
string partString = part as string;
int index = partString.LastIndexOf(OrderByToken, StringComparison.OrdinalIgnoreCase);
if (index >= 0)
{
lastOrderByPartIndex = builder.Count - 1;
lastOrderByIndex = index;
}
IgnoreOrderByInSubselect(partString);
}
private void IgnoreOrderByInSubselect(string partString)
{
var index = partString.LastIndexOf(")", StringComparison.Ordinal);
if (index >= 0 && ParenIsAfterLastOrderBy(index))
{
lastOrderByPartIndex = -1;
lastOrderByIndex = -1;
}
}
private bool ParenIsAfterLastOrderBy(int index)
{
return (builder.Count - 1) > lastOrderByPartIndex || index > lastOrderByIndex;
}
private void RemoveLastOrderByClause()
{
if (lastOrderByPartIndex < 0)
{
// No ORDER BY clause
return;
}
while (builder.Count > lastOrderByPartIndex + 1)
{
builder.RemoveAt(builder.Count - 1);
}
string lastPart = builder[builder.Count - 1] as string;
if (lastPart != null)
{
builder[builder.Count - 1] = lastPart.Substring(0, lastOrderByIndex);
}
}
}
}