Module: Lore::Table_Updater

Defined in:
lib/lore/table_updater.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.atomic_update_query(table_name, attributes, primary_key_values, value_keys, explicit_fields) ⇒ Object



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lore/table_updater.rb', line 11

def self.atomic_update_query(table_name, 
                             attributes, 
                             primary_key_values, 
                             value_keys, 
                             explicit_fields)

  @logger.debug('EXPLICIT FIELDS: ' << explicit_fields.inspect)

  query_string = "\n"
  query_string += 'UPDATE '+table_name+' SET '

  set_string = String.new

  key_counter = 0
  attributes.each { |attribute_name|

    internal_attribute_name = attribute_name[0..24]
    value = value_keys[internal_attribute_name].to_s
    if value == '' then 
      value = value_keys[table_name+'.'+internal_attribute_name].to_s
    end

#     attrib = Lore.resolve_passed_value(value_keys, table_name, attribute_name)
#     value = attrib[:value]
#     internal_attribute_name = attrib[:field]
    
    # only include attribute to update query if 
    # this attribute is not marked for explicit updating or 
    # marked as explicit but non-empty: 
    if(
       !(explicit_fields && explicit_fields.include?(internal_attribute_name) && value.empty?) &&
       !(primary_key_values[attribute_name] && value.empty?)
      )
    
      if key_counter > 0
        set_string += ', '
      end # if
      set_string += attribute_name + '=\'' + value.to_s + '\' '
      
      key_counter = 1
      
    end # if
  }
  query_string += set_string

  query_string += 'WHERE '

  field_counter=0
  primary_key_values.each_pair { |field, value|
    query_string += field + '=\'' + value.to_s + '\' '
    if field_counter < primary_key_values.keys.length-1
      query_string += 'AND '
    end
    field_counter += 1
  }
  query_string += ';'
    
  query_string

end

.block_update(accessor, &block) ⇒ Object

def



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lore/table_updater.rb', line 72

def self.block_update(accessor, 
                      &block)
  
  query_string = 'UPDATE '+accessor.get_table_name

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

  query_string += clause.set_part
  query_string += clause.where_part
  
  Lore::Context.enter(accessor.get_context) unless accessor.get_context.nil?

  begin
    Lore::Connection.perform(query_string)
  ensure
    Lore::Context.leave unless accessor.get_context.nil?
  end

end

.perform_update(accessor, accessor_instance) ⇒ Object

:nodoc



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

def self.perform_update(accessor, accessor_instance)
  
  query_string = update_query(accessor.get_table_name, 
                              accessor.get_is_a, 
                              accessor.get_attributes, 
                              accessor_instance.get_primary_key_values, 
                              accessor_instance.get_attribute_values, 
                              accessor.get_explicit)
  
  Context.enter(accessor.get_context) unless accessor.get_context.nil?
  begin
    Lore::Connection.perform("BEGIN;\n#{query_string}\nCOMMIT;")
  rescue ::Exception => excep
    Lore::Connection.perform("ROLLBACK;")
  ensure
    Context.leave unless accessor.get_context.nil?
  end

  accessor.flush_entity_cache()
  
end

.update_query(table_name, is_a_hierarchy, attributes, primary_key_values, value_keys, explicit_fields, query_string = '') ⇒ Object

:nodoc



96
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
130
# File 'lib/lore/table_updater.rb', line 96

def self.update_query(table_name, 
                      is_a_hierarchy, 
                      
                      attributes,
                      primary_key_values,
                      value_keys,
                      explicit_fields,
                      
                      query_string='')

  is_a_hierarchy.each_pair { |table, base_tables| 
    
    # pass base tables first, recursively, as IS_A-based creation has
    # to be done bottom-up:
    query_string += update_query(table, 
                                 base_tables, 

                                 attributes, 
                                 primary_key_values, 
                                 value_keys,
                                 explicit_fields 
                                ).to_s
  }
  # finally, add query string for this table: 
  query_string += atomic_update_query(table_name, 
                                      attributes[table_name], 
                                      primary_key_values[table_name], 
                                      value_keys[table_name], 
                                    #  value_keys[table_name], 
                                      explicit_fields[table_name]
                                     ).to_s
    
  query_string

end