@@ -43,25 +43,30 @@ struct PreparedStatementStateMachine {
43
43
name: String ,
44
44
rowDescription: RowDescription ?
45
45
) -> 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 ) " )
48
48
}
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
+ )
53
67
}
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) " )
65
70
}
66
71
}
67
72
@@ -71,13 +76,18 @@ struct PreparedStatementStateMachine {
71
76
}
72
77
73
78
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) " )
76
91
}
77
- self . preparedStatements [ name] = . error( error)
78
- return ErrorHappenedAction (
79
- statements: statements,
80
- error: error
81
- )
82
92
}
83
93
}
0 commit comments