Skip to content

Commit f121eb1

Browse files
committed
execute request
1 parent 310e85c commit f121eb1

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

Sources/PostgreSQL/Message/Base/PostgreSQLMessage.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ enum PostgreSQLMessage {
1313
case dataRow(PostgreSQLDataRow)
1414
case close(PostgreSQLCloseCommand)
1515
case parse(PostgreSQLParseRequest)
16+
/// Identifies the message as a Bind command.
17+
case bind(PostgreSQLBindRequest)
18+
/// Identifies the message as an Execute command.
19+
case execute(PostgreSQLExecuteRequest)
1620
/// Identifies the message as a Sync command.
1721
case sync
1822
/// Identifies the message as a Parse-complete indicator.
1923
case parseComplete
20-
/// Identifies the message as a Bind command.
21-
case bind(PostgreSQLBindRequest)
24+
/// Identifies the message as a Bind-complete indicator.
25+
case bindComplete
2226
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// Identifies the message as an Execute command.
2+
struct PostgreSQLExecuteRequest: Encodable {
3+
/// The name of the destination portal (an empty string selects the unnamed portal).
4+
var portalName: String
5+
6+
/// Maximum number of rows to return, if portal contains a query that
7+
/// returns rows (ignored otherwise). Zero denotes “no limit”.
8+
var maxRows: Int32
9+
}

Sources/PostgreSQL/Message/PostgreSQLMessageDecoder.swift

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ final class PostgreSQLMessageDecoder {
2929
case .D: message = try .dataRow(decoder.decode())
3030
case .C: message = try .close(decoder.decode())
3131
case .one: message = .parseComplete
32+
case .two: message = .bindComplete
3233
default:
3334
let string = String(bytes: [type], encoding: .ascii) ?? "n/a"
3435
fatalError("Unrecognized message type: \(string) (\(type)")

Sources/PostgreSQL/Message/PostgreSQLMessageEncoder.swift

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ final class PostgreSQLMessageEncoder {
2525
case .bind(let bind):
2626
identifier = .B
2727
try bind.encode(to: encoder)
28+
case .execute(let execute):
29+
identifier = .E
30+
try execute.encode(to: encoder)
2831
default: fatalError("Unsupported encodable type: \(type(of: message))")
2932
}
3033
encoder.updateSize()

Sources/PostgreSQL/PostgreSQLClient.swift

+25-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ final class PostgreSQLClient {
6868
}
6969
}
7070

71-
func parameterizedQuery(_ string: String, _ parameters: [PostgreSQLData] = []) throws -> Future<Void> {
71+
func parameterizedQuery(
72+
_ string: String,
73+
_ parameters: [PostgreSQLData] = [],
74+
onRow: @escaping ([String: PostgreSQLData]) -> ()
75+
) throws -> Future<Void> {
7276
let parse = PostgreSQLParseRequest(
7377
statementName: "",
7478
query: string,
@@ -81,12 +85,29 @@ final class PostgreSQLClient {
8185
parameters: parameters.map { try .serialize(data: $0) },
8286
resultFormatCodes: [.binary]
8387
)
84-
return queueStream.enqueue([.parse(parse), .bind(bind)]) { message in
88+
let execute = PostgreSQLExecuteRequest(
89+
portalName: "",
90+
maxRows: 0
91+
)
92+
var currentRow: PostgreSQLRowDescription?
93+
return queueStream.enqueue([
94+
.parse(parse), .bind(bind), .execute(execute), .sync
95+
]) { message in
8596
print(message)
8697
switch message {
87-
case .parseComplete: return false
8898
case .errorResponse(let e): throw e
89-
case .readyForQuery: return false
99+
case .parseComplete: return false
100+
case .bindComplete: return false
101+
case .rowDescription(let row):
102+
currentRow = row
103+
return false
104+
case .dataRow(let data):
105+
let row = currentRow !! "Unexpected PostgreSQLDataRow without preceding PostgreSQLRowDescription."
106+
let parsed = try row.parse(data: data)
107+
onRow(parsed)
108+
return false
109+
case .close: return false
110+
case .readyForQuery: return true
90111
default: fatalError("Unexpected message during PostgreSQLParseRequest: \(message)")
91112
}
92113
}

0 commit comments

Comments
 (0)