Permalink
Browse files

Fix NH-3138 caused by MsSqlSelectParser

  • Loading branch information...
hazzik committed Sep 30, 2012
1 parent fb92b5e commit 99b228afbd6fc58677d0b2ca6cb9a41a9438385b
@@ -90,7 +90,7 @@ private static void BuildFromClauseForPagingQuery(MsSqlSelectParser sqlQuery, Sq
.Add(sqlQuery.SelectClause)
.Add(", ROW_NUMBER() OVER(ORDER BY ");
- int orderIndex = 0;
+ var orderIndex = 0;
foreach (var order in sqlQuery.Orders)
{
if (orderIndex++ > 0) result.Add(", ");
@@ -104,12 +104,12 @@ private static void BuildFromClauseForPagingQuery(MsSqlSelectParser sqlQuery, Sq
}
if (order.IsDescending) result.Add(" DESC");
}
+
if (orderIndex == 0)
{
result.Add("CURRENT_TIMESTAMP");
}
-
result.Add(") as __hibernate_sort_row ")
.Add(sqlQuery.FromAndWhereClause)
.Add(") as query");
@@ -198,15 +198,15 @@ private ColumnDefinition ParseSelectColumnDefinition(SqlToken beginToken, SqlTok
: alias;
var sqlIndex = beginToken.SqlIndex;
- var sqlLength = (endToken != null ? endToken.SqlIndex + endToken.Length : this.Sql.Length) - beginToken.SqlIndex;
+ var sqlLength = (endToken != null ? endToken.SqlIndex + endToken.Length : Sql.Length) - beginToken.SqlIndex;
return new ColumnDefinition(sqlIndex, sqlLength, name, alias, true);
}
private ColumnDefinition ParseOrderColumnDefinition(SqlToken beginToken, SqlToken endToken, string alias)
{
var sqlIndex = beginToken.SqlIndex;
- var sqlLength = (endToken != null ? endToken.SqlIndex + endToken.Length : this.Sql.Length) - beginToken.SqlIndex;
+ var sqlLength = (endToken != null ? endToken.SqlIndex + endToken.Length : Sql.Length) - beginToken.SqlIndex;
return new ColumnDefinition(sqlIndex, sqlLength, null, alias, false);
}
@@ -271,30 +271,24 @@ private IEnumerable<OrderDefinition> ParseOrderDefinitions(IEnumerator<SqlToken>
private OrderDefinition ParseOrderDefinition(SqlToken beginToken, SqlToken endToken, SqlToken directionToken)
{
- ColumnDefinition column;
- bool? isDescending = directionToken != null
- ? directionToken.Equals("desc", StringComparison.InvariantCultureIgnoreCase)
- : default(bool?);
+ var isDescending = directionToken != null &&
+ directionToken.Equals("desc", StringComparison.InvariantCultureIgnoreCase);
- if (beginToken == endToken)
- {
- string columnNameOrIndex = beginToken.Value;
- if (!TryGetColumnDefinition(columnNameOrIndex, out column))
- {
- // Column appears in order by clause, but not in select clause
- column = ParseOrderColumnDefinition(beginToken, endToken, "__c" + _nextOrderAliasIndex++);
- }
- }
- else
- {
- // Calculated sort order
+ var columnNameOrIndex = beginToken == endToken
+ ? beginToken.Value
+ : null;
+
+ ColumnDefinition column;
+ if (!TryGetColumnDefinition(columnNameOrIndex, out column, beginToken, endToken))
+ {
+ // Column appears in order by clause, but not in select clause
column = ParseOrderColumnDefinition(beginToken, endToken, "__c" + _nextOrderAliasIndex++);
}
- return new OrderDefinition(column, isDescending ?? false);
+ return new OrderDefinition(column, isDescending);
}
- private bool TryGetColumnDefinition(string columnNameOrIndex, out ColumnDefinition result)
+ private bool TryGetColumnDefinition(string columnNameOrIndex, out ColumnDefinition result, SqlToken beginToken, SqlToken endToken)
{
if (!string.IsNullOrEmpty(columnNameOrIndex))
{
@@ -315,6 +309,22 @@ private bool TryGetColumnDefinition(string columnNameOrIndex, out ColumnDefiniti
}
}
}
+ else
+ {
+ var sqlIndex = beginToken.SqlIndex;
+ var sqlLength = (endToken != null ? endToken.SqlIndex + endToken.Length : Sql.Length) - beginToken.SqlIndex;
+ var text = Sql.Substring(sqlIndex, sqlLength).ToString();
+ foreach (var column in _columns)
+ {
+ if (text.Equals(column.Name, StringComparison.InvariantCultureIgnoreCase) ||
+ text.Equals(column.Alias, StringComparison.InvariantCultureIgnoreCase) ||
+ text.Equals(Sql.Substring(column.SqlIndex, column.SqlLength).ToString(), StringComparison.InvariantCultureIgnoreCase))
+ {
+ result = column;
+ return true;
+ }
+ }
+ }
result = null;
return false;

0 comments on commit 99b228a

Please sign in to comment.