Skip to content

Commit

Permalink
misc: Add support of array in map
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Jul 9, 2024
1 parent 4876bb0 commit c724f9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/active_record/connection_adapters/clickhouse/oid/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ def deserialize(value)
def serialize(value)
return '{}' if value.nil?

"{#{value.map { |k, v| "'#{k}': '#{v}'" }.join(', ')}}"
res = value.map { |k, v| "#{quote(k)}: #{quote(v)}" }.join(', ')
res = "{#{res}}"

res
end

private
Expand All @@ -48,10 +51,23 @@ def cast_type(type)
:datetime
when /Date/
:date
when /Array\(*\)/
type
else
:string
end
end

def quote(value)
case value
when String, Symbol
"'#{value}'"
when ::Array
"[#{value.map { |v| quote(v) }.join(', ')}]"
else
value
end
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def cast_type(type)
:datetime
when /Date/
:date
when /Array/
type
else
:string
end
Expand Down Expand Up @@ -267,6 +269,8 @@ def quote(value)
case value
when Array
'[' + value.map { |v| quote(v) }.join(', ') + ']'
when Hash
'{' + value.map { |k, v| "#{quote(k)}: #{quote(v)}" }.join(', ') + '}'
else
super
end
Expand Down

0 comments on commit c724f9e

Please sign in to comment.