Class: ClickhouseRuby::Types::Enum

Inherits:
Base
  • Object
show all
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

Attributes inherited from Base

#name

Instance Method Summary collapse

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_valueHash{Integer => String} (readonly)

Returns mapping of integers to string values.

Returns:

  • (Hash{Integer => String})

    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

#possible_valuesArray<String> (readonly)

Returns the list of possible enum values.

Returns:



14
15
16
# File 'lib/clickhouse_ruby/types/enum.rb', line 14

def possible_values
  @possible_values
end

#value_to_intHash{String => Integer} (readonly)

Returns mapping of string values to integers.

Returns:

  • (Hash{String => Integer})

    mapping of string values to integers



17
18
19
# File 'lib/clickhouse_ruby/types/enum.rb', line 17

def value_to_int
  @value_to_int
end

Instance Method Details

#cast(value) ⇒ String?

Converts a Ruby value to a valid enum value

Parameters:

Returns:

  • (String, nil)

    the enum value (string)

Raises:



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

Parameters:

  • value (Object)

    the value from ClickHouse

Returns:

  • (String)

    the string value



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

Parameters:

  • value (String, nil)

    the value to serialize

Returns:

  • (String)

    the SQL literal



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