|
| 1 | +import Logging |
| 2 | +import FluentKit |
| 3 | +import AsyncKit |
| 4 | +import NIOCore |
| 5 | +import NIOSSL |
| 6 | +import Foundation |
| 7 | +import PostgresKit |
| 8 | +import PostgresNIO |
| 9 | + |
| 10 | +// N.B.: This excessive method duplication is required to maintain the original public API, which allowed defaulting |
| 11 | +// either the encoder, decoder, or both. The "defaulting both" versions now forward to the new API, the others are here. |
| 12 | + |
| 13 | +// Factory methods accepting both encoder and decoder |
| 14 | +extension DatabaseConfigurationFactory { |
| 15 | + enum FluentPostgresError: Error { |
| 16 | + case invalidURL(String) |
| 17 | + } |
| 18 | + |
| 19 | + @available(*, deprecated, message: "Use `.postgres(url:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 20 | + public static func postgres( |
| 21 | + url: String, maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 22 | + encoder: PostgresDataEncoder, decoder: PostgresDataDecoder, sqlLogLevel: Logger.Level = .debug |
| 23 | + ) throws -> DatabaseConfigurationFactory { |
| 24 | + guard let configuration = PostgresConfiguration(url: url) else { throw FluentPostgresError.invalidURL(url) } |
| 25 | + return .postgres(configuration: configuration, |
| 26 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 27 | + encoder: encoder, decoder: decoder, sqlLogLevel: sqlLogLevel |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + @available(*, deprecated, message: "Use `.postgres(url:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 32 | + public static func postgres( |
| 33 | + url: URL, maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 34 | + encoder: PostgresDataEncoder, decoder: PostgresDataDecoder, sqlLogLevel: Logger.Level = .debug |
| 35 | + ) throws -> DatabaseConfigurationFactory { |
| 36 | + guard let configuration = PostgresConfiguration(url: url) else { throw FluentPostgresError.invalidURL(url.absoluteString) } |
| 37 | + return .postgres( configuration: configuration, |
| 38 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 39 | + encoder: encoder, decoder: decoder, sqlLogLevel: sqlLogLevel |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + @available(*, deprecated, message: "Use `.postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 44 | + public static func postgres( |
| 45 | + hostname: String, port: Int = PostgresConfiguration.ianaPortNumber, |
| 46 | + username: String, password: String, database: String? = nil, tlsConfiguration: TLSConfiguration? = nil, |
| 47 | + maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 48 | + encoder: PostgresDataEncoder, decoder: PostgresDataDecoder, sqlLogLevel: Logger.Level = .debug |
| 49 | + ) -> DatabaseConfigurationFactory { |
| 50 | + .postgres(configuration: .init( |
| 51 | + hostname: hostname, port: port, username: username, password: password, database: database, tlsConfiguration: tlsConfiguration), |
| 52 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 53 | + encoder: encoder, decoder: decoder, sqlLogLevel: sqlLogLevel |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + @available(*, deprecated, message: "Use `.postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 58 | + public static func postgres( |
| 59 | + configuration: PostgresConfiguration, |
| 60 | + maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 61 | + encoder: PostgresDataEncoder, decoder: PostgresDataDecoder, sqlLogLevel: Logger.Level = .debug |
| 62 | + ) -> DatabaseConfigurationFactory { |
| 63 | + .postgres( |
| 64 | + configuration: .init(legacyConfiguration: configuration), |
| 65 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 66 | + encodingContext: .init(jsonEncoder: TypeErasedPostgresJSONEncoder(json: encoder.json)), |
| 67 | + decodingContext: .init(jsonDecoder: TypeErasedPostgresJSONDecoder(json: decoder.json)), |
| 68 | + sqlLogLevel: sqlLogLevel |
| 69 | + ) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Factory methods accepting only encoder or only decoder. |
| 74 | +extension DatabaseConfigurationFactory { |
| 75 | + @available(*, deprecated, message: "Use `.postgres(url:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 76 | + public static func postgres( |
| 77 | + url: String, maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 78 | + encoder: PostgresDataEncoder, sqlLogLevel: Logger.Level = .debug |
| 79 | + ) throws -> DatabaseConfigurationFactory { |
| 80 | + try .postgres(url: url, |
| 81 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 82 | + encoder: encoder, decoder: .init(), sqlLogLevel: sqlLogLevel |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + @available(*, deprecated, message: "Use `.postgres(url:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 87 | + public static func postgres( |
| 88 | + url: String, maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 89 | + decoder: PostgresDataDecoder, sqlLogLevel: Logger.Level = .debug |
| 90 | + ) throws -> DatabaseConfigurationFactory { |
| 91 | + try .postgres(url: url, |
| 92 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 93 | + encoder: .init(), decoder: decoder, sqlLogLevel: sqlLogLevel |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + @available(*, deprecated, message: "Use `.postgres(url:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 98 | + public static func postgres( |
| 99 | + url: URL, maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 100 | + encoder: PostgresDataEncoder, sqlLogLevel: Logger.Level = .debug |
| 101 | + ) throws -> DatabaseConfigurationFactory { |
| 102 | + try .postgres(url: url, |
| 103 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 104 | + encoder: encoder, decoder: .init(), sqlLogLevel: sqlLogLevel |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + @available(*, deprecated, message: "Use `.postgres(url:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 109 | + public static func postgres( |
| 110 | + url: URL, maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 111 | + decoder: PostgresDataDecoder, sqlLogLevel: Logger.Level = .debug |
| 112 | + ) throws -> DatabaseConfigurationFactory { |
| 113 | + try .postgres(url: url, |
| 114 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 115 | + encoder: .init(), decoder: decoder, sqlLogLevel: sqlLogLevel |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + @available(*, deprecated, message: "Use `.postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 120 | + public static func postgres( |
| 121 | + hostname: String, port: Int = PostgresConfiguration.ianaPortNumber, |
| 122 | + username: String, password: String, database: String? = nil, tlsConfiguration: TLSConfiguration? = nil, |
| 123 | + maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 124 | + encoder: PostgresDataEncoder, sqlLogLevel: Logger.Level = .debug |
| 125 | + ) -> DatabaseConfigurationFactory { |
| 126 | + .postgres( |
| 127 | + hostname: hostname, port: port, username: username, password: password, database: database, tlsConfiguration: tlsConfiguration, |
| 128 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 129 | + encoder: encoder, decoder: .init(), sqlLogLevel: sqlLogLevel |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + @available(*, deprecated, message: "Use `.postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 134 | + public static func postgres( |
| 135 | + hostname: String, port: Int = PostgresConfiguration.ianaPortNumber, |
| 136 | + username: String, password: String, database: String? = nil, tlsConfiguration: TLSConfiguration? = nil, |
| 137 | + maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 138 | + decoder: PostgresDataDecoder, sqlLogLevel: Logger.Level = .debug |
| 139 | + ) -> DatabaseConfigurationFactory { |
| 140 | + .postgres( |
| 141 | + hostname: hostname, port: port, username: username, password: password, database: database, tlsConfiguration: tlsConfiguration, |
| 142 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 143 | + encoder: .init(), decoder: decoder, sqlLogLevel: sqlLogLevel |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + @available(*, deprecated, message: "Use `.postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 148 | + public static func postgres( |
| 149 | + configuration: PostgresConfiguration, maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 150 | + encoder: PostgresDataEncoder, sqlLogLevel: Logger.Level = .debug |
| 151 | + ) -> DatabaseConfigurationFactory { |
| 152 | + .postgres(configuration: configuration, |
| 153 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 154 | + encoder: encoder, decoder: .init(), sqlLogLevel: sqlLogLevel |
| 155 | + ) |
| 156 | + } |
| 157 | + |
| 158 | + @available(*, deprecated, message: "Use `.postgres(configuration:maxConnectionsPerEventLoop:connectionPoolTimeout:encodingContext:decodingContext:sqlLogLevel:)` instead.") |
| 159 | + public static func postgres( |
| 160 | + configuration: PostgresConfiguration, maxConnectionsPerEventLoop: Int = 1, connectionPoolTimeout: TimeAmount = .seconds(10), |
| 161 | + decoder: PostgresDataDecoder, sqlLogLevel: Logger.Level = .debug |
| 162 | + ) -> DatabaseConfigurationFactory { |
| 163 | + .postgres(configuration: configuration, |
| 164 | + maxConnectionsPerEventLoop: maxConnectionsPerEventLoop, connectionPoolTimeout: connectionPoolTimeout, |
| 165 | + encoder: .init(), decoder: decoder, sqlLogLevel: sqlLogLevel |
| 166 | + ) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +// N.B.: If you change something in these two types, update the copies in PostgresKit too. |
| 171 | +fileprivate struct TypeErasedPostgresJSONDecoder: PostgresJSONDecoder { |
| 172 | + let json: any PostgresJSONDecoder |
| 173 | + func decode<T: Decodable>(_: T.Type, from: Data) throws -> T { try self.json.decode(T.self, from: from) } |
| 174 | + func decode<T: Decodable>(_: T.Type, from: ByteBuffer) throws -> T { try self.json.decode(T.self, from: from) } |
| 175 | +} |
| 176 | + |
| 177 | +fileprivate struct TypeErasedPostgresJSONEncoder: PostgresJSONEncoder { |
| 178 | + let json: any PostgresJSONEncoder |
| 179 | + func encode<T: Encodable>(_ value: T) throws -> Data { try self.json.encode(value) } |
| 180 | + func encode<T: Encodable>(_ value: T, into: inout ByteBuffer) throws { try self.json.encode(value, into: &into) } |
| 181 | +} |
0 commit comments