Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/spatialite_adapter.rb

Overview

ActiveRecord looks for the spatialite_connection factory method in this class.

Class Method Summary collapse

Class Method Details

.spatialite_connection(config_) ⇒ Object

Create a spatialite connection adapter.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_record/connection_adapters/spatialite_adapter.rb', line 55

def self.spatialite_connection(config_)
  unless 'spatialite' == config_[:adapter]
    raise ::ArgumentError, 'adapter name should be "spatialite"'
  end
  unless config_[:database]
    raise ::ArgumentError, "No database file specified. Missing argument: database"
  end

  # Allow database path relative to Rails.root, but only if
  # the database path is not the special path that tells
  # Sqlite to build a database only in memory.
  if defined?(::Rails.root) && ':memory:' != config_[:database]
    config_[:database] = ::File.expand_path(config_[:database], ::Rails.root)
  end

  db_ = ::SQLite3::Database.new(config_[:database], :results_as_hash => true)
  db_.busy_timeout(config_[:timeout]) unless config_[:timeout].nil?

  # Load SpatiaLite
  path_ = config_[:libspatialite]
  if path_ && (!::File.file?(path_) || !::File.readable?(path_))
    raise "Cannot read libspatialite library at #{path_}"
  end
  unless path_
    prefixes_ = ['/usr/local/spatialite', '/usr/local/libspatialite', '/usr/local', '/opt/local', '/sw/local', '/usr']
    suffixes_ = ['so', 'dylib'].join(',')
    prefixes_.each do |prefix_|
      pa_ = ::Dir.glob("#{prefix_}/lib/libspatialite.{#{suffixes_}}")
      if pa_.size > 0
        path_ = pa_.first
        break
      end
    end
  end
  unless path_
    raise 'Cannot find libspatialite in the usual places. Please provide the path in the "libspatialite" config parameter.'
  end
  if db_.respond_to?(:enable_load_extension)
    db_.enable_load_extension(1)
    db_.load_extension(path_)
  end

  ::ActiveRecord::ConnectionAdapters::SpatiaLiteAdapter::MainAdapter.new(db_, logger, config_)
end