Module: ClickHouse::Extend::ConnectionInserting

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

Constant Summary collapse

DEFAULT_JSON_EACH_ROW_FORMAT =
'JSONEachRow'
DEFAULT_JSON_COMPACT_EACH_ROW_FORMAT =
'JSONCompactEachRow'

Instance Method Summary collapse

Instance Method Details

#insert(table, body = [], **opts) {|body| ... } ⇒ Boolean, Response::Execution

Example with a block

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

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

end

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

Parameters:

  • body (Array, Hash) (defaults to: [])

Yields:

  • (body)

Returns:

  • (Boolean)
  • (Response::Execution)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/click_house/extend/connection_inserting.rb', line 20

def insert(table, body = [], **opts)
  # In Ruby < 3.0, if the last argument is a hash, and the method being called
  # accepts keyword arguments, then it is always converted to keyword arguments.
  columns = opts.fetch(:columns, [])
  values =  opts.fetch(:values, [])
  format = opts.fetch(:format, nil)

  yield(body) if block_given?

  # values: [{id: 1}]
  if values.any? && columns.empty?
    return insert_rows(table, values, format: format)
  end

  # body: [{id: 1}]
  if body.any? && columns.empty?
    return insert_rows(table, body, format: format)
  end

  # body: [1], columns: ["id"]
  if body.any? && columns.any?
    return insert_compact(table, columns: columns, values: body, format: format)
  end

  # columns: ["id"], values: [[1]]
  if columns.any? && values.any?
    return insert_compact(table, columns: columns, values: values, format: format)
  end

  Response::Factory.empty_exec(config)
end

#insert_compact(table, columns: [], values: [], format: nil) {|values| ... } ⇒ Response::Execution

Yields:

  • (values)

Returns:

  • (Response::Execution)


75
76
77
78
79
80
81
82
# File 'lib/click_house/extend/connection_inserting.rb', line 75

def insert_compact(table, columns: [], values: [], format: nil)
  format ||= DEFAULT_JSON_COMPACT_EACH_ROW_FORMAT

  yield(values) if block_given?

  response = execute("INSERT INTO #{table} (#{columns.join(',')}) FORMAT #{format}", config.json_serializer.dump_each_row(values))
  Response::Factory.exec(response)
end

#insert_rows(table, body, format: nil) ⇒ Response::Execution

Sometimes it’s needed to use other format than JSONEachRow For example if you want to send BigDecimal’s you could use JSONStringsEachRow format so string representation of BigDecimal will be parsed

Parameters:

  • table (String)
  • body (Array, Hash)
  • format (String) (defaults to: nil)

Returns:

  • (Response::Execution)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/click_house/extend/connection_inserting.rb', line 61

def insert_rows(table, body, format: nil)
  format ||= DEFAULT_JSON_EACH_ROW_FORMAT

  case body
  when Hash
    Response::Factory.exec(execute("INSERT INTO #{table} FORMAT #{format}", config.json_serializer.dump(body)))
  when Array
    Response::Factory.exec(execute("INSERT INTO #{table} FORMAT #{format}", config.json_serializer.dump_each_row(body)))
  else
    raise ArgumentError, "unknown body class <#{body.class}>"
  end
end