Module: ActiveRecord::ConnectionAdapters::PostGIS::SchemaStatements

Included in:
ActiveRecord::ConnectionAdapters::PostGISAdapter
Defined in:
lib/active_record/connection_adapters/postgis/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#columns(table_name) ⇒ Object

override pass table_name to #new_column



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/active_record/connection_adapters/postgis/schema_statements.rb', line 7

def columns(table_name)
  # Limit, precision, and scale are all handled by the superclass.
  column_definitions(table_name).map do |column_name, type, default, notnull, oid, fmod, collation|
    oid = oid.to_i
    fmod = fmod.to_i
     = (column_name, type, oid, fmod)
    cast_type = get_oid_type(oid.to_i, fmod.to_i, column_name, type)
    default_value = extract_value_from_default(default)

    default_function = extract_default_function(default_value, default)
    new_column(table_name, column_name, default_value, cast_type, , !notnull, default_function, collation)
  end
end

#create_table_definition(*args) ⇒ Object

override



81
82
83
# File 'lib/active_record/connection_adapters/postgis/schema_statements.rb', line 81

def create_table_definition(*args)
  PostGIS::TableDefinition.new(*args)
end

#initialize_type_map(map = type_map) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/active_record/connection_adapters/postgis/schema_statements.rb', line 91

def initialize_type_map(map = type_map)
  super

  %w(
    geography
    geometry
    geometry_collection
    line_string
    multi_line_string
    multi_point
    multi_polygon
    st_point
    st_polygon
  ).each do |geo_type|
    map.register_type(geo_type) do |oid, _, sql_type|
      OID::Spatial.new(oid, sql_type)
    end
  end
end

#native_database_typesObject

override



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/active_record/connection_adapters/postgis/schema_statements.rb', line 64

def native_database_types
  # Add spatial types
  super.merge(
    geography:           { name: "geography" },
    geometry:            { name: "geometry" },
    geometry_collection: { name: "geometry_collection" },
    line_string:         { name: "line_string" },
    multi_line_string:   { name: "multi_line_string" },
    multi_point:         { name: "multi_point" },
    multi_polygon:       { name: "multi_polygon" },
    spatial:             { name: "geometry" },
    st_point:            { name: "st_point" },
    st_polygon:          { name: "st_polygon" }
  )
end

#new_column(table_name, column_name, default, cast_type, sql_type_metadata = nil, null = true, default_function = nil, collation = nil) ⇒ Object

override



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_record/connection_adapters/postgis/schema_statements.rb', line 22

def new_column(table_name, column_name, default, cast_type,  = nil, null = true, default_function = nil, collation = nil)
  # JDBC gets true/false in Rails 4, where other platforms get 't'/'f' strings.
  if null.is_a?(String)
    null = (null == "t")
  end

  column_info = spatial_column_info(table_name).get(column_name, .sql_type)

  SpatialColumn.new(column_name,
                    default,
                    ,
                    null,
                    table_name,
                    default_function,
                    collation,
                    cast_type,
                    column_info)
end

#spatial_column_info(table_name) ⇒ Object

memoize hash of column infos for tables



86
87
88
89
# File 'lib/active_record/connection_adapters/postgis/schema_statements.rb', line 86

def spatial_column_info(table_name)
  @spatial_column_info ||= {}
  @spatial_column_info[table_name.to_sym] ||= SpatialColumnInfo.new(self, table_name.to_s)
end

#type_to_sql(type, options = {}) ⇒ Object

override github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L583

returns Postgresql sql type string examples:

"geometry(Point,4326)"
"geography(Point,4326)"

note: type alone is not enough to detect the sql type, so ‘limit` is used to pass the additional information. :(

type_to_sql(:geography, “Point,4326”)

> “geography(Point,4326)”



54
55
56
57
58
59
60
61
# File 'lib/active_record/connection_adapters/postgis/schema_statements.rb', line 54

def type_to_sql(type, options = {})
  case type
  when :geometry, :geography
    "#{type}(#{options[:limit]})"
  else
    super
  end
end