Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions Sources/PostgresNIO/Data/PostgresData+Numeric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,16 +268,13 @@ private extension Collection {
// splits the collection into chunks of the supplied size
// if the collection is not evenly divisible, the first chunk will be smaller
func reverseChunked(by maxSize: Int) -> [SubSequence] {
var lastDistance = 0
let firstChunkSize = self.count % maxSize
if firstChunkSize == 0 {
return self.chunked(by: maxSize)
}
var chunkStartIndex = self.startIndex
return stride(from: 0, to: self.count, by: maxSize).reversed().map { current in
let distance = (self.count - current) - lastDistance
lastDistance = distance
let chunkEndOffset = Swift.min(
self.distance(from: chunkStartIndex, to: self.endIndex),
distance
)
let chunkEndIndex = self.index(chunkStartIndex, offsetBy: chunkEndOffset)
return stride(from: firstChunkSize, through: self.count, by: maxSize).map { current in
let chunkEndIndex = self.index(self.startIndex, offsetBy: current)
defer { chunkStartIndex = chunkEndIndex }
return self[chunkStartIndex..<chunkEndIndex]
}
Expand Down
8 changes: 6 additions & 2 deletions Tests/IntegrationTests/PostgresNIOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -463,20 +463,24 @@ final class PostgresNIOTests: XCTestCase {
let a = PostgresNumeric(string: "123456.789123")!
let b = PostgresNumeric(string: "-123456.789123")!
let c = PostgresNumeric(string: "3.14159265358979")!
let d = PostgresNumeric(string: "1234567890123")!
var rows: PostgresQueryResult?
XCTAssertNoThrow(rows = try conn?.query("""
select
$1::numeric::text as a,
$2::numeric::text as b,
$3::numeric::text as c
$3::numeric::text as c,
$4::numeric::text as d
""", [
.init(numeric: a),
.init(numeric: b),
.init(numeric: c)
.init(numeric: c),
.init(numeric: d)
]).wait())
XCTAssertEqual(rows?.first?.column("a")?.string, "123456.789123")
XCTAssertEqual(rows?.first?.column("b")?.string, "-123456.789123")
XCTAssertEqual(rows?.first?.column("c")?.string, "3.14159265358979")
XCTAssertEqual(rows?.first?.column("d")?.string, "1234567890123")
}

func testMoney() {
Expand Down