Module: Clickhouse::Connection::Query
- Included in:
- Clickhouse::Connection
- Defined in:
- lib/clickhouse/connection/query.rb,
lib/clickhouse/connection/query/table.rb,
lib/clickhouse/connection/query/result_row.rb,
lib/clickhouse/connection/query/result_set.rb
Defined Under Namespace
Classes: ResultRow, ResultSet, Table
Instance Method Summary
collapse
Instance Method Details
#count(options) ⇒ Object
72
73
74
75
|
# File 'lib/clickhouse/connection/query.rb', line 72
def count(options)
options = options.merge(:select => "COUNT(*)")
select_value(options).to_i
end
|
#create_table(name, &block) ⇒ Object
#databases ⇒ Object
20
21
22
|
# File 'lib/clickhouse/connection/query.rb', line 20
def databases
query("SHOW DATABASES").flatten
end
|
#describe_table(name) ⇒ Object
32
33
34
|
# File 'lib/clickhouse/connection/query.rb', line 32
def describe_table(name)
query("DESCRIBE TABLE #{name}").to_a
end
|
#drop_table(name) ⇒ Object
43
44
45
|
# File 'lib/clickhouse/connection/query.rb', line 43
def drop_table(name)
execute("DROP TABLE #{name}")
end
|
#execute(query, body = nil) ⇒ Object
9
10
11
12
|
# File 'lib/clickhouse/connection/query.rb', line 9
def execute(query, body = nil)
body = post(query, body)
body.empty? ? true : body
end
|
#insert_rows(table, options = {}) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/clickhouse/connection/query.rb', line 47
def insert_rows(table, options = {})
options[:csv] ||= begin
options[:rows] ||= yield([])
generate_csv options[:rows], options[:names]
end
execute("INSERT INTO #{table} FORMAT CSVWithNames", options[:csv])
end
|
#query(query) ⇒ Object
14
15
16
17
18
|
# File 'lib/clickhouse/connection/query.rb', line 14
def query(query)
query = Utils.(query)[0]
query += " FORMAT JSONCompact"
parse_data get(query)
end
|
#rename_table(*args) ⇒ Object
36
37
38
39
40
41
|
# File 'lib/clickhouse/connection/query.rb', line 36
def rename_table(*args)
names = (args[0].is_a?(Hash) ? args[0].to_a : [args]).flatten
raise Clickhouse::InvalidQueryError, "Odd number of table names" unless (names.size % 2) == 0
names = Hash[*names].collect{|(from, to)| "#{from} TO #{to}"}
execute("RENAME TABLE #{names.join(", ")}")
end
|
#select_row(options) ⇒ Object
59
60
61
|
# File 'lib/clickhouse/connection/query.rb', line 59
def select_row(options)
select_rows(options)[0]
end
|
#select_rows(options) ⇒ Object
55
56
57
|
# File 'lib/clickhouse/connection/query.rb', line 55
def select_rows(options)
query to_select_query(options)
end
|
#select_value(options) ⇒ Object
67
68
69
70
|
# File 'lib/clickhouse/connection/query.rb', line 67
def select_value(options)
values = select_values(options)
values[0] if values
end
|
#select_values(options) ⇒ Object
63
64
65
|
# File 'lib/clickhouse/connection/query.rb', line 63
def select_values(options)
select_rows(options).collect{|row| row[0]}
end
|
#tables ⇒ Object
24
25
26
|
# File 'lib/clickhouse/connection/query.rb', line 24
def tables
query("SHOW TABLES").flatten
end
|
#to_select_query(options) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/clickhouse/connection/query.rb', line 77
def to_select_query(options)
to_select_options(options).collect do |(key, value)|
next if value.nil? || (value.respond_to?(:empty?) && value.empty?)
statement = [key.to_s.upcase]
statement << "BY" if %W(GROUP ORDER).include?(statement[0])
statement << to_segment(key, value)
statement.join(" ")
end.compact.join("\n").force_encoding("UTF-8")
end
|