-
Notifications
You must be signed in to change notification settings - Fork 22
/
state.go
147 lines (105 loc) · 3.25 KB
/
state.go
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
// Copyright 2010 The go-pgsql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pgsql
const invalidOpForStateMsg = "invalid operation for this state"
// state is the interface that all states must implement.
type state interface {
// code returns the ConnStatus that matches the state.
code() ConnStatus
// execute sends Bind and Execute packets to the server.
execute(stmt *Statement, rs *ResultSet)
// flush sends a Flush packet to the server.
flush(conn *Conn)
// prepare sends a Parse packet to the server.
prepare(stmt *Statement)
// query sends a Query packet to the server.
query(conn *Conn, rs *ResultSet, sql string)
}
// abstractState can be embedded in any real state struct, so it satisfies
// the state interface without implementing all state methods itself.
type abstractState struct{}
func (abstractState) execute(stmt *Statement, rs *ResultSet) {
panic(invalidOpForStateMsg)
}
func (abstractState) flush(conn *Conn) {
panic(invalidOpForStateMsg)
}
func (abstractState) prepare(stmt *Statement) {
panic(invalidOpForStateMsg)
}
func (abstractState) query(conn *Conn, rs *ResultSet, sql string) {
panic(invalidOpForStateMsg)
}
// copyState is the state that is active when the connection is used
// to exchange CopyData messages for bulk import/export.
type copyState struct {
abstractState
}
func (copyState) code() ConnStatus {
return StatusCopy
}
// disconnectedState is the initial state before a connection is established.
type disconnectedState struct {
abstractState
}
func (disconnectedState) code() ConnStatus {
return StatusDisconnected
}
// processingQueryState is the state that is active when
// the results of a query are being processed.
type processingQueryState struct {
abstractState
}
func (processingQueryState) code() ConnStatus {
return StatusProcessingQuery
}
// readyState is the state that is active when the connection to the
// PostgreSQL server is ready for queries.
type readyState struct {
abstractState
}
func (readyState) code() ConnStatus {
return StatusReady
}
func (readyState) execute(stmt *Statement, rs *ResultSet) {
conn := stmt.conn
if conn.LogLevel >= LogDebug {
defer conn.logExit(conn.logEnter("readyState.execute"))
}
succeeded := false
conn.onErrorDontRequireReadyForQuery = true
defer func() {
conn.onErrorDontRequireReadyForQuery = false
if !succeeded {
conn.writeSync()
conn.readBackendMessages(nil)
}
}()
conn.writeBind(stmt)
conn.readBackendMessages(rs)
conn.writeDescribe(stmt)
conn.readBackendMessages(rs)
conn.writeExecute(stmt)
conn.writeSync()
conn.state = processingQueryState{}
succeeded = true
}
func (readyState) prepare(stmt *Statement) {
conn := stmt.conn
if conn.LogLevel >= LogDebug {
defer conn.logExit(conn.logEnter("readyState.prepare"))
}
conn.writeParse(stmt)
conn.onErrorDontRequireReadyForQuery = true
defer func() { conn.onErrorDontRequireReadyForQuery = false }()
conn.readBackendMessages(nil)
}
func (readyState) query(conn *Conn, rs *ResultSet, command string) {
if conn.LogLevel >= LogDebug {
defer conn.logExit(conn.logEnter("readyState.query"))
}
conn.writeQuery(command)
conn.readBackendMessages(rs)
conn.state = processingQueryState{}
}