Class: TingYun::Agent::Database::Statement

Inherits:
Object
  • Object
show all
Includes:
ExplainPlanHelpers
Defined in:
lib/ting_yun/agent/database/statement.rb

Constant Summary collapse

SUPPORTED_ADAPTERS_FOR_EXPLAIN =
[:postgres, :mysql2, :mysql, :sqlite]

Constants included from ExplainPlanHelpers

ExplainPlanHelpers::EMPTY_STRING, ExplainPlanHelpers::KNOWN_OPERATIONS, ExplainPlanHelpers::MYSQL2_PREFIX, ExplainPlanHelpers::MYSQL_PREFIX, ExplainPlanHelpers::POSTGRES_PREFIX, ExplainPlanHelpers::QUERY_PLAN, ExplainPlanHelpers::SQLITE_EXPLAIN_COLUMNS, ExplainPlanHelpers::SQLITE_PREFIX, ExplainPlanHelpers::SQL_COMMENT_REGEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExplainPlanHelpers

#handle_exception_in_explain, #is_select?, #parameterized?, #parse_operation_from_query, #process_explain_results_mysql, #process_explain_results_mysql2, #process_explain_results_postgres, #process_explain_results_sqlite, #process_resultset, #string_explain_plan_results, #symbolized_adapter

Constructor Details

#initialize(sql, config = {}, explainer = nil, binds = [], name = DEFAULT_QUERY_NAME) ⇒ Statement

Returns a new instance of Statement.



13
14
15
16
17
18
19
# File 'lib/ting_yun/agent/database/statement.rb', line 13

def initialize(sql, config={}, explainer=nil, binds=[], name=DEFAULT_QUERY_NAME)
  @sql = TingYun::Agent::Database.capture_query(sql)
  @config = config
  @explainer = explainer
  @binds = binds
  @name = name
end

Instance Attribute Details

#bindsObject

Returns the value of attribute binds.



11
12
13
# File 'lib/ting_yun/agent/database/statement.rb', line 11

def binds
  @binds
end

#configObject

Returns the value of attribute config.



11
12
13
# File 'lib/ting_yun/agent/database/statement.rb', line 11

def config
  @config
end

#explainerObject

Returns the value of attribute explainer.



11
12
13
# File 'lib/ting_yun/agent/database/statement.rb', line 11

def explainer
  @explainer
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/ting_yun/agent/database/statement.rb', line 11

def name
  @name
end

#sqlObject

Returns the value of attribute sql.



11
12
13
# File 'lib/ting_yun/agent/database/statement.rb', line 11

def sql
  @sql
end

Instance Method Details

#adapterObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ting_yun/agent/database/statement.rb', line 21

def adapter
  return unless @config

  @adapter ||= if @config[:adapter]
                 symbolized_adapter(@config[:adapter].to_s.downcase)
               elsif @config[:uri] && @config[:uri].to_s =~ /^jdbc:([^:]+):/
                 # This case is for Sequel with the jdbc-mysql, jdbc-postgres, or jdbc-sqlite3 gems.
                 symbolized_adapter($1)
               else
                 nil
               end
end

#explainObject



38
39
40
41
42
43
44
# File 'lib/ting_yun/agent/database/statement.rb', line 38

def explain
  return unless explainable?
  handle_exception_in_explain do
    plan = explainer.call(self)
    return process_resultset(plan, adapter) if plan
  end
end

#explainable?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ting_yun/agent/database/statement.rb', line 47

def explainable?
  return false unless @explainer && is_select?(sql)

  if sql[-3,3] == '...'
    TingYun::Agent.logger.debug('Unable to collect explain plan for truncated query.')
    return false
  end

  if parameterized?(@sql) && @binds.empty?
    TingYun::Agent.logger.debug('Unable to collect explain plan for parameter-less parameterized query.')
    return false
  end

  if !SUPPORTED_ADAPTERS_FOR_EXPLAIN.include?(adapter)
    TingYun::Agent.logger.debug("Not collecting explain plan because an unknown connection adapter ('#{adapter}') was used.")
    return false
  end

  true
end