Module: RGeo::ActiveRecord::SpatialToSql

Defined in:
lib/rgeo/active_record/arel_spatial_queries.rb

Overview

A set of common Arel visitor hacks for spatial ToSql visitors. Generally, a spatial ActiveRecord adapter should provide a custom ToSql Arel visitor that includes and customizes this module. See the existing spatial adapters (i.e. postgis, spatialite, mysqlspatial, and mysql2spatial) for usage examples.

Instance Method Summary collapse

Instance Method Details

#st_func(standard_name) ⇒ Object

Map a standard OGC SQL function name to the actual name used by a particular database. This method should take a name and return either the changed name or the original name.



15
16
17
# File 'lib/rgeo/active_record/arel_spatial_queries.rb', line 15

def st_func(standard_name)
  standard_name
end

#visit_in_spatial_context(node, collector) ⇒ Object

Generates SQL for a spatial node. The node must be a string (in which case it is treated as WKT), an RGeo feature, or a spatial attribute.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rgeo/active_record/arel_spatial_queries.rb', line 36

def visit_in_spatial_context(node, collector)
  case node
  when ::String
    collector << "#{st_func('ST_WKTToSQL')}(#{quote(node)})"
  when ::RGeo::Feature::Instance
    collector << visit_RGeo_Feature_Instance(node, *args)
  when ::RGeo::Cartesian::BoundingBox
    collector << visit_RGeo_Cartesian_BoundingBox(node, *args)
  else
    visit(node, collector)
  end
end

#visit_RGeo_ActiveRecord_SpatialNamedFunction(node, collector) ⇒ Object

Visit the SpatialNamedFunction node. This operates similarly to the standard NamedFunction node, but it performs function name mapping for the database, and it also uses the type information in the node to determine when to cast string arguments to WKT,



24
25
26
27
28
29
30
31
# File 'lib/rgeo/active_record/arel_spatial_queries.rb', line 24

def visit_RGeo_ActiveRecord_SpatialNamedFunction(node, collector)
  name = st_func(node.name)
  exprs = []
  node.expressions.each_with_index do |expr, index|
    exprs << (node.spatial_argument?(index) ? visit_in_spatial_context(expr, collector) : visit(expr, collector))
  end
  collector << "#{name}(#{node.distinct ? 'DISTINCT ' : ''}#{exprs.join(', ')})#{node.alias ? " AS #{visit(node.alias, *args)}" : ''}"
end