@@ -329,4 +329,60 @@ final class IntegrationTests: XCTestCase {
329329 XCTAssertEqual ( obj? . bar, 2 )
330330 }
331331 }
332+
333+ #if swift(>=5.5.2)
334+ func testBindMaximumParameters( ) async throws {
335+ let eventLoopGroup = MultiThreadedEventLoopGroup ( numberOfThreads: 1 )
336+ defer { XCTAssertNoThrow ( try eventLoopGroup. syncShutdownGracefully ( ) ) }
337+ let eventLoop = eventLoopGroup. next ( )
338+
339+ try await withTestConnection ( on: eventLoop) { connection in
340+ // Max binds limit is UInt16.max which is 65535 which is 3 * 5 * 17 * 257
341+ // Max columns limit is 1664, so we will only make 5 * 257 columns which is less
342+ // Then we will insert 3 * 17 rows
343+ // In the insertion, there will be a total of 3 * 17 * 5 * 257 == UInt16.max bindings
344+ // If the test is successful, it means Postgres supports UInt16.max bindings
345+ let columnsCount = 5 * 257
346+ let rowsCount = 3 * 17
347+
348+ let createQuery = PostgresQuery (
349+ unsafeSQL: """
350+ CREATE TABLE table1 (
351+ \( ( 0 ..< columnsCount) . map ( { #""int \#( $0) " int NOT NULL"# } ) . joined ( separator: " , " ) )
352+ );
353+ """
354+ )
355+ try await connection. query ( createQuery, logger: . psqlTest)
356+
357+ var binds = PostgresBindings ( capacity: Int ( UInt16 . max) )
358+ for _ in ( 0 ..< rowsCount) {
359+ for num in ( 0 ..< columnsCount) {
360+ try binds. append ( num, context: . default)
361+ }
362+ }
363+ XCTAssertEqual ( binds. count, Int ( UInt16 . max) )
364+
365+ let insertionValues = ( 0 ..< rowsCount) . map { rowIndex in
366+ let indices = ( 0 ..< columnsCount) . map { columnIndex -> String in
367+ " $ \( rowIndex * columnsCount + columnIndex + 1 ) "
368+ }
369+ return " ( \( indices. joined ( separator: " , " ) ) ) "
370+ } . joined ( separator: " , " )
371+ let insertionQuery = PostgresQuery (
372+ unsafeSQL: " INSERT INTO table1 VALUES \( insertionValues) " ,
373+ binds: binds
374+ )
375+ try await connection. query ( insertionQuery, logger: . psqlTest)
376+
377+ let countQuery = PostgresQuery ( unsafeSQL: " SELECT COUNT(*) FROM table1 " )
378+ let countRows = try await connection. query ( countQuery, logger: . psqlTest)
379+ var countIterator = countRows. makeAsyncIterator ( )
380+ let insertedRowsCount = try await countIterator. next ( ) ? . decode ( Int . self, context: . default)
381+ XCTAssertEqual ( rowsCount, insertedRowsCount)
382+
383+ let dropQuery = PostgresQuery ( unsafeSQL: " DROP TABLE table1 " )
384+ try await connection. query ( dropQuery, logger: . psqlTest)
385+ }
386+ }
387+ #endif
332388}
0 commit comments