module Clickhouse::Table::ClassMethods

Public Instance Methods

connection() click to toggle source
# File lib/clickhouse/table.rb, line 75
def connection
  ::Clickhouse.connection
end
create(row)
Alias for: insert_row
empty_row() click to toggle source
# File lib/clickhouse/table.rb, line 52
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_row(row) { |row| ... } click to toggle source
# File lib/clickhouse/table.rb, line 14
def insert_row(row)
  return if row.nil?

  connection.insert_rows(table_name) do
    complete_row = prepare_row(block_given? ? yield(row) : row)

    [complete_row]
  end
end
Also aliased as: create
insert_rows(batch_rows) { |row| ... } click to toggle source
# File lib/clickhouse/table.rb, line 25
def insert_rows(batch_rows)
  connection.insert_rows(table_name) do |table_rows|
    batch_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
logger() click to toggle source
# File lib/clickhouse/table.rb, line 79
def logger
  ::Rails.logger
end
prepare_row(row) click to toggle source
# File lib/clickhouse/table.rb, line 66
def prepare_row(row)
  validate_row(row)

  row.stringify_keys!
  crop_row(row)

  empty_row.merge(row)
end
rows(attributes = {}) click to toggle source
# File lib/clickhouse/table.rb, line 48
def rows(attributes = {})
  connection.select_rows(attributes.merge(from: table_name))
end
table_columns() click to toggle source
# File lib/clickhouse/table.rb, line 39
def table_columns
  @table_columns ||=
    connection.select_rows(
      select: 'name, type',
      from: 'system.columns',
      where: "table = '#{table_name}'"
    ).to_h
end
table_name() click to toggle source
# File lib/clickhouse/table.rb, line 10
def table_name
  @table_name ||= to_s.tableize
end

Private Instance Methods

crop_row(row) click to toggle source
# File lib/clickhouse/table.rb, line 91
def crop_row(row)
  undefined_columns = row.keys - empty_row.keys

  return if undefined_columns.empty?

  logger.warn("Clickhouse: Undefined columns for #{table_name}: #{undefined_columns}")
  row.except!(*undefined_columns)
end
validate_row(row) click to toggle source
# File lib/clickhouse/table.rb, line 85
def validate_row(row)
  return if row.is_a?(Hash)

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