Class: NewRelic::Agent::Database::Statement
- Inherits:
-
Object
- Object
- NewRelic::Agent::Database::Statement
show all
- Includes:
- ExplainPlanHelpers
- Defined in:
- lib/new_relic/agent/database.rb
Constant Summary
collapse
- DEFAULT_QUERY_NAME =
'SQL'.freeze
- NEWLINE =
"\n".freeze
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
#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
#binds ⇒ Object
211
212
213
|
# File 'lib/new_relic/agent/database.rb', line 211
def binds
@binds
end
|
#config ⇒ Object
211
212
213
|
# File 'lib/new_relic/agent/database.rb', line 211
def config
@config
end
|
#database_name ⇒ Object
211
212
213
|
# File 'lib/new_relic/agent/database.rb', line 211
def database_name
@database_name
end
|
#explainer ⇒ Object
211
212
213
|
# File 'lib/new_relic/agent/database.rb', line 211
def explainer
@explainer
end
|
#host ⇒ Object
211
212
213
|
# File 'lib/new_relic/agent/database.rb', line 211
def host
@host
end
|
#name ⇒ Object
211
212
213
|
# File 'lib/new_relic/agent/database.rb', line 211
def name
@name
end
|
#port_path_or_id ⇒ Object
211
212
213
|
# File 'lib/new_relic/agent/database.rb', line 211
def port_path_or_id
@port_path_or_id
end
|
#sql ⇒ Object
211
212
213
|
# File 'lib/new_relic/agent/database.rb', line 211
def sql
@sql
end
|
Instance Method Details
#adapter ⇒ Object
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:([^:]+):/
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
|
#explain ⇒ Object
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_sql ⇒ Object
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.