@@ -476,6 +476,87 @@ final class AsyncPostgresConnectionTests: XCTestCase {
476
476
XCTFail ( " Unexpected error: \( String ( describing: error) ) " )
477
477
}
478
478
}
479
+
480
+ static let preparedStatementWithOptionalTestTable = " AsyncTestPreparedStatementWithOptionalTestTable "
481
+ func testPreparedStatementWithOptionalBinding( ) async throws {
482
+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
483
+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
484
+ let eventLoop = eventLoopGroup. next ( )
485
+
486
+ struct InsertPreparedStatement : PostgresPreparedStatement {
487
+ static let name = " INSERT-AsyncTestPreparedStatementWithOptionalTestTable "
488
+
489
+ static let sql = #"INSERT INTO " \#( AsyncPostgresConnectionTests . preparedStatementWithOptionalTestTable) " (uuid) VALUES ($1);"#
490
+ typealias Row = ( )
491
+
492
+ var uuid : UUID ?
493
+
494
+ func makeBindings( ) -> PostgresBindings {
495
+ var bindings = PostgresBindings ( )
496
+ bindings. append ( self . uuid)
497
+ return bindings
498
+ }
499
+
500
+ func decodeRow( _ row: PostgresNIO . PostgresRow ) throws -> Row {
501
+ ( )
502
+ }
503
+ }
504
+
505
+ struct SelectPreparedStatement : PostgresPreparedStatement {
506
+ static let name = " SELECT-AsyncTestPreparedStatementWithOptionalTestTable "
507
+
508
+ static let sql = #"SELECT id, uuid FROM " \#( AsyncPostgresConnectionTests . preparedStatementWithOptionalTestTable) " WHERE id <= $1;"#
509
+ typealias Row = ( Int , UUID ? )
510
+
511
+ var id : Int
512
+
513
+ func makeBindings( ) -> PostgresBindings {
514
+ var bindings = PostgresBindings ( )
515
+ bindings. append ( self . id)
516
+ return bindings
517
+ }
518
+
519
+ func decodeRow( _ row: PostgresNIO . PostgresRow ) throws -> Row {
520
+ try row. decode ( ( Int, UUID? ) . self)
521
+ }
522
+ }
523
+
524
+ do {
525
+ try await withTestConnection ( on: eventLoop) { connection in
526
+ try await connection. query ( """
527
+ CREATE TABLE IF NOT EXISTS " \( unescaped: Self . preparedStatementWithOptionalTestTable) " (
528
+ id SERIAL PRIMARY KEY,
529
+ uuid UUID
530
+ )
531
+ """ ,
532
+ logger: . psqlTest
533
+ )
534
+
535
+ _ = try await connection. execute ( InsertPreparedStatement ( uuid: nil ) , logger: . psqlTest)
536
+ _ = try await connection. execute ( InsertPreparedStatement ( uuid: . init( ) ) , logger: . psqlTest)
537
+ _ = try await connection. execute ( InsertPreparedStatement ( uuid: nil ) , logger: . psqlTest)
538
+ _ = try await connection. execute ( InsertPreparedStatement ( uuid: . init( ) ) , logger: . psqlTest)
539
+ _ = try await connection. execute ( InsertPreparedStatement ( uuid: nil ) , logger: . psqlTest)
540
+
541
+ let rows = try await connection. execute ( SelectPreparedStatement ( id: 3 ) , logger: . psqlTest)
542
+ var counter = 0
543
+ for try await (id, uuid) in rows {
544
+ Logger . psqlTest. info ( " Received row " , metadata: [
545
+ " id " : " \( id) " , " uuid " : " \( String ( describing: uuid) ) "
546
+ ] )
547
+ counter += 1
548
+ }
549
+
550
+ try await connection. query ( """
551
+ DROP TABLE " \( unescaped: Self . preparedStatementWithOptionalTestTable) " ;
552
+ """ ,
553
+ logger: . psqlTest
554
+ )
555
+ }
556
+ } catch {
557
+ XCTFail ( " Unexpected error: \( String ( describing: error) ) " )
558
+ }
559
+ }
479
560
}
480
561
481
562
extension XCTestCase {
0 commit comments