spatial_adapter 0.3.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -28,7 +28,7 @@ dependencies as well.
28
28
 
29
29
  gem install spatial_adapter
30
30
 
31
- In your Rails app, you can add a gem dependency in environment.rb:
31
+ In a Rails app, you can add a gem dependency in environment.rb:
32
32
 
33
33
  config.gem "spatial_adapter"
34
34
 
@@ -40,6 +40,25 @@ script/plugin install git://github.com/fragility/spatial_adapter.git
40
40
 
41
41
  You need to have Git installed first.
42
42
 
43
+ == Configuration
44
+
45
+ Choose the database type for which you would like to use spatial_adapter, and
46
+ load each with
47
+
48
+ require 'spatial_adapter/[database]'
49
+
50
+ where [database] should be replaced with one of the following:
51
+
52
+ - postgresql
53
+ - mysql
54
+
55
+ For example to use the PostgreSQL spatial adapter:
56
+
57
+ require 'spatial_adapter/postgresql'
58
+
59
+ In a Rails app, spatial_adapter will automatically load the adapter for the database
60
+ specified in your database.yml configuration.
61
+
43
62
  == Operations
44
63
 
45
64
  Geometric columns in your ActiveRecord models now appear just like any other
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 1.0.0
@@ -1,3 +1,4 @@
1
+ require 'spatial_adapter'
1
2
  require 'active_record/connection_adapters/mysql_adapter'
2
3
 
3
4
  ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
@@ -1,3 +1,4 @@
1
+ require 'spatial_adapter'
1
2
  require 'active_record/connection_adapters/postgresql_adapter'
2
3
 
3
4
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
@@ -145,28 +146,28 @@ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
145
146
  end
146
147
 
147
148
  # Returns the list of all indexes for a table.
149
+ #
148
150
  # This is a full replacement for the ActiveRecord method and as a result
149
- # has a higher probability of breaking in future releases
151
+ # has a higher probability of breaking in future releases.
150
152
  def indexes(table_name, name = nil)
151
- schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
152
- result = query(<<-SQL, name)
153
- SELECT distinct i.relname, d.indisunique, d.indkey, t.oid, am.amname
154
- FROM pg_class t, pg_class i, pg_index d, pg_attribute a, pg_am am
155
- WHERE i.relkind = 'i'
156
- AND d.indexrelid = i.oid
157
- AND d.indisprimary = 'f'
158
- AND t.oid = d.indrelid
159
- AND i.relam = am.oid
160
- AND t.relname = '#{table_name}'
161
- AND a.attrelid = t.oid
162
- AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum
163
- OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum
164
- OR d.indkey[4]=a.attnum OR d.indkey[5]=a.attnum
165
- OR d.indkey[6]=a.attnum OR d.indkey[7]=a.attnum
166
- OR d.indkey[8]=a.attnum OR d.indkey[9]=a.attnum )
167
- ORDER BY i.relname
153
+ schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
154
+
155
+ # Changed from upstread: link to pg_am to grab the index type (e.g. "gist")
156
+ result = query(<<-SQL, name)
157
+ SELECT distinct i.relname, d.indisunique, d.indkey, t.oid, am.amname
158
+ FROM pg_class t, pg_class i, pg_index d, pg_attribute a, pg_am am
159
+ WHERE i.relkind = 'i'
160
+ AND d.indexrelid = i.oid
161
+ AND d.indisprimary = 'f'
162
+ AND t.oid = d.indrelid
163
+ AND t.relname = '#{table_name}'
164
+ AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (#{schemas}) )
165
+ AND i.relam = am.oid
166
+ AND a.attrelid = t.oid
167
+ ORDER BY i.relname
168
168
  SQL
169
169
 
170
+
170
171
  indexes = []
171
172
 
172
173
  indexes = result.map do |row|
@@ -174,16 +175,21 @@ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
174
175
  unique = row[1] == 't'
175
176
  indkey = row[2].split(" ")
176
177
  oid = row[3]
177
- spatial = row[4] == "gist"
178
+ indtype = row[4]
178
179
 
179
- columns = query(<<-SQL, "Columns for index #{row[0]} on #{table_name}").inject({}) {|attlist, r| attlist[r[1]] = r[0]; attlist}
180
- SELECT a.attname, a.attnum
181
- FROM pg_attribute a
180
+ # Changed from upstream: need to get the column types to test for spatial indexes
181
+ columns = query(<<-SQL, "Columns for index #{row[0]} on #{table_name}").inject({}) {|attlist, r| attlist[r[1]] = [r[0], r[2]]; attlist}
182
+ SELECT a.attname, a.attnum, t.typname
183
+ FROM pg_attribute a, pg_type t
182
184
  WHERE a.attrelid = #{oid}
183
185
  AND a.attnum IN (#{indkey.join(",")})
186
+ AND a.atttypid = t.oid
184
187
  SQL
185
188
 
186
- column_names = indkey.map {|attnum| columns[attnum] }
189
+ # Only GiST indexes on spatial columns denote a spatial index
190
+ spatial = indtype == 'gist' && columns.size == 1 && (columns.values.first[1] == 'geometry' || columns.values.first[1] == 'geography')
191
+
192
+ column_names = indkey.map {|attnum| columns[attnum] ? columns[attnum][0] : nil }
187
193
  ActiveRecord::ConnectionAdapters::IndexDefinition.new(table_name, index_name, unique, column_names, spatial)
188
194
  end
189
195
 
@@ -1,10 +1,21 @@
1
+ # This file should typically not be directly require'd into your project. You
2
+ # should require the database-specific adapter you desire, e.g.
3
+ #
4
+ # require 'spatial_adapter/postgresql'
5
+ #
6
+ # Why is this file here?
7
+ #
8
+ # Mostly to keep Rails happy when using config.gem to specify dependencies.
9
+ # The Rails init code (rails/init.rb) will then load the adapter that matches
10
+ # your database.yml configuration.
11
+
1
12
  require 'geo_ruby'
2
13
  require 'active_record'
3
14
 
4
15
  include GeoRuby::SimpleFeatures
5
16
 
6
17
  module SpatialAdapter
7
- #Translation of geometric data types
18
+ # Translation of geometric data types
8
19
  def geometry_data_types
9
20
  {
10
21
  :point => { :name => "POINT" },
@@ -19,10 +30,8 @@ module SpatialAdapter
19
30
  end
20
31
  end
21
32
 
22
- require 'spatial_adapter/raw_geom_info'
23
- require 'spatial_adapter/spatial_column'
24
- require 'spatial_adapter/schema_definitions'
25
- require 'spatial_adapter/schema_dumper'
26
- require 'spatial_adapter/table_definition'
27
- require 'spatial_adapter/adapters/postgis'
28
- require 'spatial_adapter/adapters/mysql'
33
+ require 'spatial_adapter/common/raw_geom_info'
34
+ require 'spatial_adapter/common/spatial_column'
35
+ require 'spatial_adapter/common/schema_definitions'
36
+ require 'spatial_adapter/common/schema_dumper'
37
+ require 'spatial_adapter/common/table_definition'
data/rails/init.rb CHANGED
@@ -1 +1,16 @@
1
- require 'spatial_adapter'
1
+ # Rails initialization
2
+ #
3
+ # This will load the adapter for the currently used database configuration, if
4
+ # it exists.
5
+
6
+ module SpatialAdapter
7
+ class NotCompatibleError < ::StandardError
8
+ end
9
+ end
10
+
11
+ begin
12
+ adapter = ActiveRecord::Base.configurations[RAILS_ENV]['adapter']
13
+ require "spatial_adapter/#{adapter}"
14
+ rescue LoadError
15
+ raise SpatialAdapter::NotCompatibleError.new("spatial_adapter does not currently support the #{adapter} database.")
16
+ end
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'spatial_adapter/mysql'
2
3
  require 'db/mysql_raw'
3
4
  require 'models/common'
4
5
 
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'spatial_adapter/mysql'
2
3
 
3
4
  class MigratedGeometryModel < ActiveRecord::Base
4
5
  end
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'spatial_adapter/mysql'
2
3
  require 'db/mysql_raw'
3
4
  require 'models/common'
4
5
 
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'spatial_adapter/mysql'
2
3
 
3
4
  describe "Spatially-enabled Schema Dumps" do
4
5
  before :all do
@@ -1,4 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ require 'spatial_adapter/postgresql'
2
4
  require 'db/postgis_raw'
3
5
  require 'models/common'
4
6
 
@@ -165,7 +167,7 @@ describe "Modified PostgreSQLAdapter" do
165
167
 
166
168
  describe "#indexes" do
167
169
  before :each do
168
- @indexes = @connection.indexes('point_models', 'index_point_models_on_geom')
170
+ @indexes = @connection.indexes('point_models')
169
171
  end
170
172
 
171
173
  it "should return an IndexDefinition for each index on the table" do
@@ -180,13 +182,33 @@ describe "Modified PostgreSQLAdapter" do
180
182
  @indexes.select{|i| i.name == 'index_point_models_on_extra'}.first.columns.should == ['extra', 'more_extra']
181
183
  end
182
184
 
183
- it "should be marked as spatial if a GIST index" do
185
+ it "should be marked as spatial if a GiST index on a geometry column" do
184
186
  @indexes.select{|i| i.name == 'index_point_models_on_geom'}.first.spatial.should == true
185
187
  end
186
188
 
187
- it "should not be marked as spatial if not a GIST index" do
189
+ it "should be marked as spatial if a GiST index on a geography column" do
190
+ @indexes = @connection.indexes('geography_point_models')
191
+ @indexes.select{|i| i.name == 'index_geography_point_models_on_geom'}.first.spatial.should == true
192
+ end
193
+
194
+ it "should not be marked as spatial if not a GiST index" do
188
195
  @indexes.select{|i| i.name == 'index_point_models_on_extra'}.first.spatial.should == false
189
196
  end
197
+
198
+ it "should not be marked as spatial if a GiST index on a non-geometry column" do
199
+ @connection.execute(<<-SQL)
200
+ create table non_spatial_models
201
+ (
202
+ id serial primary key,
203
+ location point,
204
+ extra varchar(255)
205
+ );
206
+ create index index_non_spatial_models_on_location on non_spatial_models using gist (box(location, location));
207
+ SQL
208
+ @indexes = @connection.indexes('non_spatial_models')
209
+ @indexes.select{|i| i.name == 'index_non_spatial_models_on_location'}.first.spatial.should == false
210
+ @connection.execute 'drop table non_spatial_models'
211
+ end
190
212
  end
191
213
 
192
214
  describe "#add_index" do
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'spatial_adapter/postgresql'
2
3
 
3
4
  class MigratedGeometryModel < ActiveRecord::Base
4
5
  end
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'spatial_adapter/postgresql'
2
3
  require 'db/postgis_raw'
3
4
  require 'models/common'
4
5
 
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'spatial_adapter/postgresql'
2
3
 
3
4
  describe "Spatially-enabled Schema Dumps" do
4
5
  before :all do
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,10 @@
1
1
  require 'rubygems'
2
2
  require 'spec'
3
+ require 'geo_ruby'
3
4
  gem 'activerecord', '=2.3.5'
5
+ require 'active_record'
4
6
 
5
7
  $:.unshift((File.join(File.dirname(__FILE__), '..', 'lib')))
6
- require 'spatial_adapter'
7
8
 
8
9
  include GeoRuby::SimpleFeatures
9
10
 
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spatial_adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Pete Deffendol
@@ -9,29 +14,37 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-22 00:00:00 -07:00
17
+ date: 2010-04-19 00:00:00 -06:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: activerecord
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 2
30
+ - 2
23
31
  version: 2.2.2
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: GeoRuby
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 3
44
+ - 0
33
45
  version: 1.3.0
34
- version:
46
+ type: :runtime
47
+ version_requirements: *id002
35
48
  description: Provides enhancements to ActiveRecord to handle spatial datatypes in PostgreSQL and MySQL.
36
49
  email: pete@fragility.us
37
50
  executables: []
@@ -45,13 +58,13 @@ files:
45
58
  - README.rdoc
46
59
  - VERSION
47
60
  - lib/spatial_adapter.rb
48
- - lib/spatial_adapter/adapters/mysql.rb
49
- - lib/spatial_adapter/adapters/postgis.rb
50
- - lib/spatial_adapter/raw_geom_info.rb
51
- - lib/spatial_adapter/schema_definitions.rb
52
- - lib/spatial_adapter/schema_dumper.rb
53
- - lib/spatial_adapter/spatial_column.rb
54
- - lib/spatial_adapter/table_definition.rb
61
+ - lib/spatial_adapter/common/raw_geom_info.rb
62
+ - lib/spatial_adapter/common/schema_definitions.rb
63
+ - lib/spatial_adapter/common/schema_dumper.rb
64
+ - lib/spatial_adapter/common/spatial_column.rb
65
+ - lib/spatial_adapter/common/table_definition.rb
66
+ - lib/spatial_adapter/mysql.rb
67
+ - lib/spatial_adapter/postgresql.rb
55
68
  - rails/init.rb
56
69
  has_rdoc: true
57
70
  homepage: http://github.com/fragility/spatial_adapter
@@ -66,18 +79,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
79
  requirements:
67
80
  - - ">="
68
81
  - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
69
84
  version: "0"
70
- version:
71
85
  required_rubygems_version: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="
74
88
  - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
75
91
  version: "0"
76
- version:
77
92
  requirements: []
78
93
 
79
94
  rubyforge_project:
80
- rubygems_version: 1.3.5
95
+ rubygems_version: 1.3.6
81
96
  signing_key:
82
97
  specification_version: 3
83
98
  summary: Spatial Adapter for ActiveRecord
@@ -89,9 +104,9 @@ test_files:
89
104
  - spec/mysql/migration_spec.rb
90
105
  - spec/mysql/models_spec.rb
91
106
  - spec/mysql/schema_dumper_spec.rb
92
- - spec/postgis/connection_adapter_spec.rb
93
- - spec/postgis/migration_spec.rb
94
- - spec/postgis/models_spec.rb
95
- - spec/postgis/schema_dumper_spec.rb
107
+ - spec/postgresql/connection_adapter_spec.rb
108
+ - spec/postgresql/migration_spec.rb
109
+ - spec/postgresql/models_spec.rb
110
+ - spec/postgresql/schema_dumper_spec.rb
96
111
  - spec/spec_helper.rb
97
112
  - spec/README.txt