Class: ActiveRecord::ConnectionAdapters::Mysql2SpatialAdapter::MainAdapter

Inherits:
ConnectionAdapters::Mysql2Adapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb

Constant Summary collapse

NATIVE_DATABASE_TYPES =
Mysql2Adapter::NATIVE_DATABASE_TYPES.merge(spatial: { name: 'geometry' })

Instance Method Summary collapse

Constructor Details

#initialize(*args_) ⇒ MainAdapter

Returns a new instance of MainAdapter.



42
43
44
45
46
47
48
# File 'lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb', line 42

def initialize(*args_)
  super
  # Rails 3.2 way of defining the visitor: do so in the constructor
  if defined?(@visitor) && @visitor
    @visitor = ::Arel::Visitors::MySQL2Spatial.new(self)
  end
end

Instance Method Details

#adapter_nameObject



54
55
56
# File 'lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb', line 54

def adapter_name
  Mysql2SpatialAdapter::ADAPTER_NAME
end

#add_index(table_name_, column_name_, options_ = {}) ⇒ Object



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

def add_index(table_name_, column_name_, options_={})
  if options_[:spatial]
    index_name_ = index_name(table_name_, column: Array(column_name_))
    if ::Hash === options_
      index_name_ = options_[:name] || index_name_
    end
    execute "CREATE SPATIAL INDEX #{index_name_} ON #{table_name_} (#{Array(column_name_).join(", ")})"
  else
    super
  end
end

#columns(table_name_, name_ = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb', line 95

def columns(table_name_, name_=nil)
  result_ = execute("SHOW FIELDS FROM #{quote_table_name(table_name_)}", :skip_logging)
  columns_ = []
  result_.each(symbolize_keys: true, as: :hash) do |field_|
    columns_ << SpatialColumn.new(@rgeo_factory_settings, table_name_.to_s,
      field_[:Field], field_[:Default], field_[:Type], field_[:Null] == "YES")
  end
  columns_
end

#indexes(table_name_, name_ = nil) ⇒ Object

Returns an array of indexes for the given table.



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
# File 'lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb', line 106

def indexes(table_name_, name_=nil)
  indexes_ = []
  current_index_ = nil
  result_ = execute("SHOW KEYS FROM #{quote_table_name(table_name_)}", name_)
  result_.each(symbolize_keys: true, as: :hash) 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]
      indexes_ << if mysql_index_type == :spatial
        options.push(true)
        ::RGeo::ActiveRecord::SpatialIndexDefinition.new(*options)
      else
        IndexDefinition.new(*options)
      end
    end
    last_index_ = indexes_.last
    last_index_.columns << row_[:Column_name]
    last_index_.lengths << row_[:Sub_part] unless mysql_index_type == :spatial
  end
  indexes_
end

#native_database_typesObject



62
63
64
# File 'lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb', line 62

def native_database_types
  NATIVE_DATABASE_TYPES
end

#quote(value_, column_ = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb', line 66

def quote(value_, column_=nil)
  if ::RGeo::Feature::Geometry.check_type(value_)
    "GeomFromWKB(0x#{::RGeo::WKRep::WKBGenerator.new(hex_format: true).generate(value_)},#{value_.srid})"
  else
    super
  end
end

#set_rgeo_factory_settings(factory_settings_) ⇒ Object



50
51
52
# File 'lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb', line 50

def set_rgeo_factory_settings(factory_settings_)
  @rgeo_factory_settings = factory_settings_
end

#spatial_column_constructor(name_) ⇒ Object



58
59
60
# File 'lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb', line 58

def spatial_column_constructor(name_)
  ::RGeo::ActiveRecord::DEFAULT_SPATIAL_COLUMN_CONSTRUCTORS[name_]
end

#type_to_sql(type_, limit_ = nil, precision_ = nil, scale_ = nil) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb', line 74

def type_to_sql(type_, limit_=nil, precision_=nil, scale_=nil)
  if (info_ = spatial_column_constructor(type_.to_sym))
    type_ = limit_[:type] || type_ if limit_.is_a?(::Hash)
    type_ = 'geometry' if type_.to_s == 'spatial'
    type_ = type_.to_s.gsub('_', '').upcase
  end
  super(type_, limit_, precision_, scale_)
end