Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
Closes #432
Browse files Browse the repository at this point in the history
  • Loading branch information
TrevorPilley committed Dec 8, 2015
1 parent 4a40b75 commit ef1a8ed
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
26 changes: 13 additions & 13 deletions MicroLite.Tests/Dialect/MsSql2005DialectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void PageNonQualifiedQuery()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT CustomerId, Name, DoB, StatusId,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM Customers) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT CustomerId, Name, DoB, StatusId,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM Customers) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);

Assert.Equal(DbType.Int32, paged.Arguments[0].DbType);
Assert.Equal(1, paged.Arguments[0].Value);
Expand All @@ -159,7 +159,7 @@ public void PageNonQualifiedWildcardQuery()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM Customers) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM Customers) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);

Assert.Equal(DbType.Int32, paged.Arguments[0].DbType);
Assert.Equal(1, paged.Arguments[0].Value);
Expand Down Expand Up @@ -196,7 +196,7 @@ public void PageQueryWithoutSubQuery()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 2, resultsPerPage: 10));

Assert.Equal("SELECT * FROM (SELECT [Created],[CreditLimit],[DateOfBirth],[Id],[Name],[CustomerStatusId],[Updated],[Website],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM [Customers] WHERE (Name LIKE @p0)) AS [MicroLitePagedResults] WHERE (RowNumber >= @p1 AND RowNumber <= @p2)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [Created],[CreditLimit],[DateOfBirth],[Id],[Name],[CustomerStatusId],[Updated],[Website],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM [Customers] WHERE (Name LIKE @p0)) AS [MicroLitePagedResults] WHERE (RowNumber >= @p1 AND RowNumber <= @p2)", paged.CommandText);

Assert.Equal(DbType.String, paged.Arguments[0].DbType);
Assert.Equal("Fred%", paged.Arguments[0].Value);
Expand Down Expand Up @@ -227,7 +227,7 @@ public void PageQueryWithSubQuery()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 2, resultsPerPage: 10));

Assert.Equal("SELECT * FROM (SELECT [Created],[CreditLimit],[DateOfBirth],[Id],[Name],[CustomerStatusId],[Updated],[Website],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM [Customers] WHERE (Name LIKE @p0) AND ([SourceId] IN (SELECT SourceId FROM Source WHERE Status = @p1))) AS [MicroLitePagedResults] WHERE (RowNumber >= @p2 AND RowNumber <= @p3)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [Created],[CreditLimit],[DateOfBirth],[Id],[Name],[CustomerStatusId],[Updated],[Website],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM [Customers] WHERE (Name LIKE @p0) AND ([SourceId] IN (SELECT SourceId FROM Source WHERE Status = @p1))) AS [MicroLitePagedResults] WHERE (RowNumber >= @p2 AND RowNumber <= @p3)", paged.CommandText);

Assert.Equal(DbType.String, paged.Arguments[0].DbType);
Assert.Equal("Fred%", paged.Arguments[0].Value);
Expand All @@ -254,7 +254,7 @@ public void PageWithJoin()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Invoices].[InvoiceId],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM [Customers] INNER JOIN [Invoices] ON [Invoices].[CustomerId] = [Customers].[CustomerId]) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Invoices].[InvoiceId],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM [Customers] INNER JOIN [Invoices] ON [Invoices].[CustomerId] = [Customers].[CustomerId]) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);

Assert.Equal(DbType.Int32, paged.Arguments[0].DbType);
Assert.Equal(1, paged.Arguments[0].Value);
Expand All @@ -275,7 +275,7 @@ public void PageWithJoinWithAlias()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId] AS CustId, [Invoices].[InvoiceId],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM [Customers] INNER JOIN [Invoices] ON [Invoices].[CustomerId] = [Customers].[CustomerId]) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId] AS CustId, [Invoices].[InvoiceId],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM [Customers] INNER JOIN [Invoices] ON [Invoices].[CustomerId] = [Customers].[CustomerId]) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);

Assert.Equal(DbType.Int32, paged.Arguments[0].DbType);
Assert.Equal(1, paged.Arguments[0].Value);
Expand Down Expand Up @@ -304,7 +304,7 @@ ORDER BY

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY [Customers].[Name] ASC, [Customers].[DoB] ASC) AS RowNumber FROM [Sales].[Customers] WHERE ([Customers].[StatusId] = @p0 AND [Customers].[DoB] > @p1)) AS [MicroLitePagedResults] WHERE (RowNumber >= @p2 AND RowNumber <= @p3)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY [Customers].[Name] ASC, [Customers].[DoB] ASC) AS MicroLiteRowNumber FROM [Sales].[Customers] WHERE ([Customers].[StatusId] = @p0 AND [Customers].[DoB] > @p1)) AS [MicroLitePagedResults] WHERE (RowNumber >= @p2 AND RowNumber <= @p3)", paged.CommandText);

Assert.Equal(sqlQuery.Arguments[0], paged.Arguments[0]);
Assert.Equal(sqlQuery.Arguments[1], paged.Arguments[1]);
Expand All @@ -325,7 +325,7 @@ public void PageWithNoWhereButOrderBy()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT [CustomerId], [Name], [DoB], [StatusId],ROW_NUMBER() OVER(ORDER BY [CustomerId] ASC) AS RowNumber FROM [dbo].[Customers]) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [CustomerId], [Name], [DoB], [StatusId],ROW_NUMBER() OVER(ORDER BY [CustomerId] ASC) AS MicroLiteRowNumber FROM [dbo].[Customers]) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);

Assert.Equal(DbType.Int32, paged.Arguments[0].DbType);
Assert.Equal(1, paged.Arguments[0].Value);
Expand All @@ -343,7 +343,7 @@ public void PageWithNoWhereOrOrderByFirstResultsPage()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM [Sales].[Customers]) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM [Sales].[Customers]) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);

Assert.Equal(DbType.Int32, paged.Arguments[0].DbType);
Assert.Equal(1, paged.Arguments[0].Value);
Expand All @@ -361,7 +361,7 @@ public void PageWithNoWhereOrOrderBySecondResultsPage()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 2, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM [Sales].[Customers]) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM [Sales].[Customers]) AS [MicroLitePagedResults] WHERE (RowNumber >= @p0 AND RowNumber <= @p1)", paged.CommandText);

Assert.Equal(DbType.Int32, paged.Arguments[0].DbType);
Assert.Equal(26, paged.Arguments[0].Value);
Expand All @@ -379,7 +379,7 @@ public void PageWithWhereAndOrderBy()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY [Customers].[Name] ASC) AS RowNumber FROM [Sales].[Customers] WHERE [Customers].[StatusId] = @p0) AS [MicroLitePagedResults] WHERE (RowNumber >= @p1 AND RowNumber <= @p2)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY [Customers].[Name] ASC) AS MicroLiteRowNumber FROM [Sales].[Customers] WHERE [Customers].[StatusId] = @p0) AS [MicroLitePagedResults] WHERE (RowNumber >= @p1 AND RowNumber <= @p2)", paged.CommandText);
Assert.Equal(sqlQuery.Arguments[0], paged.Arguments[0]);

Assert.Equal(DbType.Int32, paged.Arguments[1].DbType);
Expand Down Expand Up @@ -408,7 +408,7 @@ ORDER BY

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY [Customers].[Name] ASC) AS RowNumber FROM [Sales].[Customers] WHERE [Customers].[StatusId] = @p0) AS [MicroLitePagedResults] WHERE (RowNumber >= @p1 AND RowNumber <= @p2)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY [Customers].[Name] ASC) AS MicroLiteRowNumber FROM [Sales].[Customers] WHERE [Customers].[StatusId] = @p0) AS [MicroLitePagedResults] WHERE (RowNumber >= @p1 AND RowNumber <= @p2)", paged.CommandText);
Assert.Equal(sqlQuery.Arguments[0], paged.Arguments[0]);

Assert.Equal(DbType.Int32, paged.Arguments[1].DbType);
Expand All @@ -427,7 +427,7 @@ public void PageWithWhereButNoOrderBy()

var paged = sqlDialect.PageQuery(sqlQuery, PagingOptions.ForPage(page: 1, resultsPerPage: 25));

Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS RowNumber FROM [Sales].[Customers] WHERE [Customers].[StatusId] = @p0) AS [MicroLitePagedResults] WHERE (RowNumber >= @p1 AND RowNumber <= @p2)", paged.CommandText);
Assert.Equal("SELECT * FROM (SELECT [Customers].[CustomerId], [Customers].[Name], [Customers].[DoB], [Customers].[StatusId],ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS MicroLiteRowNumber FROM [Sales].[Customers] WHERE [Customers].[StatusId] = @p0) AS [MicroLitePagedResults] WHERE (RowNumber >= @p1 AND RowNumber <= @p2)", paged.CommandText);
Assert.Equal(sqlQuery.Arguments[0], paged.Arguments[0]);

Assert.Equal(DbType.Int32, paged.Arguments[1].DbType);
Expand Down
25 changes: 25 additions & 0 deletions MicroLite.Tests/Mapping/ExpandoObjectInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if !NET_3_5

using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using MicroLite.Mapping;
Expand Down Expand Up @@ -34,6 +35,30 @@ public void CreateInstance()
Assert.Equal("Fred Flintstone", instance.Name);
}

[Fact]
public void CreateInstanceIgnoresMicroLiteRowNumber()
{
var objectInfo = new ExpandoObjectInfo();

var mockReader = new Mock<IDataReader>();
mockReader.Setup(x => x.FieldCount).Returns(3);
mockReader.Setup(x => x.IsDBNull(It.IsAny<int>())).Returns(false);

mockReader.Setup(x => x.GetName(0)).Returns("Id");
mockReader.Setup(x => x.GetName(1)).Returns("Name");
mockReader.Setup(x => x.GetName(2)).Returns("MicroLiteRowNumber");

mockReader.Setup(x => x.GetValue(0)).Returns(12345);
mockReader.Setup(x => x.GetValue(1)).Returns("Fred Flintstone");
mockReader.Setup(x => x.GetValue(2)).Returns(1);

var instance = (dynamic)objectInfo.CreateInstance(mockReader.Object);

var dictionary = (IDictionary<string, object>)instance;

Assert.False(dictionary.ContainsKey("MicroLiteRowNumber"));
}

[Fact]
public void CreateInstanceSetsPropertyValueToNullIfPassedDBNull()
{
Expand Down
2 changes: 1 addition & 1 deletion MicroLite/Dialect/MsSql2005Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public override SqlQuery PageQuery(SqlQuery sqlQuery, PagingOptions pagingOption
var orderByClause = !string.IsNullOrEmpty(sqlString.OrderBy) ? sqlString.OrderBy : "(SELECT NULL)";

var stringBuilder = new StringBuilder(sqlQuery.CommandText.Length * 2)
.AppendFormat(CultureInfo.InvariantCulture, "SELECT * FROM (SELECT {0},ROW_NUMBER() OVER(ORDER BY {1}) AS RowNumber FROM {2}{3}) AS [MicroLitePagedResults]", sqlString.Select, orderByClause, sqlString.From, whereClause)
.AppendFormat(CultureInfo.InvariantCulture, "SELECT * FROM (SELECT {0},ROW_NUMBER() OVER(ORDER BY {1}) AS MicroLiteRowNumber FROM {2}{3}) AS [MicroLitePagedResults]", sqlString.Select, orderByClause, sqlString.From, whereClause)
.AppendFormat(CultureInfo.InvariantCulture, " WHERE (RowNumber >= {0} AND RowNumber <= {1})", this.SqlCharacters.GetParameterName(arguments.Length - 2), this.SqlCharacters.GetParameterName(arguments.Length - 1));

return new SqlQuery(stringBuilder.ToString(), arguments);
Expand Down
8 changes: 6 additions & 2 deletions MicroLite/Mapping/ExpandoObjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ public object CreateInstance(IDataReader reader)
for (int i = 0; i < reader.FieldCount; i++)
{
var columnName = reader.GetName(i);
var value = reader.IsDBNull(i) ? null : reader.GetValue(i);

dictionary[columnName] = value;
if (!"MicroLiteRowNumber".Equals(columnName))
{
var value = reader.IsDBNull(i) ? null : reader.GetValue(i);

dictionary[columnName] = value;
}
}

return instance;
Expand Down

0 comments on commit ef1a8ed

Please sign in to comment.