Class: ClickHouse::Response::ResultSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/click_house/response/result_set.rb

Constant Summary collapse

KEY_META_NAME =
'name'
KEY_META_TYPE =
'type'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, meta:, data:, summary:, to_a: nil) ⇒ ResultSet

Returns a new instance of ResultSet.

Parameters:



34
35
36
37
38
39
40
# File 'lib/click_house/response/result_set.rb', line 34

def initialize(config:, meta:, data:, summary:, to_a: nil)
  @config = config
  @meta = meta
  @data = data
  @summary = summary
  @to_a = to_a
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/click_house/response/result_set.rb', line 20

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



20
21
22
# File 'lib/click_house/response/result_set.rb', line 20

def data
  @data
end

#metaObject (readonly)

Returns the value of attribute meta.



20
21
22
# File 'lib/click_house/response/result_set.rb', line 20

def meta
  @meta
end

#summaryObject (readonly)

Returns the value of attribute summary.



20
21
22
# File 'lib/click_house/response/result_set.rb', line 20

def summary
  @summary
end

Class Method Details

.raw(config:, data:, summary:) ⇒ ResultSet

Parameters:

Returns:



25
26
27
# File 'lib/click_house/response/result_set.rb', line 25

def raw(config:, data:, summary:)
  new(config: config, data: data, to_a: data, meta: [], summary: summary)
end

Instance Method Details

#serialize(data) ⇒ Array, Hash

Parameters:

  • data (Array, Hash)

Returns:

  • (Array, Hash)


44
45
46
47
48
49
50
51
52
53
# File 'lib/click_house/response/result_set.rb', line 44

def serialize(data)
  case data
  when Hash
    serialize_one(data)
  when Array
    data.map(&method(:serialize_one))
  else
    raise ArgumentError, "expect Hash or Array, got: #{data.class}"
  end
end

#serialize_column(name, value) ⇒ Object

Parameters:

  • name (String)

    column name

  • value (Any)


65
66
67
68
69
70
71
72
# File 'lib/click_house/response/result_set.rb', line 65

def serialize_column(name, value)
  stmt = types.fetch(name)
  serialize_type(stmt, value)
rescue KeyError => e
  raise SerializeError, "field <#{name}> does not exists in table schema: #{types}", e.backtrace
rescue StandardError => e
  raise SerializeError, "failed to serialize <#{name}> with #{stmt}, #{e.class}, #{e.message}", e.backtrace
end

#serialize_one(row) ⇒ Hash

Parameters:

  • row (Hash)

Returns:

  • (Hash)


57
58
59
60
61
# File 'lib/click_house/response/result_set.rb', line 57

def serialize_one(row)
  row.each_with_object({}) do |(key, value), object|
    object[key] = serialize_column(key, value)
  end
end

#to_aObject



74
75
76
77
78
79
80
# File 'lib/click_house/response/result_set.rb', line 74

def to_a
  @to_a ||= data.each do |row|
    row.each do |name, value|
      row[name] = cast_type(types.fetch(name), value)
    end
  end
end

#typesHash<String, Ast::Statement>

Returns:



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/click_house/response/result_set.rb', line 83

def types
  @types ||= meta.each_with_object({}) do |row, object|
    column = row.fetch(config.key(KEY_META_NAME))
    # make symbol keys, if config.symbolize_keys is true,
    # to be able to cast and serialize properly
    object[config.key(column)] = begin
      current = Ast::Parser.new(row.fetch(config.key(KEY_META_TYPE))).parse
      assign_type(current)
      current
    end
  end
end