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_, *args) ⇒ 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_, *args)
  case node_
  when ::String
    "#{st_func('ST_WKTToSQL')}(#{visit_String(node_, *args)})"
  when ::RGeo::Feature::Instance
    visit_RGeo_Feature_Instance(node_, *args)
  when ::RGeo::Cartesian::BoundingBox
    visit_RGeo_Cartesian_BoundingBox(node_, *args)
  else
    visit(node_, *args)
  end
end

#visit_RGeo_ActiveRecord_SpatialNamedFunction(node_, *args) ⇒ 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_, *args)
  name_ = st_func(node_.name)
  exprs_ = []
  node_.expressions.each_with_index do |expr_, index_|
    exprs_ << (node_.spatial_argument?(index_) ? visit_in_spatial_context(expr_, *args) : visit(expr_, *args))
  end
  "#{name_}(#{node_.distinct ? 'DISTINCT ' : ''}#{exprs_.join(', ')})#{node_.alias ? " AS #{visit(node_.alias, *args)}" : ''}"
end