Skip to content

[release/3.1] Microsoft.Data.Sqlite: Set temp and data directory on UWP #20077

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!--
This file contains Runtime Directives used by .NET Native.
-->

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Library>
<Type Name="Windows.Storage.ApplicationData" Dynamic="Required All" />
<Type Name="Windows.Storage.StorageFolder" Dynamic="Required All" />
</Library>
</Directives>
46 changes: 46 additions & 0 deletions src/Microsoft.Data.Sqlite.Core/Utilities/ApplicationDataHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Reflection;

namespace Microsoft.Data.Sqlite.Utilities
{
internal class ApplicationDataHelper
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, this is the same code we had in 1.1

{
private static object _appData;
private static string _localFolder;
private static string _tempFolder;

public static object CurrentApplicationData
=> _appData ??= LoadAppData();

public static string TemporaryFolderPath
=> _tempFolder ??= GetFolderPath("TemporaryFolder");

public static string LocalFolderPath
=> _localFolder ??= GetFolderPath("LocalFolder");

private static object LoadAppData()
{
try
{
return Type.GetType("Windows.Storage.ApplicationData, Windows, ContentType=WindowsRuntime")
?.GetRuntimeProperty("Current").GetValue(null);
}
catch
{
// Ignore "The process has no package identity."
return null;
}
}

private static string GetFolderPath(string propertyName)
{
var appDataType = CurrentApplicationData?.GetType();
var temporaryFolder = appDataType?.GetRuntimeProperty(propertyName).GetValue(CurrentApplicationData);

return temporaryFolder?.GetType().GetRuntimeProperty("Path").GetValue(temporaryFolder) as string;
}
}
}
30 changes: 26 additions & 4 deletions src/Microsoft.Data.Sqlite.Core/Utilities/BundleInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Reflection;

using static SQLitePCL.raw;

namespace Microsoft.Data.Sqlite.Utilities
{
internal static class BundleInitializer
{
private const int SQLITE_WIN32_DATA_DIRECTORY_TYPE = 1;
private const int SQLITE_WIN32_TEMP_DIRECTORY_TYPE = 2;

public static void Initialize()
{
Assembly assembly;
Assembly assembly = null;
try
{
assembly = Assembly.Load(new AssemblyName("SQLitePCLRaw.batteries_v2"));
}
catch
{
return;
}

assembly.GetType("SQLitePCL.Batteries_V2").GetTypeInfo().GetDeclaredMethod("Init")
.Invoke(null, null);
if (assembly != null)
{
assembly.GetType("SQLitePCL.Batteries_V2").GetTypeInfo().GetDeclaredMethod("Init")
.Invoke(null, null);
}

if ((!AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue19754", out var isEnabled) || !isEnabled)
&& ApplicationDataHelper.CurrentApplicationData != null)
{
var rc = sqlite3_win32_set_directory(
SQLITE_WIN32_DATA_DIRECTORY_TYPE,
ApplicationDataHelper.LocalFolderPath);
SqliteException.ThrowExceptionForRC(rc, db: null);

rc = sqlite3_win32_set_directory(
SQLITE_WIN32_TEMP_DIRECTORY_TYPE,
ApplicationDataHelper.TemporaryFolderPath);
SqliteException.ThrowExceptionForRC(rc, db: null);
}
}
}
}