-
Notifications
You must be signed in to change notification settings - Fork 19
/
conn.go
112 lines (91 loc) · 3.06 KB
/
conn.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
package wire
import (
"context"
"net"
"github.com/jackc/pgx/v5/pgtype"
)
type ctxKey int
const (
ctxTypeMap ctxKey = iota
ctxClientMetadata
ctxServerMetadata
ctxRemoteAddr
)
// setTypeInfo constructs a new Postgres type connection info for the given value
func setTypeInfo(ctx context.Context, info *pgtype.Map) context.Context {
return context.WithValue(ctx, ctxTypeMap, info)
}
// TypeMap returns the Postgres type connection info if it has been set inside
// the given context.
func TypeMap(ctx context.Context) *pgtype.Map {
val := ctx.Value(ctxTypeMap)
if val == nil {
return nil
}
return val.(*pgtype.Map)
}
func setRemoteAddress(ctx context.Context, addr net.Addr) context.Context {
return context.WithValue(ctx, ctxRemoteAddr, addr)
}
// RemoteAddress returns the Postgres remote address connection info if it has been set inside
// the given context.
func RemoteAddress(ctx context.Context) net.Addr {
val := ctx.Value(ctxRemoteAddr)
if val == nil {
return nil
}
return val.(net.Addr)
}
// Parameters represents a parameters collection of parameter status keys and
// their values.
type Parameters map[ParameterStatus]string
// ParameterStatus represents a metadata key that could be defined inside a server/client
// metadata definition.
type ParameterStatus string
// At present there is a hard-wired set of parameters for which ParameterStatus
// will be generated.
// https://www.postgresql.org/docs/13/protocol-flow.html#PROTOCOL-ASYNC
const (
ParamServerEncoding ParameterStatus = "server_encoding"
ParamClientEncoding ParameterStatus = "client_encoding"
ParamIsSuperuser ParameterStatus = "is_superuser"
ParamSessionAuthorization ParameterStatus = "session_authorization"
ParamApplicationName ParameterStatus = "application_name"
ParamDatabase ParameterStatus = "database"
ParamUsername ParameterStatus = "user"
ParamServerVersion ParameterStatus = "server_version"
)
// setClientParameters constructs a new context containing the given parameters.
// Any previously defined metadata will be overriden.
func setClientParameters(ctx context.Context, params Parameters) context.Context {
if params == nil {
return ctx
}
return context.WithValue(ctx, ctxClientMetadata, params)
}
// ClientParameters returns the connection parameters if it has been set inside
// the given context.
func ClientParameters(ctx context.Context) Parameters {
val := ctx.Value(ctxClientMetadata)
if val == nil {
return nil
}
return val.(Parameters)
}
// setServerParameters constructs a new context containing the given parameters map.
// Any previously defined metadata will be overriden.
func setServerParameters(ctx context.Context, params Parameters) context.Context {
if params == nil {
return ctx
}
return context.WithValue(ctx, ctxServerMetadata, params)
}
// ServerParameters returns the connection parameters if it has been set inside
// the given context.
func ServerParameters(ctx context.Context) Parameters {
val := ctx.Value(ctxServerMetadata)
if val == nil {
return nil
}
return val.(Parameters)
}