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



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
76
77
78
79
80
# File 'lib/click_house/extend/connection_table.rb', line 47

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.response(execute("DESCRIBE TABLE #{name} FORMAT JSON"), config)
end

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



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/click_house/extend/connection_table.rb', line 33

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/click_house/extend/connection_table.rb', line 99

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)


22
23
24
25
26
27
28
29
30
31
# File 'lib/click_house/extend/connection_table.rb', line 22

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

#table_schema(name) ⇒ ResultSet

Returns:

  • (ResultSet)


17
18
19
# File 'lib/click_house/extend/connection_table.rb', line 17

def table_schema(name)
  Response::Factory.response(execute("SELECT * FROM #{name} WHERE 1=0 FORMAT JSON"), config)
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



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/click_house/extend/connection_table.rb', line 83

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



95
96
97
# File 'lib/click_house/extend/connection_table.rb', line 95

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