Sha256: 72f387b0d157fb6121485ef0537c08dbceac7b3c0427e69f738d4e1bf82b942e

Contents?: true

Size: 1.05 KB

Versions: 7

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module TableSaw
  module DependencyGraph
    class DumpTable
      attr_reader :manifest, :name, :partial, :ids

      def initialize(manifest:, name:, partial: true)
        @manifest = manifest
        @name = name
        @partial = partial
        @ids = Set.new
      end

      def copy_statement
        if partial
          "select * from #{name} where #{primary_key} in (#{ids.to_a.join(',')})"
        else
          "select * from #{name}"
        end
      end

      def fetch_associations(directive)
        directive.ids = directive.ids - ids.to_a
        ids.merge(directive.ids)
        fetch_belongs_to(directive) + fetch_has_many(directive)
      end

      private

      def fetch_belongs_to(directive)
        TableSaw::DependencyGraph::BelongsToDirectives.new(directive).call
      end

      def fetch_has_many(directive)
        TableSaw::DependencyGraph::HasManyDirectives.new(manifest, directive).call
      end

      def primary_key
        TableSaw.information_schema.primary_keys[name]
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
table_saw-2.1.0 lib/table_saw/dependency_graph/dump_table.rb
table_saw-2.0.0 lib/table_saw/dependency_graph/dump_table.rb
table_saw-1.3.0 lib/table_saw/dependency_graph/dump_table.rb
table_saw-1.2.0 lib/table_saw/dependency_graph/dump_table.rb
table_saw-1.1.0 lib/table_saw/dependency_graph/dump_table.rb
table_saw-1.0.1 lib/table_saw/dependency_graph/dump_table.rb
table_saw-1.0.0 lib/table_saw/dependency_graph/dump_table.rb