Class: ROM::SQL::Schema::TypeBuilder Private

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::ClassAttributes
Defined in:
lib/rom/sql/schema/type_builder.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

DECIMAL_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/(?:decimal|numeric)\((\d+)(?:,\s*(\d+))?\)/.freeze
TYPE_GUESSES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  /datetime\((\d+)\)/ => :datetime
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](db_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
# File 'lib/rom/sql/schema/type_builder.rb', line 16

def self.[](db_type)
  registry[db_type]
end

.register(db_type, builder) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
# File 'lib/rom/sql/schema/type_builder.rb', line 12

def self.register(db_type, builder)
  registry[db_type] = builder
end

Instance Method Details

#call(primary_key:, db_type:, type:, allow_null:, **rest) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rom/sql/schema/type_builder.rb', line 44

def call(primary_key:, db_type:, type:, allow_null:, **rest)
  if primary_key
    map_pk_type(type, db_type, **rest)
  else
    mapped_type = map_type(type, db_type, **rest)

    if mapped_type
      read_type = mapped_type.meta[:read]

      if read_type && allow_null
        mapped_type.optional.meta(read: read_type.optional)
      elsif allow_null
        mapped_type.optional
      else
        mapped_type
      end
    end
  end
end

#guess_type(db_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



83
84
85
86
87
88
89
# File 'lib/rom/sql/schema/type_builder.rb', line 83

def guess_type(db_type)
  TYPE_GUESSES.find do |regex, type|
    if db_type.is_a?(String) && db_type.match(regex)
      break type
    end
  end
end

#map_decimal_type(type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rom/sql/schema/type_builder.rb', line 92

def map_decimal_type(type)
  precision = DECIMAL_REGEX.match(type)

  if precision
    prcsn, scale = precision[1..2].map(&:to_i)

    self.class.ruby_type_mapping[:decimal].meta(
      precision: prcsn,
      scale: scale
    )
  else
    self.class.ruby_type_mapping[:decimal]
  end
end

#map_pk_type(_ruby_type, _db_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
# File 'lib/rom/sql/schema/type_builder.rb', line 65

def map_pk_type(_ruby_type, _db_type, **)
  self.class.numeric_pk_type.meta(primary_key: true)
end

#map_type(ruby_type, db_type, **kw) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rom/sql/schema/type_builder.rb', line 70

def map_type(ruby_type, db_type, **kw)
  type = self.class.ruby_type_mapping[ruby_type || guess_type(db_type)]

  if db_type.is_a?(String) && db_type.include?('numeric') || db_type.include?('decimal')
    map_decimal_type(db_type)
  elsif db_type.is_a?(String) && db_type.include?('char') && kw[:max_length]
    type.meta(limit: kw[:max_length])
  else
    type
  end
end