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.



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/new_relic/agent/database.rb', line 215

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
  @safe_sql = nil
end

Instance Attribute Details

#bindsObject



211
212
213
# File 'lib/new_relic/agent/database.rb', line 211

def binds
  @binds
end

#configObject



211
212
213
# File 'lib/new_relic/agent/database.rb', line 211

def config
  @config
end

#database_nameObject



211
212
213
# File 'lib/new_relic/agent/database.rb', line 211

def database_name
  @database_name
end

#explainerObject



211
212
213
# File 'lib/new_relic/agent/database.rb', line 211

def explainer
  @explainer
end

#hostObject



211
212
213
# File 'lib/new_relic/agent/database.rb', line 211

def host
  @host
end

#nameObject



211
212
213
# File 'lib/new_relic/agent/database.rb', line 211

def name
  @name
end

#port_path_or_idObject



211
212
213
# File 'lib/new_relic/agent/database.rb', line 211

def port_path_or_id
  @port_path_or_id
end

#sqlObject



211
212
213
# File 'lib/new_relic/agent/database.rb', line 211

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



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

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



267
268
269
270
271
# File 'lib/new_relic/agent/database.rb', line 267

def append_sql(new_sql)
  return if new_sql.empty?

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

#explainObject



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

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.



229
230
231
232
233
234
235
236
# File 'lib/new_relic/agent/database.rb', line 229

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