Skip to content

Commit 125c70a

Browse files
committed
Address review comments
1 parent 1beeeab commit 125c70a

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

Sources/PostgresNIO/Connection/PostgresConnection+CopyFrom.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public struct PostgresCopyFromWriter: Sendable {
1313
let promise = eventLoop.makePromise(of: Void.self)
1414
self.channelHandler.value.checkBackendCanReceiveCopyData(promise: promise)
1515
promise.futureResult.flatMap {
16-
if eventLoop.inEventLoop {
17-
return eventLoop.makeCompletedFuture(withResultOf: {
16+
if self.eventLoop.inEventLoop {
17+
return self.eventLoop.makeCompletedFuture(withResultOf: {
1818
try self.channelHandler.value.sendCopyData(byteBuffer)
1919
})
2020
} else {
21-
let promise = eventLoop.makePromise(of: Void.self)
22-
eventLoop.execute {
21+
let promise = self.eventLoop.makePromise(of: Void.self)
22+
self.eventLoop.execute {
2323
promise.completeWith(Result(catching: { try self.channelHandler.value.sendCopyData(byteBuffer) }))
2424
}
2525
return promise.futureResult
@@ -31,7 +31,7 @@ public struct PostgresCopyFromWriter: Sendable {
3131

3232
/// Send data for a `COPY ... FROM STDIN` operation to the backend.
3333
///
34-
/// - Throws: If an error occurs during the write of if the backend sent an `ErrorResponse` during the copy
34+
/// - Throws: If an error occurs during the write of if the backend sent an `ErrorResponse` during the copy
3535
/// operation, eg. to indicate that a **previous** `write` call had an invalid format.
3636
public func write(_ byteBuffer: ByteBuffer, isolation: isolated (any Actor)? = #isolation) async throws {
3737
// Check for cancellation. This is cheap and makes sure that we regularly check for cancellation in the
@@ -41,10 +41,10 @@ public struct PostgresCopyFromWriter: Sendable {
4141
try await withTaskCancellationHandler {
4242
do {
4343
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
44-
if eventLoop.inEventLoop {
44+
if self.eventLoop.inEventLoop {
4545
writeAssumingInEventLoop(byteBuffer, continuation)
4646
} else {
47-
eventLoop.execute {
47+
self.eventLoop.execute {
4848
writeAssumingInEventLoop(byteBuffer, continuation)
4949
}
5050
}
@@ -59,10 +59,10 @@ public struct PostgresCopyFromWriter: Sendable {
5959
throw error
6060
}
6161
} onCancel: {
62-
if eventLoop.inEventLoop {
62+
if self.eventLoop.inEventLoop {
6363
self.channelHandler.value.cancel()
6464
} else {
65-
eventLoop.execute {
65+
self.eventLoop.execute {
6666
self.channelHandler.value.cancel()
6767
}
6868
}
@@ -73,10 +73,10 @@ public struct PostgresCopyFromWriter: Sendable {
7373
/// the backend.
7474
func done(isolation: isolated (any Actor)? = #isolation) async throws {
7575
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
76-
if eventLoop.inEventLoop {
76+
if self.eventLoop.inEventLoop {
7777
self.channelHandler.value.sendCopyDone(continuation: continuation)
7878
} else {
79-
eventLoop.execute {
79+
self.eventLoop.execute {
8080
self.channelHandler.value.sendCopyDone(continuation: continuation)
8181
}
8282
}
@@ -87,10 +87,10 @@ public struct PostgresCopyFromWriter: Sendable {
8787
/// the backend.
8888
func failed(error: any Error, isolation: isolated (any Actor)? = #isolation) async throws {
8989
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, any Error>) in
90-
if eventLoop.inEventLoop {
90+
if self.eventLoop.inEventLoop {
9191
self.channelHandler.value.sendCopyFail(message: "Client failed copy", continuation: continuation)
9292
} else {
93-
eventLoop.execute {
93+
self.eventLoop.execute {
9494
self.channelHandler.value.sendCopyFail(message: "Client failed copy", continuation: continuation)
9595
}
9696
}
@@ -99,7 +99,7 @@ public struct PostgresCopyFromWriter: Sendable {
9999
}
100100

101101
/// Specifies the format in which data is transferred to the backend in a COPY operation.
102-
///
102+
///
103103
/// See the Postgres documentation at https://www.postgresql.org/docs/current/sql-copy.html for the option's meanings
104104
/// and their default values.
105105
public struct PostgresCopyFromFormat: Sendable {
@@ -129,7 +129,7 @@ public struct PostgresCopyFromFormat: Sendable {
129129
/// An empty `columns` array signifies that no columns should be specified in the query and that all columns will be
130130
/// copied by the caller.
131131
///
132-
/// - Important: The table and column names are inserted into the `COPY FROM` query as passed and might thus be
132+
/// - Warning: The table and column names are inserted into the `COPY FROM` query as passed and might thus be
133133
/// susceptible to SQL injection. Ensure no untrusted data is contained in these strings.
134134
private func buildCopyFromQuery(
135135
table: String,
@@ -141,7 +141,7 @@ private func buildCopyFromQuery(
141141
"""
142142
if !columns.isEmpty {
143143
query += "("
144-
query += columns.map { #"""# + $0.description + #"""# }.joined(separator: ",")
144+
query += columns.map { #""\#($0)""# }.joined(separator: ",")
145145
query += ")"
146146
}
147147
query += " FROM STDIN"
@@ -207,9 +207,9 @@ extension PostgresConnection {
207207
// threw instead of the one that got relayed back, so it's better to ignore the error here.
208208
// - The backend sent us an `ErrorResponse` during the copy, eg. because of an invalid format. This puts
209209
// the `ExtendedQueryStateMachine` in the error state. Trying to send a `CopyFail` will throw but trigger
210-
// a `Sync` that takes the backend out of copy mode. If `writeData` threw the error from from the
211-
// `PostgresCopyFromWriter.write` call, `writer.failed` will throw with the same error, so it doesn't
212-
// matter that we ignore the error here. If the user threw some other error, it's better to honor the
210+
// a `Sync` that takes the backend out of copy mode. If `writeData` threw the error from from the
211+
// `PostgresCopyFromWriter.write` call, `writer.failed` will throw with the same error, so it doesn't
212+
// matter that we ignore the error here. If the user threw some other error, it's better to honor the
213213
// user's error.
214214
try? await writer.failed(error: error)
215215

Sources/PostgresNIO/New/Connection State Machine/ConnectionStateMachine.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ struct ConnectionStateMachine {
9191
/// Fail a query's execution by resuming the continuation with the given error. When `sync` is `true`, send a
9292
/// `Sync` message to the backend.
9393
case failQueryContinuation(AnyErrorContinuation, with: PSQLError, sync: Bool, cleanupContext: CleanUpContext?)
94-
/// Fail a query's execution by resuming the continuation with the given error and send a `Sync` message to the
95-
/// backend.
94+
/// Succeed the promise with the rows from the given query result.
9695
case succeedQuery(EventLoopPromise<PSQLRowStream>, with: QueryResult)
9796
/// Succeed the continuation with a void result. When `sync` is `true`, send a `Sync` message to the backend.
9897
case succeedQueryContinuation(CheckedContinuation<Void, any Error>, sync: Bool)

Sources/PostgresNIO/New/Connection State Machine/ExtendedQueryStateMachine.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,7 @@ struct ExtendedQueryStateMachine {
427427
if case .error(let error) = self.state {
428428
// The backend sent us an ErrorResponse during the copy operation. Indicate to the client that it should
429429
// abort the data transfer.
430-
promise.fail(error)
431-
return . failPromise(promise, error: error)
430+
return .failPromise(promise, error: error)
432431
}
433432
guard case .copyingData(.readyToSend) = self.state else {
434433
preconditionFailure("Not ready to send data")

0 commit comments

Comments
 (0)