Skip to content

Commit e10e441

Browse files
committed
Upgrate Active Record dependency to 4.2. Drop rgeo 0.4 appraisal.
1 parent a6ff36e commit e10e441

File tree

9 files changed

+76
-120
lines changed

9 files changed

+76
-120
lines changed

Appraisals

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
appraise 'rgeo-0.4.0' do
2-
gem 'rgeo', '0.4.0'
3-
end
4-
5-
appraise 'rgeo-0.6.0' do
1+
appraise 'current' do
2+
gem 'activerecord', '4.2.9'
3+
gem 'mysql2', '0.3.21'
64
gem 'rgeo', '0.6.0'
75
end

Gemfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,3 @@
3535
source "https://rubygems.org"
3636

3737
gemspec
38-
39-
# This dependency is here because gemspecs can't use git sources.
40-
# It overrides the less specific dependency in gemspec.
41-
gem 'rgeo-activerecord', git: 'https://github.com/rgeo/rgeo-activerecord.git', branch: '1.0'

activerecord-mysql2spatial-adapter.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
['Version']
4848
s_.extra_rdoc_files = ::Dir.glob("*.rdoc")
4949
s_.platform = ::Gem::Platform::RUBY
50-
s_.add_dependency('activerecord', '>= 4.0', '< 4.2')
51-
s_.add_dependency('rgeo-activerecord', '~> 1.0')
50+
s_.add_dependency('activerecord', '~> 4.2.9')
51+
s_.add_dependency('rgeo-activerecord', '~> 2.1.1')
5252
s_.add_dependency('mysql2', '>= 0.2.13', '< 0.4.0')
5353
s_.add_development_dependency('appraisal')
5454
s_.add_development_dependency('pry')

gemfiles/rgeo_0.6.0.gemfile renamed to gemfiles/current.gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
source "https://rubygems.org"
44

5-
gem "rgeo-activerecord", git: "https://github.com/rgeo/rgeo-activerecord.git", branch: "1.0"
5+
gem "activerecord", "4.2.9"
6+
gem "mysql2", "0.3.21"
67
gem "rgeo", "0.6.0"
78

89
gemspec path: "../"

gemfiles/rgeo_0.4.0.gemfile

Lines changed: 0 additions & 8 deletions
This file was deleted.

lib/active_record/connection_adapters/mysql2spatial_adapter.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3232
# POSSIBILITY OF SUCH DAMAGE.
3333
# -----------------------------------------------------------------------------
34-
;
35-
3634

3735
require 'rgeo/active_record'
3836
require 'active_record/connection_adapters/mysql2_adapter'
@@ -88,3 +86,4 @@ module Mysql2SpatialAdapter
8886
require 'active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb'
8987
require 'active_record/connection_adapters/mysql2spatial_adapter/spatial_column.rb'
9088
require 'active_record/connection_adapters/mysql2spatial_adapter/arel_tosql.rb'
89+
require 'active_record/type/spatial.rb'

lib/active_record/connection_adapters/mysql2spatial_adapter/main_adapter.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3232
# POSSIBILITY OF SUCH DAMAGE.
3333
# -----------------------------------------------------------------------------
34-
;
35-
3634

3735
# :stopdoc:
3836

@@ -111,11 +109,11 @@ def add_index(table_name_, column_name_, options_={})
111109

112110

113111
def columns(table_name_, name_=nil)
114-
result_ = execute("SHOW FIELDS FROM #{quote_table_name(table_name_)}", :skip_logging)
112+
result_ = @connection.query "SHOW FULL FIELDS FROM #{quote_table_name(table_name_)}"
115113
columns_ = []
116114
result_.each(:symbolize_keys => true, :as => :hash) do |field_|
117115
columns_ << SpatialColumn.new(@rgeo_factory_settings, table_name_.to_s,
118-
field_[:Field], field_[:Default], field_[:Type], field_[:Null] == "YES")
116+
field_[:Field], field_[:Default], lookup_cast_type(field_[:Type]), field_[:Type], field_[:Null] == "YES", field_[:Collation], field_[:Extra])
119117
end
120118
columns_
121119
end
@@ -147,6 +145,15 @@ def indexes(table_name_, name_=nil)
147145
indexes_
148146
end
149147

148+
protected
149+
150+
def initialize_type_map(m)
151+
super
152+
register_class_with_limit m, %r(geometry)i, Type::Spatial
153+
m.alias_type %r(point)i, 'geometry'
154+
m.alias_type %r(linestring)i, 'geometry'
155+
m.alias_type %r(polygon)i, 'geometry'
156+
end
150157

151158
end
152159

lib/active_record/connection_adapters/mysql2spatial_adapter/spatial_column.rb

Lines changed: 4 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -31,116 +31,26 @@
3131
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3232
# POSSIBILITY OF SUCH DAMAGE.
3333
# -----------------------------------------------------------------------------
34-
;
35-
3634

3735
# :stopdoc:
3836

3937
module ActiveRecord
40-
4138
module ConnectionAdapters
42-
4339
module Mysql2SpatialAdapter
44-
45-
46-
# ActiveRecord 3.2 uses ConnectionAdapters::Mysql2Adapter::Column
47-
# whereas 3.0 and 3.1 use ConnectionAdapters::Mysql2Column
48-
column_base_class_ = defined?(ConnectionAdapters::Mysql2Adapter::Column) ?
49-
ConnectionAdapters::Mysql2Adapter::Column : ConnectionAdapters::Mysql2Column
50-
51-
class SpatialColumn < column_base_class_
52-
53-
40+
class SpatialColumn < ConnectionAdapters::Mysql2Adapter::Column
5441
FACTORY_SETTINGS_CACHE = {}
5542

56-
57-
def initialize(factory_settings_, table_name_, name_, default_, sql_type_=nil, null_=true)
58-
@factory_settings = factory_settings_
59-
@table_name = table_name_
60-
super(name_, default_,sql_type_, null_)
61-
@geometric_type = ::RGeo::ActiveRecord.geometric_type_from_name(sql_type_)
43+
def initialize(factory_settings_, table_name_, name_, default_, cast_type_ = nil, sql_type_ = nil, null_ = true, collation_ = nil, extra_ = "")
44+
super(name_, default_, cast_type_, sql_type_, null_, collation_, false, extra_)
6245
if type == :spatial
63-
@limit = { type: @geometric_type.type_name.underscore }
46+
cast_type.set_geo_params(factory_settings_, table_name_, ::RGeo::ActiveRecord.geometric_type_from_name(sql_type_))
6447
end
6548
FACTORY_SETTINGS_CACHE[factory_settings_.object_id] = factory_settings_
6649
end
6750

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

14656
# :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)