<commit_before># coding: utf-8
class BxTableDefService
  include BxService

  # table group {{{
  def create_table_group
    table_group = BxTableGroup.create!(@input.params_for(:table_group, :lock_version))
    create_table_group_data_types!(table_group)
    BxTableDefHistoryService.new.register_create_table_group_history(table_group, @input.relational_issue_ids)
    table_group
  end

  def update_table_group(table_group)
    old_data_types = table_group.data_types.to_a
    table_group.update_attributes!(@input.params_for(:table_group, :project_id))
    BxTableGroupDataType.delete_all(:table_group_id => table_group.id)
    create_table_group_data_types!(table_group)
    BxTableDefHistoryService.new.register_update_table_group_history(table_group, old_data_types, @input.relational_issue_ids)
    table_group
  end
  # }}}

  # common column {{{
  def create_common_column_def
    common_column_def = BxCommonColumnDef.new(@input.params_for(:common_column_def, :lock_version))
    base_defs = common_column_def.header? ? common_column_def.table_group.common_header_column_defs : common_column_def.table_group.common_footer_column_defs
    common_column_def.position = base_defs.length + 1
    common_column_def.save!
    BxTableDefHistoryService.new.register_create_common_column_def_history(common_column_def, @input.relational_issues)
  end

  def update_common_column_def(common_column_def)
    common_column_def.update_attributes!(@input.params_for(:common_column_def, :table_group_id))
    BxTableDefHistoryService.new.register_update_common_column_def_history(common_column_def, @input.relational_issues)
    common_column_def
  end

  def delete_common_column_def(common_column_def, history_registration = true)
    common_column_def.destroy
    BxTableDefHistoryService.new.register_delete_common_column_def_history(common_column_def) if history_registration
  end

  def up_common_column_def_position(common_column_def)
    return unless common_column_def.can_up?

    another = BxCommonColumnDef.where(:table_group_id => common_column_def.table_group_id,
                                      :position_type => common_column_def.position_type,
                                      :position => common_column_def.position - 1).first
    transpose_position!(common_column_def, another)
  end

  def down_common_column_def_position(common_column_def)
    return unless common_column_def.can_down?

    another = BxCommonColumnDef.where(:table_group_id => common_column_def.table_group_id,
                                      :position_type => common_column_def.position_type,
                                      :position => common_column_def.position + 1).first
    transpose_position!(common_column_def, another)
  end
  # }}}

  # table def {{{
  def create_table_def
    table_def = BxTableDef.create!(@input.params_for(:table_def, :lock_version))
    valid_common_column_defs = BxCommonColumnDef.where(:table_group_id => table_def.table_group_id, :id => @input.using_common_column_defs)
    valid_common_column_defs.each.with_index(1) do |common_column_def, position|
      BxColumnDef.create!(:table_def_id => table_def.id,
                          :physical_name => common_column_def.physical_name,
                          :logical_name => common_column_def.logical_name,
                          :data_type_id => common_column_def.data_type_id,
                          :size => common_column_def.size,
                          :scale => common_column_def.scale,
                          :nullable => common_column_def.nullable,
                          :default_value => common_column_def.default_value,
                          :common_column_def_id => common_column_def.id,
                          :primary_key_number => common_column_def.primary_key_number,
                          :position => position)
    end
    BxTableDefHistoryService.new.register_create_table_def_history(table_def, @input.relational_issue_ids)
    table_def
  end<commit_msg>fixed common columns are not ordered when create table definition<commit_after># coding: utf-8
class BxTableDefService
  include BxService

  # table group {{{
  def create_table_group
    table_group = BxTableGroup.create!(@input.params_for(:table_group, :lock_version))
    create_table_group_data_types!(table_group)
    BxTableDefHistoryService.new.register_create_table_group_history(table_group, @input.relational_issue_ids)
    table_group
  end

  def update_table_group(table_group)
    old_data_types = table_group.data_types.to_a
    table_group.update_attributes!(@input.params_for(:table_group, :project_id))
    BxTableGroupDataType.delete_all(:table_group_id => table_group.id)
    create_table_group_data_types!(table_group)
    BxTableDefHistoryService.new.register_update_table_group_history(table_group, old_data_types, @input.relational_issue_ids)
    table_group
  end
  # }}}

  # common column {{{
  def create_common_column_def
    common_column_def = BxCommonColumnDef.new(@input.params_for(:common_column_def, :lock_version))
    base_defs = common_column_def.header? ? common_column_def.table_group.common_header_column_defs : common_column_def.table_group.common_footer_column_defs
    common_column_def.position = base_defs.length + 1
    common_column_def.save!
    BxTableDefHistoryService.new.register_create_common_column_def_history(common_column_def, @input.relational_issues)
  end

  def update_common_column_def(common_column_def)
    common_column_def.update_attributes!(@input.params_for(:common_column_def, :table_group_id))
    BxTableDefHistoryService.new.register_update_common_column_def_history(common_column_def, @input.relational_issues)
    common_column_def
  end

  def delete_common_column_def(common_column_def, history_registration = true)
    common_column_def.destroy
    BxTableDefHistoryService.new.register_delete_common_column_def_history(common_column_def) if history_registration
  end

  def up_common_column_def_position(common_column_def)
    return unless common_column_def.can_up?

    another = BxCommonColumnDef.where(:table_group_id => common_column_def.table_group_id,
                                      :position_type => common_column_def.position_type,
                                      :position => common_column_def.position - 1).first
    transpose_position!(common_column_def, another)
  end

  def down_common_column_def_position(common_column_def)
    return unless common_column_def.can_down?

    another = BxCommonColumnDef.where(:table_group_id => common_column_def.table_group_id,
                                      :position_type => common_column_def.position_type,
                                      :position => common_column_def.position + 1).first
    transpose_position!(common_column_def, another)
  end
  # }}}

  # table def {{{
  def create_table_def
    table_def = BxTableDef.create!(@input.params_for(:table_def, :lock_version))
    valid_common_column_defs = BxCommonColumnDef.where(:table_group_id => table_def.table_group_id, :id => @input.using_common_column_defs)
                                                .order("position_type desc", :position)
    valid_common_column_defs.each.with_index(1) do |common_column_def, position|
      BxColumnDef.create!(:table_def_id => table_def.id,
                          :physical_name => common_column_def.physical_name,
                          :logical_name => common_column_def.logical_name,
                          :data_type_id => common_column_def.data_type_id,
                          :size => common_column_def.size,
                          :scale => common_column_def.scale,
                          :nullable => common_column_def.nullable,
                          :default_value => common_column_def.default_value,
                          :common_column_def_id => common_column_def.id,
                          :primary_key_number => common_column_def.primary_key_number,
                          :position => position)
    end
    BxTableDefHistoryService.new.register_create_table_def_history(table_def, @input.relational_issue_ids)
    table_def
  end
{'fraction_non_alphanumeric': 0.07385106905873678, 'fraction_numerical': 0.001228803145736053, 'mean_word_length': 3.58277027027027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7642061', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2457, 'n_words': 297}