Sha256: af84de1ef9dfaa6c288ee452b3d68897830991bf6deef5bb8f724e9fe154eefe

Contents?: true

Size: 1.98 KB

Versions: 53

Compression:

Stored size: 1.98 KB

Contents

module Sequel 
  class Dataset
    module StoredProcedureMethods
      # The name of the stored procedure to call
      attr_accessor :sproc_name
      
      # The name of the stored procedure to call
      attr_writer :sproc_args
      
      # Call the stored procedure with the given args
      def call(*args, &block)
        sp = clone
        sp.sproc_args = args
        sp.run(&block)
      end

      # Programmer friendly string showing this is a stored procedure,
      # showing the name of the procedure.
      def inspect
        "<#{self.class.name}/StoredProcedure name=#{@sproc_name}>"
      end
      
      # Run the stored procedure with the current args on the database
      def run(&block)
        case @sproc_type
        when :select, :all
          all(&block)
        when :first
          first
        when :insert
          insert
        when :update
          update
        when :delete
          delete
        end
      end
      
      # Set the type of the stored procedure and override the corresponding _sql
      # method to return the empty string (since the result will be
      # ignored anyway).
      def sproc_type=(type)
        @sproc_type = type
        @opts[:sql] = ''
      end
    end
  
    module StoredProcedures
      # For the given type (:select, :first, :insert, :update, or :delete),
      # run the database stored procedure with the given name with the given
      # arguments.
      def call_sproc(type, name, *args)
        prepare_sproc(type, name).call(*args)
      end
      
      # Transform this dataset into a stored procedure that you can call
      # multiple times with new arguments.
      def prepare_sproc(type, name)
        sp = clone
        prepare_extend_sproc(sp)
        sp.sproc_type = type
        sp.sproc_name = name
        sp
      end
      
      private
      
      # Extend the dataset with the stored procedure methods.
      def prepare_extend_sproc(ds)
        ds.extend(StoredProcedureMethods)
      end
    end
  end
end

Version data entries

53 entries across 53 versions & 2 rubygems

Version Path
sequel-4.30.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.29.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.28.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.27.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.26.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.25.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.24.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.23.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.22.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.21.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.20.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.19.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.18.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.17.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.16.0 lib/sequel/adapters/utils/stored_procedures.rb
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/sequel-4.15.0/lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.15.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.14.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.13.0 lib/sequel/adapters/utils/stored_procedures.rb
sequel-4.12.0 lib/sequel/adapters/utils/stored_procedures.rb