Module: RuboCop::Cop::Isucon::Correctors::NPlusOneQueryCorrector::CorrectableMethods

Included in:
RuboCop::Cop::Isucon::Correctors::NPlusOneQueryCorrector
Defined in:
lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector/correctable_methods.rb

Overview

Check whether can correct

Instance Method Summary collapse

Instance Method Details

#correctable?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector/correctable_methods.rb', line 11

def correctable?
  correctable_gda? && correctable_xquery_arg? &&
    correctable_parent_receiver? && current_node.child_nodes.count == 3 &&
    xquery_lvar.lvasgn_type? && %i[first last].include?(xquery_chained_method)
end

#correctable_gda?Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector/correctable_methods.rb', line 18

def correctable_gda?
  gda&.select_query? && gda.table_names.count == 1 && !gda.limit_clause? &&
    !gda.group_by_clause? && !gda.contains_aggregate_functions? && where_clause_with_only_single_unique_key?
end

#correctable_parent_receiver?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector/correctable_methods.rb', line 58

def correctable_parent_receiver?
  parent_receiver.lvar_type? || parent_receiver.send_type?
end

#correctable_xquery_arg?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector/correctable_methods.rb', line 47

def correctable_xquery_arg? # rubocop:disable Metrics/AbcSize
  return false if !xquery_arg&.send_type? || xquery_arg.node_parts.count != 3 || !xquery_arg.node_parts[0].lvar_type?

  # Check one of hash[:key], hash["key"], hash.fetch(:key), hash.fetch("key")
  return false unless %i[[] fetch].include?(xquery_arg.node_parts[1])
  return false unless %i[sym str].include?(xquery_arg.node_parts[2].type)

  true
end

#where_clause_with_only_primary_key?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector/correctable_methods.rb', line 29

def where_clause_with_only_primary_key?
  return false unless gda.where_nodes.count == 1

  primary_keys = connection.primary_keys(gda.table_names[0])
  return false unless primary_keys.count == 1

  primary_keys.first == where_column_without_quote
end

#where_clause_with_only_single_unique_index_column?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector/correctable_methods.rb', line 39

def where_clause_with_only_single_unique_index_column?
  return false unless gda.where_nodes.count == 1

  unique_index_columns = connection.unique_index_columns(gda.table_names[0])
  unique_index_columns.any? { |columns| columns.count == 1 && columns.first == where_column_without_quote }
end

#where_clause_with_only_single_unique_key?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector/correctable_methods.rb', line 24

def where_clause_with_only_single_unique_key?
  where_clause_with_only_primary_key? || where_clause_with_only_single_unique_index_column?
end