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

client.rb « click_house « lib « click_house-client « gems - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 22c42d7be6eaa33893723d26f0cefd2f03aab655 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

require 'addressable'
require 'json'
require 'active_support/time'
require_relative "client/database"
require_relative "client/configuration"
require_relative "client/formatter"
require_relative "client/response"

module ClickHouse
  module Client
    class << self
      def configuration
        @configuration ||= Configuration.new
      end

      def configure
        yield(configuration)
        configuration.validate!
      end
    end

    Error = Class.new(StandardError)
    ConfigurationError = Class.new(Error)
    DatabaseError = Class.new(Error)

    # Executes a SELECT database query
    def self.select(query, database, configuration = self.configuration)
      db = lookup_database(configuration, database)

      response = configuration.http_post_proc.call(
        db.uri.to_s,
        db.headers,
        "#{query} FORMAT JSON" # always return JSON
      )

      raise DatabaseError, response.body unless response.success?

      Formatter.format(configuration.json_parser.parse(response.body))
    end

    # Executes any kinds of database query without returning any data (INSERT, DELETE)
    def self.execute(query, database, configuration = self.configuration)
      db = lookup_database(configuration, database)

      response = configuration.http_post_proc.call(
        db.uri.to_s,
        db.headers,
        query
      )

      raise DatabaseError, response.body unless response.success?

      true
    end

    private_class_method def self.lookup_database(configuration, database)
      configuration.databases[database].tap do |db|
        raise ConfigurationError, "The database '#{database}' is not configured" unless db
      end
    end
  end
end