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.


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rgeo/active_record/arel_spatial_queries.rb', line 42

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, collector)
  when ::RGeo::Cartesian::BoundingBox
    collector << visit_RGeo_Cartesian_BoundingBox(node, collector)
  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
32
33
34
35
36
37
# 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
  collector << "("
  collector << "DISTINCT " if node.distinct
  collector << exprs.join(', ')
  collector << ")"
  collector << " AS #{ visit(node.alias, collector) }" if node.alias
  collector
end