Skip to content

Fix paging in DB2Dialect #2547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/NHibernate.Test/DialectTest/DB2DialectFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void GetLimitString()

SqlString limited = dialect.GetLimitString(sql, new SqlString("111"), new SqlString("222"));
Assert.AreEqual(
"select * from (select rownumber() over(order by a, x) as rownum, a, b, c from d where X = ? and Z = ? order by a, x) as tempresult where rownum between 111+1 and 222",
"select a,b,c from (select rownumber() over(order by a, x) as rownum, a, b, c from d where X = ? and Z = ? order by a, x) as tempresult where rownum between 111+1 and 222",
limited.ToString());
Assert.AreEqual(2, limited.GetParameterCount());
}
Expand Down Expand Up @@ -58,4 +58,4 @@ public void GetLimitString_NoOffsetSpecified_UsesFetchFirstOnly()
Assert.AreEqual(2, limited.GetParameterCount());
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate.Test/ReadOnly/TextHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TextHolder
/// </summary>
public static bool SupportedForDialect(Dialect.Dialect dialect)
{
return !(dialect is FirebirdDialect || dialect is Oracle8iDialect || dialect is MsSqlCeDialect || dialect is HanaRowStoreDialect);
return !(dialect is FirebirdDialect || dialect is Oracle8iDialect || dialect is MsSqlCeDialect || dialect is HanaRowStoreDialect || dialect is DB2Dialect);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking this is not related, but let it be here.

}

private long id;
Expand Down
17 changes: 9 additions & 8 deletions src/NHibernate/Dialect/DB2Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,28 +232,29 @@ public override bool SupportsVariableLimit
get { return false; }
}

public override SqlString GetLimitString(SqlString querySqlString, SqlString offset, SqlString limit)
public override SqlString GetLimitString(SqlString sql, SqlString offset, SqlString limit)
{
if (offset == null)
{
return new SqlString(querySqlString,
" fetch first ",
limit,
" rows only");
return new SqlString(sql, " fetch first ", limit, " rows only");
}

ExtractColumnOrAliasNames(sql, out var selectColumns, out _, out _);

/*
* "select * from (select row_number() over(orderby_clause) as rownum, "
* querySqlString_without select
* " ) as tempresult where rownum between ? and ?"
*/
string rownumClause = GetRowNumber(querySqlString);
string rownumClause = GetRowNumber(sql);

SqlStringBuilder pagingBuilder = new SqlStringBuilder();
pagingBuilder
.Add("select * from (select ")
.Add("select " )
.Add(string.Join(",", selectColumns))
.Add(" from (select ")
.Add(rownumClause)
.Add(querySqlString.Substring(7))
.Add(sql.Substring(7))
.Add(") as tempresult where rownum ");

if (limit != null)
Expand Down