Sha256: fb2f329a6a28daa24158fc5c65f9b640bd25a35f5a220f87561aa56b818c8431

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

module Alf
  module Sequel
    class Connection
      module UpdateMethods

        # Yields the block in a transaction
        def in_transaction(*args, &bl)
          sequel_db.transaction(*args, &bl)
        end

        def lock(name, mode, &bl)
          with_dataset(name){|ds|
            if ds.respond_to?(:lock)
              ds.lock(mode.to_s.upcase, &bl)
            else
              yield
            end
          }
        end

        # Inserts `tuples` in the relvar called `name`
        def insert(name, tuples)
          insert_uow(name, tuples).run
        end

        # Delete from the relvar called `name`
        def delete(name, predicate)
          delete_uow(name, predicate).run
        end

        # Updates the relvar called `name`
        def update(name, updating, predicate)
          update_uow(name, updating, predicate).run
        end

      public # should be private

        def with_dataset(name, predicate = nil)
          ds = name.is_a?(Symbol) ? sequel_db[name] : name
          if predicate && !predicate.tautology?
            ds = ds.where(Translator.new(sequel_db).call(predicate.sexpr))
          end
          yield(ds) if block_given?
        end

      private

        def insert_uow(name, tuples)
          UnitOfWork::Insert.new(self, name, tuples)
        end

        def delete_uow(name, predicate)
          UnitOfWork::Delete.new(self, name, predicate)
        end

        def update_uow(name, updating, predicate)
          UnitOfWork::Update.new(self, name, updating, predicate)
        end

      end # module UpdateMethods
    end # module Connection
  end # module Sequel
end # module Alf

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
alf-sequel-0.16.3 lib/alf/sequel/connection/update_methods.rb
alf-sequel-0.16.2 lib/alf/sequel/connection/update_methods.rb
alf-sequel-0.16.1 lib/alf/sequel/connection/update_methods.rb
alf-sequel-0.16.0 lib/alf/sequel/connection/update_methods.rb