Sha256: 8213bd1e18a2ddb2afb0ca0e9bc067cf523e805c010921cabc57bde4d5ecd9fb

Contents?: true

Size: 755 Bytes

Versions: 1

Compression:

Stored size: 755 Bytes

Contents

# frozen_string_literal: true

module TableSaw
  module Queries
    class ExecuteInsertStatement
      attr_reader :statement, :row

      def initialize(statement, row)
        @statement = statement
        @row = row
      end

      def call
        "EXECUTE #{statement.name}(#{values});"
      end

      private

      def values
        TableSaw.schema_cache.columns(statement.table_name).zip(row)
          .map { |column, value| quote_value(column, value) }
          .join(', ')
      end

      def adapter
        TableSaw::Connection.adapter
      end

      def quote_value(column, value)
        type = adapter.lookup_cast_type_from_column(column)
        adapter.quote(type.serialize(type.deserialize(value)))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
table_saw-3.1.0 lib/table_saw/queries/execute_insert_statement.rb