Skip to content

Commit

Permalink
Merge pull request #165 from vench/add_pass_param_from_context
Browse files Browse the repository at this point in the history
  • Loading branch information
DoubleDi authored Dec 4, 2022
2 parents b259988 + 18744eb commit 96d5fe5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (
QueryID key = iota
// QuotaKey uses for setting quota_key request param for request to Clickhouse
QuotaKey
// RequestQueryParams uses for custom setting request params for request to Clickhouse
// presented as map[string]string -> key1=value1&key2=value2... etc
RequestQueryParams

quotaKeyParamName = "quota_key"
queryIDParamName = "query_id"
Expand Down Expand Up @@ -340,7 +343,18 @@ func (c *conn) buildRequest(ctx context.Context, query string, params []driver.V
reqQuery.Add(queryIDParamName, queryID)
}

requestQueryParams, requestQueryParamsOk := ctx.Value(RequestQueryParams).(map[string]string)
if requestQueryParamsOk && len(requestQueryParams) != 0 {
if reqQuery == nil {
reqQuery = req.URL.Query()
}

for name, value := range requestQueryParams {
reqQuery.Add(name, value)
}
}
}

if reqQuery != nil {
req.URL.RawQuery = reqQuery.Encode()
}
Expand Down
12 changes: 12 additions & 0 deletions conn_go18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ func (s *connSuite) TestPing() {
s.EqualError(s.conn.PingContext(ctx), "context canceled")
}

func (s *connSuite) TestPassRequestQueryParamsFromContext() {
ctx := context.WithValue(context.Background(), RequestQueryParams, map[string]string{
"max_read_buffer_size": "1",
})
s.NoError(s.conn.PingContext(ctx))

ctx = context.WithValue(context.Background(), RequestQueryParams, map[string]string{
"no_cache": "1",
})
s.EqualError(s.conn.PingContext(ctx), "Code: 115, Message: Unknown setting no_cache")
}

func (s *connSuite) TestColumnTypes() {
rows, err := s.conn.Query("SELECT * FROM data LIMIT 1")
s.Require().NoError(err)
Expand Down

0 comments on commit 96d5fe5

Please sign in to comment.