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
65
66
67
68
69
70
71
72
73
74
|
# frozen_string_literal: true
require 'addressable'
require 'json'
require 'active_support/time'
require 'active_support/notifications'
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)
instrumented_execute(query, database, configuration) do |response, instrument|
parsed_response = configuration.json_parser.parse(response.body)
instrument[:statistics] = parsed_response['statistics']&.symbolize_keys
Formatter.format(parsed_response)
end
end
# Executes any kinds of database query without returning any data (INSERT, DELETE)
def self.execute(query, database, configuration = self.configuration)
instrumented_execute(query, database, configuration) do |response, instrument|
if response.headers['x-clickhouse-summary']
instrument[:statistics] =
Gitlab::Json.parse(response.headers['x-clickhouse-summary']).symbolize_keys
end
end
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
private_class_method def self.instrumented_execute(query, database, configuration)
db = lookup_database(configuration, database)
ActiveSupport::Notifications.instrument('sql.click_house', { query: query, database: database }) do |instrument|
response = configuration.http_post_proc.call(
db.uri.to_s,
db.headers,
query
)
raise DatabaseError, response.body unless response.success?
yield response, instrument
end
end
end
end
|