Module: Clickhouse::Table::ClassMethods

Defined in:
lib/clickhouse/table.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



61
62
63
# File 'lib/clickhouse/table.rb', line 61

def connection
  ::Clickhouse.connection
end

#empty_rowObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/clickhouse/table.rb', line 41

def empty_row
  @empty_row ||= table_columns.map do |k, v|
    value =
      case v
      when /UInt/ then 0
      when /Float/ then 0.0
      else
        ''
      end

    [k, value]
  end.to_h
end

#insert_rows(rows) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/clickhouse/table.rb', line 14

def insert_rows(rows)
  connection.insert_rows(table_name) do |table_rows|
    rows.each do |row|
      next if row.nil?

      complete_row = prepare_row(block_given? ? yield(row) : row)

      table_rows << complete_row
    end

    table_rows
  end
end

#prepare_row(row) ⇒ Object

Raises:



55
56
57
58
59
# File 'lib/clickhouse/table.rb', line 55

def prepare_row(row)
  return empty_row.merge(row.stringify_keys) if row.is_a?(Hash)

  raise WrongTypeRowError, "#{row.inspect} has wrong type"
end

#rows(attributes = {}) ⇒ Object



37
38
39
# File 'lib/clickhouse/table.rb', line 37

def rows(attributes = {})
  connection.select_rows(attributes.merge(from: table_name))
end

#table_columnsObject



28
29
30
31
32
33
34
35
# File 'lib/clickhouse/table.rb', line 28

def table_columns
  @table_columns ||=
    connection.select_rows(
      select: 'name, type',
      from: 'system.columns',
      where: "table = '#{table_name}'"
    ).to_h
end

#table_nameObject



10
11
12
# File 'lib/clickhouse/table.rb', line 10

def table_name
  @table_name ||= to_s.tableize
end