Skip to content

Commit ea004c1

Browse files
committed
use same connectioninfo struct for all handlers
1 parent df825ff commit ea004c1

File tree

4 files changed

+46
-40
lines changed

4 files changed

+46
-40
lines changed

pkg/detectors/jdbc/jdbc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ type pingResult struct {
217217
determinate bool
218218
}
219219

220+
// ConnectionInfo holds parsed connection information
221+
type ConnectionInfo struct {
222+
Host string // includes port if specified, e.g., "host:port"
223+
Database string
224+
User string
225+
Password string
226+
Params map[string]string
227+
}
228+
220229
type jdbc interface {
221230
ping(context.Context) pingResult
222231
}

pkg/detectors/jdbc/mysql.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,15 @@ import (
1313
)
1414

1515
type MysqlJDBC struct {
16-
Conn string
17-
User string
18-
Password string
19-
Host string
20-
Params string
21-
Database string
16+
ConnectionInfo
2217
}
2318

2419
func (s *MysqlJDBC) ping(ctx context.Context) pingResult {
2520
return ping(ctx, "mysql", isMySQLErrorDeterminate,
2621
BuildMySQLConnectionString(s.Host, "", s.User, s.Password, s.Params))
2722
}
2823

29-
func BuildMySQLConnectionString(host, database, user, password, params string) string {
24+
func BuildMySQLConnectionString(host, database, user, password string, params map[string]string) string {
3025
conn := host + "/" + database
3126
userPass := user
3227
if password != "" {
@@ -35,8 +30,12 @@ func BuildMySQLConnectionString(host, database, user, password, params string) s
3530
if userPass != "" {
3631
conn = userPass + "@" + conn
3732
}
38-
if params != "" {
39-
conn = conn + "?" + params
33+
if len(params) > 0 {
34+
var paramList []string
35+
for k, v := range params {
36+
paramList = append(paramList, fmt.Sprintf("%s=%s", k, v))
37+
}
38+
conn = conn + "?" + strings.Join(paramList, "&")
4039
}
4140
return conn
4241
}
@@ -77,12 +76,13 @@ func ParseMySQL(ctx logContext.Context, subname string) (jdbc, error) {
7776
return nil, fmt.Errorf("missing host or password in connection string")
7877
}
7978
return &MysqlJDBC{
80-
Conn: subname[2:],
81-
User: cfg.User,
82-
Password: cfg.Passwd,
83-
Host: fmt.Sprintf("tcp(%s)", cfg.Addr),
84-
Params: "timeout=5s",
85-
Database: cfg.DBName,
79+
ConnectionInfo: ConnectionInfo{
80+
User: cfg.User,
81+
Password: cfg.Passwd,
82+
Host: fmt.Sprintf("tcp(%s)", cfg.Addr),
83+
Params: map[string]string{"timeout": "5s"},
84+
Database: cfg.DBName,
85+
},
8686
}, nil
8787
}
8888

@@ -122,12 +122,13 @@ func parseMySQLURI(ctx logContext.Context, subname string) (jdbc, error) {
122122
}
123123

124124
return &MysqlJDBC{
125-
Conn: subname[2:],
126-
User: user,
127-
Password: pass,
128-
Host: fmt.Sprintf("tcp(%s)", u.Host),
129-
Params: "timeout=5s",
130-
Database: dbName,
125+
ConnectionInfo: ConnectionInfo{
126+
User: user,
127+
Password: pass,
128+
Host: fmt.Sprintf("tcp(%s)", u.Host),
129+
Params: map[string]string{"timeout": "5s"},
130+
Database: dbName,
131+
},
131132
}, nil
132133

133134
}

pkg/detectors/jdbc/postgres.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ import (
1313
)
1414

1515
type PostgresJDBC struct {
16-
Host string
17-
User string
18-
Password string
19-
Database string
20-
Params map[string]string
16+
ConnectionInfo
2117
}
2218

2319
func (s *PostgresJDBC) ping(ctx context.Context) pingResult {
@@ -84,9 +80,11 @@ func ParsePostgres(ctx logContext.Context, subname string) (jdbc, error) {
8480
}
8581

8682
postgresJDBC := &PostgresJDBC{
87-
Host: u.Host,
88-
Database: dbName,
89-
Params: params,
83+
ConnectionInfo: ConnectionInfo{
84+
Host: u.Host,
85+
Database: dbName,
86+
Params: params,
87+
},
9088
}
9189

9290
if u.User != nil {
@@ -134,7 +132,7 @@ func BuildPostgresConnectionString(host string, user string, password string, db
134132
if user != "" {
135133
data["user"] = user
136134
}
137-
if h, p, found := strings.Cut(host, ":"); found {
135+
if h, p, ok := strings.Cut(host, ":"); ok {
138136
data["host"] = h
139137
data["port"] = p
140138
}

pkg/detectors/jdbc/sqlserver.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ import (
1212
)
1313

1414
type SqlServerJDBC struct {
15-
Host string
16-
User string
17-
Password string
18-
Database string
19-
Params map[string]string
15+
ConnectionInfo
2016
}
2117

2218
func (s *SqlServerJDBC) ping(ctx context.Context) pingResult {
@@ -87,11 +83,13 @@ func ParseSqlServer(ctx logContext.Context, subname string) (jdbc, error) {
8783
}
8884

8985
return &SqlServerJDBC{
90-
Host: host + ":" + port,
91-
User: user,
92-
Password: password,
93-
Database: database,
94-
Params: params,
86+
ConnectionInfo: ConnectionInfo{
87+
Host: host + ":" + port,
88+
User: user,
89+
Password: password,
90+
Database: database,
91+
Params: params,
92+
},
9593
}, nil
9694
}
9795

0 commit comments

Comments
 (0)