/
main.go
138 lines (122 loc) · 3.1 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/SoftwareAG/adabas-go-api/adabas"
"github.com/SoftwareAG/adabas-go-api/adatypes"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const destMap = "EmployeeMap"
type streamStruct struct {
store *adabas.StoreRequest
}
func initLogLevelWithFile(fileName string, level zapcore.Level) (err error) {
p := os.Getenv("LOGPATH")
if p == "" {
p = "."
}
name := p + string(os.PathSeparator) + fileName
rawJSON := []byte(`{
"level": "error",
"encoding": "console",
"outputPaths": [ "XXX"],
"errorOutputPaths": ["stderr"],
"encoderConfig": {
"messageKey": "message",
"levelKey": "level",
"levelEncoder": "lowercase"
}
}`)
var cfg zap.Config
if err := json.Unmarshal(rawJSON, &cfg); err != nil {
fmt.Printf("Initial logging JSON configuration error: %v\n", err)
os.Exit(1)
}
cfg.Level.SetLevel(level)
cfg.OutputPaths = []string{name}
logger, err := cfg.Build()
if err != nil {
fmt.Printf("Initial logging error: %v\n", err)
os.Exit(1)
}
defer logger.Sync()
sugar := logger.Sugar()
adatypes.Central.SetDebugLevel(true)
sugar.Infof("Start logging with level %v", level)
adatypes.Central.Log = sugar
return
}
func updateStream(record *adabas.Record, x interface{}) error {
tc := x.(*streamStruct)
updateRecord, err := tc.store.CreateRecord()
if err != nil {
return err
}
updateRecord.Isn = record.Isn
record.DumpValues()
last := record.ValueQuantity("AS")
fmt.Println("Quantity", last)
for i := uint32(0); i < uint32(last); i++ {
vi, err := record.SearchValueIndex("AS", []uint32{i + 1})
if err != nil {
return err
}
sv, _ := vi.Int32()
sv += 1000
err = updateRecord.SetValueWithIndex("AS", []uint32{i + 1}, sv)
if err != nil {
return err
}
}
fmt.Println("Read", record)
fmt.Println("Update", updateRecord)
return nil // tc.store.Update(updateRecord)
}
func main() {
initLogLevelWithFile("employees.log", zapcore.DebugLevel)
adabasModDBIDs := "1"
if len(os.Args) > 1 {
adabasModDBIDs = os.Args[1]
}
fmt.Println("Open connection to", adabasModDBIDs)
connection, err := adabas.NewConnection(fmt.Sprintf("acj;target=%s", adabasModDBIDs))
if err != nil {
fmt.Println("Error connecting database:", err)
return
}
defer connection.Close()
readRequest, rerr := connection.CreateFileReadRequest(11)
if rerr != nil {
fmt.Println("Error creating read request:", rerr)
return
}
err = readRequest.QueryFields("AA,AB,AS")
if err != nil {
fmt.Println("Error query field:", err)
return
}
storeRequest, serr := connection.CreateStoreRequest(11)
if serr != nil {
fmt.Println("Error creating store request:", serr)
return
}
serr = storeRequest.StoreFields("AS")
if serr != nil {
fmt.Println("Error define store fields:", serr)
return
}
fmt.Println("Read logical search...", destMap)
tc := &streamStruct{store: storeRequest}
_, err = readRequest.ReadLogicalWithStream("AE='SMITH'", updateStream, tc)
if err != nil {
fmt.Println("Error updating records:", err)
return
}
/*err = storeRequest.EndTransaction()
if err != nil {
fmt.Println("Error end of transaction:", err)
return
}*/
}