Skip to content

Commit 611ff00

Browse files
committed
Update SqliteConnectionStringParser.cs
Done!
1 parent ab21646 commit 611ff00

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

SQLite.CodeFirst/SqliteConnectionStringParser.cs

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

44
namespace SQLite.CodeFirst
55
{
66
internal static class SqliteConnectionStringParser
77
{
8-
private const string DataDirectoryToken= "|DataDirectory|";
8+
private const string DataDirectoryToken = "|datadirectory|";
99
private const char KeyValuePairSeperator = ';';
1010
private const char KeyValueSeperator = '=';
1111
private const int KeyPosition = 0;
@@ -28,12 +28,53 @@ public static IDictionary<string, string> ParseSqliteConnectionString(string con
2828

2929
public static string GetDataSource(string connectionString)
3030
{
31-
if (connectionString.ToLower().Contains(DataDirectoryToken.ToLower()))
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
3273
{
33-
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory + @"\";
34-
connectionString = connectionString.ToLower().Replace(DataDirectoryToken.ToLower(), baseDirectory).Replace(@"\\", @"\");
74+
// simply concatenate the strings
75+
fullPath = rootFolderPath + path.Substring(fileNamePosition);
3576
}
36-
return ParseSqliteConnectionString(connectionString)["data source"];
77+
return fullPath;
3778
}
3879
}
3980
}

0 commit comments

Comments
 (0)