Class: ActiveRecord::ConnectionAdapters::Clickhouse::SchemaCreation
- Inherits:
-
ConnectionAdapters::SchemaCreation
- Object
- ConnectionAdapters::SchemaCreation
- ActiveRecord::ConnectionAdapters::Clickhouse::SchemaCreation
- Defined in:
- lib/active_record/connection_adapters/clickhouse/schema_creation.rb
Overview
:nodoc:
Instance Method Summary collapse
- #add_as_clause!(create_sql, options) ⇒ Object
- #add_column_options!(sql, options) ⇒ Object
- #add_table_options!(create_sql, options) ⇒ Object
- #add_to_clause!(create_sql, options) ⇒ Object
- #assign_database_to_subquery!(subquery) ⇒ Object
- #current_database ⇒ Object
-
#table_modifier_in_create(o) ⇒ Object
Returns any SQL string to go between CREATE and TABLE.
- #visit_AddColumnDefinition(o) ⇒ Object
- #visit_ChangeColumnDefinition(o) ⇒ Object
- #visit_CreateIndexDefinition(o) ⇒ Object
- #visit_IndexDefinition(o, create = false) ⇒ Object
- #visit_TableDefinition(o) ⇒ Object
Instance Method Details
#add_as_clause!(create_sql, options) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 60 def add_as_clause!(create_sql, ) return unless .as assign_database_to_subquery!(.as) if .view create_sql << " AS #{to_sql(.as)}" end |
#add_column_options!(sql, options) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 20 def (sql, ) if [:value] sql.gsub!(/\s+(.*)/, " \\1(#{[:value]})") end if [:fixed_string] sql.gsub!(/\s+(.*)/, " FixedString(#{[:fixed_string]})") end if [:null] || [:null].nil? sql.gsub!(/\s+(.*)/, ' Nullable(\1)') end if [:low_cardinality] sql.gsub!(/\s+(.*)/, ' LowCardinality(\1)') end if [:array] sql.gsub!(/\s+(.*)/, ' Array(\1)') end if [:map] sql.gsub!(/\s+(.*)/, ' Map(String, \1)') end sql.gsub!(/(\sString)\(\d+\)/, '\1') sql << " DEFAULT #{quote_default_expression([:default], [:column])}" if () sql end |
#add_table_options!(create_sql, options) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 44 def (create_sql, ) opts = [:options] if .respond_to?(:options) # rails 6.1 opts ||= . end if opts.present? create_sql << " ENGINE = #{opts}" else create_sql << " ENGINE = Log()" end create_sql end |
#add_to_clause!(create_sql, options) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 79 def add_to_clause!(create_sql, ) # If you do not specify a database explicitly, ClickHouse will use the "default" database. return unless .to match = .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
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 67 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_database ⇒ Object
155 156 157 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 155 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.
115 116 117 118 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 115 def table_modifier_in_create(o) " TEMPORARY" if o.temporary " MATERIALIZED" if o.materialized end |
#visit_AddColumnDefinition(o) ⇒ Object
14 15 16 17 18 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 14 def visit_AddColumnDefinition(o) sql = +"ADD COLUMN #{accept(o.column)}" sql << " AFTER " + quote_column_name(o.column.[:after]) if o.column..key?(:after) sql end |
#visit_ChangeColumnDefinition(o) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 120 def visit_ChangeColumnDefinition(o) column = o.column column.sql_type = type_to_sql(column.type, **column.) = (column) quoted_column_name = quote_column_name(o.name) type = column.sql_type type = "Nullable(#{type})" if [:null] change_column_sql = +"MODIFY COLUMN #{quoted_column_name} #{type}" if .key?(:default) quoted_default = quote_default_expression([:default], column) change_column_sql << " DEFAULT #{quoted_default}" end change_column_sql end |
#visit_CreateIndexDefinition(o) ⇒ Object
151 152 153 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 151 def visit_CreateIndexDefinition(o) visit_IndexDefinition(o.index, true) end |
#visit_IndexDefinition(o, create = false) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 138 def visit_IndexDefinition(o, create = false) sql = create ? ["ALTER TABLE #{quote_table_name(o.table)} ADD"] : [] sql << "INDEX" sql << "IF NOT EXISTS" if o.if_not_exists sql << "IF EXISTS" if o.if_exists sql << "#{quote_column_name(o.name)} (#{o.expression}) TYPE #{o.type}" sql << "GRANULARITY #{o.granularity}" if o.granularity sql << "FIRST #{quote_column_name(o.first)}" if o.first sql << "AFTER #{quote_column_name(o.after)}" if o.after sql.join(' ') end |
#visit_TableDefinition(o) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 90 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_as_clause!(create_sql, o) if o.as && !o.view 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 if supports_indexes_in_create? indexes = o.indexes.map do |expression, | accept(@conn.(o.name, expression, **)) end statements.concat(indexes) end create_sql << "(#{statements.join(', ')})" if statements.present? # Attach options for only table or materialized view without TO section (create_sql, o) if !o.view || o.view && o.materialized && !o.to add_as_clause!(create_sql, o) if o.as && o.view create_sql end |