Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prepare(query:) when no data returned #123

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extension PostgresDatabase {
let name = "nio-postgres-\(UUID().uuidString)"
let prepare = PrepareQueryRequest(query, as: name)
return self.send(prepare, logger: self.logger).map { () -> (PreparedQuery) in
let prepared = PreparedQuery(database: self, name: name, rowDescription: prepare.rowLookupTable!)
let prepared = PreparedQuery(database: self, name: name, rowDescription: prepare.rowLookupTable)
return prepared
}
}
Expand All @@ -25,9 +25,9 @@ extension PostgresDatabase {
public struct PreparedQuery {
let database: PostgresDatabase
let name: String
let rowLookupTable: PostgresRow.LookupTable
let rowLookupTable: PostgresRow.LookupTable?

init(database: PostgresDatabase, name: String, rowDescription: PostgresRow.LookupTable) {
init(database: PostgresDatabase, name: String, rowDescription: PostgresRow.LookupTable?) {
self.database = database
self.name = name
self.rowLookupTable = rowDescription
Expand Down Expand Up @@ -74,6 +74,8 @@ private final class PrepareQueryRequest: PostgresRequest {
resultFormat: self.resultFormatCodes
)
return []
case .noData:
return []
case .parseComplete, .parameterDescription:
return []
case .readyForQuery:
Expand Down Expand Up @@ -125,7 +127,10 @@ private final class ExecutePreparedQuery: PostgresRequest {
return []
case .dataRow:
let data = try PostgresMessage.DataRow(message: message)
let row = PostgresRow(dataRow: data, lookupTable: query.rowLookupTable)
guard let rowLookupTable = query.rowLookupTable else {
fatalError("row lookup was requested but never set")
}
let row = PostgresRow(dataRow: data, lookupTable: rowLookupTable)
try onRow(row)
return []
case .noData:
Expand Down
18 changes: 18 additions & 0 deletions Tests/PostgresNIOTests/PostgresNIOTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,24 @@ final class PostgresNIOTests: XCTestCase {
XCTAssertEqual(rows[2][0].column("foo")?.string, "c")
}

// https://github.com/vapor/postgres-nio/issues/122
func testPreparedQueryNoResults() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }

_ = try conn.simpleQuery("DROP TABLE IF EXISTS \"table_no_results\"").wait()
_ = try conn.simpleQuery("""
CREATE TABLE table_no_results (
"id" int8 NOT NULL,
"stringValue" text,
PRIMARY KEY ("id")
);
""").wait()
defer { _ = try! conn.simpleQuery("DROP TABLE \"table_no_results\"").wait() }

_ = try conn.prepare(query: "DELETE FROM \"table_no_results\" WHERE id = $1").wait()
}


// https://github.com/vapor/postgres-nio/issues/71
func testChar1Serialization() throws {
Expand Down