forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrepareStatementStateMachine.swift
147 lines (118 loc) · 5.03 KB
/
PrepareStatementStateMachine.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
struct PrepareStatementStateMachine {
enum State {
case initialized(PrepareStatementContext)
case parseDescribeSent(PrepareStatementContext)
case parseCompleteReceived(PrepareStatementContext)
case parameterDescriptionReceived(PrepareStatementContext)
case rowDescriptionReceived
case noDataMessageReceived
case error(PSQLError)
}
enum Action {
case sendParseDescribeSync(name: String, query: String)
case succeedPreparedStatementCreation(PrepareStatementContext, with: RowDescription?)
case failPreparedStatementCreation(PrepareStatementContext, with: PSQLError)
case read
case wait
}
var state: State
init(createContext: PrepareStatementContext) {
self.state = .initialized(createContext)
}
#if DEBUG
/// for testing purposes only
init(_ state: State) {
self.state = state
}
#endif
mutating func start() -> Action {
guard case .initialized(let createContext) = self.state else {
preconditionFailure("Start must only be called after the query has been initialized")
}
self.state = .parseDescribeSent(createContext)
return .sendParseDescribeSync(name: createContext.name, query: createContext.query)
}
mutating func parseCompletedReceived() -> Action {
guard case .parseDescribeSent(let createContext) = self.state else {
return self.setAndFireError(.unexpectedBackendMessage(.parseComplete))
}
self.state = .parseCompleteReceived(createContext)
return .wait
}
mutating func parameterDescriptionReceived(_ parameterDescription: PostgresBackendMessage.ParameterDescription) -> Action {
guard case .parseCompleteReceived(let createContext) = self.state else {
return self.setAndFireError(.unexpectedBackendMessage(.parameterDescription(parameterDescription)))
}
self.state = .parameterDescriptionReceived(createContext)
return .wait
}
mutating func noDataReceived() -> Action {
guard case .parameterDescriptionReceived(let queryContext) = self.state else {
return self.setAndFireError(.unexpectedBackendMessage(.noData))
}
self.state = .noDataMessageReceived
return .succeedPreparedStatementCreation(queryContext, with: nil)
}
mutating func rowDescriptionReceived(_ rowDescription: RowDescription) -> Action {
guard case .parameterDescriptionReceived(let queryContext) = self.state else {
return self.setAndFireError(.unexpectedBackendMessage(.rowDescription(rowDescription)))
}
self.state = .rowDescriptionReceived
return .succeedPreparedStatementCreation(queryContext, with: rowDescription)
}
mutating func errorReceived(_ errorMessage: PostgresBackendMessage.ErrorResponse) -> Action {
let error = PSQLError.server(errorMessage)
switch self.state {
case .initialized:
return self.setAndFireError(.unexpectedBackendMessage(.error(errorMessage)))
case .parseDescribeSent,
.parseCompleteReceived,
.parameterDescriptionReceived:
return self.setAndFireError(error)
case .rowDescriptionReceived,
.noDataMessageReceived,
.error:
preconditionFailure("""
This state must not be reached. If the prepared statement `.isComplete`, the
ConnectionStateMachine must not send any further events to the substate machine.
""")
}
}
mutating func errorHappened(_ error: PSQLError) -> Action {
return self.setAndFireError(error)
}
private mutating func setAndFireError(_ error: PSQLError) -> Action {
switch self.state {
case .initialized(let context),
.parseDescribeSent(let context),
.parseCompleteReceived(let context),
.parameterDescriptionReceived(let context):
self.state = .error(error)
return .failPreparedStatementCreation(context, with: error)
case .rowDescriptionReceived,
.noDataMessageReceived,
.error:
preconditionFailure("""
This state must not be reached. If the prepared statement `.isComplete`, the
ConnectionStateMachine must not send any further events to the substate machine.
""")
}
}
// MARK: Channel actions
mutating func readEventCaught() -> Action {
return .read
}
var isComplete: Bool {
switch self.state {
case .rowDescriptionReceived,
.noDataMessageReceived,
.error:
return true
case .initialized,
.parseDescribeSent,
.parseCompleteReceived,
.parameterDescriptionReceived:
return false
}
}
}