@@ -20,6 +20,7 @@ import (
20
20
"errors"
21
21
"fmt"
22
22
"reflect"
23
+ "strings"
23
24
"time"
24
25
25
26
_ "github.com/ClickHouse/clickhouse-go/v2"
@@ -40,6 +41,8 @@ type clickhouseMetadata struct {
40
41
ClickHouseURL string
41
42
Database string
42
43
Table string
44
+ Username string
45
+ Password string
43
46
}
44
47
45
48
func NewClickHouseStateStore (logger logger.Logger ) state.Store {
@@ -55,7 +58,28 @@ func (c *StateStore) Init(ctx context.Context, metadata state.Metadata) error {
55
58
}
56
59
c .config = config
57
60
58
- db , err := sql .Open ("clickhouse" , c .config .ClickHouseURL )
61
+ // Construct DSN with authentication if provided
62
+ dsn := c .config .ClickHouseURL
63
+ // If username and password are provided and not already in the URL, add them to the DSN
64
+ if c .config .Username != "" && ! strings .Contains (dsn , "username=" ) {
65
+ if ! strings .Contains (dsn , "?" ) {
66
+ dsn += "?"
67
+ } else {
68
+ dsn += "&"
69
+ }
70
+ dsn += "username=" + c .config .Username
71
+ }
72
+
73
+ if c .config .Password != "" && ! strings .Contains (dsn , "password=" ) {
74
+ if ! strings .Contains (dsn , "?" ) {
75
+ dsn += "?"
76
+ } else if ! strings .HasSuffix (dsn , "&" ) {
77
+ dsn += "&"
78
+ }
79
+ dsn += "password=" + c .config .Password
80
+ }
81
+
82
+ db , err := sql .Open ("clickhouse" , dsn )
59
83
if err != nil {
60
84
return fmt .Errorf ("error opening connection: %v" , err )
61
85
}
@@ -295,6 +319,15 @@ func parseAndValidateMetadata(metadata state.Metadata) (clickhouseMetadata, erro
295
319
return config , errors .New ("ClickHouse table name is missing" )
296
320
}
297
321
322
+ // Get username and password if provided
323
+ if val , ok := metadata .Properties ["username" ]; ok {
324
+ config .Username = val
325
+ }
326
+
327
+ if val , ok := metadata .Properties ["password" ]; ok {
328
+ config .Password = val
329
+ }
330
+
298
331
return config , nil
299
332
}
300
333
0 commit comments