@@ -2,6 +2,7 @@ import CLibMongoC
22import Foundation
33import NIO
44import NIOConcurrencyHelpers
5+ import SwiftBSON
56
67/// A connection to the database.
78internal class Connection {
@@ -77,8 +78,12 @@ internal class ConnectionPool {
7778
7879 internal static let PoolClosedError = MongoError . LogicError ( message: " ConnectionPool was already closed " )
7980
80- /// Initializes the pool using the provided `ConnectionString`.
81- internal init ( from connString: ConnectionString , executor: OperationExecutor , serverAPI: MongoServerAPI ? ) throws {
81+ /// Initializes the pool using the provided `MongoConnectionString`.
82+ internal init (
83+ from connString: MongoConnectionString ,
84+ executor: OperationExecutor ,
85+ serverAPI: MongoServerAPI ?
86+ ) throws {
8287 let poolFut = executor. execute ( on: nil ) { ( ) -> OpaquePointer in
8388 try connString. withMongocURI { uriPtr in
8489 guard let pool = mongoc_client_pool_new ( uriPtr) else {
@@ -240,16 +245,53 @@ internal class ConnectionPool {
240245 }
241246 }
242247
243- /// Retrieves the connection string used to create this pool. If SDAM has been started in libmongoc, the getters
244- /// on the returned connection string will return any values that were retrieved from TXT records . Throws an error
245- /// if the connection string cannot be retrieved.
246- internal func getConnectionString( ) throws -> ConnectionString {
248+ /// Retrieves the connection string used to create this pool. Note that options retrieved from TXT records will not
249+ /// be present in this connection string. To inspect those options, use `getConnectionStringOptions` . Throws an
250+ /// error if the connection string cannot be retrieved.
251+ internal func getConnectionString( ) throws -> MongoConnectionString {
247252 try self . withConnection { connection in
248253 try connection. withMongocConnection { connPtr in
249254 guard let uri = mongoc_client_get_uri ( connPtr) else {
250255 throw MongoError . InternalError ( message: " Couldn't retrieve client's connection string " )
251256 }
252- return ConnectionString ( copying: uri)
257+ guard let uriString = mongoc_uri_get_string ( uri) else {
258+ throw MongoError . InternalError ( message: " Couldn't retrieve URI string " )
259+ }
260+ return try MongoConnectionString ( string: String ( cString: uriString) )
261+ }
262+ }
263+ }
264+
265+ /// Retrieves the options configured on the connection string used to create this pool. If SDAM has been started in
266+ /// libmongoc, these will include any values that were retrieved from TXT records. Note that these options will not
267+ /// include `authSource`; to retrieve that value, use `getConnectionStringAuthSource`. Throws an error if the
268+ /// connection string cannot be retrieved.
269+ internal func getConnectionStringOptions( ) throws -> BSONDocument {
270+ try self . withConnection { connection in
271+ try connection. withMongocConnection { connPtr in
272+ guard let uri = mongoc_client_get_uri ( connPtr) else {
273+ throw MongoError . InternalError ( message: " Couldn't retrieve client's connection string " )
274+ }
275+ guard let options = mongoc_uri_get_options ( uri) else {
276+ return BSONDocument ( )
277+ }
278+ return BSONDocument ( copying: options)
279+ }
280+ }
281+ }
282+
283+ /// Retrieves the `authSource` configured on the connection string used to create this pool. If SDAM has been
284+ /// started in libmongoc, this will return the up-to-date value after SRV lookup.
285+ internal func getConnectionStringAuthSource( ) throws -> String ? {
286+ try self . withConnection { connection in
287+ try connection. withMongocConnection { connPtr in
288+ guard let uri = mongoc_client_get_uri ( connPtr) else {
289+ throw MongoError . InternalError ( message: " Couldn't retrieve client's connection string " )
290+ }
291+ guard let authSource = mongoc_uri_get_auth_source ( uri) else {
292+ return nil
293+ }
294+ return String ( cString: authSource)
253295 }
254296 }
255297 }
0 commit comments