Sha256: fd3a8a13e0eb75e9c447c6e55465a06222ed569b85a2fc7a5fab8837a0ae6b9e

Contents?: true

Size: 1.03 KB

Versions: 25

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module Multiwoven
  module Integrations::Core
    class QueryBuilder
      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(", ")
          # TODO: support bulk insert
          "INSERT INTO #{table} (#{columns}) VALUES (#{values});"
        when "update"
          # Ensure primary key is a string and exists within record for the WHERE clause
          return "Primary key '#{primary_key}' not found in record." if record[primary_key].nil?

          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
    end
  end
end

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
etl-integrations-0.1.82 lib/multiwoven/integrations/core/query_builder.rb
etl-integrations-0.1.81 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.73 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.72 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.64 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.63 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.62 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.61 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.60 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.59 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.58 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.57 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.56 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.55 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.54 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.53 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.52 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.51 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.50 lib/multiwoven/integrations/core/query_builder.rb
multiwoven-integrations-0.1.49 lib/multiwoven/integrations/core/query_builder.rb