Class: GeneralizedQuery

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/dm-cutie/models/generalized_query.rb

Overview

GeneralizedQuery - Keeps track of a unique queries without bind_values

Constant Summary collapse

OPERATIONS =
Enum[ *DataMapper::Cutie::OPERATIONS ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_make(query, stmnt) ⇒ Object

query - Override query to set :signature & :operation properties

Parameters:

  • query (Query)
    • DataMapper Query object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dm-cutie/models/generalized_query.rb', line 50

def GeneralizedQuery.find_or_make(query, stmnt)                                                              
  _repo_signature = Digest::SHA1.hexdigest("#{query.repository.name}: #{stmnt}")
  _qs = GeneralizedQuery.first(:repo_signature => _repo_signature) || GeneralizedQuery.new

  if _qs.new?
    _qs.attribute_set :statement,      stmnt
    _qs.attribute_set :signature,      Digest::SHA1.hexdigest(stmnt) 
    _qs.attribute_set :repo_signature, _repo_signature

    # The main_storage is denormalized into this table, I didn't want to make the Resource between GeneralizedQuery & RepositoryStorage
    #   a full fledged model just to track a 'main_storage' boolean.
    main_storage = RepositoryStorage.find_or_make(
      query.repository.name,
      query.repository.adapter.options[:adapter],                    
      query.model.storage_names[query.repository.name],
      Extlib::Inflection.underscore(query.model.name)
    )                                          
    # Add main_storage to the repository_storages array
    main_storage_link = QueryStorageLink.new
    main_storage_link.repository_storage  = main_storage
    main_storage_link.primary_storage     = true
    
    _qs.query_storage_links << main_storage_link

    stmnt =~ /^(SELECT|INSERT|UPDATE|DELETE) /i
    _qs.attribute_set :operation,      ($1 || 'none').downcase.to_sym

    # if there are links, track the RepositoryStorage for each
    _qs.attribute_set :has_links, !(query.links.empty?)
    
    query.links.each do |this_link|   
      if query.model == this_link.parent_model
        other_model, other_repo_name = [this_link.child_model,   this_link.child_repository_name]
      else                                                                                       
        other_model, other_repo_name = [this_link.parent_model,  this_link.parent_repository_name]
      end
      
      other_repo_name ||= :default
        
      _linked_storage = RepositoryStorage.find_or_make(
        other_repo_name, 
        repository(other_repo_name).adapter.options[:adapter],
        other_model.storage_names[other_repo_name],
        Extlib::Inflection.underscore(other_model.name)
      )
      _linked_storage_link = QueryStorageLink.new
      _linked_storage_link.repository_storage  = _linked_storage
      _linked_storage_link.primary_storage     = false

      _qs.query_storage_links << _linked_storage_link
    end       
    
    _qs.save   
  end

  _qs
end

Instance Method Details

#execution_countObject



40
41
42
# File 'lib/dm-cutie/models/generalized_query.rb', line 40

def execution_count
  self.executed_queries.count
end

#primary_storageObject



19
20
21
# File 'lib/dm-cutie/models/generalized_query.rb', line 19

def primary_storage
  self.query_storage_links.first(:primary_storage => true).repository_storage
end