Class: ActiveRecord::ConnectionAdapters::PostgreSQLColumn

Inherits:
Column
  • Object
show all
Extended by:
ArrayParser, Cast
Defined in:
lib/active_record/connection_adapters/postgresql_adapter.rb,
lib/active_record/connection_adapters/postgresql/cast.rb,
lib/active_record/connection_adapters/postgresql/array_parser.rb

Overview

PostgreSQL-specific extensions to column definitions in a table.

Defined Under Namespace

Modules: ArrayParser, Cast

Constant Summary

Constants inherited from Column

Column::FALSE_VALUES, Column::TRUE_VALUES

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Column

#coder, #default, #default_function, #limit, #name, #null, #precision, #primary, #scale, #sql_type, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cast

array_to_string, cidr_to_string, hstore_to_string, json_to_string, point_to_string, range_to_string, string_to_array, string_to_bit, string_to_cidr, string_to_hstore, string_to_json, string_to_point, string_to_time

Methods inherited from Column

#binary?, binary_to_string, #extract_default, #has_default?, #human_name, #klass, string_to_binary, #string_to_binary, string_to_dummy_time, string_to_time, #type_cast_code, #type_cast_for_write, value_to_boolean, value_to_date, value_to_decimal, value_to_integer

Constructor Details

#initialize(name, default, oid_type, sql_type = nil, null = true) ⇒ PostgreSQLColumn

Returns a new instance of PostgreSQLColumn.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 50

def initialize(name, default, oid_type, sql_type = nil, null = true)
  @oid_type = oid_type
  default_value     = self.class.extract_value_from_default(default)

  if sql_type =~ /\[\]$/
    @array = true
    super(name, default_value, sql_type[0..sql_type.length - 3], null)
  else
    @array = false
    super(name, default_value, sql_type, null)
  end

  @default_function = default if has_default_function?(default_value, default)
end

Class Attribute Details

.money_precisionObject

Returns the value of attribute money_precision.



77
78
79
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 77

def money_precision
  @money_precision
end

Instance Attribute Details

#arrayObject

:nodoc:



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

def array
  @array
end

Class Method Details

.extract_value_from_default(default) ⇒ Object

Extracts the value from a PostgreSQL column default definition.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 82

def self.extract_value_from_default(default)
  # This is a performance optimization for Ruby 1.9.2 in development.
  # If the value is nil, we return nil straight away without checking
  # the regular expressions. If we check each regular expression,
  # Regexp#=== will call NilClass#to_str, which will trigger
  # method_missing (defined by whiny nil in ActiveSupport) which
  # makes this method very very slow.
  return default unless default

  case default
    when /\A'(.*)'::(num|date|tstz|ts|int4|int8)range\z/m
      $1
    # Numeric types
    when /\A\(?(-?\d+(\.\d*)?\)?(::bigint)?)\z/
      $1
    # Character types
    when /\A\(?'(.*)'::.*\b(?:character varying|bpchar|text)\z/m
      $1
    # Binary data types
    when /\A'(.*)'::bytea\z/m
      $1
    # Date/time types
    when /\A'(.+)'::(?:time(?:stamp)? with(?:out)? time zone|date)\z/
      $1
    when /\A'(.*)'::interval\z/
      $1
    # Boolean type
    when 'true'
      true
    when 'false'
      false
    # Geometric types
    when /\A'(.*)'::(?:point|line|lseg|box|"?path"?|polygon|circle)\z/
      $1
    # Network address types
    when /\A'(.*)'::(?:cidr|inet|macaddr)\z/
      $1
    # Bit string types
    when /\AB'(.*)'::"?bit(?: varying)?"?\z/
      $1
    # XML type
    when /\A'(.*)'::xml\z/m
      $1
    # Arrays
    when /\A'(.*)'::"?\D+"?\[\]\z/
      $1
    # Hstore
    when /\A'(.*)'::hstore\z/
      $1
    # JSON
    when /\A'(.*)'::json\z/
      $1
    # Object identifier types
    when /\A-?\d+\z/
      $1
    else
      # Anything else is blank, some user type, or some function
      # and we can't know the value of that, so return nil.
      nil
  end
end

Instance Method Details

#number?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 65

def number?
  !array && super
end

#text?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 69

def text?
  !array && super
end

#type_cast(value) ⇒ Object



144
145
146
147
148
149
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 144

def type_cast(value)
  return if value.nil?
  return super if encoded?

  @oid_type.type_cast value
end