forked from PNixx/clickhouse-activerecord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Map data types in the schema
- Loading branch information
Showing
4 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
lib/active_record/connection_adapters/clickhouse/oid/map.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecord | ||
module ConnectionAdapters | ||
module Clickhouse | ||
module OID # :nodoc: | ||
class Map < Type::Value # :nodoc: | ||
def initialize(sql_type) | ||
match_data = /map\(([^,]+),\s([^\)]+)\)/i.match(sql_type) | ||
@key_type = ClickhouseAdapter::TYPE_MAP.lookup(match_data[1]) | ||
@value_type = ClickhouseAdapter::TYPE_MAP.lookup(match_data[2]) | ||
end | ||
|
||
def type | ||
@value_type.type | ||
end | ||
|
||
def key_type | ||
@key_type.type | ||
end | ||
|
||
def serialize(value) | ||
if value.is_a?(Hash) | ||
value.each_with_object({}) { |(k, v), h| h[@key_type.serialize(k)] = @value_type.serialize(v) } | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters