Skip to content

Commit f0e3a08

Browse files
authored
Expose dbsql.connOption type (#202)
Fixes: #201 That way we can programmatically add arguments to the `NewConnector` function instead of copy/pasting all of them across conditionals. Signed-off-by: Miguel Palau <[email protected]>
1 parent a21b124 commit f0e3a08

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

connector.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
5151
},
5252
CanUseMultipleCatalogs: &c.cfg.CanUseMultipleCatalogs,
5353
})
54-
5554
if err != nil {
5655
return nil, dbsqlerrint.NewRequestError(ctx, fmt.Sprintf("error connecting: host=%s port=%d, httpPath=%s", c.cfg.Host, c.cfg.Port, c.cfg.HTTPPath), err)
5756
}
@@ -84,11 +83,11 @@ func (c *connector) Driver() driver.Driver {
8483

8584
var _ driver.Connector = (*connector)(nil)
8685

87-
type connOption func(*config.Config)
86+
type ConnOption func(*config.Config)
8887

8988
// NewConnector creates a connection that can be used with `sql.OpenDB()`.
9089
// This is an easier way to set up the DB instead of having to construct a DSN string.
91-
func NewConnector(options ...connOption) (driver.Connector, error) {
90+
func NewConnector(options ...ConnOption) (driver.Connector, error) {
9291
// config with default options
9392
cfg := config.WithDefaults()
9493
cfg.DriverVersion = DriverVersion
@@ -102,14 +101,14 @@ func NewConnector(options ...connOption) (driver.Connector, error) {
102101
return &connector{cfg: cfg, client: client}, nil
103102
}
104103

105-
func withUserConfig(ucfg config.UserConfig) connOption {
104+
func withUserConfig(ucfg config.UserConfig) ConnOption {
106105
return func(c *config.Config) {
107106
c.UserConfig = ucfg
108107
}
109108
}
110109

111110
// WithServerHostname sets up the server hostname. Mandatory.
112-
func WithServerHostname(host string) connOption {
111+
func WithServerHostname(host string) ConnOption {
113112
return func(c *config.Config) {
114113
protocol, hostname := parseHostName(host)
115114
if protocol != "" {
@@ -143,7 +142,7 @@ func parseHostName(host string) (protocol, hostname string) {
143142
}
144143

145144
// WithPort sets up the server port. Mandatory.
146-
func WithPort(port int) connOption {
145+
func WithPort(port int) ConnOption {
147146
return func(c *config.Config) {
148147
c.Port = port
149148
}
@@ -153,7 +152,7 @@ func WithPort(port int) connOption {
153152
// By default retryWaitMin = 1 * time.Second
154153
// By default retryWaitMax = 30 * time.Second
155154
// By default retryMax = 4
156-
func WithRetries(retryMax int, retryWaitMin time.Duration, retryWaitMax time.Duration) connOption {
155+
func WithRetries(retryMax int, retryWaitMin time.Duration, retryWaitMax time.Duration) ConnOption {
157156
return func(c *config.Config) {
158157
c.RetryWaitMax = retryWaitMax
159158
c.RetryWaitMin = retryWaitMin
@@ -162,7 +161,7 @@ func WithRetries(retryMax int, retryWaitMin time.Duration, retryWaitMax time.Dur
162161
}
163162

164163
// WithAccessToken sets up the Personal Access Token. Mandatory for now.
165-
func WithAccessToken(token string) connOption {
164+
func WithAccessToken(token string) ConnOption {
166165
return func(c *config.Config) {
167166
if token != "" {
168167
c.AccessToken = token
@@ -175,7 +174,7 @@ func WithAccessToken(token string) connOption {
175174
}
176175

177176
// WithHTTPPath sets up the endpoint to the warehouse. Mandatory.
178-
func WithHTTPPath(path string) connOption {
177+
func WithHTTPPath(path string) ConnOption {
179178
return func(c *config.Config) {
180179
if !strings.HasPrefix(path, "/") {
181180
path = "/" + path
@@ -185,7 +184,7 @@ func WithHTTPPath(path string) connOption {
185184
}
186185

187186
// WithMaxRows sets up the max rows fetched per request. Default is 10000
188-
func WithMaxRows(n int) connOption {
187+
func WithMaxRows(n int) ConnOption {
189188
return func(c *config.Config) {
190189
if n != 0 {
191190
c.MaxRows = n
@@ -194,31 +193,31 @@ func WithMaxRows(n int) connOption {
194193
}
195194

196195
// WithTimeout adds timeout for the server query execution. Default is no timeout.
197-
func WithTimeout(n time.Duration) connOption {
196+
func WithTimeout(n time.Duration) ConnOption {
198197
return func(c *config.Config) {
199198
c.QueryTimeout = n
200199
}
201200
}
202201

203202
// Sets the initial catalog name and schema name in the session.
204203
// Use <select * from foo> instead of <select * from catalog.schema.foo>
205-
func WithInitialNamespace(catalog, schema string) connOption {
204+
func WithInitialNamespace(catalog, schema string) ConnOption {
206205
return func(c *config.Config) {
207206
c.Catalog = catalog
208207
c.Schema = schema
209208
}
210209
}
211210

212211
// Used to identify partners. Set as a string with format <isv-name+product-name>.
213-
func WithUserAgentEntry(entry string) connOption {
212+
func WithUserAgentEntry(entry string) ConnOption {
214213
return func(c *config.Config) {
215214
c.UserAgentEntry = entry
216215
}
217216
}
218217

219218
// Sessions params will be set upon opening the session by calling SET function.
220219
// If using connection pool, session params can avoid successive calls of "SET ..."
221-
func WithSessionParams(params map[string]string) connOption {
220+
func WithSessionParams(params map[string]string) ConnOption {
222221
return func(c *config.Config) {
223222
for k, v := range params {
224223
if strings.ToLower(k) == "timezone" {
@@ -227,7 +226,6 @@ func WithSessionParams(params map[string]string) connOption {
227226
} else {
228227
c.Location = loc
229228
}
230-
231229
}
232230
}
233231
c.SessionParams = params
@@ -249,35 +247,35 @@ func WithSkipTLSHostVerify() connOption {
249247
}
250248

251249
// WithAuthenticator sets up the Authentication. Mandatory if access token is not provided.
252-
func WithAuthenticator(authr auth.Authenticator) connOption {
250+
func WithAuthenticator(authr auth.Authenticator) ConnOption {
253251
return func(c *config.Config) {
254252
c.Authenticator = authr
255253
}
256254
}
257255

258256
// WithTransport sets up the transport configuration to be used by the httpclient.
259-
func WithTransport(t http.RoundTripper) connOption {
257+
func WithTransport(t http.RoundTripper) ConnOption {
260258
return func(c *config.Config) {
261259
c.Transport = t
262260
}
263261
}
264262

265263
// WithCloudFetch sets up the use of cloud fetch for query execution. Default is false.
266-
func WithCloudFetch(useCloudFetch bool) connOption {
264+
func WithCloudFetch(useCloudFetch bool) ConnOption {
267265
return func(c *config.Config) {
268266
c.UseCloudFetch = useCloudFetch
269267
}
270268
}
271269

272270
// WithMaxDownloadThreads sets up maximum download threads for cloud fetch. Default is 10.
273-
func WithMaxDownloadThreads(numThreads int) connOption {
271+
func WithMaxDownloadThreads(numThreads int) ConnOption {
274272
return func(c *config.Config) {
275273
c.MaxDownloadThreads = numThreads
276274
}
277275
}
278276

279277
// Setup of Oauth M2m authentication
280-
func WithClientCredentials(clientID, clientSecret string) connOption {
278+
func WithClientCredentials(clientID, clientSecret string) ConnOption {
281279
return func(c *config.Config) {
282280
if clientID != "" && clientSecret != "" {
283281
authr := m2m.NewAuthenticator(clientID, clientSecret, c.Host)

0 commit comments

Comments
 (0)