1- using System ;
2- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
33
44namespace 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