forked from jeroenrinzema/psql-wire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
94 lines (76 loc) · 2.6 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
package wire
import (
"context"
"github.com/jackc/pgtype"
)
type ctxKey int
const (
ctxTypeInfo ctxKey = iota
ctxClientMetadata
ctxServerMetadata
)
// setTypeInfo constructs a new Postgres type connection info for the given value
func setTypeInfo(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxTypeInfo, pgtype.NewConnInfo())
}
// TypeInfo returns the Postgres type connection info if it has been set inside
// the given context.
func TypeInfo(ctx context.Context) *pgtype.ConnInfo {
val := ctx.Value(ctxTypeInfo)
if val == nil {
return nil
}
return val.(*pgtype.ConnInfo)
}
// 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"
)
// 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)
}