Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Applied patch for NH-1193

SVN: branches/1.2.x@3116
  • Loading branch information...
commit 4d71b9d52552f89a3079238c8353f4051b16b129 1 parent c6a7b24
Sergey Koshcheyev authored
View
22 src/NHibernate.Test/DialectTest/MsSql2005DialectFixture.cs
@@ -14,34 +14,44 @@ public void GetLimitString()
SqlString str = d.GetLimitString(new SqlString("SELECT fish.id FROM fish"), 0, 10);
Assert.AreEqual(
- "SELECT TOP 10 id FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__) as row, query.id FROM (SELECT fish.id, CURRENT_TIMESTAMP as __hibernate_sort_expr_1__ FROM fish) query ) page WHERE page.row > 0",
+ "SELECT TOP 10 id FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__) as row, query.id, query.__hibernate_sort_expr_1__ FROM (SELECT fish.id, CURRENT_TIMESTAMP as __hibernate_sort_expr_1__ FROM fish) query ) page WHERE page.row > 0 ORDER BY __hibernate_sort_expr_1__",
str.ToString());
str = d.GetLimitString(new SqlString("SELECT DISTINCT fish_.id FROM fish fish_"), 0, 10);
Assert.AreEqual(
- "SELECT TOP 10 id FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__) as row, query.id FROM (SELECT DISTINCT fish_.id, CURRENT_TIMESTAMP as __hibernate_sort_expr_1__ FROM fish fish_) query ) page WHERE page.row > 0",
+ "SELECT TOP 10 id FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__) as row, query.id, query.__hibernate_sort_expr_1__ FROM (SELECT DISTINCT fish_.id, CURRENT_TIMESTAMP as __hibernate_sort_expr_1__ FROM fish fish_) query ) page WHERE page.row > 0 ORDER BY __hibernate_sort_expr_1__",
str.ToString());
str = d.GetLimitString(new SqlString("SELECT DISTINCT fish_.id as ixx9_ FROM fish fish_"), 0, 10);
Assert.AreEqual(
- "SELECT TOP 10 ixx9_ FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__) as row, query.ixx9_ FROM (SELECT DISTINCT fish_.id as ixx9_, CURRENT_TIMESTAMP as __hibernate_sort_expr_1__ FROM fish fish_) query ) page WHERE page.row > 0",
+ "SELECT TOP 10 ixx9_ FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__) as row, query.ixx9_, query.__hibernate_sort_expr_1__ FROM (SELECT DISTINCT fish_.id as ixx9_, CURRENT_TIMESTAMP as __hibernate_sort_expr_1__ FROM fish fish_) query ) page WHERE page.row > 0 ORDER BY __hibernate_sort_expr_1__",
str.ToString());
str = d.GetLimitString(new SqlString("SELECT * FROM fish ORDER BY name"), 5, 15);
Assert.AreEqual(
- "SELECT TOP 15 * FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__) as row, query.* FROM (SELECT *, name as __hibernate_sort_expr_1__ FROM fish) query ) page WHERE page.row > 5",
+ "SELECT TOP 15 * FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__) as row, query.*, query.__hibernate_sort_expr_1__ FROM (SELECT *, name as __hibernate_sort_expr_1__ FROM fish) query ) page WHERE page.row > 5 ORDER BY __hibernate_sort_expr_1__",
str.ToString());
str = d.GetLimitString(new SqlString("SELECT fish.id, fish.name FROM fish ORDER BY name DESC"), 7, 28);
Assert.AreEqual(
- "SELECT TOP 28 id, name FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__ DESC) as row, query.id, query.name FROM (SELECT fish.id, fish.name, name as __hibernate_sort_expr_1__ FROM fish) query ) page WHERE page.row > 7",
+ "SELECT TOP 28 id, name FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__ DESC) as row, query.id, query.name, query.__hibernate_sort_expr_1__ FROM (SELECT fish.id, fish.name, name as __hibernate_sort_expr_1__ FROM fish) query ) page WHERE page.row > 7 ORDER BY __hibernate_sort_expr_1__ DESC",
str.ToString());
str =
d.GetLimitString(
new SqlString("SELECT * FROM fish LEFT JOIN (SELECT * FROM meat ORDER BY weight) AS t ORDER BY name DESC"), 10, 20);
Assert.AreEqual(
- "SELECT TOP 20 * FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__ DESC) as row, query.* FROM (SELECT *, name as __hibernate_sort_expr_1__ FROM fish LEFT JOIN (SELECT * FROM meat ORDER BY weight) AS t) query ) page WHERE page.row > 10",
+ "SELECT TOP 20 * FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__ DESC) as row, query.*, query.__hibernate_sort_expr_1__ FROM (SELECT *, name as __hibernate_sort_expr_1__ FROM fish LEFT JOIN (SELECT * FROM meat ORDER BY weight) AS t) query ) page WHERE page.row > 10 ORDER BY __hibernate_sort_expr_1__ DESC",
+ str.ToString());
+
+ str = d.GetLimitString(new SqlString("SELECT *, (SELECT COUNT(1) FROM fowl WHERE fish_id = fish.id) AS some_count FROM fish"), 0, 10);
+ Assert.AreEqual(
+ "SELECT TOP 10 *, some_count FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__) as row, query.*, query.some_count, query.__hibernate_sort_expr_1__ FROM (SELECT *, (SELECT COUNT(1) FROM fowl WHERE fish_id = fish.id) AS some_count, CURRENT_TIMESTAMP as __hibernate_sort_expr_1__ FROM fish) query ) page WHERE page.row > 0 ORDER BY __hibernate_sort_expr_1__",
+ str.ToString());
+
+ str = d.GetLimitString(new SqlString("SELECT * FROM fish WHERE scales = ", Parameter.Placeholder), 0, 10);
+ Assert.AreEqual(
+ "SELECT TOP 10 * FROM (SELECT ROW_NUMBER() OVER(ORDER BY __hibernate_sort_expr_1__) as row, query.*, query.__hibernate_sort_expr_1__ FROM (SELECT *, CURRENT_TIMESTAMP as __hibernate_sort_expr_1__ FROM fish WHERE scales = ?) query ) page WHERE page.row > 0 ORDER BY __hibernate_sort_expr_1__",
str.ToString());
}
}
View
10 src/NHibernate.Test/SqlCommandTest/SqlStringFixture.cs
@@ -351,5 +351,13 @@ public void GetSubselectStringWithTwoFormulas()
"select (select foo from bar where foo=col order by foo), (select foo from bar where foo=col order by foo) from table where col = ? order by col");
Assert.AreEqual(" from table where col = ? ", sql.GetSubselectString().ToString());
}
+
+ [Test]
+ public void GetSubselectStringWithOrderByInSubselect()
+ {
+ SqlString sql = SqlString.Parse("select col from table where (col = test) and id in (select id from foo order by bar)");
+ Assert.AreEqual(" from table where (col = test) and id in (select id from foo order by bar)", sql.GetSubselectString().ToString());
+ }
+
}
-}
+}
View
56 src/NHibernate/Dialect/MsSql2005Dialect.cs
@@ -40,7 +40,7 @@ public MsSql2005Dialect()
/// </remarks>
public override SqlString GetLimitString(SqlString querySqlString, int offset, int last)
{
- int fromIndex = querySqlString.IndexOfCaseInsensitive(" from ");
+ int fromIndex = GetFromIndex(querySqlString);
SqlString select = querySqlString.Substring(0, fromIndex);
ArrayList columnsOrAliases;
Hashtable aliasToColumn;
@@ -69,18 +69,7 @@ public override SqlString GetLimitString(SqlString querySqlString, int offset, i
.Add(StringHelper.Join(", ", columnsOrAliases))
.Add(" FROM (SELECT ROW_NUMBER() OVER(ORDER BY ");
- for (int i = 1; i <= sortExpressions.Length; i++)
- {
- if (i > 1)
- result.Add(", ");
-
- result.Add("__hibernate_sort_expr_")
- .Add(i.ToString())
- .Add("__");
-
- if (sortExpressions[i - 1].Trim().ToLower().EndsWith("desc"))
- result.Add(" DESC");
- }
+ AppendSortExpressions(sortExpressions, result);
result.Add(") as row, ");
@@ -92,6 +81,13 @@ public override SqlString GetLimitString(SqlString querySqlString, int offset, i
result.Add(", ");
}
+ for (int i = 1; i <= sortExpressions.Length; ++i)
+ {
+ result.Add(", query.__hibernate_sort_expr_")
+ .Add(i.ToString())
+ .Add("__");
+ }
+
result.Add(" FROM (")
.Add(select);
@@ -121,7 +117,10 @@ public override SqlString GetLimitString(SqlString querySqlString, int offset, i
result.Add(" ")
.Add(from)
.Add(") query ) page WHERE page.row > ")
- .Add(offset.ToString());
+ .Add(offset.ToString())
+ .Add(" ORDER BY ");
+
+ AppendSortExpressions(sortExpressions, result);
return result.ToSqlString();
}
@@ -138,7 +137,7 @@ public override SqlString GetLimitString(SqlString querySqlString, int offset, i
while ((currentIndex = selectString.IndexOf(",", lastIndex)) != -1)
{
string columnAndAlias = selectString.Substring(lastIndex, currentIndex - lastIndex);
- int seperatorPosition = columnAndAlias.IndexOf(" as ");
+ int seperatorPosition = columnAndAlias.IndexOf(" as ", StringComparison.InvariantCultureIgnoreCase);
string columnOrAliasName;
if (seperatorPosition != -1)
{
@@ -158,6 +157,33 @@ public override SqlString GetLimitString(SqlString querySqlString, int offset, i
}
}
+ private static void AppendSortExpressions(string[] sortExpressions, SqlStringBuilder result)
+ {
+ for (int i = 1; i <= sortExpressions.Length; i++)
+ {
+ if (i > 1)
+ result.Add(", ");
+
+ result.Add("__hibernate_sort_expr_")
+ .Add(i.ToString())
+ .Add("__");
+
+ if (sortExpressions[i - 1].Trim().ToLower().EndsWith("desc"))
+ result.Add(" DESC");
+ }
+ }
+
+ private static int GetFromIndex(SqlString querySqlString)
+ {
+ string subselect = querySqlString.GetSubselectString().ToString();
+ int fromIndex = querySqlString.IndexOfCaseInsensitive(subselect);
+ if (fromIndex == -1)
+ {
+ fromIndex = querySqlString.ToString().ToLowerInvariant().IndexOf(subselect.ToLowerInvariant());
+ }
+ return fromIndex;
+ }
+
/// <summary>
/// Sql Server 2005 supports a query statement that provides <c>LIMIT</c>
/// functionality.
View
20 src/NHibernate/SqlCommand/SubselectClauseExtractor.cs
@@ -153,6 +153,24 @@ private void CheckLastPartForOrderByClause()
lastOrderByPartIndex = builder.Count - 1;
lastOrderByIndex = index;
}
+
+ IgnoreOrderByInSubselect(partString);
+ }
+
+ private void IgnoreOrderByInSubselect(string partString)
+ {
+ int index;
+ index = StringHelper.LastIndexOfCaseInsensitive(partString, ")");
+ if (index >= 0 && ParenIsAfterLastOrderBy(index))
+ {
+ lastOrderByPartIndex = -1;
+ lastOrderByIndex = -1;
+ }
+ }
+
+ private bool ParenIsAfterLastOrderBy(int index)
+ {
+ return (builder.Count - 1) > lastOrderByPartIndex || index > lastOrderByIndex;
}
private void RemoveLastOrderByClause()
@@ -175,4 +193,4 @@ private void RemoveLastOrderByClause()
}
}
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.