Skip to content

Commit 9b0c88d

Browse files
author
Marc Sallin
committed
Merge pull request #5 from MichaelCsitkovics/patch-2
Update SqliteConnectionStringParser.cs to support the "|DataDirectory|" token in a ConnectionString.
2 parents 4cabd27 + 611ff00 commit 9b0c88d

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

SQLite.CodeFirst/SqliteConnectionStringParser.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace SQLite.CodeFirst
45
{
56
internal static class SqliteConnectionStringParser
67
{
8+
private const string DataDirectoryToken = "|datadirectory|";
79
private const char KeyValuePairSeperator = ';';
810
private const char KeyValueSeperator = '=';
911
private const int KeyPosition = 0;
@@ -26,7 +28,53 @@ public static IDictionary<string, string> ParseSqliteConnectionString(string con
2628

2729
public static string GetDataSource(string connectionString)
2830
{
29-
return ParseSqliteConnectionString(connectionString)["data source"];
31+
var path = ExpandDataDirectory(ParseSqliteConnectionString(connectionString)["data source"]);
32+
return path;
33+
}
34+
35+
private static string ExpandDataDirectory(string path)
36+
{
37+
if (path == null || !path.StartsWith(DataDirectoryToken, StringComparison.OrdinalIgnoreCase))
38+
{
39+
return path;
40+
}
41+
42+
string fullPath;
43+
44+
// find the replacement path
45+
object rootFolderObject = AppDomain.CurrentDomain.GetData("DataDirectory");
46+
string rootFolderPath = (rootFolderObject as string);
47+
if (rootFolderObject != null && rootFolderPath == null)
48+
{
49+
throw new InvalidOperationException("The value stored in the AppDomains 'DataDirectory' variable has to be a string!");
50+
}
51+
if (string.IsNullOrEmpty(rootFolderPath))
52+
{
53+
rootFolderPath = AppDomain.CurrentDomain.BaseDirectory;
54+
}
55+
56+
// We don't know if rootFolderpath ends with '\', and we don't know if the given name starts with onw
57+
int fileNamePosition = DataDirectoryToken.Length; // filename starts right after the '|datadirectory|' keyword
58+
bool rootFolderEndsWith = (0 < rootFolderPath.Length) && rootFolderPath[rootFolderPath.Length - 1] == '\\';
59+
bool fileNameStartsWith = (fileNamePosition < path.Length) && path[fileNamePosition] == '\\';
60+
61+
// replace |datadirectory| with root folder path
62+
if (!rootFolderEndsWith && !fileNameStartsWith)
63+
{
64+
// need to insert '\'
65+
fullPath = rootFolderPath + '\\' + path.Substring(fileNamePosition);
66+
}
67+
else if (rootFolderEndsWith && fileNameStartsWith)
68+
{
69+
// need to strip one out
70+
fullPath = rootFolderPath + path.Substring(fileNamePosition + 1);
71+
}
72+
else
73+
{
74+
// simply concatenate the strings
75+
fullPath = rootFolderPath + path.Substring(fileNamePosition);
76+
}
77+
return fullPath;
3078
}
3179
}
3280
}

0 commit comments

Comments
 (0)