76 lines
2.2 KiB
C#
76 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using FluentResults;
|
|
using Npgsql;
|
|
|
|
namespace pgLabII.PgUtils.ConnectionStrings;
|
|
|
|
public enum ConnStringFormat
|
|
{
|
|
Libpq,
|
|
Npgsql,
|
|
Url,
|
|
Jdbc
|
|
}
|
|
|
|
public sealed class HostEndpoint
|
|
{
|
|
public string Host { get; init; } = string.Empty;
|
|
public ushort? Port { get; init; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Canonical, format-agnostic representation of a PostgreSQL connection.
|
|
/// Keep minimal fields for broad interoperability; store extras in Properties.
|
|
/// </summary>
|
|
public sealed class ConnectionDescriptor
|
|
{
|
|
public string? Name { get; init; }
|
|
|
|
// Primary hosts (support multi-host). If empty, implies localhost default.
|
|
public IReadOnlyList<HostEndpoint> Hosts { get; init; } = new List<HostEndpoint>();
|
|
|
|
public string? Database { get; init; }
|
|
public string? Username { get; init; }
|
|
public string? Password { get; init; }
|
|
|
|
public SslMode? SslMode { get; init; }
|
|
|
|
// Common optional fields
|
|
public string? ApplicationName { get; init; }
|
|
public int? TimeoutSeconds { get; init; } // connect_timeout
|
|
|
|
// Additional parameters preserved across conversions
|
|
public IReadOnlyDictionary<string, string> Properties { get; init; } =
|
|
new Dictionary<string, string>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Codec for a specific connection string format (parse and format only for its own format).
|
|
/// Do not implement format specifics yet; provide interface only.
|
|
/// </summary>
|
|
public interface IConnectionStringCodec
|
|
{
|
|
ConnStringFormat Format { get; }
|
|
string FormatName { get; }
|
|
|
|
// Parse input in this codec's format into a descriptor.
|
|
Result<ConnectionDescriptor> TryParse(string input);
|
|
|
|
// Format a descriptor into this codec's format.
|
|
Result<string> TryFormat(ConnectionDescriptor descriptor);
|
|
}
|
|
|
|
/// <summary>
|
|
/// High-level service to detect, parse, format and convert between formats.
|
|
/// Implementations will compose specific codecs.
|
|
/// </summary>
|
|
public interface IConnectionStringService
|
|
{
|
|
Result<ConnStringFormat> DetectFormat(string input);
|
|
|
|
Result<ConnectionDescriptor> ParseToDescriptor(string input);
|
|
|
|
Result<string> FormatFromDescriptor(ConnectionDescriptor descriptor, ConnStringFormat targetFormat);
|
|
|
|
Result<string> Convert(string input, ConnStringFormat targetFormat);
|
|
}
|