Class: DataMapper::Adapters::DataObjectsAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-cutie/tracker/data_objects/helper.rb,
lib/dm-cutie/tracker/data_objects/overrides.rb

Overview

@note: This adds a few helper methods to DataObjectsAdapter

Instance Method Summary collapse

Instance Method Details

#_calls_deepObject

This source method is 6 calls deep in apps started with a bin like merb, rails; otherwise, we are looking for the last line

/Volumes/Workspace/dm-cutie-0.3.13/lib/dm-cutie/tracker/data_objects_adapter_helper.rb:38:in `read'
/Volumes/Workspace/dm-core-0.10.1/lib/dm-core/adapters/data_objects_adapter.rb:266:in `with_connection'
/Volumes/Workspace/dm-cutie-0.3.13/lib/dm-cutie/tracker/data_objects_adapter_override.rb:67:in `read'
/Volumes/Workspace/dm-core-0.10.1/lib/dm-core/repository.rb:141:in `read'
/Volumes/Workspace/dm-core-0.10.1/lib/dm-core/model.rb:303:in `first'
/Volumes/Workspace/dm-core-0.10.1/lib/dm-core/model.rb:204:in `get'
/Volumes/Workspace/dm-cutie-0.3.13/kickstart.rb:50


16
# File 'lib/dm-cutie/tracker/data_objects/helper.rb', line 16

def _calls_deep;6;end

#bind_statement(statement, *bind_values) ⇒ String

  • bind_statement: binds values to a SQL statement

Parameters:

  • statement (~String)

    SQL Statement

  • *bind_values (*splat)

    Values to bind

Returns:

  • (String)

    Bound SQL Statement



28
29
30
31
32
33
34
35
# File 'lib/dm-cutie/tracker/data_objects/helper.rb', line 28

def bind_statement(statement, *bind_values)
  _ret_stmnt = ''
  with_connection do |connection|
    command = connection.create_command(statement)
    _ret_stmnt = command.send :escape_sql, *bind_values
  end
  _ret_stmnt
end

#create(resources) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dm-cutie/tracker/data_objects/overrides.rb', line 2

def create(resources)
  resources.each do |resource|
    model      = resource.model
    serial     = model.serial(name)
    attributes = resource.dirty_attributes

    properties  = []
    bind_values = []

    # make the order of the properties consistent
    model.properties(name).each do |property|
      next unless attributes.key?(property)

      bind_value = attributes[property]

      # skip insering NULL for columns that are serial or without a default
      next if bind_value.nil? && (property.serial? || !property.default?)

      # if serial is being set explicitly, do not set it again
      if property.equal?(serial)
        serial = nil
      end

      properties  << property
      bind_values << bind_value
    end

    statement = insert_statement(model, properties, serial) 
    
    #### BEGIN DataMapper::Cutie ####  
    # @note: Need a Query object for the tracker or to rewrite what the tracker takes, it was easier to just
    #   make a BS Query object to pass in since the other CRUD methods have a query object.
    query = DataMapper::Query.new(resource.repository, resource.model)
    
    start_tracking_timer(query) 

    result    = execute(statement, *bind_values)                                        

    #### END DataMapper::Cutie ####
    track_query(query, statement, bind_values)
    
    if result.to_i == 1 && serial
      serial.set!(resource, result.insert_id)
    end
  end
end

#delete(collection) ⇒ Integer

Constructs and executes DELETE statement for given query

Parameters:

  • collection (Collection)

    collection of records to be deleted

Returns:

  • (Integer)

    the number of records deleted



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/dm-cutie/tracker/data_objects/overrides.rb', line 143

def delete(collection)
  query = collection.query

  # TODO: if the query contains any links, a limit or an offset
  # use a subselect to get the rows to be deleted

  statement, bind_values = delete_statement(query)
  
  ####  BEGIN DataMapper::Cutie ####
  start_tracking_timer(query)
  
  result = execute(statement, *bind_values).to_i
  
  ####  END DataMapper::Cutie ####
  track_query(query, statement, bind_values)  
  
  result
end

#query_timersObject

Accessor for query timers



19
# File 'lib/dm-cutie/tracker/data_objects/helper.rb', line 19

def query_timers;(@__query_timers ||= {});end

#read(query) ⇒ Array

Constructs and executes SELECT query, then instantiates one or many object from result set.

Parameters:

  • query (Query)

    composition of the query to perform

Returns:

  • (Array)

    result set of the query



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
# File 'lib/dm-cutie/tracker/data_objects/overrides.rb', line 59

def read(query)
  fields = query.fields
  types  = fields.map { |property| property.primitive }

  statement, bind_values = select_statement(query)

  records = []

  with_connection do |connection|
    command = connection.create_command(statement)
    command.set_types(types)

    #### BEGIN DataMapper::Cutie ####
    start_tracking_timer(query)
    
    reader = command.execute_reader(*bind_values)

    track_query(query, statement, bind_values)
    #### END DataMapper::Cutie ####

    begin
      while reader.next!
        records << fields.zip(reader.values).to_hash
      end
    ensure
      reader.close
    end
  end

  records
end

#start_tracking_timer(query) ⇒ Object



37
38
39
40
41
# File 'lib/dm-cutie/tracker/data_objects/helper.rb', line 37

def start_tracking_timer(query) 
  if DataMapper::Cutie.tracking_query?(query)
    query_timers[query.object_id] = Time.now
  end
end

#track_query(query, statement, bind_values) ⇒ Object



43
44
45
46
47
48
49
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
# File 'lib/dm-cutie/tracker/data_objects/helper.rb', line 43

def track_query(query, statement, bind_values)
  if DataMapper::Cutie.tracking_query? query
    query_start_time  = query_timers.delete(query.object_id)
    query_end_time    = Time.now
                                  
    _call_info = caller[_calls_deep] || caller.last
    _call_info = _call_info.split(':')
   
    DataMapper::Cutie.repo do
      generalized_query  = GeneralizedQuery.find_or_make query, statement
      DataMapper.logger.debug "BEGAN PROFILING ~ #{query.model} : #{generalized_query.operation}" 
      executed_query = ExecutedQuery.new

      executed_query.statement      = bind_statement(statement, bind_values)

      executed_query.repo_signature = Digest::SHA1.hexdigest("#{query.repository.name}: #{executed_query.statement}")
      executed_query.signature      = Digest::SHA1.hexdigest(executed_query.statement)

      executed_query.elapsed_time   = BigDecimal.new(query_end_time.to_f.to_s) - BigDecimal.new(query_start_time.to_f.to_s)
      executed_query.executed_at    = query_start_time

      # Manhandle the data if its too big, should the fields just be blobs instead?
      executed_query.caller_file    = _call_info[0][ -255, _call_info[0].length ] if _call_info[0]
      executed_query.caller_line    = _call_info[1]
      executed_query.caller_method  = _call_info[2][ 0, 255 ] if _call_info[2]

      executed_query.bind_values    = bind_values
      executed_query.generalized_query  = generalized_query
      
      executed_query.save
      
      DataMapper::Cutie.process_tracker_hooks(
        query.repository.adapter.options[:adapter], # The current adapter
        generalized_query.operation,                    # The current operation
        executed_query                              # The currently executed query
      )

    end # Finish DataMapper::Cutie.repo context
    
  end #End if tracking_query?
end

#update(attributes, collection) ⇒ Integer

Constructs and executes UPDATE statement for given attributes and a query

Parameters:

  • attributes (Hash(Property => Object))

    hash of attribute values to set, keyed by Property

  • collection (Collection)

    collection of records to be updated

Returns:

  • (Integer)

    the number of records updated



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/dm-cutie/tracker/data_objects/overrides.rb', line 103

def update(attributes, collection)
  query = collection.query

  # TODO: if the query contains any links, a limit or an offset
  # use a subselect to get the rows to be updated

  properties  = []
  bind_values = []

  # make the order of the properties consistent
  query.model.properties(name).each do |property|
    next unless attributes.key?(property)
    properties  << property
    bind_values << attributes[property]
  end

  statement, conditions_bind_values = update_statement(properties, query)

  bind_values.concat(conditions_bind_values)
        
  ####  BEGIN DataMapper::Cutie ####
  start_tracking_timer(query)
  
  result = execute(statement, *bind_values).to_i
  
  ####  END DataMapper::Cutie ####
  track_query(query, statement, bind_values)  
  
  result
end