@@ -5,7 +5,7 @@ import PostgresNIO
55import NIOTransportServices
66#endif
77
8- #if swift(>=5.5.2 )
8+ #if canImport(_Concurrency )
99final class AsyncPostgresConnectionTests : XCTestCase {
1010
1111 func test1kRoundTrips( ) async throws {
@@ -64,6 +64,60 @@ final class AsyncPostgresConnectionTests: XCTestCase {
6464 }
6565 }
6666
67+ func testBindMaximumParameters( ) async throws {
68+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
69+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
70+ let eventLoop = eventLoopGroup. next ( )
71+
72+ try await withTestConnection ( on: eventLoop) { connection in
73+ // Max binds limit is UInt16.max which is 65535 which is 3 * 5 * 17 * 257
74+ // Max columns limit is 1664, so we will only make 5 * 257 columns which is less
75+ // Then we will insert 3 * 17 rows
76+ // In the insertion, there will be a total of 3 * 17 * 5 * 257 == UInt16.max bindings
77+ // If the test is successful, it means Postgres supports UInt16.max bindings
78+ let columnsCount = 5 * 257
79+ let rowsCount = 3 * 17
80+
81+ let createQuery = PostgresQuery (
82+ unsafeSQL: """
83+ CREATE TABLE table1 (
84+ \( ( 0 ..< columnsCount) . map ( { #""int \#( $0) " int NOT NULL"# } ) . joined ( separator: " , " ) )
85+ );
86+ """
87+ )
88+ try await connection. query ( createQuery, logger: . psqlTest)
89+
90+ var binds = PostgresBindings ( capacity: Int ( UInt16 . max) )
91+ for _ in ( 0 ..< rowsCount) {
92+ for num in ( 0 ..< columnsCount) {
93+ try binds. append ( num, context: . default)
94+ }
95+ }
96+ XCTAssertEqual ( binds. count, Int ( UInt16 . max) )
97+
98+ let insertionValues = ( 0 ..< rowsCount) . map { rowIndex in
99+ let indices = ( 0 ..< columnsCount) . map { columnIndex -> String in
100+ " $ \( rowIndex * columnsCount + columnIndex + 1 ) "
101+ }
102+ return " ( \( indices. joined ( separator: " , " ) ) ) "
103+ } . joined ( separator: " , " )
104+ let insertionQuery = PostgresQuery (
105+ unsafeSQL: " INSERT INTO table1 VALUES \( insertionValues) " ,
106+ binds: binds
107+ )
108+ try await connection. query ( insertionQuery, logger: . psqlTest)
109+
110+ let countQuery = PostgresQuery ( unsafeSQL: " SELECT COUNT(*) FROM table1 " )
111+ let countRows = try await connection. query ( countQuery, logger: . psqlTest)
112+ var countIterator = countRows. makeAsyncIterator ( )
113+ let insertedRowsCount = try await countIterator. next ( ) ? . decode ( Int . self, context: . default)
114+ XCTAssertEqual ( rowsCount, insertedRowsCount)
115+
116+ let dropQuery = PostgresQuery ( unsafeSQL: " DROP TABLE table1 " )
117+ try await connection. query ( dropQuery, logger: . psqlTest)
118+ }
119+ }
120+
67121 #if canImport(Network)
68122 func testSelect10kRowsNetworkFramework( ) async throws {
69123 let eventLoopGroup = NIOTSEventLoopGroup ( )
0 commit comments