-
Notifications
You must be signed in to change notification settings - Fork 18
/
Qldb_adapter.go
143 lines (118 loc) · 3.61 KB
/
Qldb_adapter.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
package Ql
import (
"context"
"errors"
"github.com/amzn/ion-go/ion"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/qldbsession"
"github.com/awslabs/amazon-qldb-driver-go/v2/qldbdriver"
"github.com/kokizzu/gotro/L"
"github.com/kokizzu/gotro/M"
)
// https://docs.aws.amazon.com/qldb/latest/developerguide/console_QLDB.html#partiql-editor-ref-tips
// https://docs.aws.amazon.com/qldb/latest/developerguide/driver-quickstart-golang.html
type Adapter struct {
*qldbdriver.QLDBDriver
Reconnect func() *qldbdriver.QLDBDriver
}
func (a *Adapter) Shutdown() {
a.QLDBDriver.Shutdown(context.Background())
}
func Connect1(keyId, secret, region, ledger string) *qldbdriver.QLDBDriver {
conf := aws.NewConfig()
conf.WithRegion(region)
if keyId == `` || secret == `` {
// AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY
// AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY
conf.WithCredentials(credentials.NewEnvCredentials())
} else {
conf.WithCredentials(credentials.NewStaticCredentials(keyId, secret, ``))
}
awsSession, err := session.NewSession(conf)
L.PanicIf(err, `session.NewSession `+region)
qldbSession := qldbsession.New(awsSession)
driver, err := qldbdriver.New(
ledger,
qldbSession,
func(options *qldbdriver.DriverOptions) {
options.LoggerVerbosity = qldbdriver.LogInfo
})
L.PanicIf(err, `qldbdriver.New `+ledger)
return driver
}
func (a *Adapter) QMapArray(query string, eachRowFunc func(row M.SX) (exitEarly bool)) bool {
_, err := a.Execute(context.Background(), func(txn qldbdriver.Transaction) (any, error) {
tables, err := txn.Execute(query)
if L.IsError(err, `QMapArray.txn.Execute: `+query) {
return nil, err
}
for tables.Next(txn) {
ionBinary := tables.GetCurrentData()
row := M.SX{}
err := ion.Unmarshal(ionBinary, &row)
if L.IsError(err, `QMapArray.ion.Unmarshall: `+query) {
return nil, err
}
if eachRowFunc(row) {
return nil, errors.New(`QMapArray.eachRowFunc.exitEarly`)
}
}
return nil, nil
})
return err == nil
}
func (a *Adapter) QAll(selectQuery string, scanner func(rawRow []byte) error, args ...any) (total int) {
_, _ = a.Execute(context.Background(), func(txn qldbdriver.Transaction) (any, error) {
result, err := txn.Execute(selectQuery, args...)
if L.IsError(err, `QAll.Execute: `+selectQuery) {
return nil, err
}
for {
if !result.Next(txn) {
if err = result.Err(); L.IsError(err, `QAll.Next: `+selectQuery) {
return nil, err
}
break
}
ionBinary := result.GetCurrentData()
err = scanner(ionBinary)
if L.IsError(err, `QAll.scanner: `+selectQuery) {
return nil, err
}
total++
}
return nil, err
})
return
}
func (a *Adapter) QLine(selectQuery string, target any, args ...any) bool {
_, err := a.Execute(context.Background(), func(txn qldbdriver.Transaction) (any, error) {
result, err := txn.Execute(selectQuery, args...)
if L.IsError(err, `QLine.Execute: `+selectQuery) {
return nil, err
}
if !result.Next(txn) {
if err = result.Err(); L.IsError(err, `QLine.Next: `+selectQuery) {
return nil, err
}
return nil, nil
}
ionBinary := result.GetCurrentData()
err = ion.Unmarshal(ionBinary, target)
L.IsError(err, `QLine.scanner: `+selectQuery)
return nil, err
})
return err == nil
}
func (a *Adapter) DoExec(execQuery string, args ...any) bool {
_, err := a.Execute(context.Background(), func(txn qldbdriver.Transaction) (any, error) {
_, err := txn.Execute(execQuery, args...)
if L.IsError(err, `DoExec: `+execQuery) {
return nil, err
}
return nil, nil
})
return err != nil
}