Newer
Older
package eqp
import (
"errors"
"pggat2/lib/mw2"
"pggat2/lib/zap"
packets "pggat2/lib/zap/packets/v3.0"
)
type Client struct {
preparedStatements map[string]PreparedStatement
portals map[string]Portal
}
func MakeClient() Client {
return Client{
preparedStatements: make(map[string]PreparedStatement),
portals: make(map[string]Portal),
}
}
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
func (T *Client) Send(_ mw2.Context, out zap.Out) error {
in := zap.OutToIn(out)
switch in.Type() {
case packets.ParseComplete, packets.BindComplete, packets.CloseComplete:
// should've been caught by eqp.Server
panic("unreachable")
}
return nil
}
func (T *Client) Read(ctx mw2.Context, in zap.In) error {
switch in.Type() {
case packets.Query:
// clobber unnamed portal and unnamed prepared statement
delete(T.preparedStatements, "")
delete(T.portals, "")
case packets.Parse:
ctx.Cancel()
destination, query, parameterDataTypes, ok := packets.ReadParse(in)
if !ok {
return errors.New("bad packet format")
}
if destination != "" {
if _, ok = T.preparedStatements[destination]; ok {
return errors.New("prepared statement already exists")
}
}
T.preparedStatements[destination] = PreparedStatement{
Query: query,
ParameterDataTypes: parameterDataTypes,
}
// send parse complete
out := zap.InToOut(in)
out.Reset()
out.Type(packets.ParseComplete)
err := ctx.Send(out)
if err != nil {
return err
}
case packets.Bind:
ctx.Cancel()
destination, source, parameterFormatCodes, parameterValues, resultFormatCodes, ok := packets.ReadBind(in)
if !ok {
return errors.New("bad packet format")
}
if destination != "" {
if _, ok = T.portals[destination]; ok {
return errors.New("portal already exists")
}
}
T.portals[destination] = Portal{
Source: source,
ParameterFormatCodes: parameterFormatCodes,
ParameterValues: parameterValues,
ResultFormatCodes: resultFormatCodes,
}
// send bind complete
out := zap.InToOut(in)
out.Reset()
out.Type(packets.BindComplete)
err := ctx.Send(out)
if err != nil {
return err
}
case packets.Close:
ctx.Cancel()
which, target, ok := packets.ReadClose(in)
if !ok {
return errors.New("bad packet format")
}
switch which {
case 'S':
delete(T.preparedStatements, target)
case 'P':
delete(T.portals, target)
default:
return errors.New("bad packet format")
}
// send close complete
out := zap.InToOut(in)
out.Reset()
out.Type(packets.CloseComplete)
err := ctx.Send(out)
if err != nil {
return err
}
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
case packets.Describe:
// ensure target exists
which, target, ok := packets.ReadDescribe(in)
if !ok {
return errors.New("bad packet format")
}
switch which {
case 'S':
if _, ok := T.preparedStatements[target]; !ok {
return errors.New("prepared statement doesn't exist")
}
case 'P':
if _, ok := T.portals[target]; !ok {
return errors.New("portal doesn't exist")
}
default:
return errors.New("unknown describe target")
}
case packets.Execute:
target, _, ok := packets.ReadExecute(in)
if !ok {
return errors.New("bad packet format")
}
if _, ok := T.portals[target]; !ok {
return errors.New("portal doesn't exist")
}