Class: Outhad::Integrations::Core::QueryBuilder

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/outhad/integrations/core/query_builder.rb

Class Method Summary collapse

Methods included from Utils

#build_catalog, #build_stream, #convert_to_json_schema, #create_log_message, #extract_data, #handle_exception, #hash_to_string, #keys_to_symbols, #log_request_response, #logger, #map_type_to_json_schema, #report_exception, #success?

Class Method Details

.perform(action, table, record, primary_key = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/outhad/integrations/core/query_builder.rb', line 8

def self.perform(action, table, record, primary_key = nil)
  case action.downcase
  when "destination_insert"
    columns = record.keys.join(", ")
    values = record.values.map { |value| "'#{value}'" }.join(", ")
    # TODO: support bulk insert
    "INSERT INTO #{table} (#{columns}) VALUES (#{values});"
  when "destination_update"
    # Ensure primary key is a string and exists within record for the WHERE clause
    if record[primary_key].nil?
      error_message = "Primary key '#{primary_key}' not found in record."
      Integrations::Service.logger.error(error_message)
      return error_message
    end

    primary_key_value = record.delete(primary_key) # Remove and return the primary key value
    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