Class: ActiveRecord::ConnectionAdapters::Clickhouse::SchemaCreation

Inherits:
ConnectionAdapters::SchemaCreation
  • Object
show all
Defined in:
lib/active_record/connection_adapters/clickhouse/schema_creation.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_as_clause!(create_sql, options) ⇒ Object



58
59
60
61
62
63
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 58

def add_as_clause!(create_sql, options)
  return unless options.as

  assign_database_to_subquery!(options.as) if options.view
  create_sql << " AS #{to_sql(options.as)}"
end

#add_column_options!(sql, options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 21

def add_column_options!(sql, options)
  if options[:value]
    sql.gsub!(/\s+(.*)/, " \\1(#{options[:value]})")
  end
  if options[:fixed_string]
    sql.gsub!(/\s+(.*)/, " FixedString(#{options[:fixed_string]})")
  end
  if options[:null] || options[:null].nil?
    sql.gsub!(/\s+(.*)/, ' Nullable(\1)')
  end
  if options[:low_cardinality]
    sql.gsub!(/\s+(.*)/, ' LowCardinality(\1)')
  end
  if options[:array]
    sql.gsub!(/\s+(.*)/, ' Array(\1)')
  end
  sql.gsub!(/(\sString)\(\d+\)/, '\1')
  sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
  sql
end

#add_table_options!(create_sql, options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 42

def add_table_options!(create_sql, options)
  opts = options[:options]
  if options.respond_to?(:options)
    # rails 6.1
    opts ||= options.options
  end

  if opts.present?
    create_sql << " ENGINE = #{opts}"
  else
    create_sql << " ENGINE = Log()"
  end

  create_sql
end

#add_to_clause!(create_sql, options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 77

def add_to_clause!(create_sql, options)
  # If you do not specify a database explicitly, ClickHouse will use the "default" database.
  return unless options.to

  match = options.to.match(/(?<database>.+(?=\.))?(?<table_name>.+)/i)
  return unless match
  return if match[:database]

  create_sql << "TO #{current_database}.#{match[:table_name].sub('.', '')}"
end

#assign_database_to_subquery!(subquery) ⇒ Object



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

def assign_database_to_subquery!(subquery)
  # If you do not specify a database explicitly, ClickHouse will use the "default" database.
  return unless subquery

  match = subquery.match(/(?<=from)[^.\w]+(?<database>\w+(?=\.))?(?<table_name>[.\w]+)/i)
  return unless match
  return if match[:database]

  subquery[match.begin(:table_name)...match.end(:table_name)] =
    "#{current_database}.#{match[:table_name].sub('.', '')}"
end

#current_databaseObject



127
128
129
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 127

def current_database
  ActiveRecord::Base.connection_db_config.database
end

#table_modifier_in_create(o) ⇒ Object

Returns any SQL string to go between CREATE and TABLE. May be nil.



104
105
106
107
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 104

def table_modifier_in_create(o)
  " TEMPORARY" if o.temporary
  " MATERIALIZED" if o.materialized
end

#visit_AddColumnDefinition(o) ⇒ Object



15
16
17
18
19
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 15

def visit_AddColumnDefinition(o)
  sql = +"ADD COLUMN #{accept(o.column)}"
  sql << " AFTER " + quote_column_name(o.column.options[:after]) if o.column.options.key?(:after)
  sql
end

#visit_ChangeColumnDefinition(o) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 109

def visit_ChangeColumnDefinition(o)
  column = o.column
  column.sql_type = type_to_sql(column.type, column.options)
  options = column_options(column)

  quoted_column_name = quote_column_name(o.name)
  type = column.sql_type
  type = "Nullable(#{type})" if options[:null]
  change_column_sql = +"MODIFY COLUMN #{quoted_column_name} #{type}"

  if options.key?(:default)
    quoted_default = quote_default_expression(options[:default], column)
    change_column_sql << " DEFAULT #{quoted_default}"
  end

  change_column_sql
end

#visit_TableDefinition(o) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 88

def visit_TableDefinition(o)
  create_sql = +"CREATE#{table_modifier_in_create(o)} #{o.view ? "VIEW" : "TABLE"} "
  create_sql << "IF NOT EXISTS " if o.if_not_exists
  create_sql << "#{quote_table_name(o.name)} "
  add_to_clause!(create_sql, o) if o.materialized

  statements = o.columns.map { |c| accept c }
  statements << accept(o.primary_keys) if o.primary_keys
  create_sql << "(#{statements.join(', ')})" if statements.present?
  # Attach options for only table or materialized view without TO section
  add_table_options!(create_sql, o) if !o.view || o.view && o.materialized && !o.to
  add_as_clause!(create_sql, o)
  create_sql
end