forked from SAP/go-hdb
/
optionsparts.go
101 lines (85 loc) · 2.79 KB
/
optionsparts.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
package protocol
import (
"fmt"
"github.com/SAP/go-hdb/driver/internal/exp/slices"
"github.com/SAP/go-hdb/driver/internal/protocol/encoding"
)
// ClientContextOption represents a client context option.
type ClientContextOption int8
// ClientContextOption constants.
const (
CcoClientVersion ClientContextOption = 1
CcoClientType ClientContextOption = 2
CcoClientApplicationProgram ClientContextOption = 3
)
// DBConnectInfoType represents a database connect info type.
type DBConnectInfoType int8
// DBConnectInfoType constants.
const (
CiDatabaseName DBConnectInfoType = 1 // string
CiHost DBConnectInfoType = 2 // string
CiPort DBConnectInfoType = 3 // int4
CiIsConnected DBConnectInfoType = 4 // bool
)
type statementContextType int8
const (
scStatementSequenceInfo statementContextType = 1
scServerProcessingTime statementContextType = 2
scSchemaName statementContextType = 3
scFlagSet statementContextType = 4
scQueryTimeout statementContextType = 5
scClientReconnectionWaitTimeout statementContextType = 6
scServerCPUTime statementContextType = 7
scServerMemoryUsage statementContextType = 8
)
// transaction flags
type transactionFlagType int8
const (
tfRolledback transactionFlagType = 0
tfCommited transactionFlagType = 1
tfNewIsolationLevel transactionFlagType = 2
tfDDLCommitmodeChanged transactionFlagType = 3
tfWriteTransactionStarted transactionFlagType = 4
tfNowriteTransactionStarted transactionFlagType = 5
tfSessionClosingTransactionError transactionFlagType = 6
tfSessionClosingTransactionErrror transactionFlagType = 7
tfReadOnlyMode transactionFlagType = 8
)
// Options represents a generic option part.
type Options[K ~int8] map[K]any
func (ops Options[K]) String() string {
s := []string{}
for i, typ := range ops {
s = append(s, fmt.Sprintf("%v: %v", K(i), typ))
}
slices.Sort(s)
return fmt.Sprintf("%v", s)
}
func (ops Options[K]) size() int {
size := 2 * len(ops) //option + type
for _, v := range ops {
ot := optTypeViaType(v)
size += ot.size(v)
}
return size
}
func (ops Options[K]) numArg() int { return len(ops) }
func (ops *Options[K]) decode(dec *encoding.Decoder, ph *PartHeader) error {
*ops = Options[K]{} // no reuse of maps - create new one
for i := 0; i < ph.numArg(); i++ {
k := K(dec.Int8())
tc := typeCode(dec.Byte())
ot := optTypeViaTypeCode(tc)
(*ops)[k] = ot.decode(dec)
}
return dec.Error()
}
func (ops Options[K]) encode(enc *encoding.Encoder) error {
for k, v := range ops {
enc.Int8(int8(k))
ot := optTypeViaType(v)
enc.Int8(int8(ot.typeCode()))
ot.encode(enc, v)
}
return nil
}