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.



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/new_relic/agent/database.rb', line 238

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



234
235
236
# File 'lib/new_relic/agent/database.rb', line 234

def binds
  @binds
end

#configObject



234
235
236
# File 'lib/new_relic/agent/database.rb', line 234

def config
  @config
end

#database_nameObject



234
235
236
# File 'lib/new_relic/agent/database.rb', line 234

def database_name
  @database_name
end

#explainerObject



234
235
236
# File 'lib/new_relic/agent/database.rb', line 234

def explainer
  @explainer
end

#hostObject



234
235
236
# File 'lib/new_relic/agent/database.rb', line 234

def host
  @host
end

#nameObject



234
235
236
# File 'lib/new_relic/agent/database.rb', line 234

def name
  @name
end

#port_path_or_idObject



234
235
236
# File 'lib/new_relic/agent/database.rb', line 234

def port_path_or_id
  @port_path_or_id
end

#query_nameObject



234
235
236
# File 'lib/new_relic/agent/database.rb', line 234

def query_name
  @query_name
end

#sqlObject



234
235
236
# File 'lib/new_relic/agent/database.rb', line 234

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



264
265
266
267
268
269
270
271
272
273
# File 'lib/new_relic/agent/database.rb', line 264

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



291
292
293
294
295
# File 'lib/new_relic/agent/database.rb', line 291

def append_sql(new_sql)
  return if new_sql.empty?

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

#explainObject



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

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.



253
254
255
256
257
258
259
260
# File 'lib/new_relic/agent/database.rb', line 253

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