Class: ClickHouse::SQL::PlaceholderType

Inherits:
Object
  • Object
show all
Defined in:
lib/clickhouse/sql/placeholder_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ ClickHouse::SQL::PlaceholderType

Parses and normalizes a ClickHouse placeholder type.

Parameters:

  • raw (String, #to_s)

    ClickHouse type text from a placeholder.



16
17
18
19
# File 'lib/clickhouse/sql/placeholder_type.rb', line 16

def initialize(raw)
  @raw = raw.to_s.strip
  @normalized = ClickHouse::SQL.normalize_placeholder_type(@raw)
end

Instance Attribute Details

#normalizedString (readonly)

Returns Whitespace-normalized ClickHouse placeholder type text.

Returns:

  • (String)

    Whitespace-normalized ClickHouse placeholder type text.



10
11
12
# File 'lib/clickhouse/sql/placeholder_type.rb', line 10

def normalized
  @normalized
end

#rawString (readonly)

Returns Raw ClickHouse placeholder type text.

Returns:

  • (String)

    Raw ClickHouse placeholder type text.



7
8
9
# File 'lib/clickhouse/sql/placeholder_type.rb', line 7

def raw
  @raw
end

Instance Method Details

#nullable?Boolean

Whether this type accepts nil, recursing through LowCardinality wrappers since ClickHouse only permits the LowCardinality(Nullable(T)) nesting order.

Returns:

  • (Boolean)

    True if nil is a valid value for this type.



69
70
71
72
73
74
75
# File 'lib/clickhouse/sql/placeholder_type.rb', line 69

def nullable?
  base, arguments = parse_base_and_arguments(raw)
  return true if base.casecmp?("Nullable")
  return self.class.new(arguments.first).nullable? if base.casecmp?("LowCardinality") && !arguments.first.to_s.empty?

  false
end

#validate_and_normalize(value) ⇒ Object

Validates and normalizes a Ruby value for this ClickHouse type.

Parameters:

  • value (Object)

    Ruby value to validate for the placeholder type.

Returns:

  • (Object)

    Value normalized for ClickHouse parameter serialization.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/clickhouse/sql/placeholder_type.rb', line 25

def validate_and_normalize(value)
  base, arguments = parse_base_and_arguments(raw)

  if value.nil?
    return nil if nullable?

    raise Error, "#{raw} placeholder does not accept nil"
  end

  case base.downcase
  when "array"
    validate_array(value, arguments)
  when "nullable", "lowcardinality"
    validate_wrapped(value, arguments)
  when "uuid"
    validate_uuid(value)
  when "identifier"
    validate_identifier(value)
  when "string", "fixedstring"
    validate_string(value)
  when "date"
    validate_date(value)
  when "datetime"
    validate_datetime(value, timezone: timezone_argument(arguments.first))
  when "datetime64"
    validate_datetime64(value, arguments)
  when "integer"
    validate_integer(value)
  when /\Auint(\d+)\z/
    validate_sized_integer(value, bits: Regexp.last_match(1).to_i, unsigned: true)
  when /\Aint(\d+)\z/
    validate_sized_integer(value, bits: Regexp.last_match(1).to_i, unsigned: false)
  when "float32", "float64"
    validate_numeric(value)
  else
    value
  end
end