-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (129 loc) · 4.36 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
139
140
141
142
143
144
145
146
/*
* MIT License
*
* Copyright (c) 2023 Comprehensive Cancer Center Mainfranken
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package main
import (
"crypto/tls"
"database/sql"
"fmt"
"github.com/alecthomas/kong"
"github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/term"
"log"
"os"
"syscall"
)
var (
cli *CLI
context *kong.Context
db *sql.DB
)
type CLI struct {
User string `short:"U" help:"Database username" required:""`
Password string `short:"P" help:"Database password"`
Host string `short:"H" help:"Database host" default:"localhost"`
Port int `help:"Database port" default:"3306"`
Database string `short:"D" help:"Database name" default:"onkostar"`
}
func initCLI() {
cli = &CLI{}
context = kong.Parse(cli,
kong.Name("lkr-export"),
kong.Description("Tool to export data from Onkostar table 'lkr_meldung_export'"),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{
Compact: true,
}),
)
}
func main() {
initCLI()
if len(cli.Password) == 0 {
fmt.Print("Passwort: ")
if bytePw, err := term.ReadPassword(int(syscall.Stdin)); err == nil {
cli.Password = string(bytePw)
}
println()
}
dbCfg := mysql.Config{
User: cli.User,
Passwd: cli.Password,
Net: "tcp",
Addr: fmt.Sprintf("%s:%d", cli.Host, cli.Port),
DBName: cli.Database,
AllowNativePasswords: true,
TLSConfig: "skip-verify",
TLS: &tls.Config{MinVersion: tls.VersionTLS10, MaxVersion: tls.VersionTLS13},
}
if dbx, err := sql.Open("mysql", dbCfg.FormatDSN()); err == nil {
if err := dbx.Ping(); err == nil {
db = dbx
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Println("Cannot close database connection")
}
}(db)
} else {
log.Fatalf("Cannot connect to Database: %s\n", err.Error())
}
} else {
log.Fatalf("Cannot connect to Database: %s\n", err.Error())
}
query := `SELECT lkr_export, xml_daten FROM lkr_meldung_export WHERE xml_daten IS NOT NULL ORDER BY lkr_export`
var lkrExport uint32
var xmlDaten sql.NullString
if rows, err := db.Query(query); err == nil {
var xmlDataCollection = map[string][]AdtGekid{}
for rows.Next() {
if err := rows.Scan(&lkrExport, &xmlDaten); err == nil {
if value, err := xmlDaten.Value(); err == nil && value != nil {
if len(xmlDataCollection[fmt.Sprintf("%d", lkrExport)]) == 0 {
xmlDataCollection[fmt.Sprintf("%d", lkrExport)] = []AdtGekid{}
}
if data, err := UnmarschallAdtGekid([]byte(fmt.Sprint(xmlDaten))); err == nil {
content := append(xmlDataCollection[fmt.Sprintf("%d", lkrExport)], *data)
xmlDataCollection[fmt.Sprintf("%d", lkrExport)] = content
} else {
log.Printf("Problematischer Datensatz bei Export: %v", lkrExport)
}
} else {
log.Printf("Kann XML-Daten nicht lesen")
}
} else {
log.Printf("Datenbankeintrag nicht lesen")
}
}
for exportId, content := range xmlDataCollection {
if fileContent, err := ToMergedString(content); err == nil {
filename := fmt.Sprintf("ADTGEKID_Export_%s.xml", exportId)
if err := os.WriteFile(filename, []byte(fileContent), 0664); err != nil {
log.Fatal(err)
}
} else {
log.Printf("Überspringe Meldungen beim Zusammenführen von Export-ID %s: %s", exportId, err)
}
}
}
}