Sha256: 3830c0271f3ade53c64881cb5063d30f4ad10a7901415c506a9601f07bc1a7d5

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module TableSaw
  module Queries
    class PreparedInsertStatement
      attr_reader :table_name, :options

      Statement = Struct.new(:name, :table_name, :sql)

      def initialize(table_name, options: {})
        @table_name = table_name
        @options = options
      end

      def call
        Statement.new(name, table_name, sql)
      end

      private

      def name
        "#{table_name}_insert_plan"
      end

      def sql
        "#{[prepare_statement, conflict_statement].compact.join(' ')};"
      end

      def column_types
        TableSaw.schema_cache.columns(table_name).map(&:sql_type_metadata).map(&:sql_type).join(', ')
      end

      def column_names
        TableSaw.schema_cache.columns(table_name)
          .map { |column| TableSaw::Connection.adapter.quote_column_name(column.name) }
          .join(', ')
      end

      def values_clause
        1.upto(TableSaw.schema_cache.columns(table_name).size).map { |i| "$#{i}" }.join(', ')
      end

      def prepare_statement
        <<~SQL.squish
          PREPARE #{name} (#{column_types}) AS INSERT INTO #{table_name} (#{column_names}) VALUES (#{values_clause})
        SQL
      end

      def conflict_statement
        return unless options['ignore_conflict'] == 'true'

        'ON CONFLICT DO NOTHING'
      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/prepared_insert_statement.rb