Class: ActiveRecord::ConnectionAdapters::Mysql2RgeoAdapter

Inherits:
Mysql2Adapter
  • Object
show all
Includes:
ActiveRecord::ConnectionAdapters::Mysql2Rgeo::SchemaStatements
Defined in:
lib/active_record/connection_adapters/mysql2rgeo_adapter.rb

Constant Summary collapse

SPATIAL_COLUMN_OPTIONS =
{
  geometry: {},
  geometrycollection: {},
  linestring: {},
  multilinestring: {},
  multipoint: {},
  multipolygon: {},
  spatial: { type: "geometry" },
  point: {},
  polygon: {}
}
DEFAULT_SRID =
0
ADAPTER_NAME =
"Mysql2Rgeo".freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecord::ConnectionAdapters::Mysql2Rgeo::SchemaStatements

#create_table_definition, #initialize_type_map, #native_database_types, #new_column, #spatial_column_constructor, #type_to_sql

Constructor Details

#initialize(connection, logger, connection_options, config) ⇒ Mysql2RgeoAdapter

Returns a new instance of Mysql2RgeoAdapter.



44
45
46
47
48
49
# File 'lib/active_record/connection_adapters/mysql2rgeo_adapter.rb', line 44

def initialize(connection, logger, connection_options, config)
  super

  @visitor = Arel::Visitors::Mysql2Rgeo.new(self)
  @visitor.extend(DetermineIfPreparableVisitor) if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
end

Class Method Details

.spatial_column_options(key) ⇒ Object



51
52
53
# File 'lib/active_record/connection_adapters/mysql2rgeo_adapter.rb', line 51

def self.spatial_column_options(key)
  SPATIAL_COLUMN_OPTIONS[key]
end

Instance Method Details

#default_sridObject



55
56
57
# File 'lib/active_record/connection_adapters/mysql2rgeo_adapter.rb', line 55

def default_srid
  DEFAULT_SRID
end

#indexes(table_name, name = nil) ⇒ Object

Returns an array of indexes for the given table.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/active_record/connection_adapters/mysql2rgeo_adapter.rb', line 64

def indexes(table_name, name = nil) #:nodoc:
  if name
    ActiveSupport::Deprecation.warn(<<-MSG.squish)
      Passing name to #indexes is deprecated without replacement.
    MSG
  end

  indexes = []
  current_index = nil
  execute_and_free("SHOW KEYS FROM #{quote_table_name(table_name)}", "SCHEMA") do |result|
    each_hash(result) do |row|
      if current_index != row[:Key_name]
        next if row[:Key_name] == "PRIMARY" # skip the primary key
        current_index = row[:Key_name]

        mysql_index_type = row[:Index_type].downcase.to_sym
        index_type = INDEX_TYPES.include?(mysql_index_type) ? mysql_index_type : nil
        index_using = INDEX_USINGS.include?(mysql_index_type) ? mysql_index_type : nil
        options = [row[:Table], row[:Key_name], row[:Non_unique].to_i == 0, [], {}, nil, nil, index_type, index_using, row[:Index_comment].presence]
        indexes << if mysql_index_type == :spatial
                     options.push(true)
                     Mysql2Rgeo::SpatialIndexDefinition.new(*options)
                   else
                     IndexDefinition.new(row[:Table], row[:Key_name], row[:Non_unique].to_i == 0, [], {}, nil, nil, index_type, index_using, row[:Index_comment].presence)
                   end
      end

      indexes.last.columns << row[:Column_name]
      indexes.last.lengths.merge!(row[:Column_name] => row[:Sub_part].to_i) if row[:Sub_part] && mysql_index_type != :spatial
    end
  end

  indexes
end

#quote(value) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/active_record/connection_adapters/mysql2rgeo_adapter.rb', line 99

def quote(value)
  if RGeo::Feature::Geometry.check_type(value)
    "ST_GeomFromWKB(0x#{RGeo::WKRep::WKBGenerator.new(hex_format: true, little_endian: true).generate(value)},#{value.srid})"
  else
    super
  end
end

#supports_spatial?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/active_record/connection_adapters/mysql2rgeo_adapter.rb', line 59

def supports_spatial?
  !mariadb? && version >= "5.7.6"
end