module Geocoder::Store::ActiveRecord::ClassMethods

def add_exclude_condition(conditions, exclude)


Expects conditions as an array or string. Returns array.
Adds a condition to exclude a given object by ID.
#
def add_exclude_condition(conditions, exclude)
  conditions = [conditions] if conditions.is_a?(String)
  if exclude
    conditions[0] << " AND #{full_column_name(primary_key)} != ?"
    conditions << exclude.id
  end
  conditions
end

def bearing_sql(latitude, longitude, options = {})


capabilities (trig functions?).
SQL for calculating bearing based on the current database's
#
def bearing_sql(latitude, longitude, options = {})
  if !options.include?(:bearing)
    options[:bearing] = Geocoder.config.distances
  end
  if options[:bearing]
    method_prefix = using_unextended_sqlite? ? "approx" : "full"
    Geocoder::Sql.send(
      method_prefix + "_bearing",
      latitude, longitude,
      full_column_name(options[:latitude] || geocoder_options[:latitude]),
      full_column_name(options[:longitude]|| geocoder_options[:longitude]),
      options
    )
  end
end

def distance_from_sql(location, *args)

def distance_from_sql(location, *args)
  latitude, longitude = Geocoder::Calculations.extract_coordinates(location)
  if Geocoder::Calculations.coordinates_present?(latitude, longitude)
    distance_sql(latitude, longitude, *args)
  end
end

def distance_sql(latitude, longitude, options = {})


capabilities (trig functions?).
SQL for calculating distance based on the current database's
#
def distance_sql(latitude, longitude, options = {})
  method_prefix = using_unextended_sqlite? ? "approx" : "full"
  Geocoder::Sql.send(
    method_prefix + "_distance",
    latitude, longitude,
    full_column_name(options[:latitude] || geocoder_options[:latitude]),
    full_column_name(options[:longitude]|| geocoder_options[:longitude]),
    options
  )
end

def false_condition


Value which can be passed to where() to produce no results.
#
def false_condition
  using_unextended_sqlite? ? 0 : "false"
end

def full_column_name(column)


Prepend table name if column name doesn't already contain one.
#
def full_column_name(column)
  column = column.to_s
  column.include?(".") ? column : [table_name, column].join(".")
end

def near_scope_options(latitude, longitude, radius = 20, options = {})


default is 0.0
ignored if database is sqlite.
* +:min_radius+ - the value to use as the minimum radius.
* +:bearing_column+ - used to set the column name of the calculated bearing.
* +:distance_column+ - used to set the column name of the calculated distance.
* +:exclude+ - an object to exclude (used by the +nearbys+ method)
set to false or nil to omit the ORDER BY clause
* +:order+ - column(s) for ORDER BY SQL clause; default is distance;
* +:select_bearing+ - like +:select_distance+ but for bearing.
SELECT SQL fragment (e.g. AS distance)
* +:select_distance+ - whether to include the distance alias in the
* +:select+ - string with the SELECT SQL fragment (e.g. “id, name”)
Geocoder.configure[:distances] to configure default calculation method.
set to false for no bearing calculation. Use
between the given point and each found nearby point;
the method to be used for calculating the bearing (direction)
* +:bearing+ - :linear or :spherical.
Use Geocoder.configure[:units] to configure default units.
is added to each found nearby object.
for interpreting radius as well as the +distance+ attribute which
* +:units+ - :mi or :km; to be used.

Options hash may include:
records within a radius (in kilometers) of the given point.
Get options hash suitable for passing to ActiveRecord.find to get
#
def near_scope_options(latitude, longitude, radius = 20, options = {})
  if options[:units]
    options[:units] = options[:units].to_sym
  end
  latitude_attribute = options[:latitude] || geocoder_options[:latitude]
  longitude_attribute = options[:longitude] || geocoder_options[:longitude]
  options[:units] ||= (geocoder_options[:units] || Geocoder.config.units)
  select_distance = options.fetch(:select_distance)  { true }
  options[:order] = "" if !select_distance && !options.include?(:order)
  select_bearing = options.fetch(:select_bearing) { true }
  bearing = bearing_sql(latitude, longitude, options)
  distance = distance_sql(latitude, longitude, options)
  distance_column = options.fetch(:distance_column) { 'distance' }
  bearing_column = options.fetch(:bearing_column)  { 'bearing' }
  # If radius is a DB column name, bounding box should include
  # all rows within the maximum radius appearing in that column.
  # Note: performance is dependent on variability of radii.
  bb_radius = radius.is_a?(Symbol) ? maximum(radius) : radius
  b = Geocoder::Calculations.bounding_box([latitude, longitude], bb_radius, options)
  args = b + [
    full_column_name(latitude_attribute),
    full_column_name(longitude_attribute)
  ]
  bounding_box_conditions = Geocoder::Sql.within_bounding_box(*args)
  if using_unextended_sqlite?
    conditions = bounding_box_conditions
  else
    min_radius = options.fetch(:min_radius, 0).to_f
    # if radius is a DB column name,
    # find rows between min_radius and value in column
    if radius.is_a?(Symbol)
      c = "BETWEEN ? AND #{radius}"
      a = [min_radius]
    else
      c = "BETWEEN ? AND ?"
      a = [min_radius, radius]
    end
    conditions = [bounding_box_conditions + " AND (#{distance}) " + c] + a
  end
  {
    :select => select_clause(options[:select],
                             select_distance ? distance : nil,
                             select_bearing ? bearing : nil,
                             distance_column,
                             bearing_column),
    :conditions => add_exclude_condition(conditions, options[:exclude]),
    :order => options.include?(:order) ? options[:order] : "#{distance_column} ASC"
  }
end

def null_value


Use OID type when running in PosgreSQL
#
def null_value
  using_postgres? ? 'NULL::text' : 'NULL'
end

def select_clause(columns, distance = nil, bearing = nil, distance_column = 'distance', bearing_column = 'bearing')


Generate the SELECT clause.
#
def select_clause(columns, distance = nil, bearing = nil, distance_column = 'distance', bearing_column = 'bearing')
  if columns == :id_only
    return full_column_name(primary_key)
  elsif columns == :geo_only
    clause = ""
  else
    clause = (columns || full_column_name("*"))
  end
  if distance
    clause += ", " unless clause.empty?
    clause += "#{distance} AS #{distance_column}"
  end
  if bearing
    clause += ", " unless clause.empty?
    clause += "#{bearing} AS #{bearing_column}"
  end
  clause
end

def using_postgres?

def using_postgres?
  connection.adapter_name.match(/postgres/i)
end

def using_sqlite?

def using_sqlite?
  !!connection.adapter_name.match(/sqlite/i)
end

def using_sqlite_with_extensions?

def using_sqlite_with_extensions?
  connection.adapter_name.match(/sqlite/i) &&
    defined?(::SqliteExt) &&
    %W(MOD POWER SQRT PI SIN COS ASIN ATAN2).all?{ |fn_name|
      connection.raw_connection.function_created?(fn_name)
    }
end

def using_unextended_sqlite?

def using_unextended_sqlite?
  using_sqlite? && !using_sqlite_with_extensions?
end