Class: ClickHouse::Client::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/click_house/client/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database:, url:, username:, password:, variables: {}) ⇒ Database

Returns a new instance of Database.



8
9
10
11
12
13
14
15
16
17
# File 'lib/click_house/client/database.rb', line 8

def initialize(database:, url:, username:, password:, variables: {})
  @database = database
  @url = url
  @username = username
  @password = password
  @variables = {
    database:,
    enable_http_compression: 1 # enable HTTP compression by default
  }.merge(variables).freeze
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



6
7
8
# File 'lib/click_house/client/database.rb', line 6

def database
  @database
end

Instance Method Details

#build_custom_uri(extra_variables: {}) ⇒ Object



23
24
25
26
27
# File 'lib/click_house/client/database.rb', line 23

def build_custom_uri(extra_variables: {})
  parsed = Addressable::URI.parse(@url)
  parsed.query_values = @variables.merge(extra_variables)
  parsed
end

#headersObject



29
30
31
32
33
34
35
# File 'lib/click_house/client/database.rb', line 29

def headers
  @headers ||= {
    'X-ClickHouse-User' => @username,
    'X-ClickHouse-Key' => @password,
    'X-ClickHouse-Format' => 'JSON' # always return JSON data
  }.freeze
end

#uriObject



19
20
21
# File 'lib/click_house/client/database.rb', line 19

def uri
  @uri ||= build_custom_uri
end

#with_default_databaseObject



37
38
39
40
41
42
43
44
45
# File 'lib/click_house/client/database.rb', line 37

def with_default_database
  self.class.new(
    database: 'default',
    url: @url,
    username: @username,
    password: @password,
    variables: @variables.merge(database: 'default')
  )
end