From 303dbe5f88934f3ba5f6601037b8d73883c9cab9 Mon Sep 17 00:00:00 2001 From: Vladimir Bodurov Date: Sat, 11 Apr 2020 15:31:16 -0700 Subject: [PATCH 1/3] datetimeoffset as 10 bytes --- src/SQLite.cs | 25 ++++++++++++++++++++----- tests/DateTimeOffsetTest.cs | 6 +++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/SQLite.cs b/src/SQLite.cs index cbc8d22a..2638ee43 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -2676,7 +2676,7 @@ public static string SqlType (TableMapping.Column p, bool storeDateTimeAsTicks, return storeDateTimeAsTicks ? "bigint" : "datetime"; } else if (clrType == typeof (DateTimeOffset)) { - return "bigint"; + return "blob"; } else if (clrType.GetTypeInfo ().IsEnum) { if (p.StoreAsText) @@ -3002,8 +3002,9 @@ internal static void BindParameter (Sqlite3Statement stmt, int index, object val SQLite3.BindText (stmt, index, ((DateTime)value).ToString (dateTimeStringFormat, System.Globalization.CultureInfo.InvariantCulture), -1, NegativePointer); } } - else if (value is DateTimeOffset) { - SQLite3.BindInt64 (stmt, index, ((DateTimeOffset)value).UtcTicks); + else if (value is DateTimeOffset dto) { + var dtoAsBytes = DateTimeOffsetToBytes(dto); + SQLite3.BindBlob(stmt, index, dtoAsBytes, dtoAsBytes.Length, NegativePointer); } else if (value is byte[]) { SQLite3.BindBlob (stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer); @@ -3037,7 +3038,21 @@ internal static void BindParameter (Sqlite3Statement stmt, int index, object val } } } - + static DateTimeOffset BytesToDateTimeOffset(byte[] bytes) + { + var date = BitConverter.ToInt64(bytes, 0); + var offset = BitConverter.ToInt16(bytes, 8); + return new DateTimeOffset(new DateTime(date), TimeSpan.FromMinutes(offset)); + } + static byte[] DateTimeOffsetToBytes(DateTimeOffset dto) + { + var arr1 = BitConverter.GetBytes(dto.DateTime.Ticks); + var arr2 = BitConverter.GetBytes((short)dto.Offset.TotalMinutes); + var result = new byte[10]; + Array.Copy(arr1, 0, result, 0, arr1.Length); + Array.Copy(arr2, 0, result, 8, arr2.Length); + return result; + } class Binding { public string Name { get; set; } @@ -3101,7 +3116,7 @@ object ReadCol (Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clr } } else if (clrType == typeof (DateTimeOffset)) { - return new DateTimeOffset (SQLite3.ColumnInt64 (stmt, index), TimeSpan.Zero); + return BytesToDateTimeOffset(SQLite3.ColumnByteArray(stmt, index)); } else if (clrTypeInfo.IsEnum) { if (type == SQLite3.ColType.Text) { diff --git a/tests/DateTimeOffsetTest.cs b/tests/DateTimeOffsetTest.cs index 9c06a6b2..2af24db0 100644 --- a/tests/DateTimeOffsetTest.cs +++ b/tests/DateTimeOffsetTest.cs @@ -15,7 +15,7 @@ namespace SQLite.Tests public class DateTimeOffsetTest { class TestObj - { + { [PrimaryKey, AutoIncrement] public int Id { get; set; } @@ -49,7 +49,7 @@ void TestAsyncDateTimeOffset (SQLiteAsyncConnection db) // Ticks // o = new TestObj { - ModifiedTime = new DateTimeOffset (2012, 1, 14, 3, 2, 1, TimeSpan.Zero), + ModifiedTime = new DateTimeOffset (2012, 1, 14, 3, 2, 1, TimeSpan.FromMinutes(120)), }; db.InsertAsync (o).Wait (); o2 = db.GetAsync (o.Id).Result; @@ -66,7 +66,7 @@ void TestDateTimeOffset (TestDb db) // Ticks // o = new TestObj { - ModifiedTime = new DateTimeOffset (2012, 1, 14, 3, 2, 1, TimeSpan.Zero), + ModifiedTime = new DateTimeOffset (2012, 1, 14, 3, 2, 1, TimeSpan.FromMinutes(-120)), }; db.Insert (o); o2 = db.Get (o.Id); From 13df57581dd5296e848edd2d67a92dbbc5cdf92d Mon Sep 17 00:00:00 2001 From: Vladimir Bodurov Date: Sat, 11 Apr 2020 15:34:32 -0700 Subject: [PATCH 2/3] Fix --- src/SQLite.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SQLite.cs b/src/SQLite.cs index 2638ee43..77c7a50f 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -4394,7 +4394,7 @@ public static byte[] ColumnByteArray (Sqlite3Statement stmt, int index) } return new byte[0]; } - + public static Result EnableLoadExtension (Sqlite3DatabaseHandle db, int onoff) { return (Result)Sqlite3.sqlite3_enable_load_extension (db, onoff); From 01e07e601535ab695ed26b333d94bd9bac1aa1c9 Mon Sep 17 00:00:00 2001 From: Vladimir Bodurov Date: Sat, 11 Apr 2020 15:36:03 -0700 Subject: [PATCH 3/3] Fix #932 --- src/SQLite.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SQLite.cs b/src/SQLite.cs index 77c7a50f..5e94330b 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -4414,7 +4414,7 @@ public static ExtendedResult ExtendedErrCode (Sqlite3DatabaseHandle db) { return (ExtendedResult)Sqlite3.sqlite3_extended_errcode (db); } - + public static Sqlite3BackupHandle BackupInit (Sqlite3DatabaseHandle destDb, string destName, Sqlite3DatabaseHandle sourceDb, string sourceName) { return Sqlite3.sqlite3_backup_init (destDb, destName, sourceDb, sourceName);