Class: Lore::Table_Update

Inherits:
Object show all
Defined in:
lib/lore/strategies/table_update.rb

Instance Method Summary collapse

Constructor Details

#initialize(accessor) ⇒ Table_Update

Returns a new instance of Table_Update.



12
13
14
15
# File 'lib/lore/strategies/table_update.rb', line 12

def initialize(accessor)
  @accessor = accessor
  @base_table = accessor.table_name
end

Instance Method Details

#block_update(&block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lore/strategies/table_update.rb', line 76

def block_update(&block)
# {{{
  query_string = "UPDATE #{@base_table} "

  if block_given? then
    yield_obj = Lore::Clause_Parser.new(@accessor)
    clause = yield *yield_obj
  end

  query_string += clause.set_part
  query_string += clause.where_part
  
  Lore::Context.enter(@accessor.get_context) if @accessor.get_context
  begin
    Lore::Connection.perform(query_string)
  ensure
    Lore::Context.leave if @accessor.get_context
  end

end

#perform_update(accessor_instance) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/lore/strategies/table_update.rb', line 133

def perform_update(accessor_instance)
# {{{
  query_string = update_query(@accessor, 
                              accessor_instance.update_pkey_values, 
                           #  accessor_instance.get_attribute_value_map)
                              accessor_instance.update_values)
  
  Lore::Context.enter(@accessor.get_context) if @accessor.get_context
  begin
    Lore::Connection.perform("BEGIN;\n#{query_string}\nCOMMIT;")
  rescue ::Exception => excep
    Lore::Connection.perform("ROLLBACK;")
    raise excep
  ensure
    Lore::Context.leave if @accessor.get_context
  end
  @accessor.flush_entity_cache()

end

#update_query(accessor, primary_key_values, value_keys, query_string = '') ⇒ Object

}}}



97
98
99
100
101
102
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
# File 'lib/lore/strategies/table_update.rb', line 97

def update_query(accessor, 
                 primary_key_values,
                 value_keys,
                 query_string='')
# {{{
  Lore.logger.debug { 'Update query: ' }
  Lore.logger.debug { value_keys.inspect }
  Lore.logger.debug { primary_key_values.inspect }
  
  associations   = accessor.__associations__
  is_a_hierarchy = associations.base_klasses_tree()
  joined_models  = associations.base_klasses()
  is_a_hierarchy.each_pair { |table, base_tables| 
    
    # pass base tables first, recursively, as IS_A-based creation has
    # to be done bottom-up:
    Lore.logger.debug { 'For ' << table.to_s } 
    Lore.logger.debug { joined_models.inspect }
    query_string << update_query(joined_models[table].first, 
                                 primary_key_values, 
                                 value_keys
                                ).to_s
  }
  # finally, add query string for this table: 
  table_name = accessor.table_name
  query_string << atomic_update_query(accessor, 
                                      primary_key_values[table_name], 
                                      value_keys[table_name]
                                     ).to_s
    
  query_string

end