@@ -12,8 +12,9 @@ public static class ConnectionStringParser
1212 /// Parses the given URI connection string.
1313 /// </summary>
1414 /// <param name="connectionString">The URI connection string.</param>
15+ /// <param name="sqliteBaseDirectory">Base directory used to resolve relative SQLite file paths; null keeps them verbatim.</param>
1516 /// <returns>The parsed provider and native connection string.</returns>
16- public static ParsedConnection Parse ( string connectionString )
17+ public static ParsedConnection Parse ( string connectionString , string ? sqliteBaseDirectory = null )
1718 {
1819 ArgumentException . ThrowIfNullOrWhiteSpace ( connectionString ) ;
1920
@@ -28,11 +29,14 @@ public static ParsedConnection Parse(string connectionString)
2829 var remainder = connectionString [ ( schemeEnd + 3 ) ..] ;
2930 var provider = ResolveProvider ( scheme ) ;
3031
31- var native = provider == DatabaseProviderType . Sqlite
32- ? BuildSqlite ( remainder )
33- : BuildServer ( provider , remainder ) ;
32+ if ( provider == DatabaseProviderType . Sqlite )
33+ {
34+ var filePath = ResolveSqlitePath ( remainder , sqliteBaseDirectory ) ;
35+
36+ return new ( provider , $ "Data Source={ filePath ?? remainder } ", filePath ) ;
37+ }
3438
35- return new ( provider , native ) ;
39+ return new ( provider , BuildServer ( provider , remainder ) ) ;
3640 }
3741
3842 private static string BuildServer ( DatabaseProviderType provider , string remainder )
@@ -76,14 +80,24 @@ private static string BuildServer(DatabaseProviderType provider, string remainde
7680 } ;
7781 }
7882
79- private static string BuildSqlite ( string remainder )
83+ private static string ? ResolveSqlitePath ( string remainder , string ? baseDirectory )
8084 {
8185 if ( string . IsNullOrWhiteSpace ( remainder ) )
8286 {
8387 throw new FormatException ( "SQLite connection string requires a file path or ':memory:'." ) ;
8488 }
8589
86- return $ "Data Source={ remainder } ";
90+ if ( string . Equals ( remainder , ":memory:" , StringComparison . OrdinalIgnoreCase ) )
91+ {
92+ return null ;
93+ }
94+
95+ if ( ! Path . IsPathRooted ( remainder ) && ! string . IsNullOrWhiteSpace ( baseDirectory ) )
96+ {
97+ return Path . GetFullPath ( Path . Combine ( baseDirectory , remainder ) ) ;
98+ }
99+
100+ return remainder ;
87101 }
88102
89103 private static DatabaseProviderType ResolveProvider ( string scheme )
0 commit comments