Class: ActiveRecord::ConnectionAdapters::Column

Inherits:
Object
  • Object
show all
Includes:
Deduplicable
Defined in:
activerecord/lib/active_record/connection_adapters/column.rb

Overview

An abstract definition of a column in a table.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Deduplicable

#deduplicate

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Constructor Details

#initialize(name, cast_type, default, sql_type_metadata = nil, null = true, default_function = nil, collation: nil, comment: nil) ⇒ Column

Instantiates a new column in the table.

name is the column’s name, such as supplier_id in supplier_id bigint. default is the type-casted default value, such as new in sales_stage varchar(20) default 'new'. sql_type_metadata is various information about the type of the column null determines if this column allows NULL values.



20
21
22
23
24
25
26
27
28
29
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 20

def initialize(name, cast_type, default,  = nil, null = true, default_function = nil, collation: nil, comment: nil, **)
  @name = name.freeze
  @cast_type = cast_type
  @sql_type_metadata = 
  @null = null
  @default = default.nil? || cast_type.mutable? ? default : cast_type.deserialize(default)
  @default_function = default_function
  @collation = collation
  @comment = comment
end

Instance Attribute Details

#collationObject (readonly)

Returns the value of attribute collation.



10
11
12
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 10

def collation
  @collation
end

#commentObject (readonly)

Returns the value of attribute comment.



10
11
12
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 10

def comment
  @comment
end

#defaultObject (readonly)

Returns the value of attribute default.



10
11
12
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 10

def default
  @default
end

#default_functionObject (readonly)

Returns the value of attribute default_function.



10
11
12
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 10

def default_function
  @default_function
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 10

def name
  @name
end

#nullObject (readonly)

Returns the value of attribute null.



10
11
12
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 10

def null
  @null
end

#sql_type_metadataObject (readonly)

Returns the value of attribute sql_type_metadata.



10
11
12
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 10

def 
  @sql_type_metadata
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



83
84
85
86
87
88
89
90
91
92
93
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 83

def ==(other)
  other.is_a?(Column) &&
    name == other.name &&
    cast_type == other.cast_type &&
    default == other.default &&
     == other. &&
    null == other.null &&
    default_function == other.default_function &&
    collation == other.collation &&
    comment == other.comment
end

#auto_incremented_by_db?Boolean

whether the column is auto-populated by the database using a sequence

Returns:

  • (Boolean)


75
76
77
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 75

def auto_incremented_by_db?
  false
end

#auto_populated?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 79

def auto_populated?
  auto_incremented_by_db? || default_function
end

#bigint?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 40

def bigint?
  /\Abigint\b/.match?(sql_type)
end

#encode_with(coder) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 63

def encode_with(coder)
  coder["name"] = @name
  coder["cast_type"] = @cast_type
  coder["sql_type_metadata"] = @sql_type_metadata
  coder["null"] = @null
  coder["default"] = @default
  coder["default_function"] = @default_function
  coder["collation"] = @collation
  coder["comment"] = @comment
end

#fetch_cast_type(connection) ⇒ Object

:nodoc:



31
32
33
34
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 31

def fetch_cast_type(connection) # :nodoc:
  # TODO: Remove fetch_cast_type and the need for connection after we release 8.1.
  @cast_type || connection.lookup_cast_type(sql_type)
end

#has_default?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 36

def has_default?
  !default.nil? || default_function
end

#hashObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 96

def hash
  Column.hash ^
    name.hash ^
    name.encoding.hash ^
    cast_type.hash ^
    default.hash ^
    .hash ^
    null.hash ^
    default_function.hash ^
    collation.hash ^
    comment.hash
end

#human_nameObject

Returns the human name of the column name.

Examples
Column.new('sales_stage', ...).human_name # => 'Sales stage'


48
49
50
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 48

def human_name
  Base.human_attribute_name(@name)
end

#init_with(coder) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 52

def init_with(coder)
  @name = coder["name"]
  @cast_type = coder["cast_type"]
  @sql_type_metadata = coder["sql_type_metadata"]
  @null = coder["null"]
  @default = coder["default"]
  @default_function = coder["default_function"]
  @collation = coder["collation"]
  @comment = coder["comment"]
end

#virtual?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'activerecord/lib/active_record/connection_adapters/column.rb', line 109

def virtual?
  false
end