Skip to content

Commit 4b6a2a4

Browse files
committed
Remove public static method
1 parent 17178f3 commit 4b6a2a4

File tree

3 files changed

+37
-80
lines changed

3 files changed

+37
-80
lines changed

src/Npgsql/NpgsqlConnectionStringBuilder.cs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ public bool TrustServerCertificate
15981598

15991599
internal void PostProcessAndValidate()
16001600
{
1601-
LoadConfigurationFromPgServiceFile(this);
1601+
LoadConfigurationFromPgServiceFile();
16021602

16031603
if (string.IsNullOrWhiteSpace(Host))
16041604
throw new ArgumentException("Host can't be null");
@@ -1705,7 +1705,7 @@ public override bool Equals(object? obj)
17051705
/// <summary>
17061706
/// Gets an <see cref="ICollection" /> containing the keys of the <see cref="NpgsqlConnectionStringBuilder"/>.
17071707
/// </summary>
1708-
public new ICollection<string> Keys => base.Keys.Cast<string>().ToArray()!;
1708+
public new ICollection<string> Keys => base.Keys.Cast<string>().ToArray();
17091709

17101710
/// <summary>
17111711
/// Gets an <see cref="ICollection" /> containing the values in the <see cref="NpgsqlConnectionStringBuilder"/>.
@@ -1763,60 +1763,41 @@ protected override void GetProperties(Hashtable propertyDescriptors)
17631763

17641764
#endregion
17651765

1766-
#region Static methods
1766+
#region Load from Postgres service file
17671767

1768-
/// <summary>
1769-
/// Loads configuration from a Postgres connection service file
1770-
/// </summary>
1771-
/// <param name="serviceName">The name of the service to load.</param>
1772-
/// <returns>A builder instance if the service is found; null otherwise</returns>
1773-
/// <exception cref="ArgumentException">if serviceName is null and the PGSERVICE environment variable is not set.</exception>
1774-
public static NpgsqlConnectionStringBuilder? FromPgServiceFile(string? serviceName = null)
1775-
{
1776-
return LoadConfigurationFromPgServiceFile(null, serviceName
1777-
?? PostgresEnvironment.Service
1778-
?? throw new ArgumentException($"{nameof(serviceName)} is mandatory when the PGSERVICE environment variable is not set.", nameof(serviceName)));
1779-
}
1780-
1781-
static NpgsqlConnectionStringBuilder? LoadConfigurationFromPgServiceFile(NpgsqlConnectionStringBuilder? source, string? serviceName = null)
1768+
void LoadConfigurationFromPgServiceFile()
17821769
{
1783-
var service = serviceName ?? source?.Service ?? PostgresEnvironment.Service;
1784-
if (service is null)
1770+
Service ??= PostgresEnvironment.Service;
1771+
if (Service is null)
17851772
{
1786-
return null;
1773+
return;
17871774
}
17881775

1789-
if (source is not null)
1790-
{
1791-
source.Service = service;
1792-
}
1776+
if (!LoadConfigurationFromIniFile(PostgresEnvironment.UserServiceFile))
1777+
LoadConfigurationFromIniFile(PostgresEnvironment.SystemServiceFile);
17931778

1794-
return LoadConfigurationFromIniFile(PostgresEnvironment.UserServiceFile)
1795-
?? LoadConfigurationFromIniFile(PostgresEnvironment.SystemServiceFile);
1796-
1797-
NpgsqlConnectionStringBuilder? LoadConfigurationFromIniFile(string? filePath)
1779+
bool LoadConfigurationFromIniFile(string? filePath)
17981780
{
17991781
if (filePath is null || !File.Exists(filePath))
18001782
{
1801-
return null;
1783+
return false;
18021784
}
18031785

18041786
var settings = ReadIniFile(filePath);
18051787
if (settings is null)
18061788
{
1807-
return null;
1789+
return false;
18081790
}
18091791

1810-
var builder = source ?? new NpgsqlConnectionStringBuilder();
18111792
foreach (var kv in settings)
18121793
{
1813-
if (!builder.Keys.Contains(kv.Key))
1794+
if (ContainsKey(kv.Key) && !Keys.Contains(kv.Key))
18141795
{
1815-
builder[kv.Key] = kv.Value;
1796+
this[kv.Key] = kv.Value;
18161797
}
18171798
}
18181799

1819-
return builder;
1800+
return true;
18201801
}
18211802

18221803
Dictionary<string, string>? ReadIniFile(string filePath)
@@ -1852,7 +1833,7 @@ protected override void GetProperties(Hashtable propertyDescriptors)
18521833
// remove the brackets
18531834
var sectionPrefix = line.Substring(1, line.Length - 2).Trim();
18541835
// Check whether it is the specified service
1855-
if (sectionPrefix == service)
1836+
if (sectionPrefix == Service)
18561837
{
18571838
settings = new Dictionary<string, string>();
18581839
}
@@ -1868,7 +1849,7 @@ protected override void GetProperties(Hashtable propertyDescriptors)
18681849

18691850
// key = value OR "value"
18701851
var separator = line.IndexOf('=');
1871-
if (separator < 0)
1852+
if (separator <= 0)
18721853
{
18731854
throw new FormatException($"Unrecognized line format: '{rawLine}'.");
18741855
}

src/Npgsql/PublicAPI.Unshipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ Npgsql.Replication.PhysicalReplicationConnection.StartReplication(NpgsqlTypes.Np
5252
Npgsql.Replication.PhysicalReplicationSlot.PhysicalReplicationSlot(string! slotName, NpgsqlTypes.NpgsqlLogSequenceNumber? restartLsn = null, uint? restartTimeline = null) -> void
5353
Npgsql.Replication.PhysicalReplicationSlot.RestartTimeline.get -> uint?
5454
override Npgsql.NpgsqlBatch.Dispose() -> void
55-
static Npgsql.NpgsqlConnectionStringBuilder.FromPgServiceFile(string? serviceName = null) -> Npgsql.NpgsqlConnectionStringBuilder?
5655
*REMOVED*static NpgsqlTypes.NpgsqlBox.Parse(string! s) -> NpgsqlTypes.NpgsqlBox
5756
*REMOVED*static NpgsqlTypes.NpgsqlCircle.Parse(string! s) -> NpgsqlTypes.NpgsqlCircle
5857
*REMOVED*static NpgsqlTypes.NpgsqlLine.Parse(string! s) -> NpgsqlTypes.NpgsqlLine

test/Npgsql.Tests/ConnectionStringBuilderTests.cs

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,13 @@ public void PgService_ViaProperty()
178178

179179
private void PgService_With(Func<string?, string?, int?, bool, NpgsqlConnectionStringBuilder> factory)
180180
{
181+
// Nominal case
181182
var builder = factory("MyService", "MyHost", null, false);
182183
builder.PostProcessAndValidate();
183184
Assert.That(builder, Is.Not.Null);
184185
Assert.That(builder.Count, Is.EqualTo(2));
185186

187+
// Missing service file is ignored
186188
var tempFile = Path.GetTempFileName();
187189
using var pgServiceFileVariable = SetEnvironmentVariable("PGSERVICEFILE", tempFile);
188190
builder = factory("MyService", "MyHost", null, false);
@@ -192,12 +194,14 @@ private void PgService_With(Func<string?, string?, int?, bool, NpgsqlConnectionS
192194

193195
try
194196
{
197+
// Comments are ignored
195198
File.WriteAllText(tempFile, "# test");
196199
builder = factory("MyService", "MyHost", null, false);
197200
builder.PostProcessAndValidate();
198201
Assert.That(builder, Is.Not.Null);
199202
Assert.That(builder.Count, Is.EqualTo(2));
200203

204+
// Other services are ignored
201205
File.WriteAllText(tempFile, """
202206
[OtherService]
203207
Host=test
@@ -208,6 +212,22 @@ private void PgService_With(Func<string?, string?, int?, bool, NpgsqlConnectionS
208212
Assert.That(builder, Is.Not.Null);
209213
Assert.That(builder.Count, Is.EqualTo(2));
210214

215+
// Unknown settings are ignored
216+
File.WriteAllText(tempFile, """
217+
[MyService]
218+
Unknown=test
219+
Port=1234
220+
""");
221+
builder = factory("MyService", "MyHost", null, false);
222+
builder.PostProcessAndValidate();
223+
builder.PostProcessAndValidate();
224+
Assert.That(builder, Is.Not.Null);
225+
Assert.That(builder.Count, Is.EqualTo(3));
226+
Assert.That(builder.Host, Is.EqualTo("MyHost"));
227+
Assert.That(builder.Port, Is.EqualTo(1234));
228+
Assert.That(builder.Service, Is.EqualTo("MyService"));
229+
230+
// Overridden settings are ignored
211231
File.WriteAllText(tempFile, """
212232
[MyService]
213233
Host=test
@@ -242,47 +262,4 @@ private void PgService_With(Func<string?, string?, int?, bool, NpgsqlConnectionS
242262
File.Delete(tempFile);
243263
}
244264
}
245-
246-
[Test]
247-
[NonParallelizable] // Sets environment variable
248-
public void FromPgServiceFile()
249-
{
250-
Assert.Throws<ArgumentException>(() => NpgsqlConnectionStringBuilder.FromPgServiceFile());
251-
252-
Assert.Null(NpgsqlConnectionStringBuilder.FromPgServiceFile("MyService"));
253-
254-
using var pgServiceVariable = SetEnvironmentVariable("PGSERVICE", "MyService");
255-
Assert.Null(NpgsqlConnectionStringBuilder.FromPgServiceFile());
256-
257-
var tempFile = Path.GetTempFileName();
258-
using var pgServiceFileVariable = SetEnvironmentVariable("PGSERVICEFILE", tempFile);
259-
Assert.Null(NpgsqlConnectionStringBuilder.FromPgServiceFile());
260-
261-
try
262-
{
263-
File.WriteAllText(tempFile, "# test");
264-
Assert.Null(NpgsqlConnectionStringBuilder.FromPgServiceFile());
265-
266-
File.WriteAllText(tempFile, """
267-
[OtherService]
268-
Host=test
269-
Port=1234
270-
""");
271-
Assert.Null(NpgsqlConnectionStringBuilder.FromPgServiceFile());
272-
273-
File.WriteAllText(tempFile, """
274-
[MyService]
275-
Host=test
276-
Port=1234
277-
""");
278-
var builder = NpgsqlConnectionStringBuilder.FromPgServiceFile();
279-
Assert.NotNull(builder);
280-
Assert.AreEqual("test", builder!.Host);
281-
Assert.AreEqual(1234, builder.Port);
282-
}
283-
finally
284-
{
285-
File.Delete(tempFile);
286-
}
287-
}
288265
}

0 commit comments

Comments
 (0)