activerecord-postgis-adapter 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. data/Documentation.rdoc +322 -0
  2. data/History.rdoc +5 -0
  3. data/README.rdoc +42 -290
  4. data/Version +1 -1
  5. data/lib/active_record/connection_adapters/postgis_adapter.rb +35 -21
  6. data/lib/active_record/connection_adapters/postgis_adapter/rails3/create_connection.rb +96 -0
  7. data/lib/active_record/connection_adapters/postgis_adapter/{databases.rake → rails3/databases.rake} +7 -1
  8. data/lib/active_record/connection_adapters/postgis_adapter/{main_adapter.rb → rails3/main_adapter.rb} +9 -9
  9. data/lib/active_record/connection_adapters/postgis_adapter/{spatial_column.rb → rails3/spatial_column.rb} +4 -8
  10. data/lib/active_record/connection_adapters/postgis_adapter/{spatial_table_definition.rb → rails3/spatial_table_definition.rb} +5 -9
  11. data/lib/active_record/connection_adapters/postgis_adapter/rails4/create_connection.rb +88 -0
  12. data/lib/active_record/connection_adapters/postgis_adapter/rails4/databases.rake +52 -0
  13. data/lib/active_record/connection_adapters/postgis_adapter/rails4/main_adapter.rb +310 -0
  14. data/lib/active_record/connection_adapters/postgis_adapter/rails4/postgis_database_tasks.rb +232 -0
  15. data/lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb +220 -0
  16. data/lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_table_definition.rb +140 -0
  17. data/lib/active_record/connection_adapters/postgis_adapter/railtie.rb +3 -28
  18. data/lib/active_record/connection_adapters/postgis_adapter/{arel_tosql.rb → shared/arel_tosql.rb} +3 -7
  19. data/lib/active_record/connection_adapters/postgis_adapter/shared/jdbc_compat.rb +133 -0
  20. data/lib/active_record/connection_adapters/postgis_adapter/shared/railtie.rb +66 -0
  21. data/lib/active_record/connection_adapters/postgis_adapter/shared/setup.rb +57 -0
  22. data/lib/active_record/connection_adapters/postgis_adapter/{version.rb → shared/version.rb} +1 -1
  23. data/lib/activerecord-postgis-adapter.rb +37 -0
  24. data/lib/rgeo/active_record/postgis_adapter/railtie.rb +1 -1
  25. data/test/tc_basic.rb +43 -16
  26. data/test/tc_ddl.rb +2 -2
  27. data/test/tc_nested_class.rb +2 -2
  28. data/test/tc_spatial_queries.rb +14 -9
  29. data/test/tc_tasks.rb +110 -0
  30. metadata +27 -14
  31. data/lib/active_record/connection_adapters/postgis_adapter/jdbc_connection.rb +0 -78
  32. data/lib/active_record/connection_adapters/postgis_adapter/pg_connection.rb +0 -27
data/Version CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.6.0
@@ -34,25 +34,16 @@
34
34
  ;
35
35
 
36
36
 
37
- require 'rgeo/active_record'
38
- require 'active_record/connection_adapters/postgresql_adapter'
39
-
40
- if defined?(::RUBY_ENGINE) && ::RUBY_ENGINE == 'jruby'
41
- require 'active_record/connection_adapters/postgis_adapter/jdbc_connection'
42
- else
43
- require 'active_record/connection_adapters/postgis_adapter/pg_connection'
44
- end
45
-
46
-
47
37
  # The activerecord-postgis-adapter gem installs the *postgis*
48
38
  # connection adapter into ActiveRecord.
49
39
 
50
40
  module ActiveRecord
51
41
 
52
42
  # All ActiveRecord adapters go in this namespace.
43
+ # This adapter is installed into the PostGISAdapter submodule.
53
44
  module ConnectionAdapters
54
45
 
55
- # The PostGIS Adapter
46
+ # The PostGIS Adapter lives in this namespace.
56
47
  module PostGISAdapter
57
48
 
58
49
  # The name returned by the adapter_name method of this adapter.
@@ -66,15 +57,38 @@ module ActiveRecord
66
57
  end
67
58
 
68
59
 
69
- require 'active_record/connection_adapters/postgis_adapter/version.rb'
70
- require 'active_record/connection_adapters/postgis_adapter/main_adapter.rb'
71
- require 'active_record/connection_adapters/postgis_adapter/spatial_table_definition.rb'
72
- require 'active_record/connection_adapters/postgis_adapter/spatial_column.rb'
73
- require 'active_record/connection_adapters/postgis_adapter/arel_tosql.rb'
60
+ # :stopdoc:
74
61
 
62
+ require 'active_record'
63
+ require 'active_record/connection_adapters/postgresql_adapter'
64
+ require 'rgeo/active_record'
65
+
66
+ case ::ActiveRecord::VERSION::MAJOR
67
+ when 3
68
+ require 'active_record/connection_adapters/postgis_adapter/shared/version.rb'
69
+ require 'active_record/connection_adapters/postgis_adapter/rails3/main_adapter.rb'
70
+ require 'active_record/connection_adapters/postgis_adapter/rails3/spatial_table_definition.rb'
71
+ require 'active_record/connection_adapters/postgis_adapter/rails3/spatial_column.rb'
72
+ require 'active_record/connection_adapters/postgis_adapter/shared/arel_tosql.rb'
73
+ require 'active_record/connection_adapters/postgis_adapter/shared/setup.rb'
74
+ require 'active_record/connection_adapters/postgis_adapter/rails3/create_connection'
75
+ when 4
76
+ require 'active_record/connection_adapters/postgis_adapter/shared/version.rb'
77
+ require 'active_record/connection_adapters/postgis_adapter/rails4/main_adapter.rb'
78
+ require 'active_record/connection_adapters/postgis_adapter/rails4/spatial_table_definition.rb'
79
+ require 'active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb'
80
+ require 'active_record/connection_adapters/postgis_adapter/shared/arel_tosql.rb'
81
+ require 'active_record/connection_adapters/postgis_adapter/shared/setup.rb'
82
+ require 'active_record/connection_adapters/postgis_adapter/rails4/create_connection'
83
+ require 'active_record/connection_adapters/postgis_adapter/rails4/postgis_database_tasks.rb'
84
+ else
85
+ raise "Unsupported ActiveRecord version #{::ActiveRecord::VERSION::STRING}"
86
+ end
87
+
88
+ ::ActiveRecord::ConnectionAdapters::PostGISAdapter.initial_setup
89
+
90
+ if defined?(::Rails::Railtie)
91
+ load 'active_record/connection_adapters/postgis_adapter/shared/railtie'
92
+ end
75
93
 
76
- ignore_tables_ = ::ActiveRecord::SchemaDumper.ignore_tables
77
- ignore_tables_ << 'geometry_columns' unless ignore_tables_.include?('geometry_columns')
78
- ignore_tables_ << 'spatial_ref_sys' unless ignore_tables_.include?('spatial_ref_sys')
79
- ignore_tables_ << 'layer' unless ignore_tables_.include?('layer')
80
- ignore_tables_ << 'topology' unless ignore_tables_.include?('topology')
94
+ # :startdoc:
@@ -0,0 +1,96 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # PostGIS adapter for ActiveRecord
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010-2012 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+ ;
35
+
36
+
37
+ module ActiveRecord # :nodoc:
38
+
39
+ class Base # :nodoc:
40
+
41
+ class << self
42
+
43
+
44
+ if defined?(::RUBY_ENGINE) && ::RUBY_ENGINE == 'jruby'
45
+
46
+
47
+ require 'active_record/connection_adapters/jdbcpostgresql_adapter'
48
+ require 'active_record/connection_adapters/postgis_adapter/shared/jdbc_compat'
49
+
50
+
51
+ def postgis_connection(config_)
52
+ ::ActiveRecord::ConnectionAdapters::PostGISAdapter.create_jdbc_connection(self, config_)
53
+ end
54
+
55
+ alias_method :jdbcpostgis_connection, :postgis_connection
56
+
57
+
58
+ else
59
+
60
+
61
+ require 'pg'
62
+
63
+
64
+ # ActiveRecord looks for the postgis_connection factory method in
65
+ # this class.
66
+ #
67
+ # Based on the default <tt>postgresql_connection</tt> definition from
68
+ # ActiveRecord.
69
+
70
+ def postgis_connection(config_)
71
+ config_ = config_.symbolize_keys
72
+ host_ = config_[:host]
73
+ port_ = config_[:port] || 5432
74
+ username_ = config_[:username].to_s if config_[:username]
75
+ password_ = config_[:password].to_s if config_[:password]
76
+
77
+ if config_.key?(:database)
78
+ database_ = config_[:database]
79
+ else
80
+ raise ::ArgumentError, "No database specified. Missing argument: database."
81
+ end
82
+
83
+ # The postgres drivers don't allow the creation of an unconnected PGconn object,
84
+ # so just pass a nil connection object for the time being.
85
+ ::ActiveRecord::ConnectionAdapters::PostGISAdapter::MainAdapter.new(nil, logger, [host_, port_, nil, nil, database_, username_, password_], config_)
86
+ end
87
+
88
+
89
+ end
90
+
91
+
92
+ end
93
+
94
+ end
95
+
96
+ end
@@ -1,6 +1,6 @@
1
1
  # -----------------------------------------------------------------------------
2
2
  #
3
- # Rakefile changes for PostGIS adapter
3
+ # PostGIS adapter for ActiveRecord
4
4
  #
5
5
  # -----------------------------------------------------------------------------
6
6
  # Copyright 2010-2012 Daniel Azuma
@@ -34,6 +34,9 @@
34
34
  ;
35
35
 
36
36
 
37
+ # :stopdoc:
38
+
39
+
37
40
  require 'rgeo/active_record/task_hacker'
38
41
 
39
42
 
@@ -214,3 +217,6 @@ end
214
217
  drop_database(config_)
215
218
  create_database(config_)
216
219
  end
220
+
221
+
222
+ # :startdoc:
@@ -34,16 +34,14 @@
34
34
  ;
35
35
 
36
36
 
37
- # :stopdoc:
37
+ module ActiveRecord # :nodoc:
38
38
 
39
- module ActiveRecord
39
+ module ConnectionAdapters # :nodoc:
40
40
 
41
- module ConnectionAdapters
41
+ module PostGISAdapter # :nodoc:
42
42
 
43
- module PostGISAdapter
44
43
 
45
-
46
- class MainAdapter < PostgreSQLAdapter
44
+ class MainAdapter < PostgreSQLAdapter # :nodoc:
47
45
 
48
46
 
49
47
  SPATIAL_COLUMN_CONSTRUCTORS = ::RGeo::ActiveRecord::DEFAULT_SPATIAL_COLUMN_CONSTRUCTORS.merge(
@@ -179,7 +177,11 @@ module ActiveRecord
179
177
 
180
178
  spatial_ = indtype_ == 'gist' && columns_.size == 1 && (columns_.values.first[1] == 'geometry' || columns_.values.first[1] == 'geography')
181
179
  column_names_ = columns_.values_at(*indkey_).compact.map{ |a_| a_[0] }
182
- column_names_.empty? ? nil : ::RGeo::ActiveRecord::SpatialIndexDefinition.new(table_name_, index_name_, unique_, column_names_, nil, spatial_)
180
+ if column_names_.empty?
181
+ nil
182
+ else
183
+ ::RGeo::ActiveRecord::SpatialIndexDefinition.new(table_name_, index_name_, unique_, column_names_, nil, nil, nil, spatial_ ? true : false)
184
+ end
183
185
  end.compact
184
186
  end
185
187
 
@@ -333,5 +335,3 @@ module ActiveRecord
333
335
  end
334
336
 
335
337
  end
336
-
337
- # :startdoc:
@@ -34,16 +34,14 @@
34
34
  ;
35
35
 
36
36
 
37
- # :stopdoc:
37
+ module ActiveRecord # :nodoc:
38
38
 
39
- module ActiveRecord
39
+ module ConnectionAdapters # :nodoc:
40
40
 
41
- module ConnectionAdapters
41
+ module PostGISAdapter # :nodoc:
42
42
 
43
- module PostGISAdapter
44
43
 
45
-
46
- class SpatialColumn < ConnectionAdapters::PostgreSQLColumn
44
+ class SpatialColumn < ConnectionAdapters::PostgreSQLColumn # :nodoc:
47
45
 
48
46
 
49
47
  FACTORY_SETTINGS_CACHE = {}
@@ -195,5 +193,3 @@ module ActiveRecord
195
193
  end
196
194
 
197
195
  end
198
-
199
- # :startdoc:
@@ -34,13 +34,11 @@
34
34
  ;
35
35
 
36
36
 
37
- # :stopdoc:
37
+ module ActiveRecord # :nodoc:
38
38
 
39
- module ActiveRecord
39
+ module ConnectionAdapters # :nodoc:
40
40
 
41
- module ConnectionAdapters
42
-
43
- module PostGISAdapter
41
+ module PostGISAdapter # :nodoc:
44
42
 
45
43
  TableDefinitionSuperclass = if defined?(ConnectionAdapters::PostgreSQLAdapter::TableDefinition)
46
44
  ConnectionAdapters::PostgreSQLAdapter::TableDefinition
@@ -48,7 +46,7 @@ module ActiveRecord
48
46
  ConnectionAdapters::TableDefinition
49
47
  end
50
48
 
51
- class SpatialTableDefinition < TableDefinitionSuperclass
49
+ class SpatialTableDefinition < TableDefinitionSuperclass # :nodoc:
52
50
 
53
51
  def column(name_, type_, options_={})
54
52
  if (info_ = @base.spatial_column_constructor(type_.to_sym))
@@ -88,7 +86,7 @@ module ActiveRecord
88
86
  end
89
87
 
90
88
 
91
- module SpatialColumnDefinitionMethods
89
+ module SpatialColumnDefinitionMethods # :nodoc:
92
90
 
93
91
  def spatial_type
94
92
  @spatial_type
@@ -145,5 +143,3 @@ module ActiveRecord
145
143
  end
146
144
 
147
145
  end
148
-
149
- # :startdoc:
@@ -0,0 +1,88 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # PostGIS adapter for ActiveRecord
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010-2012 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+ ;
35
+
36
+
37
+ module ActiveRecord # :nodoc:
38
+
39
+ module ConnectionHandling # :nodoc:
40
+
41
+
42
+ if defined?(::RUBY_ENGINE) && ::RUBY_ENGINE == 'jruby'
43
+
44
+ require 'active_record/connection_adapters/jdbcpostgresql_adapter'
45
+ require 'active_record/connection_adapters/postgis_adapter/shared/jdbc_compat'
46
+
47
+
48
+ def postgis_connection(config_)
49
+ ::ActiveRecord::ConnectionAdapters::PostGISAdapter.create_jdbc_connection(self, config_)
50
+ end
51
+
52
+ alias_method :jdbcpostgis_connection, :postgis_connection
53
+
54
+
55
+ else
56
+
57
+
58
+ require 'pg'
59
+
60
+
61
+ # Based on the default <tt>postgresql_connection</tt> definition from
62
+ # ActiveRecord.
63
+
64
+ def postgis_connection(config_)
65
+ # FULL REPLACEMENT because we need to create a different class.
66
+ conn_params_ = config_.symbolize_keys
67
+
68
+ conn_params_.delete_if { |_, v_| v_.nil? }
69
+
70
+ # Map ActiveRecords param names to PGs.
71
+ conn_params_[:user] = conn_params_.delete(:username) if conn_params_[:username]
72
+ conn_params_[:dbname] = conn_params_.delete(:database) if conn_params_[:database]
73
+
74
+ # Forward only valid config params to PGconn.connect.
75
+ conn_params_.keep_if { |k_, _| VALID_CONN_PARAMS.include?(k_) }
76
+
77
+ # The postgres drivers don't allow the creation of an unconnected PGconn object,
78
+ # so just pass a nil connection object for the time being.
79
+ ::ActiveRecord::ConnectionAdapters::PostGISAdapter::MainAdapter.new(nil, logger, conn_params_, config_)
80
+ end
81
+
82
+
83
+ end
84
+
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,52 @@
1
+ # -----------------------------------------------------------------------------
2
+ #
3
+ # PostGIS adapter for ActiveRecord
4
+ #
5
+ # -----------------------------------------------------------------------------
6
+ # Copyright 2010-2012 Daniel Azuma
7
+ #
8
+ # All rights reserved.
9
+ #
10
+ # Redistribution and use in source and binary forms, with or without
11
+ # modification, are permitted provided that the following conditions are met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright notice,
14
+ # this list of conditions and the following disclaimer.
15
+ # * Redistributions in binary form must reproduce the above copyright notice,
16
+ # this list of conditions and the following disclaimer in the documentation
17
+ # and/or other materials provided with the distribution.
18
+ # * Neither the name of the copyright holder, nor the names of any other
19
+ # contributors to this software, may be used to endorse or promote products
20
+ # derived from this software without specific prior written permission.
21
+ #
22
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+ # -----------------------------------------------------------------------------
34
+ ;
35
+
36
+
37
+ # :stopdoc:
38
+
39
+
40
+ namespace :db do
41
+ namespace :gis do
42
+ desc "Setup PostGIS data in the database"
43
+ task :setup => [:load_config, :rails_env] do
44
+ configs_for_environment.each do |config_|
45
+ ::ActiveRecord::ConnectionAdapters::PostGISAdapter::PostGISDatabaseTasks.new(config_).setup_gis
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+
52
+ # :startdoc: