@@ -525,6 +525,87 @@ final class AsyncPostgresConnectionTests: XCTestCase {
525525 XCTFail ( " Unexpected error: \( String ( describing: error) ) " )
526526 }
527527 }
528+
529+ static let preparedStatementWithOptionalTestTable = " AsyncTestPreparedStatementWithOptionalTestTable "
530+ func testPreparedStatementWithOptionalBinding( ) async throws {
531+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
532+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
533+ let eventLoop = eventLoopGroup. next ( )
534+
535+ struct InsertPreparedStatement : PostgresPreparedStatement {
536+ static let name = " INSERT-AsyncTestPreparedStatementWithOptionalTestTable "
537+
538+ static let sql = #"INSERT INTO " \#( AsyncPostgresConnectionTests . preparedStatementWithOptionalTestTable) " (uuid) VALUES ($1);"#
539+ typealias Row = ( )
540+
541+ var uuid : UUID ?
542+
543+ func makeBindings( ) -> PostgresBindings {
544+ var bindings = PostgresBindings ( )
545+ bindings. append ( self . uuid)
546+ return bindings
547+ }
548+
549+ func decodeRow( _ row: PostgresNIO . PostgresRow ) throws -> Row {
550+ ( )
551+ }
552+ }
553+
554+ struct SelectPreparedStatement : PostgresPreparedStatement {
555+ static let name = " SELECT-AsyncTestPreparedStatementWithOptionalTestTable "
556+
557+ static let sql = #"SELECT id, uuid FROM " \#( AsyncPostgresConnectionTests . preparedStatementWithOptionalTestTable) " WHERE id <= $1;"#
558+ typealias Row = ( Int , UUID ? )
559+
560+ var id : Int
561+
562+ func makeBindings( ) -> PostgresBindings {
563+ var bindings = PostgresBindings ( )
564+ bindings. append ( self . id)
565+ return bindings
566+ }
567+
568+ func decodeRow( _ row: PostgresNIO . PostgresRow ) throws -> Row {
569+ try row. decode ( ( Int, UUID? ) . self)
570+ }
571+ }
572+
573+ do {
574+ try await withTestConnection ( on: eventLoop) { connection in
575+ try await connection. query ( """
576+ CREATE TABLE IF NOT EXISTS " \( unescaped: Self . preparedStatementWithOptionalTestTable) " (
577+ id SERIAL PRIMARY KEY,
578+ uuid UUID
579+ )
580+ """ ,
581+ logger: . psqlTest
582+ )
583+
584+ _ = try await connection. execute ( InsertPreparedStatement ( uuid: nil ) , logger: . psqlTest)
585+ _ = try await connection. execute ( InsertPreparedStatement ( uuid: . init( ) ) , logger: . psqlTest)
586+ _ = try await connection. execute ( InsertPreparedStatement ( uuid: nil ) , logger: . psqlTest)
587+ _ = try await connection. execute ( InsertPreparedStatement ( uuid: . init( ) ) , logger: . psqlTest)
588+ _ = try await connection. execute ( InsertPreparedStatement ( uuid: nil ) , logger: . psqlTest)
589+
590+ let rows = try await connection. execute ( SelectPreparedStatement ( id: 3 ) , logger: . psqlTest)
591+ var counter = 0
592+ for try await (id, uuid) in rows {
593+ Logger . psqlTest. info ( " Received row " , metadata: [
594+ " id " : " \( id) " , " uuid " : " \( String ( describing: uuid) ) "
595+ ] )
596+ counter += 1
597+ }
598+
599+ try await connection. query ( """
600+ DROP TABLE " \( unescaped: Self . preparedStatementWithOptionalTestTable) " ;
601+ """ ,
602+ logger: . psqlTest
603+ )
604+ }
605+ } catch {
606+ XCTFail ( " Unexpected error: \( String ( describing: error) ) " )
607+ }
608+ }
528609}
529610
530611extension XCTestCase {
0 commit comments