Class: Lore::Table_Insert

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

Instance Method Summary collapse

Constructor Details

#initialize(accessor) ⇒ Table_Insert

Returns a new instance of Table_Insert.



8
9
10
11
12
13
14
15
16
17
# File 'lib/lore/strategies/table_insert.rb', line 8

def initialize(accessor)
  @accessor        = accessor
  @aggregates      = accessor.__associations__.aggregate_klasses
  @is_a            = accessor.__associations__.base_klasses_tree
  @foreign_keys    = accessor.__associations__.foreign_keys
  @base_table      = accessor.table_name
  @fields          = accessor.__attributes__.fields
  @sequences       = accessor.__attributes__.sequences
  @sequence_values = {}
end

Instance Method Details

#perform_insert(value_keys) ⇒ Object



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
48
49
50
51
52
53
54
55
56
# File 'lib/lore/strategies/table_insert.rb', line 19

def perform_insert(value_keys)
# {{{
  @aggregates.keys.each { |table|
    @is_a.delete(table)
  }
  
  Context.enter(@accessor.get_context) if @accessor.get_context
  Lore.logger.info { 'PERFORM INSERT on '+@accessor.to_s }

  @sequence_values = load_sequence_values(@sequences)
  
  # Parse sequence_values into value_keys where entries match the exact table: 
  # (thus, there is an explicit call like primary_key :this_field, :this_sequence)
  @sequence_values.each_pair { |table, fields|
    value_keys[table].update(@sequence_values[table])
  }
  # Parse sequence_values into value_keys where a table depends from a 
  # sequence field by IS_A
  value_keys 	 = update_sequence_values_deps(@base_table, 
                                             @is_a, 
                                             value_keys)
  query_string = insert_query(@base_table, 
                              @is_a, 
                              value_keys)
  
  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()

  # value_keys now are extended by sequence values: 
  return value_keys
end