Class: RuboCop::Cop::Isucon::Correctors::NPlusOneQueryCorrector

Inherits:
Object
  • Object
show all
Includes:
CorrectableMethods, ReplaceMethods, Mixin::Mysql2XqueryMethods
Defined in:
lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector.rb,
lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector/replace_methods.rb,
lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector/correctable_methods.rb

Overview

Examples:

Before

courses.map do |course|
  teacher = db.xquery('SELECT * FROM `users` WHERE `id` = ?', course[:teacher_id]).first
end

After

courses.map do |course|
  @users_by_id ||= db.xquery('SELECT * FROM `users` WHERE `id` IN (?)', courses.map { |course| course[:teacher_id] }).each_with_object({}) { |v, hash| hash[v[:id]] = v }
  teacher = @users_by_id[course[:teacher_id]]
end

Defined Under Namespace

Modules: CorrectableMethods, ReplaceMethods

Constant Summary

Constants included from Mixin::Mysql2XqueryMethods

Mixin::Mysql2XqueryMethods::NON_STRING_WARNING_MSG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReplaceMethods

#generate_each_with_object, #generate_second_line, #indent_level, #instance_var_name, #replace, #replace_chained_method_to_each_with_object, #replace_to_2_lines, #replace_to_2_lines_for_1st_line, #replace_to_2_lines_for_2nd_line, #replace_where_condition_in_sql, #replace_xquery_2nd_arg

Methods included from CorrectableMethods

#correctable?, #correctable_gda?, #correctable_parent_receiver?, #correctable_xquery_arg?, #where_clause_with_only_primary_key?, #where_clause_with_only_single_unique_index_column?, #where_clause_with_only_single_unique_key?

Methods included from Mixin::Mysql2XqueryMethods

#find_xquery, #with_db_query

Methods included from Mixin::OffenceLocationMethods

#offense_location

Constructor Details

#initialize(corrector:, current_node:, parent_node:, type:, gda:, connection:, is_array_arg:) ⇒ NPlusOneQueryCorrector

Returns a new instance of NPlusOneQueryCorrector.

Parameters:



59
60
61
62
63
64
65
66
67
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector.rb', line 59

def initialize(corrector:, current_node:, parent_node:, type:, gda:, connection:, is_array_arg:) # rubocop:disable Metrics/ParameterLists
  @corrector = corrector
  @current_node = current_node
  @parent_node = parent_node
  @type = type
  @gda = gda
  @connection = connection
  @is_array_arg = is_array_arg
end

Instance Attribute Details

#connectionRuboCop::Isucon::DatabaseConnection (readonly)



47
48
49
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector.rb', line 47

def connection
  @connection
end

#correctorRuboCop::Cop::Corrector (readonly)

Returns:

  • (RuboCop::Cop::Corrector)


32
33
34
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector.rb', line 32

def corrector
  @corrector
end

#current_nodeRuboCop::AST::Node (readonly)

Returns:

  • (RuboCop::AST::Node)


35
36
37
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector.rb', line 35

def current_node
  @current_node
end

#gdaRuboCop::Isucon::GDA::Client (readonly)



44
45
46
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector.rb', line 44

def gda
  @gda
end

#is_array_argBoolean (readonly)

Returns:

  • (Boolean)


50
51
52
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector.rb', line 50

def is_array_arg
  @is_array_arg
end

#parent_nodeRuboCop::AST::Node (readonly)

Returns:

  • (RuboCop::AST::Node)


38
39
40
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector.rb', line 38

def parent_node
  @parent_node
end

#typeSymbol (readonly)

Returns:

  • (Symbol)


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

def type
  @type
end

Instance Method Details

#correctObject



69
70
71
# File 'lib/rubocop/cop/isucon/correctors/n_plus_one_query_corrector.rb', line 69

def correct
  replace if correctable?
end