Skip to content

Commit

Permalink
Merge pull request #302 from markrendle/301
Browse files Browse the repository at this point in the history
Fixes #301
  • Loading branch information
richardhopton committed Aug 12, 2013
2 parents b507bec + 356e370 commit 8242a66
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Simple.Data.SqlServer/SqlQueryPager.cs
Expand Up @@ -44,11 +44,12 @@ public IEnumerable<string> ApplyPaging(string sql, string[] keys, int skip, int
builder.AppendFormat(string.Join(" AND ", keys.Select(MakeDataJoin)));
else
builder.AppendFormat(MakeDataJoin(keys[0]));
var groupBy = ExtractGroupBy(ref fromEtc);
var rest = Regex.Replace(fromEtc, @"^from (\[.*?\]\.\[.*?\])", @"");
builder.Append(rest);

builder.AppendFormat(" AND [_#_] BETWEEN {0} AND {1}", skip + 1, skip + take);

builder.AppendFormat(" AND [_#_] BETWEEN {0} AND {1} ", skip + 1, skip + take);
builder.Append(groupBy);
yield return builder.ToString();
}

Expand Down Expand Up @@ -79,5 +80,17 @@ private static string ExtractOrderBy(string columns, string[] keys, ref string f
}
return orderBy;
}

private static string ExtractGroupBy(ref string fromEtc)
{
string groupBy = string.Empty;
int index = fromEtc.IndexOf("GROUP BY", StringComparison.InvariantCultureIgnoreCase);
if (index > -1)
{
groupBy = fromEtc.Substring(index).Trim();
fromEtc = fromEtc.Remove(index).Trim();
}
return groupBy;
}
}
}

0 comments on commit 8242a66

Please sign in to comment.