|
| 1 | +using Npgsql; |
| 2 | + |
| 3 | +namespace NpgsqlRest; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Helper methods for creating and opening database connections. |
| 7 | +/// </summary> |
| 8 | +public static class ConnectionHelper |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Creates and opens a connection using the standard resolution order: |
| 12 | + /// 1. Named DataSource (if connectionName provided) |
| 13 | + /// 2. Named ConnectionString (if connectionName provided) |
| 14 | + /// 3. Default DataSource |
| 15 | + /// 4. Default ConnectionString |
| 16 | + /// </summary> |
| 17 | + /// <param name="options">NpgsqlRest options containing connection configuration</param> |
| 18 | + /// <param name="connectionName">Optional connection name for named DataSource or ConnectionString lookup</param> |
| 19 | + /// <param name="loggingMode">Mode for logging PostgreSQL NOTICE events</param> |
| 20 | + /// <param name="cancellationToken">Cancellation token</param> |
| 21 | + /// <param name="logger">Optional logger for retry logging</param> |
| 22 | + /// <returns>An opened NpgsqlConnection</returns> |
| 23 | + /// <exception cref="InvalidOperationException"> |
| 24 | + /// Thrown when connectionName is specified but not found, or when no default connection is configured |
| 25 | + /// </exception> |
| 26 | + public static async Task<NpgsqlConnection> OpenConnectionAsync( |
| 27 | + NpgsqlRestOptions options, |
| 28 | + string? connectionName, |
| 29 | + PostgresConnectionNoticeLoggingMode loggingMode, |
| 30 | + CancellationToken cancellationToken, |
| 31 | + ILogger? logger = null) |
| 32 | + { |
| 33 | + NpgsqlConnection connection; |
| 34 | + |
| 35 | + if (connectionName is not null) |
| 36 | + { |
| 37 | + // Named DataSource lookup |
| 38 | + if (options.DataSources?.TryGetValue(connectionName, out var namedDataSource) is true) |
| 39 | + { |
| 40 | + connection = namedDataSource.CreateConnection(); |
| 41 | + } |
| 42 | + // Named ConnectionString lookup |
| 43 | + else if (options.ConnectionStrings?.TryGetValue(connectionName, out var connString) is true) |
| 44 | + { |
| 45 | + connection = new NpgsqlConnection(connString); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + throw new InvalidOperationException( |
| 50 | + $"Connection name '{connectionName}' not found in DataSources or ConnectionStrings"); |
| 51 | + } |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + // Default DataSource |
| 56 | + if (options.DataSource is not null) |
| 57 | + { |
| 58 | + connection = options.DataSource.CreateConnection(); |
| 59 | + } |
| 60 | + // Default ConnectionString |
| 61 | + else if (!string.IsNullOrEmpty(options.ConnectionString)) |
| 62 | + { |
| 63 | + connection = new NpgsqlConnection(options.ConnectionString); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + throw new InvalidOperationException( |
| 68 | + "No DataSource or ConnectionString configured"); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Setup notice logging |
| 73 | + if (options.LogConnectionNoticeEvents) |
| 74 | + { |
| 75 | + connection.Notice += (sender, args) => |
| 76 | + { |
| 77 | + NpgsqlRestLogger.LogConnectionNotice(args.Notice, loggingMode); |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + // Open with retry |
| 82 | + await connection.OpenRetryAsync(options.ConnectionRetryOptions, cancellationToken, logger); |
| 83 | + |
| 84 | + return connection; |
| 85 | + } |
| 86 | +} |
0 commit comments