Class: ActiveRecord::ConnectionAdapters::Clickhouse::TableDefinition

Inherits:
TableDefinition
  • Object
show all
Defined in:
lib/active_record/connection_adapters/clickhouse/schema_definitions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn, name, temporary: false, if_not_exists: false, options: nil, as: nil, comment: nil, view: false, materialized: false, to: nil) ⇒ TableDefinition

Returns a new instance of TableDefinition.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_record/connection_adapters/clickhouse/schema_definitions.rb', line 10

def initialize(
    conn,
    name,
    temporary: false,
    if_not_exists: false,
    options: nil,
    as: nil,
    comment: nil,
    view: false,
    materialized: false,
    to: nil,
    **
  )
  @conn = conn
  @columns_hash = {}
  @indexes = []
  @foreign_keys = []
  @primary_keys = nil
  @temporary = temporary
  @if_not_exists = if_not_exists
  @options = options
  @as = as
  @name = name
  @comment = comment
  @view = view || materialized
  @materialized = materialized
  @to = to
end

Instance Attribute Details

#if_not_existsObject (readonly)

Returns the value of attribute if_not_exists.



8
9
10
# File 'lib/active_record/connection_adapters/clickhouse/schema_definitions.rb', line 8

def if_not_exists
  @if_not_exists
end

#materializedObject (readonly)

Returns the value of attribute materialized.



8
9
10
# File 'lib/active_record/connection_adapters/clickhouse/schema_definitions.rb', line 8

def materialized
  @materialized
end

#toObject (readonly)

Returns the value of attribute to.



8
9
10
# File 'lib/active_record/connection_adapters/clickhouse/schema_definitions.rb', line 8

def to
  @to
end

#viewObject (readonly)

Returns the value of attribute view.



8
9
10
# File 'lib/active_record/connection_adapters/clickhouse/schema_definitions.rb', line 8

def view
  @view
end

Instance Method Details

#datetime(*args, **options) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/active_record/connection_adapters/clickhouse/schema_definitions.rb', line 66

def datetime(*args, **options)
  kind = :datetime

  if options[:precision]
    kind = :datetime64
  end

  args.each { |name| column(name, kind, **options) }
end

#enum(*args, **options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/active_record/connection_adapters/clickhouse/schema_definitions.rb', line 80

def enum(*args, **options)
  kind = :enum8

  unless options[:value].is_a? Hash
    raise ArgumentError, "Column #{args.first}: option 'value' must be Hash, got: #{options[:value].class}"
  end

  options[:value] = options[:value].each_with_object([]) { |(k, v), arr| arr.push("'#{k}' = #{v}") }.join(', ')

  if options[:limit]
    kind = :enum8  if options[:limit] == 1
    kind = :enum16 if options[:limit] == 2
  end

  args.each { |name| column(name, kind, **options.except(:limit)) }
end

#integer(*args, **options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_record/connection_adapters/clickhouse/schema_definitions.rb', line 39

def integer(*args, **options)
  # default to unsigned
  unsigned = options[:unsigned]
  unsigned = true if unsigned.nil?

  kind = :uint32 # default

  if options[:limit]
    if unsigned
      kind = :uint8       if options[:limit] == 1
      kind = :uint16      if options[:limit] == 2
      kind = :uint32      if [3,4].include?(options[:limit])
      kind = :uint64      if [5,6,7].include?(options[:limit])
      kind = :big_integer if options[:limit] == 8
      kind = :uint256     if options[:limit] > 8
    else
      kind = :int8       if options[:limit] == 1
      kind = :int16      if options[:limit] == 2
      kind = :int32      if [3,4].include?(options[:limit])
      kind = :int64     if options[:limit] > 5 && options[:limit] <= 8
      kind = :int128     if options[:limit] > 8 && options[:limit] <= 16
      kind = :int256     if options[:limit] > 16
    end
  end
  args.each { |name| column(name, kind, **options.except(:limit, :unsigned)) }
end

#uuid(*args, **options) ⇒ Object



76
77
78
# File 'lib/active_record/connection_adapters/clickhouse/schema_definitions.rb', line 76

def uuid(*args, **options)
  args.each { |name| column(name, :uuid, **options) }
end