Sha256: 91ce3276abbcc1a21147a69176978743acfccfcfcd03ce09ea0bad8245d697f3

Contents?: true

Size: 1.96 KB

Versions: 127

Compression:

Stored size: 1.96 KB

Contents

# frozen-string-literal: true
#
# 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)
#
# Related module: Sequel::CurrentDateTimeTimestamp

#
module Sequel
  module CurrentDateTimeTimestamp
    module DatasetMethods
      # Return an instance of Sequel.datetime_class that will be literalized
      # as CURRENT_TIMESTAMP.
      def current_datetime
        (Sequel.datetime_class == ::Time ? Time : DateTime).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
  end

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

Version data entries

127 entries across 110 versions & 2 rubygems

Version Path
sequel-5.43.0 lib/sequel/extensions/current_datetime_timestamp.rb
sequel-5.42.0 lib/sequel/extensions/current_datetime_timestamp.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/sequel-5.38.0/lib/sequel/extensions/current_datetime_timestamp.rb
tdiary-5.1.5 vendor/bundle/ruby/2.7.0/gems/sequel-5.41.0/lib/sequel/extensions/current_datetime_timestamp.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/sequel-5.41.0/lib/sequel/extensions/current_datetime_timestamp.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/sequel-5.39.0/lib/sequel/extensions/current_datetime_timestamp.rb
sequel-5.41.0 lib/sequel/extensions/current_datetime_timestamp.rb
sequel-5.40.0 lib/sequel/extensions/current_datetime_timestamp.rb
sequel-5.39.0 lib/sequel/extensions/current_datetime_timestamp.rb
tdiary-5.1.4 vendor/bundle/ruby/2.7.0/gems/sequel-5.38.0/lib/sequel/extensions/current_datetime_timestamp.rb
sequel-5.38.0 lib/sequel/extensions/current_datetime_timestamp.rb
sequel-5.37.0 lib/sequel/extensions/current_datetime_timestamp.rb
sequel-5.36.0 lib/sequel/extensions/current_datetime_timestamp.rb
tdiary-5.1.3 vendor/bundle/ruby/2.7.0/gems/sequel-5.35.0/lib/sequel/extensions/current_datetime_timestamp.rb
tdiary-5.1.3 vendor/bundle/ruby/2.6.0/gems/sequel-5.32.0/lib/sequel/extensions/current_datetime_timestamp.rb
sequel-5.35.0 lib/sequel/extensions/current_datetime_timestamp.rb
sequel-5.34.0 lib/sequel/extensions/current_datetime_timestamp.rb
sequel-5.33.0 lib/sequel/extensions/current_datetime_timestamp.rb
tdiary-5.1.2 vendor/bundle/ruby/2.7.0/gems/sequel-5.32.0/lib/sequel/extensions/current_datetime_timestamp.rb
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/sequel-5.26.0/lib/sequel/extensions/current_datetime_timestamp.rb