Sha256: fb53898e12e18fc6fc245da8db9ce6ce2394f1767ba88005d815aef86c45371f

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    module SQLServer
      module Type
        class DateTime < ActiveRecord::Type::DateTime

          include TimeValueFractional

          def sqlserver_type
            'datetime'.freeze
          end

          def serialize(value)
            value = super
            return value unless value.acts_like?(:time)
            datetime = value.to_s(:_sqlserver_datetime).tap do |v|
              fraction = quote_fractional(value)
              v << ".#{fraction}"
            end
            Data.new datetime, self
          end

          if defined? JRUBY_VERSION

            # Currently only called by our custom Time type for formatting
            def _formatted(value)
              "#{value.to_s(:_sqlserver_datetime)}.#{quote_fractional(value)}"
            end

            # @Override
            # We do not want the Time object to be turned into a string
            def serialize(value)
              value = super
              value.acts_like?(:time) ? CoreExt::Time._at_with_sql_type(value, self) : value
            end

          end

          def deserialize(value)
            value.is_a?(Data) ? super(value.value) : super
          end

          def type_cast_for_schema(value)
            serialize(value).quoted
          end

          def quoted(value)
            Utils.quote_string_single(value)
          end

          private

          def fast_string_to_time(string)
            fast_string_to_time_zone.strptime(string, fast_string_to_time_format).time
          rescue ArgumentError
            super
          end

          def fast_string_to_time_format
            "#{::Time::DATE_FORMATS[:_sqlserver_datetime]}.%N".freeze
          end

          def fast_string_to_time_zone
            ::Time.zone || ActiveSupport::TimeZone['UTC']
          end

        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-jdbcsqlserver-adapter-50.1.0 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-jdbcsqlserver-adapter-50.0.0 lib/active_record/connection_adapters/sqlserver/type/datetime.rb