Sha256: 9c6665ab497bf81f61f02be675e55e9e7706473e47a6295ba7e890f96a20f075

Contents?: true

Size: 1.99 KB

Versions: 18

Compression:

Stored size: 1.99 KB

Contents

# The current_datetime_timestamp extension makes Dataset#current_datetime
# return an object that operates like Sequel.datetime_class.now, but will
# be literalized as CURRENT_TIMESTAMP.
#
# This allows you to use the defaults_setter, timestamps, and touch
# model plugins and make sure that CURRENT_TIMESTAMP is used instead of
# a literalized timestamp value.
#
# The reason that CURRENT_TIMESTAMP is better than a literalized version
# of the timestamp is that it obeys correct transactional semantics
# (all calls to CURRENT_TIMESTAMP in the same transaction return the
# same timestamp, at least on some databases).
#
# To have current_datetime be literalized as CURRENT_TIMESTAMP for
# a single dataset:
#
#   ds = ds.extension(:current_datetime_timestamp)
#
# To have current_datetime be literalized as CURRENT_TIMESTAMP for all
# datasets of a given database.
#
#   DB.extension(:current_datetime_timestamp)

#
module Sequel
  module CurrentDateTimeTimestamp
    module DatasetMethods
      # Return an instance of Sequel.datetime_class that will be literalized
      # as CURRENT_TIMESTAMP.
      def current_datetime
        MAP.fetch(Sequel.datetime_class).now
      end

      private

      # Literalize custom DateTime subclass objects as CURRENT_TIMESTAMP.
      def literal_datetime_append(sql, v)
        v.is_a?(DateTime) ? literal_append(sql, Sequel::CURRENT_TIMESTAMP) : super
      end

      # Literalize custom Time subclass objects as CURRENT_TIMESTAMP.
      def literal_time_append(sql, v)
        v.is_a?(Time) ? literal_append(sql, Sequel::CURRENT_TIMESTAMP) : super
      end
    end

    # Time subclass literalized as CURRENT_TIMESTAMP
    class Time < ::Time; end

    # DateTime subclass literalized as CURRENT_TIMESTAMP
    class DateTime < ::DateTime; end

    # Mapping of Time/DateTime classes to subclasses literalized as CURRENT_TIMESTAMP
    MAP = {::Time=>Time, ::DateTime=>DateTime}
  end

  Dataset.register_extension(:current_datetime_timestamp, CurrentDateTimeTimestamp::DatasetMethods)
end

Version data entries

18 entries across 18 versions & 2 rubygems

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