Sha256: dc6bc08ff69931a20ad36e557339cdad8f1a4fb0674ed5af85df185441d475a9

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module DbMod
  module Statements
    module Configuration
      module Single
        # Wrapper for a statement or prepared method that returns
        # only the first row of the result set as a hash, to save
        # manual unboxing. Returns +nil+ if the query returns no
        # results.
        #
        #  def_statement(:a, 'SELECT a, b FROM foo').single(:row)
        #
        #  def do_stuff
        #    a # => { 'a' => '1', 'b' => '2'
        #  end
        module Row
          # Enables this module to be passed to
          # {DbMod::Statements::Configuration.process_method_results} as the
          # +wrapper+ function, where it will return the first row of the
          # result set, or +nil+ if the result set is empty.
          #
          # @param results [Object] SQL result set
          # @return [Hash<String,String>,nil]
          #   the first row of the SQL result set returned by the query
          def self.call(results)
            return nil unless results.any?

            results[0]
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
db_mod-0.0.4 lib/db_mod/statements/configuration/single/row.rb