Skip to content

Commit f14b07f

Browse files
committed
Upgrate Active Record dependency to 4.2.
This commit reapplies fjl82 and agoln's commits from fjl82/activerecord-mysql2spatial-adapter to current master. Not all tests pass yet.
1 parent b0ab983 commit f14b07f

File tree

5 files changed

+73
-85
lines changed

5 files changed

+73
-85
lines changed

activerecord-mysql2spatial-adapter.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
['Version']
4545
s_.extra_rdoc_files = ::Dir.glob("*.rdoc")
4646
s_.platform = ::Gem::Platform::RUBY
47-
s_.add_dependency('activerecord', '>= 4.0', '< 4.2')
48-
s_.add_dependency('rgeo-activerecord', '~> 1.3')
47+
s_.add_dependency('activerecord', '~> 4.2.9')
48+
s_.add_dependency('rgeo-activerecord', '~> 2.1.1')
4949
s_.add_dependency('mysql2', '>= 0.2.13', '< 0.4.0')
5050
s_.add_development_dependency('rake', '>= 0.9.2')
5151
s_.add_development_dependency('rdoc', '>= 3.12')

lib/active_record/connection_adapters/mysql2spatial_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,4 @@ module Mysql2SpatialAdapter
7575
require 'active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb'
7676
require 'active_record/connection_adapters/mysql2spatial_adapter/spatial_column.rb'
7777
require 'active_record/connection_adapters/mysql2spatial_adapter/arel_tosql.rb'
78+
require 'active_record/type/spatial.rb'

lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ def add_index(table_name_, column_name_, options_={})
9393
end
9494

9595
def columns(table_name_, name_=nil)
96-
result_ = execute("SHOW FIELDS FROM #{quote_table_name(table_name_)}", :skip_logging)
96+
result_ = @connection.query "SHOW FULL FIELDS FROM #{quote_table_name(table_name_)}"
9797
columns_ = []
9898
result_.each(symbolize_keys: true, as: :hash) do |field_|
9999
columns_ << SpatialColumn.new(@rgeo_factory_settings, table_name_.to_s,
100-
field_[:Field], field_[:Default], field_[:Type], field_[:Null] == "YES")
100+
field_[:Field], field_[:Default], lookup_cast_type(field_[:Type]), field_[:Type], field_[:Null] == "YES", field_[:Collation], field_[:Extra])
101101
end
102102
columns_
103103
end
@@ -128,6 +128,17 @@ def indexes(table_name_, name_=nil)
128128
end
129129
indexes_
130130
end
131+
132+
protected
133+
134+
def initialize_type_map(m)
135+
super
136+
register_class_with_limit m, %r(geometry)i, Type::Spatial
137+
m.alias_type %r(point)i, 'geometry'
138+
m.alias_type %r(linestring)i, 'geometry'
139+
m.alias_type %r(polygon)i, 'geometry'
140+
end
141+
131142
end
132143
end
133144
end

lib/active_record/connection_adapters/mysql2spatial_adapter/spatial_column.rb

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -35,97 +35,20 @@
3535
module ActiveRecord
3636
module ConnectionAdapters
3737
module Mysql2SpatialAdapter
38-
39-
# ActiveRecord 3.2 uses ConnectionAdapters::Mysql2Adapter::Column
40-
# whereas 3.0 and 3.1 use ConnectionAdapters::Mysql2Column
41-
column_base_class_ = defined?(ConnectionAdapters::Mysql2Adapter::Column) ?
42-
ConnectionAdapters::Mysql2Adapter::Column : ConnectionAdapters::Mysql2Column
43-
44-
class SpatialColumn < column_base_class_
45-
38+
class SpatialColumn < ConnectionAdapters::Mysql2Adapter::Column
4639
FACTORY_SETTINGS_CACHE = {}
4740

48-
def initialize(factory_settings_, table_name_, name_, default_, sql_type_=nil, null_=true)
49-
@factory_settings = factory_settings_
50-
@table_name = table_name_
51-
super(name_, default_,sql_type_, null_)
52-
@geometric_type = ::RGeo::ActiveRecord.geometric_type_from_name(sql_type_)
41+
def initialize(factory_settings_, table_name_, name_, default_, cast_type_ = nil, sql_type_ = nil, null_ = true, collation_ = nil, extra_ = "")
42+
super(name_, default_, cast_type_, sql_type_, null_, collation_, false, extra_)
5343
if type == :spatial
54-
@limit = { type: @geometric_type.type_name.underscore }
44+
cast_type.set_geo_params(factory_settings_, table_name_, ::RGeo::ActiveRecord.geometric_type_from_name(sql_type_))
5545
end
5646
FACTORY_SETTINGS_CACHE[factory_settings_.object_id] = factory_settings_
5747
end
5848

59-
attr_reader :geometric_type
60-
61-
def spatial?
62-
type == :spatial
63-
end
64-
65-
def klass
66-
type == :spatial ? ::RGeo::Feature::Geometry : super
67-
end
68-
69-
def type_cast(value_)
70-
if type == :spatial
71-
SpatialColumn.convert_to_geometry(value_, @factory_settings, @table_name, name)
72-
else
73-
super
74-
end
75-
end
76-
77-
def type_cast_code(var_name_)
78-
if type == :spatial
79-
"::ActiveRecord::ConnectionAdapters::Mysql2SpatialAdapter::SpatialColumn.convert_to_geometry("+
80-
"#{var_name_}, ::ActiveRecord::ConnectionAdapters::Mysql2SpatialAdapter::SpatialColumn::"+
81-
"FACTORY_SETTINGS_CACHE[#{@factory_settings.object_id}], #{@table_name.inspect}, #{name.inspect})"
82-
else
83-
super
84-
end
85-
end
86-
87-
private
88-
89-
def simplified_type(sql_type_)
90-
sql_type_ =~ /geometry|point|linestring|polygon/i ? :spatial : super
91-
end
92-
93-
94-
def self.convert_to_geometry(input_, factory_settings_, table_name_, column_)
95-
case input_
96-
when ::RGeo::Feature::Geometry
97-
factory_ = factory_settings_.get_column_factory(table_name_, column_, srid: input_.srid)
98-
::RGeo::Feature.cast(input_, factory_) rescue nil
99-
when ::String
100-
marker_ = input_[4,1]
101-
if marker_ == "\x00" || marker_ == "\x01"
102-
factory_ = factory_settings_.get_column_factory(table_name_, column_,
103-
srid: input_[0, 4].unpack(marker_ == "\x01" ? 'V' : 'N').first)
104-
::RGeo::WKRep::WKBParser.new(factory_).parse(input_[4..-1]) rescue nil
105-
elsif input_[0,10] =~ /[0-9a-fA-F]{8}0[01]/
106-
srid_ = input_[0,8].to_i(16)
107-
if input[9,1] == '1'
108-
srid_ = [srid_].pack('V').unpack('N').first
109-
end
110-
factory_ = factory_settings_.get_column_factory(table_name_, column_, srid: srid_)
111-
::RGeo::WKRep::WKBParser.new(factory_).parse(input_[8..-1]) rescue nil
112-
else
113-
factory_ = factory_settings_.get_column_factory(table_name_, column_)
114-
::RGeo::WKRep::WKTParser.new(factory_, support_ewkt: true).parse(input_) rescue nil
115-
end
116-
else
117-
nil
118-
end
119-
end
120-
121-
12249
end
123-
124-
12550
end
126-
12751
end
128-
12952
end
13053

13154
# :startdoc:

lib/active_record/type/spatial.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module ActiveRecord
2+
module Type
3+
class Spatial < Value # :nodoc:
4+
def type
5+
:spatial
6+
end
7+
8+
def spatial?
9+
type == :spatial
10+
end
11+
12+
def klass
13+
type == :spatial ? ::RGeo::Feature::Geometry : super
14+
end
15+
16+
def set_geo_params(factory_settings, table_name, geometric_type)
17+
@factory_settings = factory_settings
18+
@table_name = table_name
19+
@geometric_type = geometric_type
20+
end
21+
22+
private
23+
24+
def cast_value(value)
25+
case value
26+
when ::RGeo::Feature::Geometry
27+
factory = @factory_settings.get_column_factory(@table_name, @column, :srid => value.srid)
28+
::RGeo::Feature.cast(value, factory) rescue nil
29+
when ::String
30+
marker = value[4,1]
31+
if marker == "\x00" || marker == "\x01"
32+
factory = @factory_settings.get_column_factory(@table_name, @column,
33+
:srid => value[0,4].unpack(marker == "\x01" ? 'V' : 'N').first)
34+
::RGeo::WKRep::WKBParser.new(factory).parse(value[4..-1]) rescue nil
35+
elsif value[0,10] =~ /[0-9a-fA-F]{8}0[01]/
36+
srid = value[0,8].to_i(16)
37+
if value[9,1] == '1'
38+
srid = [srid].pack('V').unpack('N').first
39+
end
40+
factory = @factory_settings.get_column_factory(@table_name, @column, :srid => srid)
41+
::RGeo::WKRep::WKBParser.new(factory).parse(value[8..-1]) rescue nil
42+
else
43+
factory = @factory_settings.get_column_factory(@table_name, @column)
44+
::RGeo::WKRep::WKTParser.new(factory, support_ewkt: true).parse(value) rescue nil
45+
end
46+
else
47+
nil
48+
end
49+
end
50+
51+
end
52+
end
53+
end

0 commit comments

Comments
 (0)