@@ -43,25 +43,30 @@ struct PreparedStatementStateMachine {
4343 name: String ,
4444 rowDescription: RowDescription ?
4545 ) -> PreparationCompleteAction {
46- guard case . preparing ( let statements ) = self . preparedStatements [ name] else {
47- preconditionFailure ( " Preparation completed for an unexpected statement" )
46+ guard let state = self . preparedStatements [ name] else {
47+ fatalError ( " Unknown prepared statement \( name ) " )
4848 }
49- // When sending the bindings we are going to ask for binary data.
50- if var rowDescription {
51- for i in 0 ..< rowDescription. columns. count {
52- rowDescription. columns [ i] . format = . binary
49+ switch state {
50+ case . preparing( let statements) :
51+ // When sending the bindings we are going to ask for binary data.
52+ if var rowDescription {
53+ for i in 0 ..< rowDescription. columns. count {
54+ rowDescription. columns [ i] . format = . binary
55+ }
56+ self . preparedStatements [ name] = . prepared( rowDescription)
57+ return PreparationCompleteAction (
58+ statements: statements,
59+ rowDescription: rowDescription
60+ )
61+ } else {
62+ self . preparedStatements [ name] = . prepared( nil )
63+ return PreparationCompleteAction (
64+ statements: statements,
65+ rowDescription: nil
66+ )
5367 }
54- self . preparedStatements [ name] = . prepared( rowDescription)
55- return PreparationCompleteAction (
56- statements: statements,
57- rowDescription: rowDescription
58- )
59- } else {
60- self . preparedStatements [ name] = . prepared( nil )
61- return PreparationCompleteAction (
62- statements: statements,
63- rowDescription: nil
64- )
68+ case . prepared, . error:
69+ preconditionFailure ( " Preparation completed happened in an unexpected state \( state) " )
6570 }
6671 }
6772
@@ -71,13 +76,18 @@ struct PreparedStatementStateMachine {
7176 }
7277
7378 mutating func errorHappened( name: String , error: PSQLError ) -> ErrorHappenedAction {
74- guard case . preparing( let statements) = self . preparedStatements [ name] else {
75- preconditionFailure ( " Preparation completed for an unexpected statement " )
79+ guard let state = self . preparedStatements [ name] else {
80+ fatalError ( " Unknown prepared statement \( name) " )
81+ }
82+ switch state {
83+ case . preparing( let statements) :
84+ self . preparedStatements [ name] = . error( error)
85+ return ErrorHappenedAction (
86+ statements: statements,
87+ error: error
88+ )
89+ case . prepared, . error:
90+ preconditionFailure ( " Error happened in an unexpected state \( state) " )
7691 }
77- self . preparedStatements [ name] = . error( error)
78- return ErrorHappenedAction (
79- statements: statements,
80- error: error
81- )
8292 }
8393}
0 commit comments