Welcome to mirror list, hosted at ThFree Co, Russian Federation.

connection.rb « click_house « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 79551326d2da312b36beeaf3b4ae9a5567f4e673 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true

module ClickHouse
  class Connection
    def initialize(database, configuration = ClickHouse::Client.configuration)
      @database = database
      @configuration = configuration
    end

    def select(query)
      ClickHouse::Client.select(query, database, configuration)
    end

    def execute(query)
      ClickHouse::Client.execute(query, database, configuration)
    end

    def table_exists?(table_name)
      raw_query = <<~SQL.squish
        SELECT 1 FROM system.tables
        WHERE name = {table_name: String} AND database = {database_name: String}
      SQL

      database_name = configuration.databases[database]&.database
      placeholders = { table_name: table_name, database_name: database_name }

      query = ClickHouse::Client::Query.new(raw_query: raw_query, placeholders: placeholders)

      select(query).any?
    end

    private

    attr_reader :database, :configuration
  end
end