Class: ClickHouse::Connection

Constant Summary

Constants included from Extend::ConnectionExplaining

Extend::ConnectionExplaining::EXPLAIN, Extend::ConnectionExplaining::EXPLAIN_RE

Constants included from Extend::ConnectionInserting

Extend::ConnectionInserting::EMPTY_INSERT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extend::ConnectionExplaining

#explain

Methods included from Extend::ConnectionAltering

#add_column, #add_index, #alter_table, #clear_column, #drop_column, #drop_index, #modify_column

Methods included from Extend::ConnectionInserting

#insert

Methods included from Extend::ConnectionSelective

#select_all, #select_one, #select_value

Methods included from Extend::ConnectionTable

#create_table, #describe_table, #drop_table, #rename_table, #table_exists?, #tables, #truncate_table, #truncate_tables

Methods included from Extend::ConnectionDatabase

#create_database, #databases, #drop_database

Methods included from Extend::ConnectionHealthy

#ping, #replicas_status

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection.

Parameters:


16
17
18
# File 'lib/click_house/connection.rb', line 16

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config


13
14
15
# File 'lib/click_house/connection.rb', line 13

def config
  @config
end

Instance Method Details

#compose(path, query = {}) ⇒ Object


64
65
66
67
# File 'lib/click_house/connection.rb', line 64

def compose(path, query = {})
  # without <query.compact> "DB::Exception: Empty query" error will occur
  "#{path}?#{URI.encode_www_form({ send_progress_in_http_headers: 1 }.merge(query).compact)}"
end

#execute(query, body = nil, database: config.database) ⇒ Object


20
21
22
# File 'lib/click_house/connection.rb', line 20

def execute(query, body = nil, database: config.database)
  post(body, query: { query: query }, database: database)
end

#get(path = '/', body: '', query: {}, database: config.database) ⇒ Faraday::Response

Examples:

get(body: 'select number from system.numbers limit 100', query: { max_rows_to_read: 10 })

Parameters:

  • path (String) (defaults to: '/')

    Clickhouse HTTP endpoint, e.g. /ping, /replica_status

  • body (String) (defaults to: '')

    SQL to run

  • database (String|NilClass) (defaults to: config.database)

    database to use, nil to skip

  • query (Hash) (defaults to: {})

    other CH settings to send through params, e.g. max_rows_to_read=1

Returns:

  • (Faraday::Response)

30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/click_house/connection.rb', line 30

def get(path = '/', body: '', query: {}, database: config.database)
  # backward compatibility since
  # https://github.com/shlima/click_house/pull/12/files#diff-9c6f3f06d3b575731eae4b6b95ddbcdcc20452c432b8f6e87a3a8e8645818107R24
  if query.is_a?(String)
    query = { query: query }
    config.logger!.warn('since v1.4.0 use connection.get(body: "SELECT 1") instead of connection.get(query: "SELECT 1")')
  end

  transport.get(path) do |conn|
    conn.params = query.merge(database: database).compact
    conn.params[:send_progress_in_http_headers] = 1 unless body.empty?
    conn.body = body
  end
end

#post(body = nil, query: {}, database: config.database) ⇒ Object


45
46
47
# File 'lib/click_house/connection.rb', line 45

def post(body = nil, query: {}, database: config.database)
  transport.post(compose('/', query.merge(database: database)), body)
end

#transportObject


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/click_house/connection.rb', line 49

def transport
  @transport ||= Faraday.new(config.url!) do |conn|
    conn.options.timeout = config.timeout
    conn.options.open_timeout = config.open_timeout
    conn.headers = config.headers
    conn.ssl.verify = config.ssl_verify
    conn.basic_auth(config.username, config.password) if config.auth?
    conn.response Middleware::Logging, logger: config.logger!
    conn.response Middleware::RaiseError
    conn.response :json, content_type: %r{application/json}
    conn.response Middleware::ParseCsv, content_type: %r{text/csv}
    conn.adapter config.adapter
  end
end