From ef1a8eda4b082a37b2384d2bb1d54a2d2ce650a7 Mon Sep 17 00:00:00 2001 From: Trevor Pilley Date: Tue, 8 Dec 2015 19:13:28 +0000 Subject: [PATCH] Closes #432 --- .../Dialect/MsSql2005DialectTests.cs | 26 +++++++++---------- .../Mapping/ExpandoObjectInfoTests.cs | 25 ++++++++++++++++++ MicroLite/Dialect/MsSql2005Dialect.cs | 2 +- MicroLite/Mapping/ExpandoObjectInfo.cs | 8 ++++-- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/MicroLite.Tests/Dialect/MsSql2005DialectTests.cs b/MicroLite.Tests/Dialect/MsSql2005DialectTests.cs index 5315353d..99f49ba9 100644 --- a/MicroLite.Tests/Dialect/MsSql2005DialectTests.cs +++ b/MicroLite.Tests/Dialect/MsSql2005DialectTests.cs @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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]); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/MicroLite.Tests/Mapping/ExpandoObjectInfoTests.cs b/MicroLite.Tests/Mapping/ExpandoObjectInfoTests.cs index 5f5b513b..7838f6ab 100644 --- a/MicroLite.Tests/Mapping/ExpandoObjectInfoTests.cs +++ b/MicroLite.Tests/Mapping/ExpandoObjectInfoTests.cs @@ -3,6 +3,7 @@ #if !NET_3_5 using System; + using System.Collections.Generic; using System.Data; using System.Dynamic; using MicroLite.Mapping; @@ -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(); + mockReader.Setup(x => x.FieldCount).Returns(3); + mockReader.Setup(x => x.IsDBNull(It.IsAny())).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)instance; + + Assert.False(dictionary.ContainsKey("MicroLiteRowNumber")); + } + [Fact] public void CreateInstanceSetsPropertyValueToNullIfPassedDBNull() { diff --git a/MicroLite/Dialect/MsSql2005Dialect.cs b/MicroLite/Dialect/MsSql2005Dialect.cs index 4997c1e6..f7a171b7 100644 --- a/MicroLite/Dialect/MsSql2005Dialect.cs +++ b/MicroLite/Dialect/MsSql2005Dialect.cs @@ -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); diff --git a/MicroLite/Mapping/ExpandoObjectInfo.cs b/MicroLite/Mapping/ExpandoObjectInfo.cs index 1e46e3ba..1514af72 100644 --- a/MicroLite/Mapping/ExpandoObjectInfo.cs +++ b/MicroLite/Mapping/ExpandoObjectInfo.cs @@ -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;