Module: ClickHouse::Extend::ConnectionTable

Included in:
Connection
Defined in:
lib/click_house/extend/connection_table.rb

Instance Method Summary collapse

Instance Method Details

#create_table(name, if_not_exists: false, cluster: nil, partition: nil, order: nil, primary_key: nil, sample: nil, ttl: nil, settings: nil, engine:, &block) ⇒ Object

rubocop:disable Metrics/ParameterLists


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
75
# File 'lib/click_house/extend/connection_table.rb', line 42

def create_table(
  name,
  if_not_exists: false, cluster: nil,
  partition: nil, order: nil, primary_key: nil, sample: nil, ttl: nil, settings: nil,
  engine:,
  &block
)
  sql = <<~SQL
    CREATE TABLE %<exists>s %<name>s %<cluster>s %<definition>s %<engine>s
      %<partition>s
      %<order>s
      %<primary_key>s
      %<sample>s
      %<ttl>s
      %<settings>s
  SQL
  definition = ClickHouse::Definition::ColumnSet.new(&block)

  pattern = {
    name: name,
    exists: Util::Statement.ensure(if_not_exists, 'IF NOT EXISTS'),
    definition: definition.to_s,
    cluster: Util::Statement.ensure(cluster, "ON CLUSTER #{cluster}"),
    partition: Util::Statement.ensure(partition, "PARTITION BY #{partition}"),
    order: Util::Statement.ensure(order, "ORDER BY #{order}"),
    primary_key: Util::Statement.ensure(primary_key, "PRIMARY KEY #{primary_key}"),
    sample: Util::Statement.ensure(sample, "SAMPLE BY  #{sample}"),
    ttl: Util::Statement.ensure(ttl, "TTL #{ttl}"),
    settings: Util::Statement.ensure(settings, "SETTINGS #{settings}"),
    engine: Util::Statement.ensure(engine, "ENGINE = #{engine}")
  }

  execute(format(sql, pattern)).success?
end

#describe_table(name) ⇒ ResultSet

Returns:

  • (ResultSet)

12
13
14
# File 'lib/click_house/extend/connection_table.rb', line 12

def describe_table(name)
  Response::Factory[execute("DESCRIBE TABLE #{name} FORMAT JSON")]
end

#drop_table(name, temporary: false, if_exists: false, cluster: nil) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/click_house/extend/connection_table.rb', line 28

def drop_table(name, temporary: false, if_exists: false, cluster: nil)
  sql = 'DROP %<temporary>s TABLE %<exists>s %<name>s %<cluster>s'

  pattern = {
    name: name,
    temporary: Util::Statement.ensure(temporary, 'TEMPORARY'),
    exists: Util::Statement.ensure(if_exists, 'IF EXISTS'),
    cluster: Util::Statement.ensure(cluster, "ON CLUSTER #{cluster}"),
  }

  execute(format(sql, pattern)).success?
end

#rename_table(from, to, cluster: nil) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/click_house/extend/connection_table.rb', line 94

def rename_table(from, to, cluster: nil)
  from = Array(from)
  to = Array(to)

  unless from.length == to.length
    raise StatementException, '<from> tables length should equal <to> length'
  end

  sql = <<~SQL
    RENAME TABLE %<names>s %<cluster>s
  SQL

  pattern = {
    names: from.zip(to).map { |a| a.join(' TO ') }.join(', '),
    cluster: Util::Statement.ensure(cluster, "ON CLUSTER #{cluster}")
  }

  execute(format(sql, pattern)).success?
end

#table_exists?(name, temporary: false) ⇒ Boolean

Returns:

  • (Boolean)

17
18
19
20
21
22
23
24
25
26
# File 'lib/click_house/extend/connection_table.rb', line 17

def table_exists?(name, temporary: false)
  sql = 'EXISTS %<temporary>s TABLE  %<name>s FORMAT CSV'

  pattern = {
    name: name,
    temporary: Util::Statement.ensure(temporary, 'TEMPORARY')
  }

  Type::BooleanType.new.cast(execute(format(sql, pattern)).body.dig(0, 0))
end

#tablesArray<String>

Returns:

  • (Array<String>)

7
8
9
# File 'lib/click_house/extend/connection_table.rb', line 7

def tables
  Array(execute('SHOW TABLES FORMAT CSV').body).tap(&:flatten!)
end

#truncate_table(name, if_exists: false, cluster: nil) ⇒ Object

rubocop:enable Metrics/ParameterLists


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/click_house/extend/connection_table.rb', line 78

def truncate_table(name, if_exists: false, cluster: nil)
  sql = 'TRUNCATE TABLE %<exists>s %<name>s %<cluster>s'

  pattern = {
    name: name,
    exists: Util::Statement.ensure(if_exists, 'IF EXISTS'),
    cluster: Util::Statement.ensure(cluster, "ON CLUSTER #{cluster}")
  }

  execute(format(sql, pattern)).success?
end

#truncate_tables(names = tables, *argv) ⇒ Object


90
91
92
# File 'lib/click_house/extend/connection_table.rb', line 90

def truncate_tables(names = tables, *argv)
  Array(names).each { |name| truncate_table(name, *argv) }
end