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

TYPE_ARGV_DELIM =
','
NULLABLE =
'Nullable'
NULLABLE_TYPE_RE =
/#{NULLABLE}\((.+)\)/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta:, data:, totals: nil, statistics: nil) ⇒ ResultSet

Returns a new instance of ResultSet.

Parameters:


54
55
56
57
58
59
# File 'lib/click_house/response/result_set.rb', line 54

def initialize(meta:, data:, totals: nil, statistics: nil)
  @meta = meta
  @data = data
  @totals = totals
  @statistics = Hash(statistics)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data


17
18
19
# File 'lib/click_house/response/result_set.rb', line 17

def data
  @data
end

#metaObject (readonly)

Returns the value of attribute meta


17
18
19
# File 'lib/click_house/response/result_set.rb', line 17

def meta
  @meta
end

#statisticsObject (readonly)

Returns the value of attribute statistics


17
18
19
# File 'lib/click_house/response/result_set.rb', line 17

def statistics
  @statistics
end

#totalsObject (readonly)

Returns the value of attribute totals


17
18
19
# File 'lib/click_house/response/result_set.rb', line 17

def totals
  @totals
end

Class Method Details

.extract_type_info(type) ⇒ Array<String, Array>

  • first element is name of “ClickHouse.types.keys”

  • second element is extra arguments that should to be passed to <cast> function

Returns:

  • (Array<String, Array>)

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/click_house/response/result_set.rb', line 32

def extract_type_info(type)
  type = type.gsub(NULLABLE_TYPE_RE, '\1')
  nullable = Regexp.last_match(1)
  argv = []

  type = type.gsub(/\((.+)\)/, '')

  if (match = Regexp.last_match(1))
    counter = Array.new(match.count(TYPE_ARGV_DELIM).next) { '%s' }
    type = "#{type}(#{counter.join("#{TYPE_ARGV_DELIM} ")})"
    argv = match.split("#{TYPE_ARGV_DELIM} ")
  end

  [nullable ? "#{NULLABLE}(#{type})" : type, argv]
end

Instance Method Details

#to_aObject


61
62
63
64
65
66
67
68
# File 'lib/click_house/response/result_set.rb', line 61

def to_a
  @to_a ||= data.each do |row|
    row.each do |name, value|
      casting = types.fetch(name)
      row[name] = casting.fetch(:caster).cast(value, *casting.fetch(:arguments))
    end
  end
end

#typesObject


70
71
72
73
74
75
76
77
78
79
# File 'lib/click_house/response/result_set.rb', line 70

def types
  @types ||= meta.each_with_object({}) do |row, object|
    type_name, argv = self.class.extract_type_info(row.fetch('type'))

    object[row.fetch('name')] = {
      caster: ClickHouse.types[type_name],
      arguments: argv
    }
  end
end