Module: ClickHouse::Extend::ConnectionDatabase

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

Instance Method Summary collapse

Instance Method Details

#create_database(name, if_not_exists: false, cluster: nil, engine: nil) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/click_house/extend/connection_database.rb', line 11

def create_database(name, if_not_exists: false, cluster: nil, engine: nil)
  sql = 'CREATE DATABASE %<exists>s %<name>s %<cluster>s %<engine>s'

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

  execute(format(sql, pattern), database: nil).success?
end

#databasesArray<String>

Returns:

  • (Array<String>)

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

def databases
  Array(execute('SHOW DATABASES FORMAT CSV', database: nil).body).tap(&:flatten!)
end

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


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/click_house/extend/connection_database.rb', line 24

def drop_database(name, if_exists: false, cluster: nil)
  sql = 'DROP DATABASE %<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), database: nil).success?
end