Class: ActiveRecord::ConnectionAdapters::Mysql2Rgeo::SpatialColumnInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/mysql2rgeo/spatial_column_info.rb

Overview

Do spatial sql queries for column info and memoize that info.

Instance Method Summary collapse

Constructor Details

#initialize(adapter, table_name) ⇒ SpatialColumnInfo

Returns a new instance of SpatialColumnInfo.



8
9
10
11
# File 'lib/active_record/connection_adapters/mysql2rgeo/spatial_column_info.rb', line 8

def initialize(adapter, table_name)
  @adapter = adapter
  @table_name = table_name
end

Instance Method Details

#allObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_record/connection_adapters/mysql2rgeo/spatial_column_info.rb', line 13

def all
  info = if @adapter.supports_expression_index?
           @adapter.query(
             "SELECT column_name, srs_id, column_type FROM INFORMATION_SCHEMA.Columns WHERE table_name='#{@table_name}'"
           )
         else
           @adapter.query(
             "SELECT column_name, 0, column_type FROM INFORMATION_SCHEMA.Columns WHERE table_name='#{@table_name}'"
           )
         end

  result = {}
  info.each do |row|
    name = row[0]
    type = row[2]
    type.sub!(/m$/, "")
    result[name] = {
      name:      name,
      srid:      row[1].to_i,
      type:      type,
    }
  end
  result
end

#get(column_name, type) ⇒ Object

do not query the database for non-spatial columns/tables



39
40
41
42
43
44
# File 'lib/active_record/connection_adapters/mysql2rgeo/spatial_column_info.rb', line 39

def get(column_name, type)
  return unless Mysql2RgeoAdapter.spatial_column_options(type.to_sym)

  @spatial_column_info ||= all
  @spatial_column_info[column_name]
end