@@ -50,17 +50,42 @@ type DBInterface interface {
50
50
51
51
func NewDBClient (conf * config.Config ) (DBInterface , error ) {
52
52
ctx := context .Background ()
53
- log .Println ("Connecting to Clickhouse DB and creating schemas..." )
54
- splconn , err := clickhouse .Open (& clickhouse.Options {
55
- Addr : []string {fmt .Sprintf ("%s:%d" , conf .DBAddress , conf .DbPort )},
56
- Debug : true ,
57
- Debugf : func (format string , v ... any ) {
58
- fmt .Printf (format , v )
59
- },
60
- Settings : clickhouse.Settings {
61
- "allow_experimental_object_type" : 1 ,
62
- },
63
- })
53
+ var connOptions clickhouse.Options
54
+
55
+ if conf .ClickHouseUsername != "" && conf .ClickHousePassword != "" {
56
+ fmt .Println ("Using provided username and password" )
57
+ connOptions = clickhouse.Options {
58
+ Addr : []string {fmt .Sprintf ("%s:%d" , conf .DBAddress , conf .DbPort )},
59
+ Debug : true ,
60
+ Auth : clickhouse.Auth {
61
+ Username : conf .ClickHouseUsername ,
62
+ Password : conf .ClickHousePassword ,
63
+ },
64
+ Debugf : func (format string , v ... interface {}) {
65
+ fmt .Printf (format , v ... )
66
+ },
67
+ Settings : clickhouse.Settings {
68
+ "allow_experimental_object_type" : 1 ,
69
+ },
70
+ }
71
+ fmt .Printf ("Connecting to ClickHouse using username and password" )
72
+ } else {
73
+ fmt .Println ("Using connection without username and password" )
74
+ connOptions = clickhouse.Options {
75
+ Addr : []string {fmt .Sprintf ("%s:%d" , conf .DBAddress , conf .DbPort )},
76
+ Debug : true ,
77
+ Debugf : func (format string , v ... interface {}) {
78
+ fmt .Printf (format , v ... )
79
+ },
80
+ Settings : clickhouse.Settings {
81
+ "allow_experimental_object_type" : 1 ,
82
+ },
83
+ }
84
+ fmt .Printf ("Connecting to ClickHouse without usename and password" )
85
+
86
+ }
87
+
88
+ splconn , err := clickhouse .Open (& connOptions )
64
89
if err != nil {
65
90
return nil , err
66
91
}
@@ -69,7 +94,7 @@ func NewDBClient(conf *config.Config) (DBInterface, error) {
69
94
if exception , ok := err .(* clickhouse.Exception ); ok {
70
95
fmt .Printf ("[%d] %s \n %s\n " , exception .Code , exception .Message , exception .StackTrace )
71
96
} else {
72
- fmt .Println (err )
97
+ fmt .Println ("Authentication error:" , err ) // Print the error message here
73
98
}
74
99
return nil , err
75
100
}
@@ -80,17 +105,36 @@ func NewDBClient(conf *config.Config) (DBInterface, error) {
80
105
// return nil, err
81
106
// }
82
107
// }
83
- stdconn := clickhouse .OpenDB (& clickhouse.Options {
84
- Addr : []string {fmt .Sprintf ("%s:%d" , conf .DBAddress , conf .DbPort )},
85
- })
108
+ var connOption clickhouse.Options
109
+
110
+ if conf .ClickHouseUsername != "" && conf .ClickHousePassword != "" {
111
+ fmt .Println ("Using provided username and password" )
112
+ connOption = clickhouse.Options {
113
+ Addr : []string {fmt .Sprintf ("%s:%d" , conf .DBAddress , conf .DbPort )},
114
+ Debug : true ,
115
+ Auth : clickhouse.Auth {
116
+ Username : conf .ClickHouseUsername ,
117
+ Password : conf .ClickHousePassword ,
118
+ },
119
+ }
120
+ } else {
121
+ fmt .Println ("Using connection without username and password" )
122
+ connOption = clickhouse.Options {
123
+ Addr : []string {fmt .Sprintf ("%s:%d" , conf .DBAddress , conf .DbPort )},
124
+ }
125
+ }
126
+
127
+ stdconn := clickhouse .OpenDB (& connOption )
128
+
86
129
if err := stdconn .Ping (); err != nil {
87
130
if exception , ok := err .(* clickhouse.Exception ); ok {
88
131
fmt .Printf ("[%d] %s \n %s\n " , exception .Code , exception .Message , exception .StackTrace )
89
132
} else {
90
- fmt .Println (err )
133
+ fmt .Println ("Authentication error:" , err )
91
134
}
92
135
return nil , err
93
136
}
137
+
94
138
return & DBClient {splconn : splconn , conn : stdconn , conf : conf }, nil
95
139
}
96
140
0 commit comments