-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/Microsoft.Data.Sqlite.Core/Properties/Microsoft.Data.Sqlite.rd.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
src/Microsoft.Data.Sqlite.Core/Utilities/ApplicationDataHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
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
30
src/Microsoft.Data.Sqlite.Core/Utilities/BundleInitializer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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