Sha256: 0d811a607e436a2466affcd632009f8e3c54dc52948583b10bc9d086c3e1e946

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

module ROM
  module SQL
    module Postgres
      class TypeBuilder < Schema::TypeBuilder
        defines :db_numeric_types, :db_type_mapping, :db_array_type_matcher

        db_numeric_types %w(
          smallint integer bigint
          decimal numeric real
          double\ precision serial bigserial
        ).to_set.freeze

        db_type_mapping(
          'uuid'  => Types::UUID,
          'money' => Types::Money,
          'bytea' => Types::Bytea,
          'json'  => Types::JSON,
          'jsonb' => Types::JSONB,
          'xml' => Types::XML,
          'inet' => Types::IPAddress,
          'cidr' => Types::IPAddress,
          'macaddr' => SQL::Types::String,
          'point' => Types::Point,
          'hstore' => Types::HStore,
          'line' => Types::Line,
          'circle' => Types::Circle,
          'box' => Types::Box,
          'lseg' => Types::LineSegment,
          'polygon' => Types::Polygon,
          'path' => Types::Path
        ).freeze

        db_array_type_matcher '[]'.freeze

        def map_pk_type(type, db_type)
          if numeric?(type, db_type)
            type = self.class.numeric_pk_type
          else
            type = map_type(type, db_type)
          end

          type.meta(primary_key: true)
        end

        def map_type(ruby_type, db_type, enum_values: nil, **_)
          if db_type.end_with?(self.class.db_array_type_matcher)
            Types::Array(db_type[0...-2])
          elsif enum_values
            SQL::Types::String.enum(*enum_values)
          else
            map_db_type(db_type) || super
          end
        end

        def map_db_type(db_type)
          self.class.db_type_mapping[db_type] ||
            (db_type.start_with?('timestamp') ? SQL::Types::Time : nil)
        end

        def numeric?(ruby_type, db_type)
          self.class.db_numeric_types.include?(db_type) || ruby_type == :integer
        end
      end
    end

    Schema::TypeBuilder.register(:postgres, Postgres::TypeBuilder.new.freeze)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-sql-2.0.0 lib/rom/sql/extensions/postgres/type_builder.rb
rom-sql-2.0.0.rc1 lib/rom/sql/extensions/postgres/type_builder.rb
rom-sql-2.0.0.beta3 lib/rom/sql/extensions/postgres/type_builder.rb