|
| 1 | +using System; |
| 2 | +using System.Data; |
| 3 | +using DbUp.Engine.Output; |
| 4 | +using DbUp.Engine.Transactions; |
| 5 | +using Npgsql; |
| 6 | + |
| 7 | +namespace DbUp.Postgresql; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// A connection factory that uses Npgsql's data source pattern to create PostgreSQL database connections. |
| 11 | +/// This factory provides better performance and resource management compared to traditional connection strings |
| 12 | +/// by reusing configured data sources and connection pooling. |
| 13 | +/// </summary> |
| 14 | +internal class DataSourceConnectionFactory : IConnectionFactory |
| 15 | +{ |
| 16 | + |
| 17 | + private readonly NpgsqlDataSource dataSource; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Initializes a new instance of the <see cref="DataSourceConnectionFactory"/> class. |
| 21 | + /// </summary> |
| 22 | + /// <param name="connectionString">The PostgreSQL connection string used to configure the data source.</param> |
| 23 | + /// <param name="connectionOptions">Additional connection options including SSL certificate configuration.</param> |
| 24 | + /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="connectionString"/> or <paramref name="connectionOptions"/> is null.</exception> |
| 25 | + /// <exception cref="System.ArgumentException">Thrown when <paramref name="connectionString"/> is empty or invalid.</exception> |
| 26 | + public DataSourceConnectionFactory(string connectionString, PostgresqlConnectionOptions connectionOptions) |
| 27 | + { |
| 28 | + if (connectionString == null) |
| 29 | + { |
| 30 | + throw new ArgumentNullException(nameof(connectionString)); |
| 31 | + } |
| 32 | + if (string.IsNullOrEmpty(connectionString)) |
| 33 | + { |
| 34 | + throw new ArgumentException("Connection string cannot be empty.", nameof(connectionString)); |
| 35 | + } |
| 36 | + if (connectionOptions == null) |
| 37 | + { |
| 38 | + throw new ArgumentNullException(nameof(connectionOptions)); |
| 39 | + } |
| 40 | + var builder = new NpgsqlDataSourceBuilder(connectionString); |
| 41 | + |
| 42 | +#if NET8_0_OR_GREATER |
| 43 | + // Use the new SSL authentication callback API for .NET 8.0 with Npgsql 9 |
| 44 | + if (connectionOptions.ClientCertificate != null || connectionOptions.UserCertificateValidationCallback != null) |
| 45 | + { |
| 46 | + builder.UseSslClientAuthenticationOptionsCallback(options => |
| 47 | + { |
| 48 | + if (connectionOptions.ClientCertificate != null) |
| 49 | + { |
| 50 | + options.ClientCertificates = new System.Security.Cryptography.X509Certificates.X509CertificateCollection |
| 51 | + { |
| 52 | + connectionOptions.ClientCertificate |
| 53 | + }; |
| 54 | + } |
| 55 | + if (connectionOptions.UserCertificateValidationCallback != null) |
| 56 | + { |
| 57 | + options.RemoteCertificateValidationCallback = connectionOptions.UserCertificateValidationCallback; |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | +#else |
| 62 | + // Use legacy API for netstandard2.0 with Npgsql 8 |
| 63 | + if (connectionOptions.ClientCertificate != null) |
| 64 | + { |
| 65 | + builder.UseClientCertificate(connectionOptions.ClientCertificate); |
| 66 | + } |
| 67 | + if (connectionOptions.UserCertificateValidationCallback != null) |
| 68 | + { |
| 69 | + builder.UseUserCertificateValidationCallback(connectionOptions.UserCertificateValidationCallback); |
| 70 | + } |
| 71 | +#endif |
| 72 | + dataSource = builder.Build(); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Creates a new database connection using the configured data source. |
| 77 | + /// </summary> |
| 78 | + /// <param name="upgradeLog">The upgrade log for recording connection-related messages. This parameter is not used in this implementation.</param> |
| 79 | + /// <param name="databaseConnectionManager">The database connection manager. This parameter is not used in this implementation.</param> |
| 80 | + /// <returns>A new <see cref="IDbConnection"/> instance ready for use.</returns> |
| 81 | + /// <remarks> |
| 82 | + /// The returned connection is not automatically opened. The caller is responsible for opening and properly disposing of the connection. |
| 83 | + /// The connection benefits from the data source's connection pooling and configuration reuse. |
| 84 | + /// </remarks> |
| 85 | + public IDbConnection CreateConnection(IUpgradeLog upgradeLog, DatabaseConnectionManager databaseConnectionManager) => dataSource.CreateConnection(); |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Creates a new database connection using the configured data source. |
| 89 | + /// Simpler implementation of <see cref="CreateConnection(IUpgradeLog, DatabaseConnectionManager)"/> for internal use. |
| 90 | + /// </summary> |
| 91 | + /// <returns>A new <see cref="IDbConnection"/> instance ready for use.</returns> |
| 92 | + internal NpgsqlConnection CreateConnection() => dataSource.CreateConnection(); |
| 93 | +} |
0 commit comments