@@ -4,15 +4,14 @@ struct PreparedStatementStateMachine {
4
4
enum State {
5
5
case preparing( [ PreparedStatementContext ] )
6
6
case prepared( RowDescription ? )
7
- // TODO: store errors
8
- // case error(PostgresBackendMessage.ErrorResponse)
7
+ case error( PSQLError )
9
8
}
10
9
11
10
enum Action {
12
- case none
13
11
case prepareStatement
14
12
case waitForAlreadyInFlightPreparation
15
13
case executePendingStatements( [ PreparedStatementContext ] , RowDescription ? )
14
+ case returnError( [ PreparedStatementContext ] , PSQLError )
16
15
}
17
16
18
17
var preparedStatements : [ String : State ]
@@ -30,6 +29,8 @@ struct PreparedStatementStateMachine {
30
29
return . waitForAlreadyInFlightPreparation
31
30
case . prepared( let rowDescription) :
32
31
return . executePendingStatements( [ context] , rowDescription)
32
+ case . error( let error) :
33
+ return . returnError( [ context] , error)
33
34
}
34
35
} else {
35
36
self . preparedStatements [ name] = . preparing( [ context] )
@@ -41,24 +42,27 @@ struct PreparedStatementStateMachine {
41
42
name: String ,
42
43
rowDescription: RowDescription ?
43
44
) -> Action {
44
- guard let state = self . preparedStatements [ name] else {
45
+ guard case . preparing ( let statements ) = self . preparedStatements [ name] else {
45
46
preconditionFailure ( " Preparation completed for an unexpected statement " )
46
47
}
47
- switch state {
48
- case . preparing( let statements) :
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
53
- }
54
- self . preparedStatements [ name] = . prepared( rowDescription)
55
- return . executePendingStatements( statements, rowDescription)
56
- } else {
57
- self . preparedStatements [ name] = . prepared( nil )
58
- return . executePendingStatements( statements, nil )
48
+ // When sending the bindings we are going to ask for binary data.
49
+ if var rowDescription {
50
+ for i in 0 ..< rowDescription. columns. count {
51
+ rowDescription. columns [ i] . format = . binary
59
52
}
60
- case . prepared( _) :
61
- return . none
53
+ self . preparedStatements [ name] = . prepared( rowDescription)
54
+ return . executePendingStatements( statements, rowDescription)
55
+ } else {
56
+ self . preparedStatements [ name] = . prepared( nil )
57
+ return . executePendingStatements( statements, nil )
58
+ }
59
+ }
60
+
61
+ mutating func errorHappened( name: String , error: PSQLError ) -> Action {
62
+ guard case . preparing( let statements) = self . preparedStatements [ name] else {
63
+ preconditionFailure ( " Preparation completed for an unexpected statement " )
62
64
}
65
+ self . preparedStatements [ name] = . error( error)
66
+ return . returnError( statements, error)
63
67
}
64
68
}
0 commit comments