Class: Sequel::Schema::DbColumn

Inherits:
Struct
  • Object
show all
Defined in:
lib/sequel/schema/db_column.rb,
lib/sequel/schema/db_column.rb

Overview

A column in a database table.

Responsible for generating all migration method calls used by migration operations.

Defined Under Namespace

Classes: OptionBuilder

Constant Summary collapse

INTEGER_TYPES =

Database column types that hold integers.

[:tinyint, :integer, :smallint, :mediumint, :bigint]
DECIMAL_TYPES =

Database column types that hold fractional values.

[:decimal, :float, :double, :real]
NUMERIC_TYPES =

All numeric database column types.

INTEGER_TYPES + DECIMAL_TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ DbColumn

Returns a new instance of DbColumn.



30
31
32
33
# File 'lib/sequel/schema/db_column.rb', line 30

def initialize(*args)
  super
  normalize_default
end

Instance Attribute Details

#column_typeObject

Returns the value of attribute column_type

Returns:

  • (Object)

    the current value of column_type



6
7
8
# File 'lib/sequel/schema/db_column.rb', line 6

def column_type
  @column_type
end

#defaultObject

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



6
7
8
# File 'lib/sequel/schema/db_column.rb', line 6

def default
  @default
end

#elementsObject

Returns the value of attribute elements

Returns:

  • (Object)

    the current value of elements



6
7
8
# File 'lib/sequel/schema/db_column.rb', line 6

def elements
  @elements
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



6
7
8
# File 'lib/sequel/schema/db_column.rb', line 6

def name
  @name
end

#nullObject

Returns the value of attribute null

Returns:

  • (Object)

    the current value of null



6
7
8
# File 'lib/sequel/schema/db_column.rb', line 6

def null
  @null
end

#single_primary_keyObject

Returns the value of attribute single_primary_key

Returns:

  • (Object)

    the current value of single_primary_key



6
7
8
# File 'lib/sequel/schema/db_column.rb', line 6

def single_primary_key
  @single_primary_key
end

#sizeObject

Returns the value of attribute size

Returns:

  • (Object)

    the current value of size



6
7
8
# File 'lib/sequel/schema/db_column.rb', line 6

def size
  @size
end

#unsignedObject

Returns the value of attribute unsigned

Returns:

  • (Object)

    the current value of unsigned



6
7
8
# File 'lib/sequel/schema/db_column.rb', line 6

def unsigned
  @unsigned
end

Class Method Details

.build_from_hash(attrs = {}) ⇒ Object

Builds a DbColumn from a Hash of attribute values. Keys can be strings or symbols.



26
27
28
# File 'lib/sequel/schema/db_column.rb', line 26

def self.build_from_hash(attrs={})
  self.new *members.map {|key| attrs[key.to_s] || attrs[key.to_sym] }
end

Instance Method Details

#add_statementObject

Returns a Sequel migration statement to add the column to a table in an alter_table block.



55
56
57
# File 'lib/sequel/schema/db_column.rb', line 55

def add_statement
  ["add_column #{name.inspect}", column_type.inspect, options].compact.join(", ")
end

#change_default_statementObject

Returns a Sequel migration statement to change a column’s default value.



69
70
71
# File 'lib/sequel/schema/db_column.rb', line 69

def change_default_statement
  "set_column_default #{name.inspect}, #{default.inspect}"
end

#change_null_statementObject

Returns a Sequel migration statement to change whether a column allows null values.



62
63
64
# File 'lib/sequel/schema/db_column.rb', line 62

def change_null_statement
  "set_column_allow_null #{name.inspect}, #{(!!null).inspect}"
end

#change_type_statementObject

Returns a Sequel migration statement to change the type of an existing column. Null changes must be handled separately.



76
77
78
# File 'lib/sequel/schema/db_column.rb', line 76

def change_type_statement
  ["set_column_type #{name.inspect}", column_type.inspect, change_options].compact.join(", ")
end

#define_statementObject

Returns a Sequel migration statement to define a column in a create_table block.



38
39
40
41
42
43
44
# File 'lib/sequel/schema/db_column.rb', line 38

def define_statement
  if single_primary_key
    ["primary_key #{name.inspect}, :type => #{column_type.inspect}", options].compact.join(", ")
  else
    ["#{column_type} #{name.inspect}", options].compact.join(", ")
  end
end

#diff(other) ⇒ Object

Returns an Set of attributes that are different between this and another column.



83
84
85
86
87
88
89
90
91
92
# File 'lib/sequel/schema/db_column.rb', line 83

def diff(other)
  { :null        => :boolean_attribute_different?,
    :unsigned    => :boolean_attribute_different?,
    :name        => :attribute_different?,
    :column_type => :attribute_different?,
    :elements    => :attribute_different?,
    :default     => :defaults_different?,
    :size        => :sizes_different? 
  }.select {|attribute, method| __send__(method, attribute, other) }.map {|a| a.first }.to_set
end

#drop_statementObject

Returns a Sequel migration statement to remove the column.



48
49
50
# File 'lib/sequel/schema/db_column.rb', line 48

def drop_statement
  "drop_column #{name.inspect}"
end

#integer_type?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/sequel/schema/db_column.rb', line 94

def integer_type?
  INTEGER_TYPES.include?(column_type)
end

#numeric?Boolean

Returns true if this column is numeric.

Returns:

  • (Boolean)


100
101
102
# File 'lib/sequel/schema/db_column.rb', line 100

def numeric?
  NUMERIC_TYPES.include?(column_type)
end