Skip to content

Commit

Permalink
did not project every column, so did delete overriding methods.
Browse files Browse the repository at this point in the history
and changed a method, cast_value.
  • Loading branch information
jeff(황용대) committed Feb 14, 2017
1 parent 5c5d8b0 commit 93c96f7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 64 deletions.
24 changes: 0 additions & 24 deletions lib/active_record/connection_adapters/mysql2rgeo/arel_tosql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,6 @@ def st_func(standard_name)
FUNC_MAP[standard_name.downcase] || standard_name
end

def visit_Arel_Nodes_SelectCore(o, collector)
len = o.projections.length - 1
if len == 0
if !o.projections.first.nil? && o.projections.first.respond_to?(:relation)
projections = []
@connection.columns(o.projections.first.relation.name).each do |x|
projections << o.projections.first.relation[x.name.to_sym]
end
o.projections = projections
end
end
super
end

def visit_Arel_Attributes_Attribute(o, collector)
join_name = o.relation.table_alias || o.relation.name

collector << if (!column_for(o).nil? && column_for(o).type == :geometry) && !collector.value.include?(" WHERE ")
"ST_AsText(#{quote_table_name join_name}.#{quote_column_name o.name}) as #{quote_column_name o.name}"
else
"#{quote_table_name join_name}.#{quote_column_name o.name}"
end
end

def visit_String(node, collector)
collector << "#{st_func('ST_WKTToSQL')}(#{quote(node)})"
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ActiveRecord
module ConnectionAdapters
module Mysql2Rgeo
VERSION = "1.0.2".freeze
VERSION = "1.0.3".freeze
end
end
end
60 changes: 28 additions & 32 deletions lib/active_record/type/spatial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,50 +59,46 @@ def spatial?

def spatial_factory
@spatial_factory ||=
RGeo::ActiveRecord::SpatialFactoryStore.instance.factory(
geo_type: @geo_type,
sql_type: @sql_type,
srid: @srid
)
RGeo::ActiveRecord::SpatialFactoryStore.instance.factory(
geo_type: @geo_type,
sql_type: @sql_type,
srid: @srid
)
end

# support setting an RGeo object or a WKT string
def serialize(value)
return if value.nil?
geo_value = cast_value(value)
geo_value
# TODO: - only valid types should be allowed

# TODO - only valid types should be allowed
# e.g. linestring is not valid for point column
# raise "maybe should raise" unless RGeo::Feature::Geometry.check_type(geo_value)
# RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(geo_value)
raise "maybe should raise" unless RGeo::Feature::Geometry.check_type(geo_value)
geo_value
end

private

# Convenience method for types which do not need separate type casting
# behavior for user and database inputs. Called by Value#cast for
# values except +nil+.
def cast_value(value) # :doc:
def cast_value(value)
return if value.nil?
value.class === "String" ? parse_wkt(value) : parse_wkt(value.to_s)
end

# convert WKT string into RGeo object
def parse_wkt(string)
wkt_parser(string).parse(string)
rescue RGeo::Error::ParseError
nil
end

def binary_string?(string)
string[0] == "\x00" || string[0] == "\x01" || string[0, 4] =~ /[0-9a-fA-F]{4}/
end

def wkt_parser(string)
if binary_string?(string)
RGeo::WKRep::WKBParser.new(spatial_factory, support_ewkb: true, default_srid: @srid)
else
RGeo::WKRep::WKTParser.new(spatial_factory, support_ewkt: true, default_srid: @srid)
case value
when ::RGeo::Feature::Geometry
value
# RGeo::Feature.cast(value, spatial_factory) rescue nil
when ::String
marker = value[4, 1]
if marker == "\x00" || marker == "\x01"
srid = value[0, 4].unpack(marker == "\x01" ? 'V' : 'N').first
RGeo::WKRep::WKBParser.new(spatial_factory, support_ewkb: true, default_srid: srid).parse(value[4..-1]) rescue nil
elsif value[0, 10] =~ /[0-9a-fA-F]{8}0[01]/
srid = value[0, 8].to_i(16)
srid = [srid].pack('V').unpack('N').first if value[9, 1] == '1'
RGeo::WKRep::WKBParser.new(spatial_factory, support_ewkb: true, default_srid: srid).parse(value[8..-1]) rescue nil
else
RGeo::WKRep::WKTParser.new(spatial_factory, support_ewkt: true, default_srid: @srid).parse(value) rescue nil
end
else
nil
end
end
end
Expand Down
9 changes: 3 additions & 6 deletions test/spatial_queries_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ def test_query_point_wkt
obj.latlon = factory.point(1.0, 2.0)
obj.save!
id = obj.id
obj2 = SpatialModel.where(latlon: "SRID=3785;POINT(1 2)").first
obj2 = SpatialModel.where(latlon: "POINT(1 2)").first
puts obj2
refute_nil(obj2)
assert_equal(id, obj2.id)
obj3 = SpatialModel.where(latlon: "SRID=3785;POINT(2 2)").first
obj3 = SpatialModel.where(latlon: "POINT(2 2)").first
assert_nil(obj3)
end

Expand All @@ -33,11 +34,9 @@ def test_query_st_distance
obj.latlon = factory.point(1.0, 2.0)
obj.save!
id = obj.id
# obj2 = SpatialModel.where(SpatialModel.arel_table[:latlon].st_distance("SRID=3785;POINT(2 3)").lt(2)).first
obj2 = SpatialModel.where(SpatialModel.arel_table[:latlon].st_distance("POINT(2 3)").lt(2)).first
refute_nil(obj2)
assert_equal(id, obj2.id)
# obj3 = SpatialModel.where(SpatialModel.arel_table[:latlon].st_distance("SRID=3785;POINT(2 3)").gt(2)).first
obj3 = SpatialModel.where(SpatialModel.arel_table[:latlon].st_distance("POINT(2 3)").gt(2)).first
assert_nil(obj3)
end
Expand All @@ -48,11 +47,9 @@ def test_query_st_distance_from_constant
obj.latlon = factory.point(1.0, 2.0)
obj.save!
id = obj.id
# obj2 = SpatialModel.where(::Arel.spatial("SRID=3785;POINT(2 3)").st_distance(SpatialModel.arel_table[:latlon]).lt(2)).first
obj2 = SpatialModel.where(::Arel.spatial("POINT(2 3)").st_distance(SpatialModel.arel_table[:latlon]).lt(2)).first
refute_nil(obj2)
assert_equal(id, obj2.id)
# obj3 = SpatialModel.where(::Arel.spatial("SRID=3785;POINT(2 3)").st_distance(SpatialModel.arel_table[:latlon]).gt(2)).first
obj3 = SpatialModel.where(::Arel.spatial("POINT(2 3)").st_distance(SpatialModel.arel_table[:latlon]).gt(2)).first
assert_nil(obj3)
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SpatialMultiModel < ActiveRecord::Base
end

def factory
RGeo::Cartesian.preferred_factory(srid: 3785)
RGeo::Cartesian.preferred_factory(srid: 0)
end

def geographic_factory
Expand Down

0 comments on commit 93c96f7

Please sign in to comment.