Class: NewRelic::Agent::Database::Statement

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

Constant Summary collapse

DEFAULT_QUERY_NAME =
'SQL'.freeze
NEWLINE =
"\n".freeze

Constants included from ExplainPlanHelpers

ExplainPlanHelpers::MULTIPLE_QUERIES, ExplainPlanHelpers::QUERY_PLAN, ExplainPlanHelpers::SELECT, ExplainPlanHelpers::SQLITE_EXPLAIN_COLUMNS, ExplainPlanHelpers::SUPPORTED_ADAPTERS_FOR_EXPLAIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExplainPlanHelpers

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

Constructor Details

#initialize(sql, config = {}, explainer = nil, binds = nil, name = DEFAULT_QUERY_NAME, host = nil, port_path_or_id = nil, database_name = nil) ⇒ Statement

Returns a new instance of Statement.



260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/new_relic/agent/database.rb', line 260

def initialize(sql, config = {}, explainer = nil, binds = nil, name = DEFAULT_QUERY_NAME, host = nil, port_path_or_id = nil, database_name = nil)
  @sql = Database.capture_query(sql)
  @config = config
  @explainer = explainer
  @binds = binds
  @name = name
  @host = host
  @port_path_or_id = port_path_or_id
  @database_name = database_name
  @query_name = Database.extract_query_name_from_sql(sql)
  @safe_sql = nil
end

Instance Attribute Details

#bindsObject



256
257
258
# File 'lib/new_relic/agent/database.rb', line 256

def binds
  @binds
end

#configObject



256
257
258
# File 'lib/new_relic/agent/database.rb', line 256

def config
  @config
end

#database_nameObject



256
257
258
# File 'lib/new_relic/agent/database.rb', line 256

def database_name
  @database_name
end

#explainerObject



256
257
258
# File 'lib/new_relic/agent/database.rb', line 256

def explainer
  @explainer
end

#hostObject



256
257
258
# File 'lib/new_relic/agent/database.rb', line 256

def host
  @host
end

#nameObject



256
257
258
# File 'lib/new_relic/agent/database.rb', line 256

def name
  @name
end

#port_path_or_idObject



256
257
258
# File 'lib/new_relic/agent/database.rb', line 256

def port_path_or_id
  @port_path_or_id
end

#query_nameObject



256
257
258
# File 'lib/new_relic/agent/database.rb', line 256

def query_name
  @query_name
end

#sqlObject



256
257
258
# File 'lib/new_relic/agent/database.rb', line 256

def sql
  @sql
end

Instance Method Details

#adapterObject

This takes a connection config hash from ActiveRecord or Sequel and returns a symbol describing the associated database adapter



286
287
288
289
290
291
292
293
294
295
# File 'lib/new_relic/agent/database.rb', line 286

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)
  end
end

#append_sql(new_sql) ⇒ Object



313
314
315
316
317
# File 'lib/new_relic/agent/database.rb', line 313

def append_sql(new_sql)
  return if new_sql.empty?

  @sql = Database.truncate_query(@sql << NEWLINE << new_sql)
end

#explainObject



297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/new_relic/agent/database.rb', line 297

def explain
  return unless explainable?

  handle_exception_in_explain do
    start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    plan = @explainer.call(self)
    ::NewRelic::Agent.record_metric(
      'Supportability/Database/execute_explain_plan',
      Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
    )
    return process_resultset(plan, adapter) if plan
  end
end

#safe_sqlObject

Returns an sql statement that will be in the form most permissable by the config. The format will be safe for transmission to New Relic.



275
276
277
278
279
280
281
282
# File 'lib/new_relic/agent/database.rb', line 275

def safe_sql
  @safe_sql ||= case Database.record_sql_method
    when :obfuscated
      Database.obfuscate_sql(self)
    when :raw
      sql.to_s
  end
end