Skip to content
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

fixes #308 #309

Merged
merged 1 commit into from Sep 25, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions Simple.Data.Ado/AdoAdapterQueryRunner.cs
Expand Up @@ -183,13 +183,14 @@ private void ApplyPaging(SimpleQuery query, List<ICommandBuilder> commandBuilder
else
{
var table = _adapter.GetSchema().FindTable(query.TableName);
if (table.PrimaryKey == null || table.PrimaryKey.Length == 0)
var keys = new string[0];
if (table.PrimaryKey != null && table.PrimaryKey.Length > 0)
{
throw new AdoAdapterException(string.Format("Cannot apply paging to table '{0}' with no primary key.", table.ActualName));
keys = table.PrimaryKey.AsEnumerable()
.Select(k => string.Format("{0}.{1}", table.QualifiedName, _adapter.GetSchema().QuoteObjectName(k)))
.ToArray();
}
var keys = table.PrimaryKey.AsEnumerable()
.Select(k => string.Format("{0}.{1}", table.QualifiedName, _adapter.GetSchema().QuoteObjectName(k)))
.ToArray();

int skip = skipClause == null ? 0 : skipClause.Count;
int take = takeClause == null ? maxInt : takeClause.Count;
commandTexts = queryPager.ApplyPaging(mainCommandBuilder.Text, keys, skip, take);
Expand Down
9 changes: 7 additions & 2 deletions Simple.Data.SqlServer/SqlQueryPager.cs
Expand Up @@ -20,8 +20,13 @@ public IEnumerable<string> ApplyLimit(string sql, int take)
yield return SelectMatch.Replace(sql, match => match.Value + " TOP " + take + " ");
}

public IEnumerable<string> ApplyPaging(string sql, string[] keys, int skip, int take)
{
public IEnumerable<string> ApplyPaging(string sql, string[] keys, int skip, int take)
{
if (keys == null || keys.Length == 0)
{
throw new AdoAdapterException("Cannot apply paging to table with no primary key.");
}

sql = sql.Replace(Environment.NewLine, " ");
var builder = new StringBuilder("WITH __Data AS (SELECT ");

Expand Down
11 changes: 11 additions & 0 deletions Simple.Data.SqlTest/SqlQueryPagerTest.cs
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Simple.Data.Ado;
using Simple.Data.SqlServer;

namespace Simple.Data.SqlTest
Expand Down Expand Up @@ -112,5 +113,15 @@ public void ShouldExcludeLeftJoinedTablesFromSubSelect()
var modified = Normalize.Replace(pagedSql, " ").ToLowerInvariant();
Assert.AreEqual(expected, modified);
}

[Test]
public void ShouldThrowIfTableHasNoPrimaryKey([Values(null, new string[0])]string[] keys)
{
var sql = "select [dbo].[d].[a] from [dbo].[b]";

Assert.Throws<AdoAdapterException>(
() => new SqlQueryPager().ApplyPaging(sql, keys, 5, 10).ToList()
);
}
}
}