Module: ClickHouse::Extend::ConnectionInserting

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

Constant Summary collapse

EMPTY_INSERT =
true

Instance Method Summary collapse

Instance Method Details

#insert(table, columns: [], values: []) {|values| ... } ⇒ Boolean

Example with a block

insert('rspec', columns: %i[name id]) do |buffer|

buffer << ['Sun', 1]
buffer << ['Moon', 2]

end

Example with a param

subject.insert('rspec', values: [{ name: 'Sun', id: 1 }, { name: 'Moon', id: 2 }])

Yields:

  • (values)

Returns:

  • (Boolean)

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

def insert(table, columns: [], values: [])
  yield(values) if block_given?

  body = if columns.empty?
    values.map(&:to_json)
  else
    values.map { |value_row| columns.zip(value_row).to_h.to_json }
  end

  return EMPTY_INSERT if values.empty?

  execute("INSERT INTO #{table} FORMAT JSONEachRow", body.join("\n")).success?
end