From 71f37716d72a4d1e06093574ae30bd2f086deea0 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 20 Apr 2020 14:09:07 -0700 Subject: [PATCH] Avoid using SQLite types in type signatures Fixes failure to reflect on the Workspaces layer when SQLite is not available at runtime. --- .../SQLite/Interop/SafeSqliteBlobHandle.cs | 22 +++++++++-- .../SQLite/Interop/SafeSqliteChildHandle`1.cs | 37 ------------------- .../SQLite/Interop/SafeSqliteHandle.cs | 16 ++++++-- .../SQLite/Interop/SafeSqliteHandle`1.cs | 29 --------------- .../Interop/SafeSqliteStatementHandle.cs | 22 +++++++++-- 5 files changed, 49 insertions(+), 77 deletions(-) delete mode 100644 src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteChildHandle`1.cs delete mode 100644 src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteHandle`1.cs diff --git a/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteBlobHandle.cs b/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteBlobHandle.cs index f7625834711f6..80523d7e6d237 100644 --- a/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteBlobHandle.cs +++ b/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteBlobHandle.cs @@ -5,20 +5,34 @@ #nullable enable using System; +using System.Runtime.InteropServices; +using Microsoft.CodeAnalysis.Shared.Extensions; using SQLitePCL; namespace Microsoft.CodeAnalysis.SQLite.Interop { - internal sealed class SafeSqliteBlobHandle : SafeSqliteChildHandle + internal sealed class SafeSqliteBlobHandle : SafeHandle { + private readonly sqlite3_blob? _wrapper; + private readonly SafeHandleLease _lease; + public SafeSqliteBlobHandle(SafeSqliteHandle sqliteHandle, sqlite3_blob? wrapper) - : base(sqliteHandle, wrapper?.ptr ?? IntPtr.Zero, wrapper) + : base(invalidHandleValue: IntPtr.Zero, ownsHandle: true) { + _wrapper = wrapper; + SetHandle(wrapper?.ptr ?? IntPtr.Zero); + _lease = sqliteHandle.Lease(); } - protected override bool ReleaseChildHandle() + public override bool IsInvalid => handle == IntPtr.Zero; + + public new sqlite3_blob DangerousGetHandle() + => _wrapper!; + + protected override bool ReleaseHandle() { - var result = (Result)raw.sqlite3_blob_close(Wrapper); + using var _ = _lease; + var result = (Result)raw.sqlite3_blob_close(_wrapper); return result == Result.OK; } } diff --git a/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteChildHandle`1.cs b/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteChildHandle`1.cs deleted file mode 100644 index 40a1c7e77265e..0000000000000 --- a/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteChildHandle`1.cs +++ /dev/null @@ -1,37 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#nullable enable - -using System; -using System.Runtime.InteropServices; -using Microsoft.CodeAnalysis.Shared.Extensions; - -namespace Microsoft.CodeAnalysis.SQLite.Interop -{ - /// - /// The base handle type for an SQLite resource that exists within the context of a parent handle, and should always - /// be released prior to the parent handle. - /// - /// The SQLite resource wrapper type. - internal abstract class SafeSqliteChildHandle : SafeSqliteHandle - where T : class - { - private readonly SafeHandleLease _lease; - - protected SafeSqliteChildHandle(SafeHandle parentHandle, IntPtr handle, T? wrapper) - : base(handle, wrapper) - { - _lease = parentHandle.Lease(); - } - - protected abstract bool ReleaseChildHandle(); - - protected sealed override bool ReleaseHandle() - { - using var _ = _lease; - return ReleaseChildHandle(); - } - } -} diff --git a/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteHandle.cs b/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteHandle.cs index 2e66c42857b40..c2fc36c1262fe 100644 --- a/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteHandle.cs +++ b/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteHandle.cs @@ -5,20 +5,30 @@ #nullable enable using System; +using System.Runtime.InteropServices; using SQLitePCL; namespace Microsoft.CodeAnalysis.SQLite.Interop { - internal sealed class SafeSqliteHandle : SafeSqliteHandle + internal sealed class SafeSqliteHandle : SafeHandle { + private readonly sqlite3? _wrapper; + public SafeSqliteHandle(sqlite3? wrapper) - : base(wrapper?.ptr ?? IntPtr.Zero, wrapper) + : base(invalidHandleValue: IntPtr.Zero, ownsHandle: true) { + _wrapper = wrapper; + SetHandle(wrapper?.ptr ?? IntPtr.Zero); } + public override bool IsInvalid => handle == IntPtr.Zero; + + public new sqlite3 DangerousGetHandle() + => _wrapper!; + protected override bool ReleaseHandle() { - var result = (Result)raw.sqlite3_close(Wrapper); + var result = (Result)raw.sqlite3_close(_wrapper); return result == Result.OK; } } diff --git a/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteHandle`1.cs b/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteHandle`1.cs deleted file mode 100644 index 77d7462545fab..0000000000000 --- a/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteHandle`1.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#nullable enable - -using System; -using System.Runtime.InteropServices; - -namespace Microsoft.CodeAnalysis.SQLite.Interop -{ - internal abstract class SafeSqliteHandle : SafeHandle - where T : class - { - protected readonly T? Wrapper; - - public SafeSqliteHandle(IntPtr handle, T? wrapper) - : base(invalidHandleValue: IntPtr.Zero, ownsHandle: true) - { - Wrapper = wrapper; - SetHandle(handle); - } - - public override bool IsInvalid => handle == IntPtr.Zero; - - public new T DangerousGetHandle() - => Wrapper!; - } -} diff --git a/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteStatementHandle.cs b/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteStatementHandle.cs index bea28c3d24420..48e1eff0b1051 100644 --- a/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteStatementHandle.cs +++ b/src/Workspaces/Core/Portable/Storage/SQLite/Interop/SafeSqliteStatementHandle.cs @@ -5,20 +5,34 @@ #nullable enable using System; +using System.Runtime.InteropServices; +using Microsoft.CodeAnalysis.Shared.Extensions; using SQLitePCL; namespace Microsoft.CodeAnalysis.SQLite.Interop { - internal sealed class SafeSqliteStatementHandle : SafeSqliteChildHandle + internal sealed class SafeSqliteStatementHandle : SafeHandle { + private readonly sqlite3_stmt? _wrapper; + private readonly SafeHandleLease _lease; + public SafeSqliteStatementHandle(SafeSqliteHandle sqliteHandle, sqlite3_stmt? wrapper) - : base(sqliteHandle, wrapper?.ptr ?? IntPtr.Zero, wrapper) + : base(invalidHandleValue: IntPtr.Zero, ownsHandle: true) { + _wrapper = wrapper; + SetHandle(wrapper?.ptr ?? IntPtr.Zero); + _lease = sqliteHandle.Lease(); } - protected override bool ReleaseChildHandle() + public override bool IsInvalid => handle == IntPtr.Zero; + + public new sqlite3_stmt DangerousGetHandle() + => _wrapper!; + + protected override bool ReleaseHandle() { - var result = (Result)raw.sqlite3_finalize(Wrapper); + using var _ = _lease; + var result = (Result)raw.sqlite3_finalize(_wrapper); return result == Result.OK; } }