Class: ActiveRecord::ConnectionAdapters::MysqlSpatialAdapter::MainAdapter

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

Constant Summary collapse

NATIVE_DATABASE_TYPES =
MysqlAdapter::NATIVE_DATABASE_TYPES.merge(:spatial => {:name => "geometry"})

Instance Method Summary collapse

Constructor Details

#initialize(*args_) ⇒ MainAdapter

Returns a new instance of MainAdapter.



52
53
54
55
56
57
58
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 52

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

Instance Method Details

#adapter_nameObject



66
67
68
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 66

def adapter_name
  MysqlSpatialAdapter::ADAPTER_NAME
end

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



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 132

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



145
146
147
148
149
150
151
152
153
154
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 145

def columns(table_name_, name_=nil)
  result_ = execute("SHOW FIELDS FROM #{quote_table_name(table_name_)}", :skip_logging)
  columns_ = []
  result_.each do |field_|
    columns_ << SpatialColumn.new(@rgeo_factory_settings, table_name_.to_s,
      field_[0], field_[4], field_[1], field_[2] == "YES")
  end
  result_.free
  columns_
end

#exec_stmt(sql_, name_, binds_) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 108

def exec_stmt(sql_, name_, binds_)
  real_binds_ = []
  binds_.each do |bind_|
    if bind_[0].spatial?
      real_binds_ << bind_
      real_binds_ << [bind_[0], bind_[1].srid]
    else
      real_binds_ << bind_
    end
  end
  super(sql_, name_, real_binds_)
end

#indexes(table_name_, name_ = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 157

def indexes(table_name_, name_=nil)
  indexes_ = []
  current_index_ = nil
  result_ = execute("SHOW KEYS FROM #{quote_table_name(table_name_)}", name_)
  result_.each do |row_|
    if current_index_ != row_[2]
      next if row_[2] == "PRIMARY" # skip the primary key
      current_index_ = row_[2]
      indexes_ << ::RGeo::ActiveRecord::SpatialIndexDefinition.new(row_[0], row_[2], row_[1] == "0", [], [], row_[10] == 'SPATIAL')
    end
    last_index_ = indexes_.last
    last_index_.columns << row_[4]
    last_index_.lengths << row_[7] unless last_index_.spatial
  end
  result_.free
  indexes_
end

#native_database_typesObject



76
77
78
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 76

def native_database_types
  NATIVE_DATABASE_TYPES
end

#quote(value_, column_ = nil) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 81

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



61
62
63
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 61

def set_rgeo_factory_settings(factory_settings_)
  @rgeo_factory_settings = factory_settings_
end

#spatial_column_constructor(name_) ⇒ Object



71
72
73
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 71

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

#substitute_at(column_, index_) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 90

def substitute_at(column_, index_)
  if column_.spatial?
    ::Arel.sql('GeomFromText(?,?)')
  else
    super
  end
end

#type_cast(value_, column_) ⇒ Object



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

def type_cast(value_, column_)
  if column_.spatial? && ::RGeo::Feature::Geometry.check_type(value_)
    ::RGeo::WKRep::WKTGenerator.new.generate(value_)
  else
    super
  end
end

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



122
123
124
125
126
127
128
129
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 122

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