Sha256: 6aac6baead3f45605421dd25776a53b75222aa0c8974b0e5fb3fffb6f219a78a

Contents?: true

Size: 1.31 KB

Versions: 27

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

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

          def sqlserver_type
            "datetime"
          end

          def serialize(value)
            value = super
            return value unless value.acts_like?(:time)

            datetime = "#{value.to_formatted_s(:_sqlserver_datetime)}.#{quote_fractional(value)}"

            Data.new datetime, self
          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)
            time = ActiveSupport::TimeZone["UTC"].strptime(string, fast_string_to_time_format)
            new_time(time.year, time.month, time.day, time.hour,
                     time.min, time.sec, Rational(time.nsec, 1_000))
          rescue ArgumentError
            super
          end

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

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
activerecord-sqlserver-adapter-8.0.0 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.2.2 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.9 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.8 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.2.1 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.2.0 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.7 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.6 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.5 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.4 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.0.7 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.3 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.2 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.1 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.0 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.0.6 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.0.rc2 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.0.rc1 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.0.5.1 lib/active_record/connection_adapters/sqlserver/type/datetime.rb
activerecord-sqlserver-adapter-7.1.0.beta1 lib/active_record/connection_adapters/sqlserver/type/datetime.rb