Sha256: a315a7f825d613d9ba42ca9a0da1ed6cad8708f8f5ead4a20926f52313bfe47c

Contents?: true

Size: 1.34 KB

Versions: 13

Compression:

Stored size: 1.34 KB

Contents

require 'rom/sql/commands/error_wrapper'

module ROM
  module SQL
    module Commands
      # Update command
      #
      # @api public
      class Update < ROM::Commands::Update
        adapter :sql

        include ErrorWrapper

        use :schema
        use :associates

        after :finalize

        # Updates existing tuple in a relation
        #
        # @return [Array<Hash>, Hash]
        #
        # @api public
        def execute(tuple)
          update(input[tuple].to_h)
        end

        private

        # @api private
        def finalize(tuples, *)
          tuples.map { |t| relation.output_schema[t] }
        end

        # Executes update statement for a given tuple
        #
        # @api private
        def update(tuple)
          pks = relation.map { |t| t[primary_key] }
          dataset = relation.dataset
          dataset.update(tuple)
          dataset.unfiltered.where(primary_key => pks).to_a
        end

        # @api private
        def primary_key
          relation.primary_key
        end

        # Yields tuples for insertion or return an enumerator
        #
        # @api private
        def with_input_tuples(tuples)
          input_tuples = Array([tuples]).flatten(1).map
          return input_tuples unless block_given?
          input_tuples.each { |tuple| yield(tuple) }
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rom-sql-3.1.0 lib/rom/sql/commands/update.rb
rom-sql-3.0.1 lib/rom/sql/commands/update.rb
rom-sql-3.0.0 lib/rom/sql/commands/update.rb
rom-sql-2.5.0 lib/rom/sql/commands/update.rb
rom-sql-2.4.0 lib/rom/sql/commands/update.rb
rom-sql-2.3.0 lib/rom/sql/commands/update.rb
rom-sql-2.2.1 lib/rom/sql/commands/update.rb
rom-sql-2.2.0 lib/rom/sql/commands/update.rb
rom-sql-2.1.0 lib/rom/sql/commands/update.rb
rom-sql-2.0.0 lib/rom/sql/commands/update.rb
rom-sql-2.0.0.rc1 lib/rom/sql/commands/update.rb
rom-sql-2.0.0.beta3 lib/rom/sql/commands/update.rb
rom-sql-2.0.0.beta2 lib/rom/sql/commands/update.rb