From 72b2cbf842aed971fa6cf00a1eaeb43eda66226a Mon Sep 17 00:00:00 2001 From: ErikWitkowski Date: Mon, 9 May 2016 17:51:33 -0300 Subject: [PATCH 1/3] DateTime.MinValue should remain unchanged despite time difference. --- src/SQLite.Net/SQLiteCommand.cs | 197 +++++++++++++++++--------------- tests/DateTimeTest.cs | 5 + 2 files changed, 108 insertions(+), 94 deletions(-) diff --git a/src/SQLite.Net/SQLiteCommand.cs b/src/SQLite.Net/SQLiteCommand.cs index 9db0d705b..2aeb1b335 100755 --- a/src/SQLite.Net/SQLiteCommand.cs +++ b/src/SQLite.Net/SQLiteCommand.cs @@ -35,7 +35,8 @@ public class SQLiteCommand { private static readonly IntPtr NegativePointer = new IntPtr(-1); - [NotNull] private readonly List _bindings; + [NotNull] + private readonly List _bindings; private readonly SQLiteConnection _conn; private readonly ISQLitePlatform _sqlitePlatform; @@ -83,13 +84,13 @@ public int ExecuteNonQuery() [PublicAPI] public IEnumerable ExecuteDeferredQuery() { - return ExecuteDeferredQuery(_conn.GetMapping(typeof (T))); + return ExecuteDeferredQuery(_conn.GetMapping(typeof(T))); } [PublicAPI] public List ExecuteQuery() { - return ExecuteDeferredQuery(_conn.GetMapping(typeof (T))).ToList(); + return ExecuteDeferredQuery(_conn.GetMapping(typeof(T))).ToList(); } [PublicAPI] @@ -145,7 +146,7 @@ public IEnumerable ExecuteDeferredQuery(TableMapping map) cols[i].SetValue(obj, val); } OnInstanceCreated(obj); - yield return (T) obj; + yield return (T)obj; } } finally @@ -170,10 +171,10 @@ public T ExecuteScalar() if (r == Result.Row) { var colType = _sqlitePlatform.SQLiteApi.ColumnType(stmt, 0); - var clrType = Nullable.GetUnderlyingType(typeof (T)) ?? typeof (T); + var clrType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); if (colType != ColType.Null) { - val = (T) ReadCol(stmt, 0, colType, clrType); + val = (T)ReadCol(stmt, 0, colType, clrType); } } else if (r == Result.Done) @@ -263,19 +264,19 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in { if (value is int) { - isqLite3Api.BindInt(stmt, index, (int) value); + isqLite3Api.BindInt(stmt, index, (int)value); } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, ((ISerializable) value).Serialize()); + isqLite3Api.BindInt(stmt, index, ((ISerializable)value).Serialize()); } else if (value is string) { - isqLite3Api.BindText16(stmt, index, (string) value, -1, NegativePointer); + isqLite3Api.BindText16(stmt, index, (string)value, -1, NegativePointer); } else if (value is ISerializable) { - isqLite3Api.BindText16(stmt, index, ((ISerializable) value).Serialize(), -1, NegativePointer); + isqLite3Api.BindText16(stmt, index, ((ISerializable)value).Serialize(), -1, NegativePointer); } else if (value is byte || value is ushort || value is sbyte || value is short) { @@ -283,27 +284,27 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable) value).Serialize())); + isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable)value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable) value).Serialize())); + isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable)value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable) value).Serialize())); + isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable)value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable) value).Serialize())); + isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable)value).Serialize())); } else if (value is bool) { - isqLite3Api.BindInt(stmt, index, (bool) value ? 1 : 0); + isqLite3Api.BindInt(stmt, index, (bool)value ? 1 : 0); } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, ((ISerializable) value).Serialize() ? 1 : 0); + isqLite3Api.BindInt(stmt, index, ((ISerializable)value).Serialize() ? 1 : 0); } else if (value is uint || value is long) { @@ -311,11 +312,11 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in } else if (value is ISerializable) { - isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable) value).Serialize())); + isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable)value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable) value).Serialize())); + isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable)value).Serialize())); } else if (value is float || value is double || value is decimal) { @@ -323,51 +324,59 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in } else if (value is ISerializable) { - isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable) value).Serialize())); + isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable)value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable) value).Serialize())); + isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable)value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable) value).Serialize())); + isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable)value).Serialize())); } else if (value is TimeSpan) { - isqLite3Api.BindInt64(stmt, index, ((TimeSpan) value).Ticks); + isqLite3Api.BindInt64(stmt, index, ((TimeSpan)value).Ticks); } else if (value is ISerializable) { - isqLite3Api.BindInt64(stmt, index, ((ISerializable) value).Serialize().Ticks); + isqLite3Api.BindInt64(stmt, index, ((ISerializable)value).Serialize().Ticks); } else if (value is DateTime) { if (storeDateTimeAsTicks) { - long ticks = ((DateTime) value).ToUniversalTime().Ticks; + DateTime date = (DateTime)value; + if (date != DateTime.MinValue) + date = date.ToUniversalTime(); + + long ticks = date.Ticks; isqLite3Api.BindInt64(stmt, index, ticks); } else { - string val = ((DateTime) value).ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture); + DateTime date = (DateTime)value; + if (date != DateTime.MinValue) + date = date.ToUniversalTime(); + + string val = date.ToString(DateTimeFormat, CultureInfo.InvariantCulture); isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer); } } else if (value is DateTimeOffset) { - isqLite3Api.BindInt64(stmt, index, ((DateTimeOffset) value).UtcTicks); + isqLite3Api.BindInt64(stmt, index, ((DateTimeOffset)value).UtcTicks); } else if (value is ISerializable) { if (storeDateTimeAsTicks) { - long ticks = ((ISerializable) value).Serialize().ToUniversalTime().Ticks; + long ticks = ((ISerializable)value).Serialize().ToUniversalTime().Ticks; isqLite3Api.BindInt64(stmt, index, ticks); } else { - string val = ((ISerializable) value).Serialize().ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture); + string val = ((ISerializable)value).Serialize().ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture); isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer); } } @@ -377,20 +386,20 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in } else if (value is byte[]) { - isqLite3Api.BindBlob(stmt, index, (byte[]) value, ((byte[]) value).Length, NegativePointer); + isqLite3Api.BindBlob(stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer); } else if (value is ISerializable) { - isqLite3Api.BindBlob(stmt, index, ((ISerializable) value).Serialize(), ((ISerializable) value).Serialize().Length, + isqLite3Api.BindBlob(stmt, index, ((ISerializable)value).Serialize(), ((ISerializable)value).Serialize().Length, NegativePointer); } else if (value is Guid) { - isqLite3Api.BindText16(stmt, index, ((Guid) value).ToString(), 72, NegativePointer); + isqLite3Api.BindText16(stmt, index, ((Guid)value).ToString(), 72, NegativePointer); } else if (value is ISerializable) { - isqLite3Api.BindText16(stmt, index, ((ISerializable) value).Serialize().ToString(), 72, NegativePointer); + isqLite3Api.BindText16(stmt, index, ((ISerializable)value).Serialize().ToString(), 72, NegativePointer); } else if (serializer != null && serializer.CanDeserialize(value.GetType())) { @@ -413,61 +422,61 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType) { return null; } - if (clrType == typeof (string)) + if (clrType == typeof(string)) { return _sqlitePlatform.SQLiteApi.ColumnText16(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnText16(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (int)) + if (clrType == typeof(int)) { return _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (bool)) + if (clrType == typeof(bool)) { return _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index) == 1; } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index) == 1; - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (double)) + if (clrType == typeof(double)) { return _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (float)) + if (clrType == typeof(float)) { - return (float) _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); + return (float)_sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { - var value = (float) _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + var value = (float)_sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (TimeSpan)) + if (clrType == typeof(TimeSpan)) { return new TimeSpan(_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index)); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { var value = new TimeSpan(_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index)); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (DateTime)) + if (clrType == typeof(DateTime)) { if (_conn.StoreDateTimeAsTicks) { @@ -475,11 +484,11 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType) } return DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index), CultureInfo.InvariantCulture); } - if (clrType == typeof (DateTimeOffset)) + if (clrType == typeof(DateTimeOffset)) { return new DateTimeOffset(_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index), TimeSpan.Zero); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { DateTime value; if (_conn.StoreDateTimeAsTicks) @@ -496,86 +505,86 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType) { return _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (clrType == typeof (long)) + if (clrType == typeof(long)) { return _sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (uint)) + if (clrType == typeof(uint)) { - return (uint) _sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); + return (uint)_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { - var value = (uint) _sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + var value = (uint)_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (decimal)) + if (clrType == typeof(decimal)) { - return (decimal) _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); + return (decimal)_sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { - var value = (decimal) _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + var value = (decimal)_sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (byte)) + if (clrType == typeof(byte)) { - return (byte) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return (byte)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { - var value = (byte) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + var value = (byte)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (ushort)) + if (clrType == typeof(ushort)) { - return (ushort) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return (ushort)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { - var value = (ushort) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + var value = (ushort)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (short)) + if (clrType == typeof(short)) { - return (short) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return (short)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { - var value = (short) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + var value = (short)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (sbyte)) + if (clrType == typeof(sbyte)) { - return (sbyte) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return (sbyte)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { - var value = (sbyte) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + var value = (sbyte)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (byte[])) + if (clrType == typeof(byte[])) { return _sqlitePlatform.SQLiteApi.ColumnByteArray(stmt, index); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnByteArray(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } - if (clrType == typeof (Guid)) + if (clrType == typeof(Guid)) { return new Guid(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index)); } - if (interfaces.Contains(typeof (ISerializable))) + if (interfaces.Contains(typeof(ISerializable))) { var value = new Guid(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index)); - return _conn.Resolver.CreateObject(clrType, new object[] {value}); + return _conn.Resolver.CreateObject(clrType, new object[] { value }); } if (_conn.Serializer != null && _conn.Serializer.CanDeserialize(clrType)) { diff --git a/tests/DateTimeTest.cs b/tests/DateTimeTest.cs index 5170f8bbc..ba5122562 100644 --- a/tests/DateTimeTest.cs +++ b/tests/DateTimeTest.cs @@ -17,6 +17,7 @@ private class TestObj public string Name { get; set; } public DateTime Time1 { get; set; } public DateTime Time2 { get; set; } + public DateTime Time3 { get; set; } } @@ -28,6 +29,7 @@ private async Task TestAsyncDateTime(SQLiteAsyncConnection db, bool storeDateTim { Time1 = DateTime.UtcNow, Time2 = DateTime.Now, + Time3 = DateTime.MinValue }; await db.InsertAsync(org); var fromDb = await db.GetAsync(org.Id); @@ -36,6 +38,8 @@ private async Task TestAsyncDateTime(SQLiteAsyncConnection db, bool storeDateTim Assert.AreEqual(fromDb.Time1.ToLocalTime(), org.Time1.ToLocalTime()); Assert.AreEqual(fromDb.Time2.ToLocalTime(), org.Time2.ToLocalTime()); + + Assert.AreEqual(fromDb.Time3, org.Time3); } private void TestDateTime(TestDb db) @@ -49,6 +53,7 @@ private void TestDateTime(TestDb db) { Time1 = DateTime.UtcNow, Time2 = DateTime.Now, + Time3 = DateTime.MinValue }; db.Insert(org); var fromDb = db.Get(org.Id); From 82c827b6e030214df01cb367b0877de9159e608b Mon Sep 17 00:00:00 2001 From: ErikWitkowski Date: Mon, 9 May 2016 17:55:18 -0300 Subject: [PATCH 2/3] Revert "DateTime.MinValue should remain unchanged despite time difference." This reverts commit 72b2cbf842aed971fa6cf00a1eaeb43eda66226a. --- src/SQLite.Net/SQLiteCommand.cs | 197 +++++++++++++++----------------- tests/DateTimeTest.cs | 5 - 2 files changed, 94 insertions(+), 108 deletions(-) diff --git a/src/SQLite.Net/SQLiteCommand.cs b/src/SQLite.Net/SQLiteCommand.cs index 2aeb1b335..9db0d705b 100755 --- a/src/SQLite.Net/SQLiteCommand.cs +++ b/src/SQLite.Net/SQLiteCommand.cs @@ -35,8 +35,7 @@ public class SQLiteCommand { private static readonly IntPtr NegativePointer = new IntPtr(-1); - [NotNull] - private readonly List _bindings; + [NotNull] private readonly List _bindings; private readonly SQLiteConnection _conn; private readonly ISQLitePlatform _sqlitePlatform; @@ -84,13 +83,13 @@ public int ExecuteNonQuery() [PublicAPI] public IEnumerable ExecuteDeferredQuery() { - return ExecuteDeferredQuery(_conn.GetMapping(typeof(T))); + return ExecuteDeferredQuery(_conn.GetMapping(typeof (T))); } [PublicAPI] public List ExecuteQuery() { - return ExecuteDeferredQuery(_conn.GetMapping(typeof(T))).ToList(); + return ExecuteDeferredQuery(_conn.GetMapping(typeof (T))).ToList(); } [PublicAPI] @@ -146,7 +145,7 @@ public IEnumerable ExecuteDeferredQuery(TableMapping map) cols[i].SetValue(obj, val); } OnInstanceCreated(obj); - yield return (T)obj; + yield return (T) obj; } } finally @@ -171,10 +170,10 @@ public T ExecuteScalar() if (r == Result.Row) { var colType = _sqlitePlatform.SQLiteApi.ColumnType(stmt, 0); - var clrType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T); + var clrType = Nullable.GetUnderlyingType(typeof (T)) ?? typeof (T); if (colType != ColType.Null) { - val = (T)ReadCol(stmt, 0, colType, clrType); + val = (T) ReadCol(stmt, 0, colType, clrType); } } else if (r == Result.Done) @@ -264,19 +263,19 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in { if (value is int) { - isqLite3Api.BindInt(stmt, index, (int)value); + isqLite3Api.BindInt(stmt, index, (int) value); } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, ((ISerializable)value).Serialize()); + isqLite3Api.BindInt(stmt, index, ((ISerializable) value).Serialize()); } else if (value is string) { - isqLite3Api.BindText16(stmt, index, (string)value, -1, NegativePointer); + isqLite3Api.BindText16(stmt, index, (string) value, -1, NegativePointer); } else if (value is ISerializable) { - isqLite3Api.BindText16(stmt, index, ((ISerializable)value).Serialize(), -1, NegativePointer); + isqLite3Api.BindText16(stmt, index, ((ISerializable) value).Serialize(), -1, NegativePointer); } else if (value is byte || value is ushort || value is sbyte || value is short) { @@ -284,27 +283,27 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable)value).Serialize())); + isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable) value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable)value).Serialize())); + isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable) value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable)value).Serialize())); + isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable) value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable)value).Serialize())); + isqLite3Api.BindInt(stmt, index, Convert.ToInt32(((ISerializable) value).Serialize())); } else if (value is bool) { - isqLite3Api.BindInt(stmt, index, (bool)value ? 1 : 0); + isqLite3Api.BindInt(stmt, index, (bool) value ? 1 : 0); } else if (value is ISerializable) { - isqLite3Api.BindInt(stmt, index, ((ISerializable)value).Serialize() ? 1 : 0); + isqLite3Api.BindInt(stmt, index, ((ISerializable) value).Serialize() ? 1 : 0); } else if (value is uint || value is long) { @@ -312,11 +311,11 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in } else if (value is ISerializable) { - isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable)value).Serialize())); + isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable) value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable)value).Serialize())); + isqLite3Api.BindInt64(stmt, index, Convert.ToInt64(((ISerializable) value).Serialize())); } else if (value is float || value is double || value is decimal) { @@ -324,59 +323,51 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in } else if (value is ISerializable) { - isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable)value).Serialize())); + isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable) value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable)value).Serialize())); + isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable) value).Serialize())); } else if (value is ISerializable) { - isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable)value).Serialize())); + isqLite3Api.BindDouble(stmt, index, Convert.ToDouble(((ISerializable) value).Serialize())); } else if (value is TimeSpan) { - isqLite3Api.BindInt64(stmt, index, ((TimeSpan)value).Ticks); + isqLite3Api.BindInt64(stmt, index, ((TimeSpan) value).Ticks); } else if (value is ISerializable) { - isqLite3Api.BindInt64(stmt, index, ((ISerializable)value).Serialize().Ticks); + isqLite3Api.BindInt64(stmt, index, ((ISerializable) value).Serialize().Ticks); } else if (value is DateTime) { if (storeDateTimeAsTicks) { - DateTime date = (DateTime)value; - if (date != DateTime.MinValue) - date = date.ToUniversalTime(); - - long ticks = date.Ticks; + long ticks = ((DateTime) value).ToUniversalTime().Ticks; isqLite3Api.BindInt64(stmt, index, ticks); } else { - DateTime date = (DateTime)value; - if (date != DateTime.MinValue) - date = date.ToUniversalTime(); - - string val = date.ToString(DateTimeFormat, CultureInfo.InvariantCulture); + string val = ((DateTime) value).ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture); isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer); } } else if (value is DateTimeOffset) { - isqLite3Api.BindInt64(stmt, index, ((DateTimeOffset)value).UtcTicks); + isqLite3Api.BindInt64(stmt, index, ((DateTimeOffset) value).UtcTicks); } else if (value is ISerializable) { if (storeDateTimeAsTicks) { - long ticks = ((ISerializable)value).Serialize().ToUniversalTime().Ticks; + long ticks = ((ISerializable) value).Serialize().ToUniversalTime().Ticks; isqLite3Api.BindInt64(stmt, index, ticks); } else { - string val = ((ISerializable)value).Serialize().ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture); + string val = ((ISerializable) value).Serialize().ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture); isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer); } } @@ -386,20 +377,20 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in } else if (value is byte[]) { - isqLite3Api.BindBlob(stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer); + isqLite3Api.BindBlob(stmt, index, (byte[]) value, ((byte[]) value).Length, NegativePointer); } else if (value is ISerializable) { - isqLite3Api.BindBlob(stmt, index, ((ISerializable)value).Serialize(), ((ISerializable)value).Serialize().Length, + isqLite3Api.BindBlob(stmt, index, ((ISerializable) value).Serialize(), ((ISerializable) value).Serialize().Length, NegativePointer); } else if (value is Guid) { - isqLite3Api.BindText16(stmt, index, ((Guid)value).ToString(), 72, NegativePointer); + isqLite3Api.BindText16(stmt, index, ((Guid) value).ToString(), 72, NegativePointer); } else if (value is ISerializable) { - isqLite3Api.BindText16(stmt, index, ((ISerializable)value).Serialize().ToString(), 72, NegativePointer); + isqLite3Api.BindText16(stmt, index, ((ISerializable) value).Serialize().ToString(), 72, NegativePointer); } else if (serializer != null && serializer.CanDeserialize(value.GetType())) { @@ -422,61 +413,61 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType) { return null; } - if (clrType == typeof(string)) + if (clrType == typeof (string)) { return _sqlitePlatform.SQLiteApi.ColumnText16(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnText16(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(int)) + if (clrType == typeof (int)) { return _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(bool)) + if (clrType == typeof (bool)) { return _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index) == 1; } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index) == 1; - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(double)) + if (clrType == typeof (double)) { return _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(float)) + if (clrType == typeof (float)) { - return (float)_sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); + return (float) _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { - var value = (float)_sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + var value = (float) _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(TimeSpan)) + if (clrType == typeof (TimeSpan)) { return new TimeSpan(_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index)); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { var value = new TimeSpan(_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index)); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(DateTime)) + if (clrType == typeof (DateTime)) { if (_conn.StoreDateTimeAsTicks) { @@ -484,11 +475,11 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType) } return DateTime.Parse(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index), CultureInfo.InvariantCulture); } - if (clrType == typeof(DateTimeOffset)) + if (clrType == typeof (DateTimeOffset)) { return new DateTimeOffset(_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index), TimeSpan.Zero); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { DateTime value; if (_conn.StoreDateTimeAsTicks) @@ -505,86 +496,86 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType) { return _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (clrType == typeof(long)) + if (clrType == typeof (long)) { return _sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(uint)) + if (clrType == typeof (uint)) { - return (uint)_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); + return (uint) _sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { - var value = (uint)_sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + var value = (uint) _sqlitePlatform.SQLiteApi.ColumnInt64(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(decimal)) + if (clrType == typeof (decimal)) { - return (decimal)_sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); + return (decimal) _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { - var value = (decimal)_sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + var value = (decimal) _sqlitePlatform.SQLiteApi.ColumnDouble(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(byte)) + if (clrType == typeof (byte)) { - return (byte)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return (byte) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { - var value = (byte)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + var value = (byte) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(ushort)) + if (clrType == typeof (ushort)) { - return (ushort)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return (ushort) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { - var value = (ushort)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + var value = (ushort) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(short)) + if (clrType == typeof (short)) { - return (short)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return (short) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { - var value = (short)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + var value = (short) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(sbyte)) + if (clrType == typeof (sbyte)) { - return (sbyte)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return (sbyte) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { - var value = (sbyte)_sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + var value = (sbyte) _sqlitePlatform.SQLiteApi.ColumnInt(stmt, index); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(byte[])) + if (clrType == typeof (byte[])) { return _sqlitePlatform.SQLiteApi.ColumnByteArray(stmt, index); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { var value = _sqlitePlatform.SQLiteApi.ColumnByteArray(stmt, index); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (clrType == typeof(Guid)) + if (clrType == typeof (Guid)) { return new Guid(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index)); } - if (interfaces.Contains(typeof(ISerializable))) + if (interfaces.Contains(typeof (ISerializable))) { var value = new Guid(_sqlitePlatform.SQLiteApi.ColumnText16(stmt, index)); - return _conn.Resolver.CreateObject(clrType, new object[] { value }); + return _conn.Resolver.CreateObject(clrType, new object[] {value}); } if (_conn.Serializer != null && _conn.Serializer.CanDeserialize(clrType)) { diff --git a/tests/DateTimeTest.cs b/tests/DateTimeTest.cs index ba5122562..5170f8bbc 100644 --- a/tests/DateTimeTest.cs +++ b/tests/DateTimeTest.cs @@ -17,7 +17,6 @@ private class TestObj public string Name { get; set; } public DateTime Time1 { get; set; } public DateTime Time2 { get; set; } - public DateTime Time3 { get; set; } } @@ -29,7 +28,6 @@ private async Task TestAsyncDateTime(SQLiteAsyncConnection db, bool storeDateTim { Time1 = DateTime.UtcNow, Time2 = DateTime.Now, - Time3 = DateTime.MinValue }; await db.InsertAsync(org); var fromDb = await db.GetAsync(org.Id); @@ -38,8 +36,6 @@ private async Task TestAsyncDateTime(SQLiteAsyncConnection db, bool storeDateTim Assert.AreEqual(fromDb.Time1.ToLocalTime(), org.Time1.ToLocalTime()); Assert.AreEqual(fromDb.Time2.ToLocalTime(), org.Time2.ToLocalTime()); - - Assert.AreEqual(fromDb.Time3, org.Time3); } private void TestDateTime(TestDb db) @@ -53,7 +49,6 @@ private void TestDateTime(TestDb db) { Time1 = DateTime.UtcNow, Time2 = DateTime.Now, - Time3 = DateTime.MinValue }; db.Insert(org); var fromDb = db.Get(org.Id); From 0f94b56899028d9599e2c705f1de39be7c8ff162 Mon Sep 17 00:00:00 2001 From: ErikWitkowski Date: Mon, 9 May 2016 18:06:29 -0300 Subject: [PATCH 3/3] DateTime.MinValue should not be changed from local to utc. --- src/SQLite.Net/SQLiteCommand.cs | 12 ++++++++++-- tests/DateTimeTest.cs | 7 +++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/SQLite.Net/SQLiteCommand.cs b/src/SQLite.Net/SQLiteCommand.cs index 9db0d705b..e50a819fe 100755 --- a/src/SQLite.Net/SQLiteCommand.cs +++ b/src/SQLite.Net/SQLiteCommand.cs @@ -345,12 +345,20 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in { if (storeDateTimeAsTicks) { - long ticks = ((DateTime) value).ToUniversalTime().Ticks; + DateTime date = (DateTime)value; + if (date != DateTime.MinValue) + date = date.ToUniversalTime(); + + long ticks = date.Ticks; isqLite3Api.BindInt64(stmt, index, ticks); } else { - string val = ((DateTime) value).ToUniversalTime().ToString(DateTimeFormat, CultureInfo.InvariantCulture); + DateTime date = (DateTime)value; + if (date != DateTime.MinValue) + date = date.ToUniversalTime(); + + string val = date.ToString(DateTimeFormat, CultureInfo.InvariantCulture); isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer); } } diff --git a/tests/DateTimeTest.cs b/tests/DateTimeTest.cs index 5170f8bbc..7fd01360c 100644 --- a/tests/DateTimeTest.cs +++ b/tests/DateTimeTest.cs @@ -17,6 +17,7 @@ private class TestObj public string Name { get; set; } public DateTime Time1 { get; set; } public DateTime Time2 { get; set; } + public DateTime Time3 { get; set; } } @@ -28,6 +29,7 @@ private async Task TestAsyncDateTime(SQLiteAsyncConnection db, bool storeDateTim { Time1 = DateTime.UtcNow, Time2 = DateTime.Now, + Time3 = DateTime.MinValue }; await db.InsertAsync(org); var fromDb = await db.GetAsync(org.Id); @@ -36,6 +38,8 @@ private async Task TestAsyncDateTime(SQLiteAsyncConnection db, bool storeDateTim Assert.AreEqual(fromDb.Time1.ToLocalTime(), org.Time1.ToLocalTime()); Assert.AreEqual(fromDb.Time2.ToLocalTime(), org.Time2.ToLocalTime()); + + Assert.AreEqual(fromDb.Time3, org.Time3); } private void TestDateTime(TestDb db) @@ -49,6 +53,7 @@ private void TestDateTime(TestDb db) { Time1 = DateTime.UtcNow, Time2 = DateTime.Now, + Time3 = DateTime.MinValue }; db.Insert(org); var fromDb = db.Get(org.Id); @@ -57,6 +62,8 @@ private void TestDateTime(TestDb db) Assert.AreEqual(fromDb.Time1.ToLocalTime(), org.Time1.ToLocalTime()); Assert.AreEqual(fromDb.Time2.ToLocalTime(), org.Time2.ToLocalTime()); + + Assert.AreEqual(fromDb.Time3, org.Time3); } [Test]