Skip to content

NH-3525 - Db2Dialect issues "FETCH FOR N ROWS ONLY" when offset is NOT specified #222

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 1 commit into from
Feb 22, 2015
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
29 changes: 28 additions & 1 deletion src/NHibernate.Test/DialectTest/DB2DialectFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,38 @@ public void GetLimitString()
" order by a, x"
});

SqlString limited = dialect.GetLimitString(sql, new SqlString("111"), new SqlString("222"));
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",
limited.ToString());
Assert.AreEqual(2, limited.GetParameterCount());
}

[Test]
public void GetLimitString_NoOffsetSpecified_UsesFetchFirstOnly()
{
// arrange
DB2Dialect dialect = new DB2Dialect();
SqlString sql = new SqlString(
new object[]
{
"select a, b, c ",
"from d",
" where X = ",
Parameter.Placeholder,
" and Z = ",
Parameter.Placeholder,
" order by a, x"
});

// act
SqlString limited = dialect.GetLimitString(sql, null, new SqlString("222"));

// assert
Assert.AreEqual(
"select a, b, c from d where X = ? and Z = ? order by a, x fetch first 222 rows only",
limited.ToString());
Assert.AreEqual(2, limited.GetParameterCount());
}
}
}
22 changes: 15 additions & 7 deletions src/NHibernate/Dialect/DB2Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,22 @@ public override bool UseMaxForLimit
get { return true; }
}

/// <summary></summary>
public override bool SupportsVariableLimit
{
get { return false; }
}

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

/*
* "select * from (select row_number() over(orderby_clause) as rownum, "
* querySqlString_without select
Expand All @@ -224,20 +238,14 @@ public override SqlString GetLimitString(SqlString querySqlString, SqlString off
.Add(querySqlString.Substring(7))
.Add(") as tempresult where rownum ");

if (offset != null && limit != null)
if (limit != null)
{
pagingBuilder
.Add("between ")
.Add(offset)
.Add("+1 and ")
.Add(limit);
}
else if (limit != null)
{
pagingBuilder
.Add("<= ")
.Add(limit);
}
else
{
// We just have an offset.
Expand Down