-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
SpatialiteLoader.cs
172 lines (154 loc) · 6.18 KB
/
SpatialiteLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 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.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyModel;
using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;
namespace Microsoft.EntityFrameworkCore.Infrastructure
{
/// <summary>
/// Finds and loads SpatiaLite.
/// </summary>
public static class SpatialiteLoader
{
private static readonly string _sharedLibraryExtension;
private static readonly string _pathVariableName;
private static bool _looked;
static SpatialiteLoader()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_sharedLibraryExtension = ".dll";
_pathVariableName = "PATH";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
_sharedLibraryExtension = ".dylib";
_pathVariableName = "DYLD_LIBRARY_PATH";
}
else
{
_sharedLibraryExtension = ".so";
_pathVariableName = "LD_LIBRARY_PATH";
}
}
/// <summary>
/// Tries to load the mod_spatialite extension into the specified connection.
/// </summary>
/// <param name="connection"> The connection. </param>
/// <returns> true if the extension was loaded; otherwise, false. </returns>
public static bool TryLoad([NotNull] DbConnection connection)
{
try
{
Load(connection);
return true;
}
catch (SqliteException ex) when (ex.SqliteErrorCode == 1)
{
return false;
}
}
/// <summary>
/// <para>
/// Loads the mod_spatialite extension into the specified connection.
/// </para>
/// <para>
/// The extension will be loaded from native NuGet assets when available.
/// </para>
/// </summary>
/// <param name="connection"> The connection. </param>
public static void Load([NotNull] DbConnection connection)
{
Check.NotNull(connection, nameof(connection));
FindExtension();
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT load_extension('mod_spatialite');";
command.ExecuteNonQuery();
}
}
private static void FindExtension()
{
if (_looked)
{
return;
}
bool hasDependencyContext;
try
{
hasDependencyContext = DependencyContext.Default != null;
}
catch (Exception ex) // Work around dotnet/core-setup#4556
{
Debug.Fail(ex.ToString());
hasDependencyContext = false;
}
if (hasDependencyContext)
{
var candidateAssets = new Dictionary<string, int>();
var rid = RuntimeEnvironment.GetRuntimeIdentifier();
var rids = DependencyContext.Default.RuntimeGraph.First(g => g.Runtime == rid).Fallbacks.ToList();
rids.Insert(0, rid);
foreach (var library in DependencyContext.Default.RuntimeLibraries)
{
foreach (var group in library.NativeLibraryGroups)
{
foreach (var file in group.RuntimeFiles)
{
if (string.Equals(
Path.GetFileName(file.Path),
"mod_spatialite" + _sharedLibraryExtension,
StringComparison.OrdinalIgnoreCase))
{
var fallbacks = rids.IndexOf(group.Runtime);
if (fallbacks != -1)
{
candidateAssets.Add(library.Path + "/" + file.Path, fallbacks);
}
}
}
}
}
var assetPath = candidateAssets.OrderBy(p => p.Value)
.Select(p => p.Key.Replace('/', Path.DirectorySeparatorChar)).FirstOrDefault();
if (assetPath != null)
{
string assetFullPath = null;
var probingDirectories = ((string)AppDomain.CurrentDomain.GetData("PROBING_DIRECTORIES"))
.Split(Path.PathSeparator);
foreach (var directory in probingDirectories)
{
var candidateFullPath = Path.Combine(directory, assetPath);
if (File.Exists(candidateFullPath))
{
assetFullPath = candidateFullPath;
}
}
Debug.Assert(assetFullPath != null);
var assetDirectory = Path.GetDirectoryName(assetFullPath);
var currentPath = Environment.GetEnvironmentVariable(_pathVariableName);
if (!currentPath.Split(Path.PathSeparator).Any(
p => string.Equals(
p.TrimEnd(Path.DirectorySeparatorChar),
assetDirectory,
StringComparison.OrdinalIgnoreCase)))
{
Environment.SetEnvironmentVariable(
_pathVariableName,
assetDirectory + Path.PathSeparator + currentPath);
}
}
}
_looked = true;
}
}
}