IGNITE-4413 .NET: Fix DateTime argument handling in SqlQuery This closes #1341
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/83710a9d Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/83710a9d Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/83710a9d Branch: refs/heads/master Commit: 83710a9d1bb7379e5f3d891ed95c86096263740b Parents: 56efb10 Author: Pavel Tupitsyn <[email protected]> Authored: Mon Dec 12 17:52:22 2016 +0300 Committer: Pavel Tupitsyn <[email protected]> Committed: Mon Dec 12 18:24:23 2016 +0300 ---------------------------------------------------------------------- .../Query/CacheQueriesCodeConfigurationTest.cs | 17 ++++++++++-- .../Cache/Query/CacheQueriesTest.cs | 8 ++++++ .../Apache.Ignite.Core/Cache/Query/QueryBase.cs | 15 ++++++++-- .../Apache.Ignite.Core/Impl/Cache/CacheImpl.cs | 29 +------------------- 4 files changed, 36 insertions(+), 33 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/83710a9d/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesCodeConfigurationTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesCodeConfigurationTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesCodeConfigurationTest.cs index 7cb999f..863e14f 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesCodeConfigurationTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesCodeConfigurationTest.cs @@ -52,7 +52,8 @@ namespace Apache.Ignite.Core.Tests.Cache.Query Fields = new[] { new QueryField("Name", typeof (string)), - new QueryField("Age", typeof (int)) + new QueryField("Age", typeof (int)), + new QueryField("Birthday", typeof(DateTime)), }, Indexes = new[] { @@ -71,7 +72,8 @@ namespace Apache.Ignite.Core.Tests.Cache.Query cache[1] = new QueryPerson("Arnold", 10); cache[2] = new QueryPerson("John", 20); - using (var cursor = cache.Query(new SqlQuery(typeof (QueryPerson), "age > 10"))) + using (var cursor = cache.Query(new SqlQuery(typeof (QueryPerson), "age > ? and birthday < ?", + 10, DateTime.UtcNow))) { Assert.AreEqual(2, cursor.GetAll().Single().Key); } @@ -145,7 +147,9 @@ namespace Apache.Ignite.Core.Tests.Cache.Query cache[2] = new AttributeQueryPerson("John", 20); - using (var cursor = cache.Query(new SqlQuery(typeof(AttributeQueryPerson), "age > ?", 10))) + using (var cursor = cache.Query(new SqlQuery(typeof(AttributeQueryPerson), + "age > ? and age < ? and birthday > ? and birthday < ?", 10, 30, + DateTime.UtcNow.AddYears(-21), DateTime.UtcNow.AddYears(-19)))) { Assert.AreEqual(2, cursor.GetAll().Single().Key); } @@ -192,6 +196,7 @@ namespace Apache.Ignite.Core.Tests.Cache.Query Name = name; Age = age; Salary = age; + Birthday = DateTime.UtcNow.AddYears(-age); } /// <summary> @@ -226,6 +231,12 @@ namespace Apache.Ignite.Core.Tests.Cache.Query /// </summary> [QuerySqlField] public decimal? Salary { get; set; } + + /// <summary> + /// Gets or sets the birthday. + /// </summary> + [QuerySqlField] + public DateTime Birthday { get; set; } } /// <summary> http://git-wip-us.apache.org/repos/asf/ignite/blob/83710a9d/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesTest.cs index 49f87c6..20fd93a 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Query/CacheQueriesTest.cs @@ -15,6 +15,7 @@ * limitations under the License. */ +// ReSharper disable UnusedAutoPropertyAccessor.Global namespace Apache.Ignite.Core.Tests.Cache.Query { using System; @@ -797,6 +798,7 @@ namespace Apache.Ignite.Core.Tests.Cache.Query { Name = name; Age = age; + Birthday = DateTime.UtcNow.AddYears(-age); } /// <summary> @@ -808,6 +810,12 @@ namespace Apache.Ignite.Core.Tests.Cache.Query /// Age. /// </summary> public int Age { get; set; } + + /// <summary> + /// Gets or sets the birthday. + /// </summary> + [QuerySqlField] // Enforce Timestamp serialization + public DateTime Birthday { get; set; } } /// <summary> http://git-wip-us.apache.org/repos/asf/ignite/blob/83710a9d/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Query/QueryBase.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Query/QueryBase.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Query/QueryBase.cs index cf1f637..d992845 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Query/QueryBase.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Query/QueryBase.cs @@ -17,6 +17,8 @@ namespace Apache.Ignite.Core.Cache.Query { + using System; + using Apache.Ignite.Core.Binary; using Apache.Ignite.Core.Impl.Binary; using Apache.Ignite.Core.Impl.Cache; @@ -66,7 +68,7 @@ namespace Apache.Ignite.Core.Cache.Query /// </summary> /// <param name="writer">Writer.</param> /// <param name="args">Arguments.</param> - internal static void WriteQueryArgs(BinaryWriter writer, object[] args) + internal static void WriteQueryArgs(IBinaryRawWriter writer, object[] args) { if (args == null) writer.WriteInt(0); @@ -75,7 +77,16 @@ namespace Apache.Ignite.Core.Cache.Query writer.WriteInt(args.Length); foreach (var arg in args) - writer.WriteObject(arg); + { + // Write DateTime as TimeStamp always, otherwise it does not make sense + // Wrapped DateTime comparison does not work in SQL + var dt = arg as DateTime?; // Works with DateTime also + + if (dt != null) + writer.WriteTimestamp(dt); + else + writer.WriteObject(arg); + } } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/83710a9d/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/CacheImpl.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/CacheImpl.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/CacheImpl.cs index e0d1a3c..186737c 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/CacheImpl.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Cache/CacheImpl.cs @@ -980,7 +980,7 @@ namespace Apache.Ignite.Core.Impl.Cache writer.WriteString(qry.Sql); writer.WriteInt(qry.PageSize); - WriteQueryArgs(writer, qry.Arguments); + QueryBase.WriteQueryArgs(writer, qry.Arguments); writer.WriteBoolean(qry.EnableDistributedJoins); writer.WriteBoolean(qry.EnforceJoinOrder); @@ -999,33 +999,6 @@ namespace Apache.Ignite.Core.Impl.Cache return new QueryCursor<TK, TV>(cursor, Marshaller, _flagKeepBinary); } - /// <summary> - /// Write query arguments. - /// </summary> - /// <param name="writer">Writer.</param> - /// <param name="args">Arguments.</param> - private static void WriteQueryArgs(BinaryWriter writer, object[] args) - { - if (args == null) - writer.WriteInt(0); - else - { - writer.WriteInt(args.Length); - - foreach (var arg in args) - { - // Write DateTime as TimeStamp always, otherwise it does not make sense - // Wrapped DateTime comparison does not work in SQL - var dt = arg as DateTime?; // Works with DateTime also - - if (dt != null) - writer.WriteTimestamp(dt); - else - writer.WriteObject(arg); - } - } - } - /** <inheritdoc /> */ public IContinuousQueryHandle QueryContinuous(ContinuousQuery<TK, TV> qry) {
