Skip to content

Commit

Permalink
Merge pull request #101 from sue445/feature/find_xquery
Browse files Browse the repository at this point in the history
Refactor. Use `with_xquery` instead of `find_xquery`
  • Loading branch information
sue445 authored Oct 20, 2021
2 parents b493d86 + f3191f8 commit ef122c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/rubocop/cop/isucon/mixin/mysql2_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ module Mysql2Methods
extend NodePattern::Macros

def_node_search :find_xquery, <<~PATTERN
(send (send nil? _) {:xquery | :query} (${str dstr} $...) ...)
(send (send nil? _) {:xquery | :query} (${str dstr lvar ivar cvar} $...) ...)
PATTERN

# @param node [RuboCop::AST::Node]
# @yieldparam type [Symbol] Node type. one of `:str`, `:dstr`
# @yieldparam root_gda [RuboCop::Isucon::GDA::Client]
# @yieldparam root_gda [RuboCop::Isucon::GDA::Client,nil]
#
# @note If arguments of `db.xquery` isn't string, `root_gda` is `nil`
def with_xquery(node)
find_xquery(node) do |type, params|
sql = xquery_param(type: type, params: params)

root_gda = RuboCop::Isucon::GDA::Client.new(sql)
root_gda = sql ? RuboCop::Isucon::GDA::Client.new(sql) : nil

yield type, root_gda
end
Expand Down
8 changes: 3 additions & 5 deletions lib/rubocop/cop/isucon/mysql2/n_plus_one_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module Mysql2
# SQL
#
class NPlusOneQuery < Base
include Mixin::Mysql2Methods

MSG = "This looks like N+1 query."

# @see https://github.com/rubocop/rubocop-performance/blob/v1.11.5/lib/rubocop/cop/performance/collection_literal_in_loop.rb#L38
Expand All @@ -42,10 +44,6 @@ class NPlusOneQuery < Base
# @see https://github.com/rubocop/rubocop-performance/blob/v1.11.5/lib/rubocop/cop/performance/collection_literal_in_loop.rb#L41
ENUMERABLE_METHOD_NAMES = (Enumerable.instance_methods + [:each]).to_set.freeze

def_node_search :find_xquery, <<-PATTERN
(send (send nil? _) {:xquery | :query} ...)
PATTERN

def_node_matcher :csv_loop?, <<~PATTERN
(block
(send (const nil? :CSV) :parse ...)
Expand All @@ -68,7 +66,7 @@ class NPlusOneQuery < Base

# @param node [RuboCop::AST::Node]
def on_send(node)
find_xquery(node) do
with_xquery(node) do |_type, _root_gda|
receiver, = *node.children

next if !receiver.send_type? || !parent_is_loop?(receiver)
Expand Down

0 comments on commit ef122c8

Please sign in to comment.