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.



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/new_relic/agent/database.rb', line 179

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



175
176
177
# File 'lib/new_relic/agent/database.rb', line 175

def binds
  @binds
end

#configObject



175
176
177
# File 'lib/new_relic/agent/database.rb', line 175

def config
  @config
end

#database_nameObject



175
176
177
# File 'lib/new_relic/agent/database.rb', line 175

def database_name
  @database_name
end

#explainerObject



175
176
177
# File 'lib/new_relic/agent/database.rb', line 175

def explainer
  @explainer
end

#hostObject



175
176
177
# File 'lib/new_relic/agent/database.rb', line 175

def host
  @host
end

#nameObject



175
176
177
# File 'lib/new_relic/agent/database.rb', line 175

def name
  @name
end

#port_path_or_idObject



175
176
177
# File 'lib/new_relic/agent/database.rb', line 175

def port_path_or_id
  @port_path_or_id
end

#sqlObject



175
176
177
# File 'lib/new_relic/agent/database.rb', line 175

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



204
205
206
207
208
209
210
211
212
213
# File 'lib/new_relic/agent/database.rb', line 204

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



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

def append_sql(new_sql)
  return if new_sql.empty?

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

#explainObject



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

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.



193
194
195
196
197
198
199
200
# File 'lib/new_relic/agent/database.rb', line 193

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