6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/etl/integrations/core/query_builder.rb', line 6
def self.perform(action, table, record, primary_key = nil)
case action.downcase
when "insert"
columns = record.keys.join(", ")
values = record.values.map { |value| "'#{value}'" }.join(", ")
"INSERT INTO #{table} (#{columns}) VALUES (#{values});"
when "update"
return "Primary key '#{primary_key}' not found in record." if record[primary_key].nil?
primary_key_value = record.delete(primary_key) set_clause = record.map { |key, value| "#{key} = '#{value}'" }.join(", ")
where_clause = "#{primary_key} = '#{primary_key_value}'"
"UPDATE #{table} SET #{set_clause} WHERE #{where_clause};"
else
"Invalid action specified."
end
end
|