Class: ActiveRecord::ConnectionAdapters::Clickhouse::OID::Map

Inherits:
Type::Value
  • Object
show all
Defined in:
lib/active_record/connection_adapters/clickhouse/oid/map.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(sql_type) ⇒ Map

Returns a new instance of Map.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_record/connection_adapters/clickhouse/oid/map.rb', line 9

def initialize(sql_type)
  case sql_type
  when /U?Int(\d+)/
    @subtype = :integer
    @limit = bits_to_limit(Regexp.last_match(1)&.to_i)
  when /DateTime/
    @subtype = :datetime
  when /Date/
    @subtype = :date
  else
    @subtype = :string
  end
end

Instance Method Details

#deserialize(value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_record/connection_adapters/clickhouse/oid/map.rb', line 27

def deserialize(value)
  if value.is_a?(::Hash)
    value.map { |k, item| [k.to_s, deserialize(item)] }.to_h
  elsif value.is_a?(::Array)
    value.map { |item| deserialize(item) }
  else
    return value if value.nil?
    case @subtype
      when :integer
        value.to_i
      when :datetime
        ::DateTime.parse(value)
      when :date
        ::Date.parse(value)
    else
      super
    end
  end
end

#serialize(value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/active_record/connection_adapters/clickhouse/oid/map.rb', line 47

def serialize(value)
  if value.is_a?(::Hash)
    value.map { |k, item| [k.to_s, serialize(item)] }.to_h
  elsif value.is_a?(::Array)
    value.map { |item| serialize(item) }
  else
    return value if value.nil?
    case @subtype
      when :integer
        value.to_i
      when :datetime
        DateTime.new.serialize(value)
      when :date
        Date.new.serialize(value)
      when :string
        value.to_s
    else
      super
    end
  end
end

#typeObject



23
24
25
# File 'lib/active_record/connection_adapters/clickhouse/oid/map.rb', line 23

def type
  @subtype
end