Sha256: dbca8586fa1e341999413d48569815be6d20f8fd52eea32c0a5b20bf17cfcc10

Contents?: true

Size: 1.27 KB

Versions: 12

Compression:

Stored size: 1.27 KB

Contents

module Sequel
  class Dataset
    # Translates a query block into a dataset. Query blocks can be useful
    # when expressing complex SELECT statements, e.g.:
    #
    #   dataset = DB[:items].query do
    #     select :x, :y, :z
    #     filter((:x > 1) & (:y > 2))
    #     order :z.desc
    #   end
    #
    # Which is the same as:
    #
    #  dataset = DB[:items].select(:x, :y, :z).filter((:x > 1) & (:y > 2)).order(:z.desc)
    #
    # Note that inside a call to query, you cannot call each, insert, update,
    # or delete (or any method that calls those), or Sequel will raise an
    # error.
    def query(&block)
      copy = clone({})
      copy.extend(QueryBlockCopy)
      copy.instance_eval(&block)
      clone(copy.opts)
    end

    # Module used by Dataset#query that has the effect of making all
    # dataset methods into !-style methods that modify the receiver.
    module QueryBlockCopy
      %w'each insert update delete'.each do |meth|
        define_method(meth){|*args| raise Error, "##{meth} cannot be invoked inside a query block."}
      end

      # Merge the given options into the receiver's options and return the receiver
      # instead of cloning the receiver.
      def clone(opts = nil)
        @opts.merge!(opts)
        self
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
sequel-2.6.0 lib/sequel_core/dataset/query.rb
sequel-2.8.0 lib/sequel_core/dataset/query.rb
sequel-2.7.1 lib/sequel_core/dataset/query.rb
sequel-2.7.0 lib/sequel_core/dataset/query.rb
sequel-2.3.0 lib/sequel_core/dataset/query.rb
sequel-2.4.0 lib/sequel_core/dataset/query.rb
sequel-2.5.0 lib/sequel_core/dataset/query.rb
sequel-2.9.0 lib/sequel_core/dataset/query.rb
sequel_core-2.1.0 lib/sequel_core/dataset/query.rb
sequel_core-2.0.0 lib/sequel_core/dataset/query.rb
sequel_core-2.0.1 lib/sequel_core/dataset/query.rb
sequel_core-2.2.0 lib/sequel_core/dataset/query.rb