Sha256: eae75fd3895856aa3bcb443421488a44fd1b0031257b9b314b58802473e9e7ac

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module TableSaw
  module DependencyGraph
    class HasManyDirectives
      attr_reader :manifest, :directive

      def initialize(manifest, directive)
        @manifest = manifest
        @directive = directive
      end

      def call
        valid_associations.map do |table, column|
          TableSaw::DependencyGraph::AddDirective.new(
            table,
            ids: query_result(table, column).map { |r| r[TableSaw.information_schema.primary_keys[table]] },
            partial: directive.partial?
          )
        end
      end

      private

      def associations
        TableSaw.information_schema.has_many.fetch(directive.table_name, [])
      end

      # rubocop:disable Metrics/AbcSize
      def valid_associations
        associations.select do |table, _column|
          next false if directive.partial? && !TableSaw.information_schema.primary_keys.key?(table)
          next true if directive.has_many.include?(table)

          manifest.has_many.fetch(directive.table_name, []).include?(table)
        end
      end
      # rubocop:enable Metrics/AbcSize

      def query_result(table, column)
        return [] unless directive.selectable?

        TableSaw::Connection.exec(
          format(
            'select %{primary_key} from %{table} where %{clause}',
            primary_key: TableSaw.information_schema.primary_keys[table], table: table,
            clause: TableSaw::Queries::SerializeSqlInClause.new(table, column, directive.ids).call
          )
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
table_saw-2.2.0 lib/table_saw/dependency_graph/has_many_directives.rb