Skip to content

Commit

Permalink
Fix map
Browse files Browse the repository at this point in the history
  • Loading branch information
syabruk committed Aug 19, 2024
1 parent d87333c commit b231b3a
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions lib/active_record/connection_adapters/clickhouse/oid/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ module ConnectionAdapters
module Clickhouse
module OID # :nodoc:
class Map < Type::Value # :nodoc:
attr_reader :key_type, :value_type

def initialize(sql_type)
@subtype = case sql_type
when /U?Int\d+/
:integer
when /DateTime/
:datetime
when /Date/
:date
else
:string
end
types = sql_type.match(/Map\((.+),\s?(.+)\)/).captures

@key_type = cast_type(types.first)
@value_type = cast_type(types.last)
end

def type
@subtype
:map
end

def cast(value)
value
end

def deserialize(value)
if value.is_a?(::Hash)
value.map { |k, item| [k.to_s, deserialize(item)] }.to_h
else
return value if value.nil?
case @subtype
case @value_type
when :integer
value.to_i
when :datetime
Expand All @@ -46,7 +45,7 @@ def serialize(value)
value.map { |k, item| [k.to_s, serialize(item)] }.to_h
else
return value if value.nil?
case @subtype
case @value_type
when :integer
value.to_i
when :datetime
Expand All @@ -61,6 +60,40 @@ def serialize(value)
end
end

private

def cast_type(type)
return type if type.nil?

case type
when /U?Int\d+/
:integer
when /DateTime/
:datetime
when /Date/
:date
when /Array\(.*\)/
type
else
:string
end
end

def quote(value, type)
case cast_type(type)
when :string
"'#{value}'"
when :integer
value
when :datetime, :date
"'#{value.iso8601}'"
when /Array\(.*\)/
sub_type = type.match(/Array\((.+)\)/).captures.first
"[#{value.map { |v| quote(v, sub_type) }.join(', ')}]"
else
value
end
end
end
end
end
Expand Down

0 comments on commit b231b3a

Please sign in to comment.