activerecord-mysqlspatial-adapter 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,15 @@
1
+ === 0.4.0 / 2011-08-15
2
+
3
+ * Various fixes towards Rails 3.1 compatibility. However, this adapter is not yet actually Rails 3.1 compatible because of a bug in the mysql gem. Use activerecord-mysql2-adapter for now if you need Rails 3.1 support.
4
+ * Now requires rgeo-activerecord 0.4.0.
5
+ * INCOMPATIBLE CHANGE: simple queries (e.g. MyClass.where(:latlon => my_point)) use an objective rather than spatial equality test. Earlier versions transformed this form to use st_equals, but now if you need to test for spatial equality, you'll need to call st_equals explicitly. I'm still evaluating which direction we want to go with this in the future, but we may be stuck with the current behavior because the hack required to transform these queries to use spatial equality was egregious and broke in Rails 3.1 with no clear workaround.
6
+
7
+ === 0.3.3 / 2011-06-21
8
+
9
+ * Require latest rgeo-activerecord to get some fixes.
10
+ * Support hex format for attribute setting.
11
+ * No longer raises exceptions if parse fails on attribute setting. (Reported by Daniel Hackney)
12
+
1
13
  === 0.3.2 / 2011-04-11
2
14
 
3
15
  * A .gemspec file is now available for gem building and bundler git integration.
@@ -77,7 +77,7 @@ Now you can interact with the data using the RGeo types:
77
77
 
78
78
  === Spatial Queries
79
79
 
80
- You can create simple queries based on spatial equality in the same way
80
+ You can create simple queries based on objective equality in the same way
81
81
  you would on a scalar column:
82
82
 
83
83
  rec = MySpatialTable.where(:latlon => RGeo::Geos.factory.point(-122, 47)).first
@@ -94,6 +94,11 @@ write more complex queries in SQL.
94
94
 
95
95
  == Installation And Configuration
96
96
 
97
+ IMPORTANT: This adapter is currently *not* compatible with Rails 3.1.
98
+ This is due to an apparent bug in the mysql gem (as of version 2.8.1),
99
+ which does not appear to recognize MYSQL_TYPE_GEOMETRY in the binary
100
+ protocol.
101
+
97
102
  === Installing The Adapter Gem
98
103
 
99
104
  This adapter has the following requirements:
@@ -102,8 +107,8 @@ This adapter has the following requirements:
102
107
  * MySQL server 5.0 or later required for spatial extensions.
103
108
  * \ActiveRecord 3.0.3 or later. Earlier versions will not work.
104
109
  * mysql gem 2.8 or later.
105
- * rgeo gem 0.2.8 or later.
106
- * rgeo-activerecord gem 0.3.3 or later.
110
+ * rgeo gem 0.3.2 or later.
111
+ * rgeo-activerecord gem 0.4.0 or later.
107
112
 
108
113
  Install this adapter as a gem:
109
114
 
@@ -156,6 +161,8 @@ Contributions are welcome. Fork the project on Github.
156
161
 
157
162
  Report bugs on Github issues at http://github.org/dazuma/activerecord-mysqlspatial-adapter/issues
158
163
 
164
+ Support available on the rgeo-users google group at http://groups.google.com/group/rgeo-users
165
+
159
166
  Contact the author at dazuma at gmail dot com.
160
167
 
161
168
  === Acknowledgments
data/Version CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.4.0
@@ -49,6 +49,11 @@ module ActiveRecord
49
49
  NATIVE_DATABASE_TYPES = MysqlAdapter::NATIVE_DATABASE_TYPES.merge(:spatial => {:name => "geometry"})
50
50
 
51
51
 
52
+ def set_rgeo_factory_settings(factory_settings_)
53
+ @rgeo_factory_settings = factory_settings_
54
+ end
55
+
56
+
52
57
  def adapter_name
53
58
  MysqlSpatialAdapter::ADAPTER_NAME
54
59
  end
@@ -73,6 +78,38 @@ module ActiveRecord
73
78
  end
74
79
 
75
80
 
81
+ def substitute_at(column_, index_)
82
+ if column_.spatial?
83
+ ::Arel.sql('GeomFromText(?,?)')
84
+ else
85
+ super
86
+ end
87
+ end
88
+
89
+
90
+ def type_cast(value_, column_)
91
+ if column_.spatial? && ::RGeo::Feature::Geometry.check_type(value_)
92
+ ::RGeo::WKRep::WKTGenerator.new.generate(value_)
93
+ else
94
+ super
95
+ end
96
+ end
97
+
98
+
99
+ def exec_stmt(sql_, name_, binds_)
100
+ real_binds_ = []
101
+ binds_.each do |bind_|
102
+ if bind_[0].spatial?
103
+ real_binds_ << bind_
104
+ real_binds_ << [bind_[0], bind_[1].srid]
105
+ else
106
+ real_binds_ << bind_
107
+ end
108
+ end
109
+ super(sql_, name_, real_binds_)
110
+ end
111
+
112
+
76
113
  def type_to_sql(type_, limit_=nil, precision_=nil, scale_=nil)
77
114
  if (info_ = spatial_column_constructor(type_.to_sym))
78
115
  type_ = limit_[:type] || type_ if limit_.is_a?(::Hash)
@@ -100,7 +137,8 @@ module ActiveRecord
100
137
  result_ = execute("SHOW FIELDS FROM #{quote_table_name(table_name_)}", :skip_logging)
101
138
  columns_ = []
102
139
  result_.each do |field_|
103
- columns_ << SpatialColumn.new(field_[0], field_[4], field_[1], field_[2] == "YES")
140
+ columns_ << SpatialColumn.new(@rgeo_factory_settings, table_name_.to_s,
141
+ field_[0], field_[4], field_[1], field_[2] == "YES")
104
142
  end
105
143
  result_.free
106
144
  columns_
@@ -46,18 +46,14 @@ module ActiveRecord
46
46
  class SpatialColumn < ConnectionAdapters::MysqlColumn
47
47
 
48
48
 
49
- def initialize(name_, default_, sql_type_=nil, null_=true)
49
+ def initialize(factory_settings_, table_name_, name_, default_, sql_type_=nil, null_=true)
50
+ @factory_settings = factory_settings_
51
+ @table_name = table_name_
50
52
  super(name_, default_, sql_type_, null_)
51
53
  @geometric_type = ::RGeo::ActiveRecord.geometric_type_from_name(sql_type_)
52
54
  if type == :spatial
53
55
  @limit = {:type => @geometric_type.type_name.underscore}
54
56
  end
55
- @ar_class = ::ActiveRecord::Base
56
- end
57
-
58
-
59
- def set_ar_class(val_)
60
- @ar_class = val_
61
57
  end
62
58
 
63
59
 
@@ -75,12 +71,21 @@ module ActiveRecord
75
71
 
76
72
 
77
73
  def type_cast(value_)
78
- type == :spatial ? SpatialColumn.convert_to_geometry(value_, @ar_class, name) : super
74
+ if type == :spatial
75
+ SpatialColumn.convert_to_geometry(value_, @factory_settings, @table_name, name)
76
+ else
77
+ super
78
+ end
79
79
  end
80
80
 
81
81
 
82
82
  def type_cast_code(var_name_)
83
- type == :spatial ? "::ActiveRecord::ConnectionAdapters::MysqlSpatialAdapter::SpatialColumn.convert_to_geometry(#{var_name_}, self.class, #{name.inspect})" : super
83
+ if type == :spatial
84
+ "::ActiveRecord::ConnectionAdapters::MysqlSpatialAdapter::SpatialColumn.convert_to_geometry("+
85
+ "#{var_name_}, self.class.rgeo_factory_settings, self.class.table_name, #{name.inspect})"
86
+ else
87
+ super
88
+ end
84
89
  end
85
90
 
86
91
 
@@ -91,19 +96,27 @@ module ActiveRecord
91
96
  end
92
97
 
93
98
 
94
- def self.convert_to_geometry(input_, ar_class_, column_)
99
+ def self.convert_to_geometry(input_, factory_settings_, table_name_, column_)
95
100
  case input_
96
101
  when ::RGeo::Feature::Geometry
97
- factory_ = ar_class_.rgeo_factory_for_column(column_, :srid => input_.srid)
98
- ::RGeo::Feature.cast(input_, factory_)
102
+ factory_ = factory_settings_.get_column_factory(table_name_, column_, :srid => input_.srid)
103
+ ::RGeo::Feature.cast(input_, factory_) rescue nil
99
104
  when ::String
100
105
  marker_ = input_[4,1]
101
106
  if marker_ == "\x00" || marker_ == "\x01"
102
- factory_ = ar_class_.rgeo_factory_for_column(column_, :srid => input_[0,4].unpack(marker_ == "\x01" ? 'V' : 'N').first)
103
- ::RGeo::WKRep::WKBParser.new(factory_).parse(input_[4..-1])
107
+ factory_ = factory_settings_.get_column_factory(table_name_, column_,
108
+ :srid => input_[0,4].unpack(marker_ == "\x01" ? 'V' : 'N').first)
109
+ ::RGeo::WKRep::WKBParser.new(factory_).parse(input_[4..-1]) rescue nil
110
+ elsif input_[0,10] =~ /[0-9a-fA-F]{8}0[01]/
111
+ srid_ = input_[0,8].to_i(16)
112
+ if input[9,1] == '1'
113
+ srid_ = [srid_].pack('V').unpack('N').first
114
+ end
115
+ factory_ = factory_settings_.get_column_factory(table_name_, column_, :srid => srid_)
116
+ ::RGeo::WKRep::WKBParser.new(factory_).parse(input_[8..-1]) rescue nil
104
117
  else
105
- factory_ = ar_class_.rgeo_factory_for_column(column_)
106
- ::RGeo::WKRep::WKTParser.new(factory_, :support_ewkt => true).parse(input_)
118
+ factory_ = factory_settings_.get_column_factory(table_name_, column_)
119
+ ::RGeo::WKRep::WKTParser.new(factory_, :support_ewkt => true).parse(input_) rescue nil
107
120
  end
108
121
  else
109
122
  nil
@@ -105,7 +105,7 @@ module RGeo
105
105
  assert_nil(obj_.latlon)
106
106
  obj_.latlon = @factory.point(1, 2)
107
107
  assert_equal(@factory.point(1, 2), obj_.latlon)
108
- assert_equal(4326, obj_.latlon.srid)
108
+ assert_equal(3785, obj_.latlon.srid)
109
109
  end
110
110
 
111
111
 
@@ -127,7 +127,7 @@ module RGeo
127
127
  id_ = obj_.id
128
128
  obj2_ = klass_.find(id_)
129
129
  assert_equal(@factory.point(1, 2), obj2_.latlon)
130
- assert_equal(4326, obj2_.latlon.srid)
130
+ assert_equal(3785, obj2_.latlon.srid)
131
131
  end
132
132
 
133
133
 
@@ -72,14 +72,14 @@ module RGeo
72
72
  obj_.latlon = @factory.point(1, 2)
73
73
  obj_.save!
74
74
  id_ = obj_.id
75
- obj2_ = klass_.where(:latlon => @factory.multi_point([@factory.point(1, 2)])).first
75
+ obj2_ = klass_.where(:latlon => @factory.point(1, 2)).first
76
76
  assert_equal(id_, obj2_.id)
77
77
  obj3_ = klass_.where(:latlon => @factory.point(2, 2)).first
78
78
  assert_nil(obj3_)
79
79
  end
80
80
 
81
81
 
82
- def test_query_point_wkt
82
+ def _test_query_point_wkt
83
83
  klass_ = populate_ar_class(:latlon_point)
84
84
  obj_ = klass_.new
85
85
  obj_.latlon = @factory.point(1, 2)
@@ -107,7 +107,7 @@ module RGeo
107
107
  assert_nil(obj3_)
108
108
  end
109
109
 
110
-
110
+
111
111
  else
112
112
 
113
113
  puts "WARNING: The current Arel does not support named functions. Spatial expression tests skipped."
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: activerecord-mysqlspatial-adapter
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.2
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Daniel Azuma
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-04-11 00:00:00 Z
13
+ date: 2011-08-15 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rgeo-activerecord
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ~>
22
22
  - !ruby/object:Gem::Version
23
- version: 0.3.3
23
+ version: 0.4.0
24
24
  type: :runtime
25
25
  version_requirements: *id001
26
26
  - !ruby/object:Gem::Dependency
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  requirements: []
78
78
 
79
79
  rubyforge_project: virtuoso
80
- rubygems_version: 1.7.2
80
+ rubygems_version: 1.8.7
81
81
  signing_key:
82
82
  specification_version: 3
83
83
  summary: An ActiveRecord adapter for MySQL Spatial Extensions, based on RGeo and the mysql gem.