Skip to content

Commit 54060b4

Browse files
committed
fix: preserve connection string values containing equals signs
ParseConnectionString split each pair on every '=' and kept only the second segment, truncating any Data Source value that contains '=' at the first one. Split on the first '=' only. Also assign via the dictionary indexer (last value wins) instead of Add, so a repeated key no longer throws.
1 parent 068a19c commit 54060b4

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

SQLite.CodeFirst.Test/UnitTests/Utility/ConnectionStringParserTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,33 @@ public void GetDataSource_ReturnsCorrectDataSource_RemovesQuotationMark()
106106
// Assert
107107
Assert.AreEqual(@".\db\footballDb\footballDb.sqlite", result);
108108
}
109+
110+
[TestMethod]
111+
public void GetDataSource_ReturnsCorrectDataSource_WhenValueContainsEqualsSign()
112+
{
113+
// Arrange
114+
string connectionString = @"data source=.\db\foo=bar\footballDb.sqlite;foreign keys=true";
115+
116+
117+
// Act
118+
string result = ConnectionStringParser.GetDataSource(connectionString);
119+
120+
// Assert
121+
Assert.AreEqual(@".\db\foo=bar\footballDb.sqlite", result);
122+
}
123+
124+
[TestMethod]
125+
public void GetDataSource_ReturnsLastValue_WhenKeyIsRepeated()
126+
{
127+
// Arrange
128+
string connectionString = @"data source=first.sqlite;data source=second.sqlite";
129+
130+
131+
// Act
132+
string result = ConnectionStringParser.GetDataSource(connectionString);
133+
134+
// Assert
135+
Assert.AreEqual(@"second.sqlite", result);
136+
}
109137
}
110138
}

SQLite.CodeFirst/Internal/Utility/ConnectionStringParser.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ private static IDictionary<string, string> ParseConnectionString(string connecti
4242
IDictionary<string, string> keyValuePairDictionary = new Dictionary<string, string>();
4343
foreach (var keyValuePair in keyValuePairs)
4444
{
45-
string[] keyValue = keyValuePair.Split(KeyValueSeperator);
45+
// Split on the first '=' only so a value that itself contains '=' is preserved.
46+
// Input: "data source=C:\a=b.sqlite" -> key="data source", value="C:\a=b.sqlite"
47+
string[] keyValue = keyValuePair.Split(new[] { KeyValueSeperator }, 2, StringSplitOptions.None);
4648
if (keyValue.Length >= 2)
4749
{
48-
keyValuePairDictionary.Add(keyValue[KeyPosition].Trim().ToLower(CultureInfo.InvariantCulture), keyValue[ValuePosition]);
50+
// Indexer assignment (last value wins) so a repeated key does not throw.
51+
keyValuePairDictionary[keyValue[KeyPosition].Trim().ToLower(CultureInfo.InvariantCulture)] = keyValue[ValuePosition];
4952
}
5053
}
5154

0 commit comments

Comments
 (0)