Module: RuboCop::Cop::Isucon::Mixin::Mysql2XqueryMethods

Overview

Helper methods for db.xquery or db.query in AST

Constant Summary collapse

NON_STRING_WARNING_MSG =
"Warning: non-string was passed to `query` or `xquery` 1st argument. " \
"So argument doesn't parsed as SQL (%<file_path>s:%<line_num>d)"

Instance Method Summary collapse

Methods included from OffenceLocationMethods

#offense_location

Instance Method Details

#find_xquery(node) ⇒ Object

Parameters:

  • node (RuboCop::AST::Node)


15
16
17
# File 'lib/rubocop/cop/isucon/mixin/mysql2_xquery_methods.rb', line 15

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

#with_db_query(node) {|type, root_gda| ... } ⇒ Object

Note:

If arguments of db.xquery isn't string, root_gda is nil

Parameters:

  • node (RuboCop::AST::Node)

Yield Parameters:



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/isucon/mixin/mysql2_xquery_methods.rb', line 27

def with_db_query(node)
  find_xquery(node) do |type, params|
    sql = xquery_param(type: type, params: params)

    unless sql
      warn format(NON_STRING_WARNING_MSG, file_path: processed_source.file_path, line_num: node.loc.expression.line)
    end

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

    yield type, root_gda
  end
end