From c687b882fc0a15dc654a195d75db2f9a5a0df472 Mon Sep 17 00:00:00 2001 From: Sean Kent Date: Mon, 22 Jul 2024 09:52:16 -0700 Subject: [PATCH 1/3] Removing unused methods. Refactored object deserialization to make it support better type creation. --- SQLite.Net.Tests/DefaultAttributeTest.cs | 17 ----------------- SQLite.Net.Tests/IgnoreTest.cs | 17 ----------------- src/SQLite.Net/Orm.cs | 4 ---- src/SQLite.Net/SQLiteCommand.cs | 13 +++---------- .../Tools/DefaultColumnInformationProvider.cs | 17 ----------------- .../Tools/IColumnInformationProvider.cs | 12 +++++++----- 6 files changed, 10 insertions(+), 70 deletions(-) diff --git a/SQLite.Net.Tests/DefaultAttributeTest.cs b/SQLite.Net.Tests/DefaultAttributeTest.cs index 3088eab4..c3e001e0 100644 --- a/SQLite.Net.Tests/DefaultAttributeTest.cs +++ b/SQLite.Net.Tests/DefaultAttributeTest.cs @@ -96,23 +96,6 @@ public object GetValue(MemberInfo m, object obj) }; } - public bool TryBindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, object value) - { - return false; - } - - public bool TryGetSqliteColumnType(Type type, out string sqliteType) - { - sqliteType = string.Empty; - return false; - } - - public bool TryReadCol(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, Type clrType, out object? value) - { - value = null; - return false; - } - public bool IsIgnored(MemberInfo p) { return false; diff --git a/SQLite.Net.Tests/IgnoreTest.cs b/SQLite.Net.Tests/IgnoreTest.cs index 04edb9b1..eadae217 100644 --- a/SQLite.Net.Tests/IgnoreTest.cs +++ b/SQLite.Net.Tests/IgnoreTest.cs @@ -54,23 +54,6 @@ public object GetValue(MemberInfo m, object obj) }; } - public bool TryBindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, object value) - { - return false; - } - - public bool TryGetSqliteColumnType(Type type, out string sqliteType) - { - sqliteType = string.Empty; - return false; - } - - public bool TryReadCol(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, Type clrType, out object? value) - { - value = null; - return false; - } - public bool IsIgnored(MemberInfo p) { return p.IsDefined(typeof (TestIgnoreAttribute), true); diff --git a/src/SQLite.Net/Orm.cs b/src/SQLite.Net/Orm.cs index 2f750813..b43e5b41 100644 --- a/src/SQLite.Net/Orm.cs +++ b/src/SQLite.Net/Orm.cs @@ -134,10 +134,6 @@ private static string SqlType(TableMapping.Column p, bool storeDateTimeAsTicks, { return "text"; } - if (ColumnInformationProvider.TryGetSqliteColumnType(clrType, out var result)) - { - return result; - } if (serializer != null && serializer.CanDeserialize(clrType)) { return "blob"; diff --git a/src/SQLite.Net/SQLiteCommand.cs b/src/SQLite.Net/SQLiteCommand.cs index b9316f74..b5c6c00f 100644 --- a/src/SQLite.Net/SQLiteCommand.cs +++ b/src/SQLite.Net/SQLiteCommand.cs @@ -171,13 +171,14 @@ public IEnumerable ExecuteDeferredQuery(TableMapping map) while (sqlite.Step(stmt) == Result.Row) { - var obj = isPrimitiveType ? null : _conn.Resolver.CreateObject(map.MappedType); - if (_conn.ColumnInformationProvider.TryReadObject(obj, sqlite, stmt)) + var obj = _conn.ColumnInformationProvider.TryReadObject(map, sqlite, stmt); + if (obj != null) { yield return (T)obj; } else { + obj = isPrimitiveType ? null : _conn.Resolver.CreateObject(map.MappedType); for (var i = 0; i < cols.Length; i++) { ColType colType; @@ -443,10 +444,6 @@ internal static void BindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, in isqLite3Api.BindText16(stmt, index, val, -1, NegativePointer); } } - else if (Orm.ColumnInformationProvider.TryBindParameter(isqLite3Api, stmt, index, value)) - { - return; - } else if (value.GetType().GetTypeInfo().IsEnum) { isqLite3Api.BindInt(stmt, index, Convert.ToInt32(value)); @@ -634,10 +631,6 @@ private object ReadCol(IDbStatement stmt, int index, ColType type, Type clrType) var value = (sbyte) sqlite.ColumnInt(stmt, index); return _conn.Resolver.CreateObject(clrType, new object[] {value}); } - if (_conn.ColumnInformationProvider.TryReadCol(sqlite, stmt, index, clrType, out var obj)) - { - return obj!; - } if (clrType == typeof (byte[])) { return sqlite.ColumnByteArray(stmt, index).ToArray(); diff --git a/src/SQLite.Net/Tools/DefaultColumnInformationProvider.cs b/src/SQLite.Net/Tools/DefaultColumnInformationProvider.cs index 6e566c20..9a27dab4 100644 --- a/src/SQLite.Net/Tools/DefaultColumnInformationProvider.cs +++ b/src/SQLite.Net/Tools/DefaultColumnInformationProvider.cs @@ -132,23 +132,6 @@ public object GetValue(MemberInfo m, object obj) _ => throw new NotSupportedException($"{m.GetType()} is not supported.") }; } - - public virtual bool TryBindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, object value) - { - return false; - } - - public virtual bool TryGetSqliteColumnType(Type type, out string sqliteType) - { - sqliteType = string.Empty; - return false; - } - - public virtual bool TryReadCol(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, Type clrType, out object? value) - { - value = null; - return false; - } #endregion } } diff --git a/src/SQLite.Net/Tools/IColumnInformationProvider.cs b/src/SQLite.Net/Tools/IColumnInformationProvider.cs index 70390e95..59d2819a 100644 --- a/src/SQLite.Net/Tools/IColumnInformationProvider.cs +++ b/src/SQLite.Net/Tools/IColumnInformationProvider.cs @@ -17,13 +17,15 @@ public interface IColumnInformationProvider string GetColumnName(Type containedType, MemberInfo p, int tupleElementIndex); Type GetMemberType(MemberInfo m); object GetValue(MemberInfo m, object obj); + /// - /// Attempts to read an object from . Returns true if successful. + /// Attempts to read an object from . Returns non-null if the object is supported. /// - bool TryReadObject(object obj, ISQLiteApi sqLiteApi, IDbStatement stmt) => false; - bool TryBindParameter(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, object value); - bool TryGetSqliteColumnType(Type type, out string sqliteType); - bool TryReadCol(ISQLiteApi isqLite3Api, IDbStatement stmt, int index, Type clrType, out object? value); + /// Table mapping for the type to return + /// SQLite API + /// Statement row to read from. + /// An object or null if the table/type is not supported. + object? TryReadObject(TableMapping mapping, ISQLiteApi sqLiteApi, IDbStatement stmt) => null; } } From 2fa1799fcc6588371372e90d135cda4d16709085 Mon Sep 17 00:00:00 2001 From: Sean Kent Date: Mon, 22 Jul 2024 09:55:04 -0700 Subject: [PATCH 2/3] Updating version. --- src/SQLite.Net/SQLite.Net2.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SQLite.Net/SQLite.Net2.csproj b/src/SQLite.Net/SQLite.Net2.csproj index 8f049a3b..213241e8 100644 --- a/src/SQLite.Net/SQLite.Net2.csproj +++ b/src/SQLite.Net/SQLite.Net2.csproj @@ -32,7 +32,7 @@ sqlite-net2 light ORM for SQLite sqlite-net2 allows applications to manage data in SQLite databases using Entity Framework like queries, but much lighter $(AssemblyName) ($(TargetFramework)) - .10 + .11 $(Version)$(VersionSuffix) Benjamin Mayrargue Benjamin Mayrargue From 4fb0d42165030626baa40ee73cd9e5a9028ff08b Mon Sep 17 00:00:00 2001 From: Sean Kent Date: Mon, 22 Jul 2024 12:29:28 -0700 Subject: [PATCH 3/3] Some more tweaks, needed to expose the sqlite handle. --- src/SQLite.Net/Interfaces/ISQLiteApi.cs | 6 +++++- src/SQLite.Net/Interop/SQLiteApi.cs | 2 ++ src/SQLite.Net/SQLite.Net2.csproj | 2 +- src/SQLite.Net/SQLiteCommand.cs | 5 ++++- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/SQLite.Net/Interfaces/ISQLiteApi.cs b/src/SQLite.Net/Interfaces/ISQLiteApi.cs index 08eb89e0..3d8f01e2 100644 --- a/src/SQLite.Net/Interfaces/ISQLiteApi.cs +++ b/src/SQLite.Net/Interfaces/ISQLiteApi.cs @@ -24,7 +24,11 @@ namespace SQLite.Net2 { public interface IDbHandle { } - public interface IDbStatement { } + + public interface IDbStatement + { + object Handle { get; } + } public interface IDbBackupHandle { } public interface ISQLiteApi diff --git a/src/SQLite.Net/Interop/SQLiteApi.cs b/src/SQLite.Net/Interop/SQLiteApi.cs index 6810f891..9fe4feb3 100644 --- a/src/SQLite.Net/Interop/SQLiteApi.cs +++ b/src/SQLite.Net/Interop/SQLiteApi.cs @@ -395,6 +395,8 @@ public bool Equals(IDbHandle other) private struct DbStatement : IDbStatement { + public object Handle => StmtPtr; + public sqlite3_stmt StmtPtr { get; private set; } public DbStatement(sqlite3_stmt stmtPtr) diff --git a/src/SQLite.Net/SQLite.Net2.csproj b/src/SQLite.Net/SQLite.Net2.csproj index 213241e8..cc45dbb7 100644 --- a/src/SQLite.Net/SQLite.Net2.csproj +++ b/src/SQLite.Net/SQLite.Net2.csproj @@ -32,7 +32,7 @@ sqlite-net2 light ORM for SQLite sqlite-net2 allows applications to manage data in SQLite databases using Entity Framework like queries, but much lighter $(AssemblyName) ($(TargetFramework)) - .11 + .13 $(Version)$(VersionSuffix) Benjamin Mayrargue Benjamin Mayrargue diff --git a/src/SQLite.Net/SQLiteCommand.cs b/src/SQLite.Net/SQLiteCommand.cs index b5c6c00f..02304dd7 100644 --- a/src/SQLite.Net/SQLiteCommand.cs +++ b/src/SQLite.Net/SQLiteCommand.cs @@ -171,7 +171,10 @@ public IEnumerable ExecuteDeferredQuery(TableMapping map) while (sqlite.Step(stmt) == Result.Row) { - var obj = _conn.ColumnInformationProvider.TryReadObject(map, sqlite, stmt); + var obj = isPrimitiveType + ? null + : _conn.ColumnInformationProvider.TryReadObject(map, sqlite, stmt); + if (obj != null) { yield return (T)obj;