Class: ClickHouse::Response::ResultSet
- Inherits:
-
Object
- Object
- ClickHouse::Response::ResultSet
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#summary ⇒ Object
readonly
Returns the value of attribute summary.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(config:, meta:, data:, summary:, to_a: nil) ⇒ ResultSet
constructor
A new instance of ResultSet.
- #serialize(data) ⇒ Array, Hash
- #serialize_column(name, value) ⇒ Object
- #serialize_one(row) ⇒ Hash
- #to_a ⇒ Object
- #types ⇒ Hash<String, Ast::Statement>
Constructor Details
#initialize(config:, meta:, data:, summary:, to_a: nil) ⇒ ResultSet
Returns a new instance of ResultSet.
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 = @data = data @summary = summary @to_a = to_a end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
20 21 22 |
# File 'lib/click_house/response/result_set.rb', line 20 def config @config end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
20 21 22 |
# File 'lib/click_house/response/result_set.rb', line 20 def data @data end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
20 21 22 |
# File 'lib/click_house/response/result_set.rb', line 20 def @meta end |
#summary ⇒ Object (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
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
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
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.}", e.backtrace end |
#serialize_one(row) ⇒ 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_a ⇒ Object
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 |
#types ⇒ Hash<String, Ast::Statement>
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 ||= .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 |