Class: ClickhouseRuby::Types::Enum
- Defined in:
- lib/clickhouse_ruby/types/enum.rb
Overview
Type handler for ClickHouse Enum types
Handles: Enum, Enum8, Enum16
Enum types store predefined string values as integers for efficient storage. Supports both explicit value assignment (‘name’ = value) and auto-increment syntax.
Instance Attribute Summary collapse
-
#int_to_value ⇒ Hash{Integer => String}
readonly
Mapping of integers to string values.
-
#possible_values ⇒ Array<String>
readonly
The list of possible enum values.
-
#value_to_int ⇒ Hash{String => Integer}
readonly
Mapping of string values to integers.
Attributes inherited from Base
Instance Method Summary collapse
-
#cast(value) ⇒ String?
Converts a Ruby value to a valid enum value.
-
#deserialize(value) ⇒ String
Converts a value from ClickHouse response format to Ruby.
-
#initialize(name, arg_types: nil) ⇒ Enum
constructor
A new instance of Enum.
-
#serialize(value) ⇒ String
Converts a Ruby value to ClickHouse SQL literal format.
Methods inherited from Base
#==, #hash, #nullable?, #to_s
Constructor Details
#initialize(name, arg_types: nil) ⇒ Enum
Returns a new instance of Enum.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/clickhouse_ruby/types/enum.rb', line 22 def initialize(name, arg_types: nil) super(name) # When called from registry, we get arg_types with parsed enum entries. # When called directly (for testing), we get the full name like "Enum8('active' = 1, 'inactive' = 2)" if arg_types && !arg_types.empty? @possible_values, @value_to_int, @int_to_value = parse_enum_from_args(name, arg_types) else @possible_values, @value_to_int, @int_to_value = parse_enum_definition(name) end end |
Instance Attribute Details
#int_to_value ⇒ Hash{Integer => String} (readonly)
Returns mapping of integers to string values.
20 21 22 |
# File 'lib/clickhouse_ruby/types/enum.rb', line 20 def int_to_value @int_to_value end |
Instance Method Details
#cast(value) ⇒ String?
Converts a Ruby value to a valid enum value
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/clickhouse_ruby/types/enum.rb', line 39 def cast(value) return nil if value.nil? case value when ::String validate_string_value!(value) value when ::Integer validate_int_value!(value) @int_to_value[value] else raise_cast_error(value, "Cannot cast #{value.class} to #{self}") end end |
#deserialize(value) ⇒ String
Converts a value from ClickHouse response format to Ruby
58 59 60 |
# File 'lib/clickhouse_ruby/types/enum.rb', line 58 def deserialize(value) value.to_s end |
#serialize(value) ⇒ String
Converts a Ruby value to ClickHouse SQL literal format
66 67 68 69 70 71 |
# File 'lib/clickhouse_ruby/types/enum.rb', line 66 def serialize(value) return "NULL" if value.nil? escaped = value.to_s.gsub("'", "\\\\'") "'#{escaped}'" end |