Class: Sqlite3ExecutionStep

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Cutie::Tracker::Hook::Abstract, DataMapper::Resource
Defined in:
lib/dm-cutie-extras/hooks/sqlite3_execution_step.rb

Overview

Tracker Hook for recording sqlite3 query plans

Constant Summary collapse

STATEMENT =
"EXPLAIN QUERY PLAN %s"

Class Method Summary collapse

Class Method Details

.hook_nameObject



16
17
18
# File 'lib/dm-cutie-extras/hooks/sqlite3_execution_step.rb', line 16

def self.hook_name
  "Sqlite3 Execution Steps"
end

.supported_adaptersObject



22
23
24
# File 'lib/dm-cutie-extras/hooks/sqlite3_execution_step.rb', line 22

def self.supported_adapters
  [:sqlite3]
end

.supported_statementsObject



19
20
21
# File 'lib/dm-cutie-extras/hooks/sqlite3_execution_step.rb', line 19

def self.supported_statements
  [:select, :insert, :update, :delete]
end

.track(executed_query) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dm-cutie-extras/hooks/sqlite3_execution_step.rb', line 26

def self.track( executed_query )
  # Perform the EXPLAIN against the original target repo      
  raw_query_plan = repository(
    executed_query.generalized_query.primary_storage.repo_name.to_sym
  ).adapter.query Sqlite3ExecutionStep::STATEMENT % executed_query.statement

  #Store the Sqlite3ExecutionStep in the DM Cutie repo
  DataMapper::Cutie.repo do
    raw_query_plan.each_with_index do |raw_query_step, idx|
      attribs = raw_query_step.attributes       
      query_step = Sqlite3ExecutionStep.new(attribs)
      query_step.order  = attribs[:order]
      query_step.from   = attribs[:from]
      query_step.detail = attribs[:detail]
      query_step.executed_query = executed_query
      query_step.save
    end
  end
end