Fix libpq parsing and refactors/code cleanup

This commit is contained in:
eelke 2025-08-31 13:11:59 +02:00
parent 0090f39910
commit 739d6bd65a
12 changed files with 234 additions and 543 deletions

View file

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Globalization;
using System.Text;
using FluentResults;
using Npgsql;
@ -222,35 +219,18 @@ public sealed class NpgsqlCodec : IConnectionStringCodec
return false;
}
private static SslMode ParseSslMode(string s)
{
switch (s.Trim().ToLowerInvariant())
{
case "disable": return SslMode.Disable;
case "allow": return SslMode.Allow;
case "prefer": return SslMode.Prefer;
case "require": return SslMode.Require;
case "verify-ca":
case "verifyca": return SslMode.VerifyCA;
case "verify-full":
case "verifyfull": return SslMode.VerifyFull;
default: throw new ArgumentException($"Not a valid SSL Mode: {s}");
}
}
private static SslMode ParseSslMode(string s) => CodecCommon.ParseSslModeLoose(s);
private static string FormatSslMode(SslMode mode)
private static string FormatSslMode(SslMode mode) => mode switch
{
return mode switch
{
SslMode.Disable => "Disable",
SslMode.Allow => "Allow",
SslMode.Prefer => "Prefer",
SslMode.Require => "Require",
SslMode.VerifyCA => "VerifyCA",
SslMode.VerifyFull => "VerifyFull",
_ => "Prefer"
};
}
SslMode.Disable => "Disable",
SslMode.Allow => "Allow",
SslMode.Prefer => "Prefer",
SslMode.Require => "Require",
SslMode.VerifyCA => "VerifyCA",
SslMode.VerifyFull => "VerifyFull",
_ => "Prefer"
};
// Npgsql/.NET connection string grammar: semicolon-separated key=value; values with special chars are wrapped in quotes, internal quotes doubled
private static string FormatPair(string key, string? value)
@ -343,37 +323,4 @@ public sealed class NpgsqlCodec : IConnectionStringCodec
return dict;
}
private sealed class ConnectionDescriptorBuilder
{
public List<HostEndpoint> Hosts { get; } = new();
public string? Database { get; set; }
public string? Username { get; set; }
public string? Password { get; set; }
public SslMode? SslMode { get; set; }
public string? ApplicationName { get; set; }
public int? TimeoutSeconds { get; set; }
public Dictionary<string, string> Properties { get; } = new(StringComparer.OrdinalIgnoreCase);
public void AddHost(string host, ushort? port)
{
if (string.IsNullOrWhiteSpace(host)) return;
Hosts.Add(new HostEndpoint { Host = host.Trim(), Port = port });
}
public ConnectionDescriptor Build()
{
return new ConnectionDescriptor
{
Hosts = Hosts,
Database = Database,
Username = Username,
Password = Password,
SslMode = SslMode,
ApplicationName = ApplicationName,
TimeoutSeconds = TimeoutSeconds,
Properties = Properties
};
}
}
}