Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ClickHouse.config do |config|
config.url = 'http://localhost:8123'
config.timeout = 60
config.open_timeout = 3
config.read_timeout = 50
config.ssl_verify = false
# set to true to symbolize keys for SELECT and INSERT statements (type casting)
config.symbolize_keys = false
Expand Down Expand Up @@ -414,6 +415,7 @@ default: &default
url: http://localhost:8123
timeout: 60
open_timeout: 3
read_timeout: 50

development:
database: ecliptic_development
Expand Down
4 changes: 4 additions & 0 deletions lib/click_house/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module ClickHouse
class Config
DEFAULTS = {
adapter: Faraday.default_adapter,
adapter_options: [],
url: nil,
scheme: 'http',
host: 'localhost',
Expand All @@ -14,6 +15,7 @@ class Config
password: nil,
timeout: nil,
open_timeout: nil,
read_timeout: nil,
ssl_verify: false,
headers: {},
global_params: {},
Expand All @@ -36,6 +38,7 @@ class Config
}.freeze

attr_accessor :adapter
attr_accessor :adapter_options
attr_accessor :logger
attr_accessor :scheme
attr_accessor :host
Expand All @@ -46,6 +49,7 @@ class Config
attr_accessor :password
attr_accessor :timeout
attr_accessor :open_timeout
attr_accessor :read_timeout
attr_accessor :ssl_verify
attr_accessor :headers
attr_accessor :global_params
Expand Down
3 changes: 2 additions & 1 deletion lib/click_house/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def transport
@transport ||= Faraday.new(config.url!) do |conn|
conn.options.timeout = config.timeout
conn.options.open_timeout = config.open_timeout
conn.options.read_timeout = config.read_timeout
conn.headers = config.headers
conn.ssl.verify = config.ssl_verify

Expand All @@ -68,7 +69,7 @@ def transport
conn.response Middleware::SummaryMiddleware, options: { config: config } # should be after logger
conn.response config.json_parser, content_type: %r{application/json}, options: { config: config }
conn.response Middleware::ParseCsv, content_type: %r{text/csv}, options: { config: config }
conn.adapter config.adapter
conn.adapter config.adapter, *config.adapter_options
end
end
# rubocop:enable Metrics/AbcSize
Expand Down
14 changes: 12 additions & 2 deletions lib/click_house/response/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@
module ClickHouse
module Response
class Factory
KEY_META = 'meta'
KEY_DATA = 'data'
KEY_META = "meta"
KEY_DATA = "data"

# @return [ResultSet]
# @params faraday [Faraday::Response]
# @params config [Config]
def self.response(faraday, config)
body = faraday.body

# Clickhouse can return strings containing JSON sometimes, where the
# content-type is text/plain but the body is actually JSON
if faraday.headers["content-type"].start_with?("text/plain") && body.is_a?(String)
begin
parsed_body = JSON.parse(body)
body = parsed_body if parsed_body.is_a?(Hash)
rescue JSON::ParserError
end
end

# wrap to be able to use connection#select_one, connection#select_value
# with other formats like binary
return raw(faraday, config) unless body.is_a?(Hash)
Expand Down