Method: Sequel::SchemaDumper#column_schema_to_ruby_type

Defined in:
lib/sequel/extensions/schema_dumper.rb

#column_schema_to_ruby_type(schema) ⇒ Object

Convert the column schema information to a hash of column options, one of which must be :type. The other options added should modify that type (e.g. :size). If a database type is not recognized, return it as a String type.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sequel/extensions/schema_dumper.rb', line 38

def column_schema_to_ruby_type(schema)
  type = schema[:db_type].downcase
  if database_type == :oracle
    type = type.sub(/ not null\z/, '')
  end
  case type
  when /\A(medium|small)?int(?:eger)?(?:\((\d+)\))?( unsigned)?\z/
    if !$1 && $2 && $2.to_i >= 10 && $3
      # Unsigned integer type with 10 digits can potentially contain values which
      # don't fit signed integer type, so use bigint type in target database.
      {:type=>:Bignum}
    else
      {:type=>Integer}
    end
  when /\Atinyint(?:\((\d+)\))?(?: unsigned)?\z/
    {:type =>schema[:type] == :boolean ? TrueClass : Integer}
  when /\Abigint(?:\((?:\d+)\))?(?: unsigned)?\z/
    {:type=>:Bignum}
  when /\A(?:real|float|double(?: precision)?|double\(\d+,\d+\))(?: unsigned)?\z/
    {:type=>Float}
  when 'boolean', 'bit', 'bool'
    {:type=>TrueClass}
  when /\A(?:(?:tiny|medium|long|n)?text|clob)\z/
    {:type=>String, :text=>true}
  when 'date'
    {:type=>Date}
  when /\A(?:small)?datetime\z/
    {:type=>DateTime}
  when /\Atimestamp(?:\((\d+)\))?(?: with(?:out)? time zone)?\z/
    {:type=>DateTime, :size=>($1.to_i if $1)}
  when /\Atime(?: with(?:out)? time zone)?\z/
    {:type=>Time, :only_time=>true}
  when /\An?char(?:acter)?(?:\((\d+)\))?\z/
    {:type=>String, :size=>($1.to_i if $1), :fixed=>true}
  when /\A(?:n?varchar2?|character varying|bpchar|string)(?:\((\d+)\))?\z/
    {:type=>String, :size=>($1.to_i if $1)}
  when /\A(?:small)?money\z/
    {:type=>BigDecimal, :size=>[19,2]}
  when /\A(?:decimal|numeric|number)(?:\((\d+)(?:,\s*(\d+))?\))?(?: unsigned)?\z/
    s = [($1.to_i if $1), ($2.to_i if $2)].compact
    {:type=>BigDecimal, :size=>(s.empty? ? nil : s)}
  when /\A(?:bytea|(?:tiny|medium|long)?blob|(?:var)?binary)(?:\((\d+)\))?\z/
    {:type=>File, :size=>($1.to_i if $1)}
  when /\A(?:year|(?:int )?identity)\z/
    {:type=>Integer}
  else
    {:type=>String}
  end
end