未验证 提交 737fa371 编写于 作者: R Rafael Mendonça França

Merge pull request #26155 from kamipo/refactor_quoted_columns_for_index

Refactor `quoted_columns_for_index` by extracted `add_options_for_index_columns`
......@@ -1161,31 +1161,34 @@ def change_column_comment(table_name, column_name, comment) #:nodoc:
end
protected
def add_index_sort_order(option_strings, column_names, options = {})
if options.is_a?(Hash) && order = options[:order]
def add_index_sort_order(quoted_columns, **options)
if order = options[:order]
case order
when Hash
column_names.each { |name| option_strings[name] += " #{order[name].upcase}" if order.has_key?(name) }
quoted_columns.each { |name, column| column << " #{order[name].upcase}" if order[name].present? }
when String
column_names.each { |name| option_strings[name] += " #{order.upcase}" }
quoted_columns.each { |name, column| column << " #{order.upcase}" if order.present? }
end
end
return option_strings
quoted_columns
end
# Overridden by the MySQL adapter for supporting index lengths
def quoted_columns_for_index(column_names, options = {})
return [column_names] if column_names.is_a?(String)
option_strings = Hash[column_names.map { |name| [name, ""] }]
# add index sort order if supported
def add_options_for_index_columns(quoted_columns, **options)
if supports_index_sort_order?
option_strings = add_index_sort_order(option_strings, column_names, options)
quoted_columns = add_index_sort_order(quoted_columns, options)
end
column_names.map { |name| quote_column_name(name) + option_strings[name] }
quoted_columns
end
def quoted_columns_for_index(column_names, **options)
return [column_names] if column_names.is_a?(String)
quoted_columns = Hash[column_names.map { |name| [name, quote_column_name(name).dup] }]
add_options_for_index_columns(quoted_columns, options).values
end
def index_name_for_remove(table_name, options = {})
......
......@@ -713,32 +713,25 @@ def fetch_type_metadata(sql_type, extra = "")
MySQL::TypeMetadata.new(super(sql_type), extra: extra, strict: strict_mode?)
end
def add_index_length(option_strings, column_names, options = {})
if options.is_a?(Hash) && length = options[:length]
def add_index_length(quoted_columns, **options)
if length = options[:length]
case length
when Hash
column_names.each { |name| option_strings[name] += "(#{length[name]})" if length.has_key?(name) && length[name].present? }
quoted_columns.each { |name, column| column << "(#{length[name]})" if length[name].present? }
when Integer
column_names.each { |name| option_strings[name] += "(#{length})" }
quoted_columns.each { |name, column| column << "(#{length})" }
end
end
return option_strings
quoted_columns
end
def quoted_columns_for_index(column_names, options = {})
option_strings = Hash[column_names.map { |name| [name, ""] }]
# add index length
option_strings = add_index_length(option_strings, column_names, options)
# add index sort order
option_strings = add_index_sort_order(option_strings, column_names, options)
column_names.map { |name| quote_column_name(name) + option_strings[name] }
def add_options_for_index_columns(quoted_columns, **options)
quoted_columns = add_index_length(quoted_columns, options)
super
end
# See https://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
# See https://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
ER_DUP_ENTRY = 1062
ER_NO_REFERENCED_ROW_2 = 1452
ER_DATA_TOO_LONG = 1406
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册