Class: ActiveRecord::ConnectionAdapters::ClickHouse::HTTPConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/clickhouse/http_connection.rb

Overview

Raw connection to a ClickHouse server over its HTTP interface: one persistent keep-alive Net::HTTP socket per adapter instance (the adapter lock serializes use). Results arrive as RowBinaryWithNamesAndTypes by default — names, type strings, then packed binary rows — so every value comes back with its server type; queries whose types have no binary decoder retry transparently on the JSON wire (select_format: :json in the config forces JSON for everything).

Defined Under Namespace

Classes: ChunkedBody, ExecutionError, RawResult

Constant Summary collapse

BINARY_FORMAT =
"RowBinaryWithNamesAndTypes"
JSON_FORMAT =
"JSONCompactEachRowWithNamesAndTypes"

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HTTPConnection

Returns a new instance of HTTPConnection.



57
58
59
60
61
# File 'lib/active_record/connection_adapters/clickhouse/http_connection.rb', line 57

def initialize(config)
  @config = config
  @select_format = config[:select_format].to_s == "json" ? JSON_FORMAT : BINARY_FORMAT
  @http = build_http
end

Instance Method Details

#closeObject



122
123
124
125
126
# File 'lib/active_record/connection_adapters/clickhouse/http_connection.rb', line 122

def close
  @http.finish if @http.started?
rescue IOError
  nil
end

#execute(sql, params: {}) ⇒ Object



90
91
92
93
94
# File 'lib/active_record/connection_adapters/clickhouse/http_connection.rb', line 90

def execute(sql, params: {})
  parse(perform(sql, params, @select_format), @select_format)
rescue RowBinary::Undecodable
  parse(perform(sql, params, JSON_FORMAT), JSON_FORMAT)
end

#execute_stream(sql, lines) ⇒ Object

Streams pre-encoded body lines as one chunked POST; the statement travels in the query string because the request body is the data.



98
99
100
101
102
103
# File 'lib/active_record/connection_adapters/clickhouse/http_connection.rb', line 98

def execute_stream(sql, lines)
  request = post_request(query_params({}, JSON_FORMAT).merge(query: sql))
  request["Transfer-Encoding"] = "chunked"
  request.body_stream = ChunkedBody.new(lines)
  parse(raise_unless_success(@http.request(request)), JSON_FORMAT)
end

#pingObject



115
116
117
118
119
120
# File 'lib/active_record/connection_adapters/clickhouse/http_connection.rb', line 115

def ping
  execute("SELECT 1")
  true
rescue StandardError
  false
end

#with_request_settings(settings) ⇒ Object

Scopes extra server settings to the requests made inside the block — the write-side counterpart of the SETTINGS clause SELECTs carry in-SQL.



107
108
109
110
111
112
113
# File 'lib/active_record/connection_adapters/clickhouse/http_connection.rb', line 107

def with_request_settings(settings)
  previous = @request_settings
  @request_settings = (previous || {}).merge(settings)
  yield
ensure
  @request_settings = previous
end