Skip to content

Commit

Permalink
Merge pull request #9 from getlago/misc-map-array
Browse files Browse the repository at this point in the history
misc: Add support of array in map
  • Loading branch information
vincent-pochet authored Jul 9, 2024
2 parents 4876bb0 + 5e82649 commit df34b59
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
22 changes: 21 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,8 @@ def deserialize(value)
def serialize(value)
return '{}' if value.nil?

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

private
Expand All @@ -48,10 +49,29 @@ def cast_type(type)
: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\(.*\)/
byebug
sub_type = type.match(/Array\((.+)\)/).captures.first
"[#{value.map { |v| quote(v, sub_type) }.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
1 change: 1 addition & 0 deletions lib/clickhouse-activerecord/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def prepare_column_options(column)
if column.type == :map
spec[:key_type] = "\"#{column.key_type}\""
spec[:value_type] = "\"#{column.value_type}\""
spec[:array] = nil
end

spec.merge(super).compact
Expand Down

0 comments on commit df34b59

Please sign in to comment.