@@ -22,8 +22,8 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
22
22
private let configuration : PostgresConnection . InternalConfiguration
23
23
private let configureSSLCallback : ( ( Channel ) throws -> Void ) ?
24
24
25
- private var listenState : ListenStateMachine
26
- private var preparedStatementState : PreparedStatementStateMachine
25
+ private var listenState = ListenStateMachine ( )
26
+ private var preparedStatementState = PreparedStatementStateMachine ( )
27
27
28
28
init (
29
29
configuration: PostgresConnection . InternalConfiguration ,
@@ -33,8 +33,6 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
33
33
) {
34
34
self . state = ConnectionStateMachine ( requireBackendKeyData: configuration. options. requireBackendKeyData)
35
35
self . eventLoop = eventLoop
36
- self . listenState = ListenStateMachine ( )
37
- self . preparedStatementState = PreparedStatementStateMachine ( )
38
36
self . configuration = configuration
39
37
self . configureSSLCallback = configureSSLCallback
40
38
self . logger = logger
@@ -238,62 +236,25 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
238
236
}
239
237
case . executePreparedStatement( let preparedStatement) :
240
238
let action = self . preparedStatementState. lookup (
241
- name: preparedStatement. name,
242
- context: preparedStatement
239
+ preparedStatement: preparedStatement
243
240
)
244
241
switch action {
245
242
case . prepareStatement:
246
- let promise = self . eventLoop. makePromise ( of: RowDescription ? . self)
247
- promise. futureResult. whenSuccess { rowDescription in
248
- self . prepareStatementComplete (
249
- name: preparedStatement. name,
250
- rowDescription: rowDescription,
251
- context: context
252
- )
253
- }
254
- promise. futureResult. whenFailure { error in
255
- self . prepareStatementFailed (
256
- name: preparedStatement. name,
257
- error: error as! PSQLError ,
258
- context: context
259
- )
260
- }
261
- psqlTask = . extendedQuery( . init(
262
- name: preparedStatement. name,
263
- query: preparedStatement. sql,
264
- logger: preparedStatement. logger,
265
- promise: promise
266
- ) )
243
+ psqlTask = self . makePrepareStatementAction (
244
+ preparedStatement: preparedStatement,
245
+ context: context
246
+ )
267
247
case . waitForAlreadyInFlightPreparation:
268
248
// The state machine already keeps track of this
269
249
// and will execute the statement as soon as it's prepared
270
250
return
271
251
case . executeStatement( let rowDescription) :
272
- psqlTask = . extendedQuery( . init(
273
- executeStatement: . init(
274
- name: preparedStatement. name,
275
- binds: preparedStatement. bindings,
276
- rowDescription: rowDescription) ,
277
- logger: preparedStatement. logger,
278
- promise: preparedStatement. promise
279
- ) )
280
- case . executePendingStatements( let pendingStatements, let rowDescription) :
281
- for statement in pendingStatements {
282
- let action = self . state. enqueue ( task: . extendedQuery( . init(
283
- executeStatement: . init(
284
- name: statement. name,
285
- binds: statement. bindings,
286
- rowDescription: rowDescription) ,
287
- logger: statement. logger,
288
- promise: statement. promise
289
- ) ) )
290
- self . run ( action, with: context)
291
- }
292
- return
293
- case . returnError( let pendingStatements, let error) :
294
- for statement in pendingStatements {
295
- statement. promise. fail ( error)
296
- }
252
+ psqlTask = self . makeExecutPreparedStatementAction (
253
+ preparedStatement: preparedStatement,
254
+ rowDescription: rowDescription
255
+ )
256
+ case . returnError( let error) :
257
+ preparedStatement. promise. fail ( error)
297
258
return
298
259
}
299
260
}
@@ -727,6 +688,55 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
727
688
}
728
689
}
729
690
691
+ private func makePrepareStatementAction(
692
+ preparedStatement: PreparedStatementContext ,
693
+ context: ChannelHandlerContext
694
+ ) -> PSQLTask {
695
+ let promise = self . eventLoop. makePromise ( of: RowDescription ? . self)
696
+ promise. futureResult. whenComplete { result in
697
+ switch result {
698
+ case . success( let rowDescription) :
699
+ self . prepareStatementComplete (
700
+ name: preparedStatement. name,
701
+ rowDescription: rowDescription,
702
+ context: context
703
+ )
704
+ case . failure( let error) :
705
+ let psqlError : PSQLError
706
+ if let error = error as? PSQLError {
707
+ psqlError = error
708
+ } else {
709
+ psqlError = . connectionError( underlying: error)
710
+ }
711
+ self . prepareStatementFailed (
712
+ name: preparedStatement. name,
713
+ error: psqlError,
714
+ context: context
715
+ )
716
+ }
717
+ }
718
+ return . extendedQuery( . init(
719
+ name: preparedStatement. name,
720
+ query: preparedStatement. sql,
721
+ logger: preparedStatement. logger,
722
+ promise: promise
723
+ ) )
724
+ }
725
+
726
+ private func makeExecutPreparedStatementAction(
727
+ preparedStatement: PreparedStatementContext ,
728
+ rowDescription: RowDescription ?
729
+ ) -> PSQLTask {
730
+ return . extendedQuery( . init(
731
+ executeStatement: . init(
732
+ name: preparedStatement. name,
733
+ binds: preparedStatement. bindings,
734
+ rowDescription: rowDescription) ,
735
+ logger: preparedStatement. logger,
736
+ promise: preparedStatement. promise
737
+ ) )
738
+ }
739
+
730
740
private func prepareStatementComplete(
731
741
name: String ,
732
742
rowDescription: RowDescription ? ,
@@ -736,15 +746,12 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
736
746
name: name,
737
747
rowDescription: rowDescription
738
748
)
739
- guard case . executePendingStatements( let statements, let rowDescription) = action else {
740
- preconditionFailure ( " Expected to have pending statements to execute " )
741
- }
742
- for preparedStatement in statements {
749
+ for preparedStatement in action. statements {
743
750
let action = self . state. enqueue ( task: . extendedQuery( . init(
744
751
executeStatement: . init(
745
752
name: preparedStatement. name,
746
753
binds: preparedStatement. bindings,
747
- rowDescription: rowDescription
754
+ rowDescription: action . rowDescription
748
755
) ,
749
756
logger: preparedStatement. logger,
750
757
promise: preparedStatement. promise
@@ -763,11 +770,8 @@ final class PostgresChannelHandler: ChannelDuplexHandler {
763
770
name: name,
764
771
error: error
765
772
)
766
- guard case . returnError( let statements, let error) = action else {
767
- preconditionFailure ( " Expected to have pending statements to execute " )
768
- }
769
- for statement in statements {
770
- statement. promise. fail ( error)
773
+ for statement in action. statements {
774
+ statement. promise. fail ( action. error)
771
775
}
772
776
}
773
777
}
0 commit comments