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

Instance Method Details

#adapter_nameObject



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

def adapter_name
  MysqlSpatialAdapter::ADAPTER_NAME
end

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



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

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



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

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(field_[0], field_[4], field_[1], field_[2] == "YES")
  end
  result_.free
  columns_
end

#indexes(table_name_, name_ = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 110

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



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

def native_database_types
  NATIVE_DATABASE_TYPES
end

#quote(value_, column_ = nil) ⇒ Object



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

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

#spatial_column_constructor(name_) ⇒ Object



57
58
59
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 57

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

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



76
77
78
79
80
81
82
83
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/main_adapter.rb', line 76

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