Class: Sequel::Schema::DbColumn
- Inherits:
-
Struct
- Object
- Struct
- Sequel::Schema::DbColumn
- 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
-
#column_type ⇒ Object
Returns the value of attribute column_type.
-
#default ⇒ Object
Returns the value of attribute default.
-
#elements ⇒ Object
Returns the value of attribute elements.
-
#name ⇒ Object
Returns the value of attribute name.
-
#null ⇒ Object
Returns the value of attribute null.
-
#single_primary_key ⇒ Object
Returns the value of attribute single_primary_key.
-
#size ⇒ Object
Returns the value of attribute size.
-
#unsigned ⇒ Object
Returns the value of attribute unsigned.
Class Method Summary collapse
-
.build_from_hash(attrs = {}) ⇒ Object
Builds a DbColumn from a Hash of attribute values.
Instance Method Summary collapse
-
#add_statement ⇒ Object
Returns a Sequel migration statement to add the column to a table in an alter_table block.
-
#change_default_statement ⇒ Object
Returns a Sequel migration statement to change a column’s default value.
-
#change_null_statement ⇒ Object
Returns a Sequel migration statement to change whether a column allows null values.
-
#change_type_statement ⇒ Object
Returns a Sequel migration statement to change the type of an existing column.
-
#define_statement ⇒ Object
Returns a Sequel migration statement to define a column in a create_table block.
-
#diff(other) ⇒ Object
Returns an Set of attributes that are different between this and another column.
-
#drop_statement ⇒ Object
Returns a Sequel migration statement to remove the column.
-
#initialize(*args) ⇒ DbColumn
constructor
A new instance of DbColumn.
- #integer_type? ⇒ Boolean
-
#numeric? ⇒ Boolean
Returns true if this column is numeric.
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_type ⇒ Object
Returns the value of attribute column_type
6 7 8 |
# File 'lib/sequel/schema/db_column.rb', line 6 def column_type @column_type end |
#default ⇒ Object
Returns the value of attribute default
6 7 8 |
# File 'lib/sequel/schema/db_column.rb', line 6 def default @default end |
#elements ⇒ Object
Returns the value of attribute elements
6 7 8 |
# File 'lib/sequel/schema/db_column.rb', line 6 def elements @elements end |
#name ⇒ Object
Returns the value of attribute name
6 7 8 |
# File 'lib/sequel/schema/db_column.rb', line 6 def name @name end |
#null ⇒ Object
Returns the value of attribute null
6 7 8 |
# File 'lib/sequel/schema/db_column.rb', line 6 def null @null end |
#single_primary_key ⇒ Object
Returns the value of attribute single_primary_key
6 7 8 |
# File 'lib/sequel/schema/db_column.rb', line 6 def single_primary_key @single_primary_key end |
#size ⇒ Object
Returns the value of attribute size
6 7 8 |
# File 'lib/sequel/schema/db_column.rb', line 6 def size @size end |
#unsigned ⇒ Object
Returns the value of attribute 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_statement ⇒ Object
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, ].compact.join(", ") end |
#change_default_statement ⇒ Object
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_statement ⇒ Object
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_statement ⇒ Object
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, ].compact.join(", ") end |
#define_statement ⇒ Object
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}", ].compact.join(", ") else ["#{column_type} #{name.inspect}", ].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_statement ⇒ Object
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
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.
100 101 102 |
# File 'lib/sequel/schema/db_column.rb', line 100 def numeric? NUMERIC_TYPES.include?(column_type) end |