Module: ClickHouse::Extend::ConnectionAltering

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

Instance Method Summary collapse

Instance Method Details

#add_column(table, name, type, default: nil, if_not_exists: false, after: nil, cluster: nil) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/click_house/extend/connection_altering.rb', line 7

def add_column(table, name, type, default: nil, if_not_exists: false, after: nil, cluster: nil)
  sql = 'ADD COLUMN %<exists>s %<name>s %<type>s %<default>s %<after>s'

  pattern = {
    name: name,
    exists: Util::Statement.ensure(if_not_exists, 'IF NOT EXISTS'),
    type: type,
    default: Util::Statement.ensure(default, "DEFAULT #{default}"),
    after: Util::Statement.ensure(after, "AFTER #{after}")
  }

  alter_table(table, format(sql, pattern), cluster: cluster)
end

#add_index(table_name, name, expression, type:, granularity: nil, after: nil, cluster: nil) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/click_house/extend/connection_altering.rb', line 70

def add_index(
  table_name,
  name,
  expression,
  type:,
  granularity: nil,
  after: nil,
  cluster: nil
)
  template = 'ADD INDEX %<name>s %<expression>s TYPE %<type>s GRANULARITY %<granularity>d %<after>s'
  pattern = {
    name: name,
    expression: expression,
    type: type,
    granularity: granularity,
    after: Util::Statement.ensure(after, "AFTER #{after}"),
  }

  alter_table(table_name, format(template, pattern), cluster: cluster)
end

#alter_table(name, sql = nil, cluster: nil) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/click_house/extend/connection_altering.rb', line 57

def alter_table(name, sql = nil, cluster: nil)
  template = 'ALTER TABLE %<name>s %<cluster>s %<sql>s'
  sql = yield(sql) if sql.nil?

  pattern = {
    name: name,
    sql: sql,
    cluster: Util::Statement.ensure(cluster, "ON CLUSTER #{cluster}"),
  }

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

#clear_column(table, name, partition:, if_exists: false, cluster: nil) ⇒ Object


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

def clear_column(table, name, partition:, if_exists: false, cluster: nil)
  sql = 'CLEAR COLUMN %<exists>s %<name>s %<partition>s'

  pattern = {
    name: name,
    exists: Util::Statement.ensure(if_exists, 'IF EXISTS'),
    partition: "IN PARTITION #{partition}"
  }

  alter_table(table, format(sql, pattern), cluster: cluster)
end

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


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

def drop_column(table, name, if_exists: false, cluster: nil)
  sql = 'DROP COLUMN %<exists>s %<name>s'

  pattern = {
    name: name,
    exists: Util::Statement.ensure(if_exists, 'IF EXISTS')
  }

  alter_table(table, format(sql, pattern), cluster: cluster)
end

#drop_index(table_name, name, cluster: nil) ⇒ Object


91
92
93
94
95
# File 'lib/click_house/extend/connection_altering.rb', line 91

def drop_index(table_name, name, cluster: nil)
  alter_table(table_name, <<~SQL, cluster: cluster)
    DROP INDEX #{name}
  SQL
end

#modify_column(table, name, type: nil, default: nil, if_exists: false, cluster: nil) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/click_house/extend/connection_altering.rb', line 44

def modify_column(table, name, type: nil, default: nil, if_exists: false, cluster: nil)
  sql = 'MODIFY COLUMN %<exists>s %<name>s %<type>s %<default>s'

  pattern = {
    name: name,
    type: type,
    exists: Util::Statement.ensure(if_exists, 'IF EXISTS'),
    default: Util::Statement.ensure(default, "DEFAULT #{default}")
  }

  alter_table(table, format(sql, pattern), cluster: cluster)
end