Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit263a7b1

Browse files
krwqroji
authored andcommitted
Improve LoadExtension to work correctly with dotnet run and lib* named libs
* Improve LoadExtension to work correctly with dotnet run and lib packages* Use [] instead of Array.Empty(cherry picked from commitb1d34dc)
1 parentbd39e43 commit263a7b1

File tree

1 file changed

+79
-3
lines changed

1 file changed

+79
-3
lines changed

‎src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs‎

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ namespace Microsoft.Data.Sqlite
2323
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/async">Async Limitations</seealso>
2424
publicpartialclassSqliteConnection:DbConnection
2525
{
26+
privatestaticreadonlyboolUseOldBehavior35715=
27+
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue35715",outvarenabled35715)&&enabled35715;
28+
2629
internalconststringMainDatabaseName="main";
2730

2831
privateconstintSQLITE_WIN32_DATA_DIRECTORY_TYPE=1;
@@ -48,6 +51,8 @@ public partial class SqliteConnection : DbConnection
4851
privatestaticreadonlyStateChangeEventArgs_fromClosedToOpenEventArgs=newStateChangeEventArgs(ConnectionState.Closed,ConnectionState.Open);
4952
privatestaticreadonlyStateChangeEventArgs_fromOpenToClosedEventArgs=newStateChangeEventArgs(ConnectionState.Open,ConnectionState.Closed);
5053

54+
privatestaticstring[]?NativeDllSearchDirectories;
55+
5156
staticSqliteConnection()
5257
{
5358
Type.GetType("SQLitePCL.Batteries_V2, SQLitePCLRaw.batteries_v2")
@@ -626,11 +631,82 @@ public virtual void LoadExtension(string file, string? proc = null)
626631

627632
privatevoidLoadExtensionCore(stringfile,string?proc)
628633
{
629-
varrc=sqlite3_load_extension(Handle,utf8z.FromString(file),utf8z.FromString(proc),outvarerrmsg);
630-
if(rc!=SQLITE_OK)
634+
if(UseOldBehavior35715)
635+
{
636+
varrc=sqlite3_load_extension(Handle,utf8z.FromString(file),utf8z.FromString(proc),outvarerrmsg);
637+
if(rc!=SQLITE_OK)
638+
{
639+
thrownewSqliteException(Resources.SqliteNativeError(rc,errmsg.utf8_to_string()),rc,rc);
640+
}
641+
}
642+
else
643+
{
644+
SqliteException?firstException=null;
645+
foreach(varpathinGetLoadExtensionPaths(file))
646+
{
647+
varrc=sqlite3_load_extension(Handle,utf8z.FromString(path),utf8z.FromString(proc),outvarerrmsg);
648+
if(rc==SQLITE_OK)
649+
{
650+
return;
651+
}
652+
653+
if(firstException==null)
654+
{
655+
// We store the first exception so that error message looks more obvious if file appears in there
656+
firstException=newSqliteException(Resources.SqliteNativeError(rc,errmsg.utf8_to_string()),rc,rc);
657+
}
658+
}
659+
660+
if(firstException!=null)
661+
{
662+
throwfirstException;
663+
}
664+
}
665+
}
666+
667+
privatestaticIEnumerable<string>GetLoadExtensionPaths(stringfile)
668+
{
669+
// we always try original input first
670+
yieldreturnfile;
671+
672+
string?dirName=Path.GetDirectoryName(file);
673+
674+
// we don't try to guess directories for user, if they pass a path either absolute or relative - they're on their own
675+
if(!string.IsNullOrEmpty(dirName))
631676
{
632-
thrownewSqliteException(Resources.SqliteNativeError(rc,errmsg.utf8_to_string()),rc,rc);
677+
yieldbreak;
633678
}
679+
680+
boolshouldTryAddingLibPrefix=!file.StartsWith("lib",StringComparison.Ordinal)&&!RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
681+
682+
if(shouldTryAddingLibPrefix)
683+
{
684+
yieldreturn$"lib{file}";
685+
}
686+
687+
NativeDllSearchDirectories??=GetNativeDllSearchDirectories();
688+
689+
foreach(stringdirinNativeDllSearchDirectories)
690+
{
691+
yieldreturnPath.Combine(dir,file);
692+
693+
if(shouldTryAddingLibPrefix)
694+
{
695+
yieldreturnPath.Combine(dir,$"lib{file}");
696+
}
697+
}
698+
}
699+
700+
privatestaticstring[]GetNativeDllSearchDirectories()
701+
{
702+
string?searchDirs=AppContext.GetData("NATIVE_DLL_SEARCH_DIRECTORIES")asstring;
703+
704+
if(string.IsNullOrEmpty(searchDirs))
705+
{
706+
return[];
707+
}
708+
709+
returnsearchDirs!.Split([Path.PathSeparator],StringSplitOptions.RemoveEmptyEntries);
634710
}
635711

636712
/// <summary>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp