Repository navigation
Expand file tree
/
Copy pathsql_db2.go
More file actions
183 lines (157 loc) · 4.76 KB
/
Copy pathsql_db2.go
File metadata and controls
183 lines (157 loc) · 4.76 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//go:build db2 && !hana && !mssql && !oracle
package main
import (
"fmt"
"log/slog"
"strings"
_ "github.com/ibmdb/go_ibm_db" // register the DB2 driver
)
// OpenConnection extracts the driver name from the DSN (expected as the URI scheme), adjusts it where necessary (e.g.
// some driver supported DSN formats don't include a scheme), opens a DB handle ensuring early termination if the
// context is closed (this is actually prevented by `database/sql` implementation), sets connection limits and returns
// the handle.
//
// # DB2 sql server
//
// Using the https://github.com/denisenkom/go-mssqldb driver, DSN format (passed through to the driver unchanged):
//
// url format:
// db2://<hostname>:<port>?user%20id=<login>&password=<password>&database=<database>&protocol=...
// DSN format!
// DATABASE=<database>; HOSTNAME=<hostname>; PORT=<port>; PROTOCOL=<protocol>; UID=<login>; PWD=<password>;
func BuildConnection(
logger *slog.Logger,
dsn string,
auth AuthConfig,
symbol_table map[string]any,
check_only bool) (string, error) {
var driver string
// Extract driver name from DSN.
idx := strings.Index(dsn, "://")
if idx == -1 {
//return nil, fmt.Errorf("missing driver in data source name. Expected format `<driver>://<dsn>`")
driver = "db2"
} else {
driver = dsn[:idx]
}
// Adjust DSN, where necessary.
var params map[string]string
switch driver {
case "db2":
var err error
if strings.HasPrefix(dsn, "db2://") {
// "db2://<hostname>:<port>?user%20id=<login>&password=<password>&database=<database>&protocol=..."
params, err = splitConnectionStringURL(dsn)
if err != nil {
return "", err
}
} else {
// DATABASE=<database>; HOSTNAME=<hostname>; PORT=<port>; PROTOCOL=<protocol>; UID=<login>; PWD=<password>;
params, err = splitRawConnectionStringDSN(dsn)
if err != nil {
return "", err
}
}
val, ok := params["user id"]
if !ok || val == "" {
if auth.Username != "" {
params["user id"] = auth.Username
} else {
return "", fmt.Errorf("user Id can't be empty")
}
}
val, ok = params["password"]
if !ok || val == "" {
if auth.Password != "" {
val = string(auth.Password)
} else {
return "", fmt.Errorf("password has to be set")
}
}
if !check_only && symbol_table != nil {
passwd := val
if strings.HasPrefix(passwd, "/encrypted/") {
if val, auth_key, err := BuildPasswd(logger, passwd, symbol_table); err == nil {
params["password"] = val
params["__auth_key"] = auth_key
} else {
return "", fmt.Errorf("unable to decrypt password")
}
params["__need_auth_key"] = "true"
} else {
params["password"] = val
params["__need_auth_key"] = "false"
}
val, ok = params["database"]
if !ok || val == "" {
return "", fmt.Errorf("database must be set")
}
if params["port"] == "" {
params["port"] = "60000"
}
if params["protocol"] == "" {
params["protocol"] = "TCPIP"
}
// remove instance from url if any has been specified
delete(params, "instance")
// add params to target symbol table
symbol_table["params"] = params
}
default:
return "", fmt.Errorf("driver '%s' not supported", driver)
}
// rebuild dsn from params because params may have changed
// dsn = genDSN(params)
// // WARNING: display password in clear text in log !!!
// logger.Debug(fmt.Sprintf("private dsn='%s", dsn))
// return dsn, nil
return genDSN(params), nil
}
// generate DSN string from parameter map
func genDSN(params map[string]string) string {
new_dns := new(strings.Builder)
// Hostname
new_dns.WriteString("HOSTNAME=")
new_dns.WriteString(params["server"])
new_dns.WriteString("; ")
// Port
if params["port"] != "" {
new_dns.WriteString("PORT=")
new_dns.WriteString(params["port"])
new_dns.WriteString("; ")
}
// Database
new_dns.WriteString("DATABASE=")
new_dns.WriteString(params["database"])
new_dns.WriteString("; ")
// Protocol
if params["protocol"] != "" {
new_dns.WriteString("PROTOCOL=")
new_dns.WriteString(params["protocol"])
new_dns.WriteString("; ")
}
// user
new_dns.WriteString("UID=")
new_dns.WriteString(params["user id"])
new_dns.WriteString("; ")
// password
new_dns.WriteString("PWD=")
new_dns.WriteString(params["password"])
new_dns.WriteString("; ")
return new_dns.String()
}
// Check if db2 server returns an error message indicating
// that something is wrong with password or login so that cnx is reset
// and the next call, tries to recompute the login/passwd only if auth_key has changed.
//
// "ORA-01005: null password given; logon denied\n"
//
// "ORA-01017: invalid username/password; logon denied\n
func check_login_error(err error) bool {
check := false
if strings.HasPrefix(err.Error(), "ORA-01005") ||
strings.HasPrefix(err.Error(), "ORA-01017") {
check = true
}
return check
}