This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
forked from mgit-at/sql_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_utils.go
210 lines (177 loc) · 4.34 KB
/
sql_utils.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"context"
"database/sql"
"fmt"
"net"
"net/url"
"strings"
)
// PingDB is a wrapper around sql.DB.PingContext() that terminates as soon as the context is closed.
//
// sql.DB does not actually pass along the context to the driver when opening a connection (which always happens if the
// database is down) and the driver uses an arbitrary timeout which may well be longer than ours. So we run the ping
// call in a goroutine and terminate immediately if the context is closed.
func PingDB(ctx context.Context, conn *sql.DB) error {
ch := make(chan error, 1)
go func() {
ch <- conn.PingContext(ctx)
close(ch)
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-ch:
return err
}
}
func splitConnectionStringURL(dsn string) (map[string]string, error) {
res := map[string]string{}
u, err := url.Parse(dsn)
if err != nil {
return res, err
}
if u.User != nil {
res["user id"] = u.User.Username()
p, exists := u.User.Password()
if exists {
// if strings.HasPrefix(p, "{") && strings.HasSuffix(p,"}") {
// p = p[1:len(p)-1]
// }
res["password"] = p
}
}
host, port, err := net.SplitHostPort(u.Host)
if err != nil {
host = u.Host
}
res["server"] = host
if len(u.Path) > 0 {
res["instance"] = u.Path[1:]
}
if len(port) > 0 {
res["port"] = port
}
query := u.Query()
for k, v := range query {
if len(v) > 1 {
return res, fmt.Errorf("key %s provided more than once", k)
}
res[strings.ToLower(k)] = v[0]
}
return res, nil
}
// #DATABASE=<database>; HOSTNAME=<hostname>; PORT=<port>; PROTOCOL=<protocol>; UID=<login>; PWD=<password>;
func splitRawConnectionStringDSN(dsn string) (map[string]string, error) {
res := map[string]string{}
m := make(url.Values)
for dsn != "" {
var key string
key, dsn, _ = strings.Cut(dsn, ";")
if key == "" {
continue
}
key, value, _ := strings.Cut(key, "=")
key = strings.Trim(key, " ")
key = strings.ToLower(key)
value = strings.Trim(value, " ")
m[key] = append(m[key], value)
}
for k, v := range m {
if len(v) > 1 {
return res, fmt.Errorf("key %s provided more than once", k)
}
switch k {
case "hostname":
k = "server"
case "uid", "user", "login":
k = "user id"
case "pwd", "passwd":
k = "password"
}
res[k] = v[0]
}
return res, nil
}
// extract key from symbol table returning a value cast to desired type
func GetMapValueString(symtab map[string]any, key string) string {
var value string
if value_raw, ok := symtab[key]; ok {
switch value_val := value_raw.(type) {
case string:
value = value_val
case int:
value = fmt.Sprintf("%d", value_val)
default:
value = ""
}
}
return value
}
// 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()
}
// generate DSN string in url format from parameters map
func GenDSNUrl(driver string, params map[string]string) string {
new_dns := new(strings.Builder)
new_dns.WriteString(driver)
new_dns.WriteString("://")
// Hostname
new_dns.WriteString(params["server"])
// Port
if params["port"] != "" {
new_dns.WriteString(":")
new_dns.WriteString(params["port"])
}
// instance
if params["instance"] != "" {
new_dns.WriteString("/")
new_dns.WriteString(params["instance"])
}
param_idx := 0
for key, val := range params {
if key == "server" || key == "port" {
continue
}
if param_idx == 0 {
new_dns.WriteString("?")
} else {
new_dns.WriteString("&")
}
new_dns.WriteString(url.QueryEscape(key))
new_dns.WriteString("=")
new_dns.WriteString(url.QueryEscape(val))
param_idx++
}
return new_dns.String()
}