Class: Miguel::Schema::Column

Inherits:
Object
  • Object
show all
Includes:
Output
Defined in:
lib/miguel/schema.rb

Overview

Class representing single database column.

Constant Summary collapse

NON_TYPE_OPTS =

Options which are not relevant to type specification.

[ :null, :default ]
CANONIC_TYPES =

Canonic names of some builtin ruby types.

{
  :fixnum => :integer,
  :bignum => :bigint,
  :bigdecimal => :decimal,
  :numeric => :decimal,
  :float => :double,
  :file => :blob,
  :trueclass => :boolean,
  :falseclass => :boolean,
}
DEFAULT_OPTS =

Default options implied for certain types.

{
  :string => { :size => 255, :text => false },
  :bigint => { :size => 20, :unsigned => false },
  :decimal => { :size => [ 10, 0 ] },
  :integer => { :unsigned => false },
  :time => { :only_time => true },
  :primary_key => { :unsigned => false, :type => :integer },
}
IGNORED_OPTS =

Options which are ignored for columns. These usually relate to the associated foreign key constraints, not the column itself.

[ :key ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Output

#out_canonic_opts, #out_columns, #out_default, #out_default_opts, #out_name, #out_opts, #out_table_name, #out_type

Constructor Details

#initialize(type, name, opts = {}) ⇒ Column

Create new column with given type and name.



85
86
87
88
89
# File 'lib/miguel/schema.rb', line 85

def initialize( type, name, opts = {} )
  @type = type
  @name = name
  @opts = opts
end

Instance Attribute Details

#nameObject (readonly)

Column type, name and options.



82
83
84
# File 'lib/miguel/schema.rb', line 82

def name
  @name
end

#optsObject (readonly)

Column type, name and options.



82
83
84
# File 'lib/miguel/schema.rb', line 82

def opts
  @opts
end

#typeObject (readonly)

Column type, name and options.



82
83
84
# File 'lib/miguel/schema.rb', line 82

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object

Compare one column with another one.



188
189
190
191
192
193
# File 'lib/miguel/schema.rb', line 188

def == other
  other.is_a?( Column ) &&
  name == other.name &&
  canonic_type == other.canonic_type &&
  canonic_opts == other.canonic_opts
end

#allow_nullObject

Check whether the column allow NULL values.



118
119
120
121
# File 'lib/miguel/schema.rb', line 118

def allow_null
  allow = opts[ :null ]
  allow.nil? || allow
end

#canonic_optsObject

Get the column options in a canonic way.



173
174
175
176
177
178
179
180
# File 'lib/miguel/schema.rb', line 173

def canonic_opts
  return {} if primary_key_constraint?
  o = { :type => canonic_type, :default => default, :null => true }
  o.merge!( DEFAULT_OPTS[ canonic_type ] || {} )
  o.merge!( opts )
  o[ :size ] = canonic_size( o[ :size ] )
  o.delete_if{ |key, value| IGNORED_OPTS.include? key }
end

#canonic_size(size) ⇒ Object

Convert given size into its canonic form.



150
151
152
153
154
155
156
# File 'lib/miguel/schema.rb', line 150

def canonic_size( size )
  if canonic_type == :decimal && size.is_a?( Integer )
    [ size, 0 ]
  else
    size
  end
end

#canonic_typeObject

Get the canonic type name, for type comparison.



144
145
146
147
# File 'lib/miguel/schema.rb', line 144

def canonic_type
  t = type.to_s.downcase.to_sym
  CANONIC_TYPES[ t ] || t
end

#defaultObject

Get the column default.



97
98
99
100
101
# File 'lib/miguel/schema.rb', line 97

def default
  d = opts[ :default ]
  d = type_default if d.nil? && ! allow_null
  d
end

#default_optsObject

Get options with default value included.



183
184
185
# File 'lib/miguel/schema.rb', line 183

def default_opts
  { :default => default }.merge( opts )
end

#dump(out) ⇒ Object

Dump column definition.



196
197
198
# File 'lib/miguel/schema.rb', line 196

def dump( out )
  out << "#{type} #{out_name}#{out_opts}"
end

#primary_key_constraint?Boolean

Test if the column is in fact just a primary key constraint.

Returns:

  • (Boolean)


92
93
94
# File 'lib/miguel/schema.rb', line 92

def primary_key_constraint?
  type == :primary_key && name.is_a?( Array )
end

#type_defaultObject

Get default default for column type.



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/miguel/schema.rb', line 104

def type_default
  case canonic_type
  when :string
    ""
  when :boolean
    false
  when :enum, :set
    [ *opts[ :elements ], "" ].first
  else
    0
  end
end

#type_optsObject

Get opts relevant to the column type only (excludes :null and :default).



127
128
129
# File 'lib/miguel/schema.rb', line 127

def type_opts
  opts.reject{ |key, value| NON_TYPE_OPTS.include? key }
end