activerecord-postgis-adapter 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Documentation.rdoc +322 -0
- data/History.rdoc +5 -0
- data/README.rdoc +42 -290
- data/Version +1 -1
- data/lib/active_record/connection_adapters/postgis_adapter.rb +35 -21
- data/lib/active_record/connection_adapters/postgis_adapter/rails3/create_connection.rb +96 -0
- data/lib/active_record/connection_adapters/postgis_adapter/{databases.rake → rails3/databases.rake} +7 -1
- data/lib/active_record/connection_adapters/postgis_adapter/{main_adapter.rb → rails3/main_adapter.rb} +9 -9
- data/lib/active_record/connection_adapters/postgis_adapter/{spatial_column.rb → rails3/spatial_column.rb} +4 -8
- data/lib/active_record/connection_adapters/postgis_adapter/{spatial_table_definition.rb → rails3/spatial_table_definition.rb} +5 -9
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/create_connection.rb +88 -0
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/databases.rake +52 -0
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/main_adapter.rb +310 -0
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/postgis_database_tasks.rb +232 -0
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_column.rb +220 -0
- data/lib/active_record/connection_adapters/postgis_adapter/rails4/spatial_table_definition.rb +140 -0
- data/lib/active_record/connection_adapters/postgis_adapter/railtie.rb +3 -28
- data/lib/active_record/connection_adapters/postgis_adapter/{arel_tosql.rb → shared/arel_tosql.rb} +3 -7
- data/lib/active_record/connection_adapters/postgis_adapter/shared/jdbc_compat.rb +133 -0
- data/lib/active_record/connection_adapters/postgis_adapter/shared/railtie.rb +66 -0
- data/lib/active_record/connection_adapters/postgis_adapter/shared/setup.rb +57 -0
- data/lib/active_record/connection_adapters/postgis_adapter/{version.rb → shared/version.rb} +1 -1
- data/lib/activerecord-postgis-adapter.rb +37 -0
- data/lib/rgeo/active_record/postgis_adapter/railtie.rb +1 -1
- data/test/tc_basic.rb +43 -16
- data/test/tc_ddl.rb +2 -2
- data/test/tc_nested_class.rb +2 -2
- data/test/tc_spatial_queries.rb +14 -9
- data/test/tc_tasks.rb +110 -0
- metadata +27 -14
- data/lib/active_record/connection_adapters/postgis_adapter/jdbc_connection.rb +0 -78
- data/lib/active_record/connection_adapters/postgis_adapter/pg_connection.rb +0 -27
@@ -0,0 +1,220 @@
|
|
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 ConnectionAdapters # :nodoc:
|
40
|
+
|
41
|
+
module PostGISAdapter # :nodoc:
|
42
|
+
|
43
|
+
|
44
|
+
class SpatialColumn < ConnectionAdapters::PostgreSQLColumn # :nodoc:
|
45
|
+
|
46
|
+
|
47
|
+
def initialize(factory_settings_, table_name_, name_, default_, oid_type_, sql_type_=nil, null_=true, opts_=nil)
|
48
|
+
@factory_settings = factory_settings_
|
49
|
+
@table_name = table_name_
|
50
|
+
@geographic = sql_type_ =~ /geography/i ? true : false
|
51
|
+
if opts_
|
52
|
+
# This case comes from an entry in the geometry_columns table
|
53
|
+
@geometric_type = ::RGeo::ActiveRecord.geometric_type_from_name(opts_[:type]) ||
|
54
|
+
::RGeo::Feature::Geometry
|
55
|
+
@srid = opts_[:srid].to_i
|
56
|
+
@has_z = opts_[:has_z] ? true : false
|
57
|
+
@has_m = opts_[:has_m] ? true : false
|
58
|
+
elsif @geographic
|
59
|
+
# Geographic type information is embedded in the SQL type
|
60
|
+
@geometric_type = ::RGeo::Feature::Geometry
|
61
|
+
@srid = 4326
|
62
|
+
@has_z = @has_m = false
|
63
|
+
if sql_type_ =~ /geography\((.*)\)$/i
|
64
|
+
params_ = $1.split(',')
|
65
|
+
if params_.size >= 2
|
66
|
+
if params_.first =~ /([a-z]+[^zm])(z?)(m?)/i
|
67
|
+
@has_z = $2.length > 0
|
68
|
+
@has_m = $3.length > 0
|
69
|
+
@geometric_type = ::RGeo::ActiveRecord.geometric_type_from_name($1)
|
70
|
+
end
|
71
|
+
if params_.last =~ /(\d+)/
|
72
|
+
@srid = $1.to_i
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
elsif sql_type_ =~ /geography|geometry|point|linestring|polygon/i
|
77
|
+
# Just in case there is a geometry column with no geometry_columns entry.
|
78
|
+
@geometric_type = ::RGeo::Feature::Geometry
|
79
|
+
@srid = @has_z = @has_m = nil
|
80
|
+
else
|
81
|
+
# Non-spatial column
|
82
|
+
@geometric_type = @has_z = @has_m = @srid = nil
|
83
|
+
end
|
84
|
+
super(name_, default_, oid_type_, sql_type_, null_)
|
85
|
+
if type == :spatial
|
86
|
+
if @srid
|
87
|
+
@limit = {:srid => @srid, :type => @geometric_type.type_name.underscore}
|
88
|
+
@limit[:has_z] = true if @has_z
|
89
|
+
@limit[:has_m] = true if @has_m
|
90
|
+
@limit[:geographic] = true if @geographic
|
91
|
+
else
|
92
|
+
@limit = {:no_constraints => true}
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
attr_reader :geographic
|
99
|
+
attr_reader :srid
|
100
|
+
attr_reader :geometric_type
|
101
|
+
attr_reader :has_z
|
102
|
+
attr_reader :has_m
|
103
|
+
|
104
|
+
alias_method :geographic?, :geographic
|
105
|
+
alias_method :has_z?, :has_z
|
106
|
+
alias_method :has_m?, :has_m
|
107
|
+
|
108
|
+
|
109
|
+
def spatial?
|
110
|
+
type == :spatial
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
def has_spatial_constraints?
|
115
|
+
!@srid.nil?
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
def klass
|
120
|
+
type == :spatial ? ::RGeo::Feature::Geometry : super
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
def type_cast(value_)
|
125
|
+
if type == :spatial
|
126
|
+
SpatialColumn.convert_to_geometry(value_, @factory_settings, @table_name, name,
|
127
|
+
@geographic, @srid, @has_z, @has_m)
|
128
|
+
else
|
129
|
+
super
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
private
|
135
|
+
|
136
|
+
|
137
|
+
def simplified_type(sql_type_)
|
138
|
+
sql_type_ =~ /geography|geometry|point|linestring|polygon/i ? :spatial : super
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
def self.convert_to_geometry(input_, factory_settings_, table_name_, column_, geographic_, srid_, has_z_, has_m_)
|
143
|
+
if srid_
|
144
|
+
constraints_ = {:geographic => geographic_, :has_z_coordinate => has_z_,
|
145
|
+
:has_m_coordinate => has_m_, :srid => srid_}
|
146
|
+
else
|
147
|
+
constraints_ = nil
|
148
|
+
end
|
149
|
+
if ::RGeo::Feature::Geometry === input_
|
150
|
+
factory_ = factory_settings_.get_column_factory(table_name_, column_, constraints_)
|
151
|
+
::RGeo::Feature.cast(input_, factory_) rescue nil
|
152
|
+
elsif input_.respond_to?(:to_str)
|
153
|
+
input_ = input_.to_str
|
154
|
+
if input_.length == 0
|
155
|
+
nil
|
156
|
+
else
|
157
|
+
factory_ = factory_settings_.get_column_factory(table_name_, column_, constraints_)
|
158
|
+
marker_ = input_[0,1]
|
159
|
+
if marker_ == "\x00" || marker_ == "\x01" || input_[0,4] =~ /[0-9a-fA-F]{4}/
|
160
|
+
::RGeo::WKRep::WKBParser.new(factory_, :support_ewkb => true).parse(input_) rescue nil
|
161
|
+
else
|
162
|
+
::RGeo::WKRep::WKTParser.new(factory_, :support_ewkt => true).parse(input_) rescue nil
|
163
|
+
end
|
164
|
+
end
|
165
|
+
else
|
166
|
+
nil
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
# Register spatial types with the postgres OID mechanism
|
175
|
+
# so we can recognize custom columns coming from the database.
|
176
|
+
|
177
|
+
class SpatialOID < PostgreSQLAdapter::OID::Type # :nodoc:
|
178
|
+
|
179
|
+
def initialize(factory_generator_)
|
180
|
+
@factory_generator = factory_generator_
|
181
|
+
end
|
182
|
+
|
183
|
+
def type_cast(value_)
|
184
|
+
return if value_.nil?
|
185
|
+
::RGeo::WKRep::WKBParser.new(@factory_generator, :support_ewkb => true).parse(value_) rescue nil
|
186
|
+
end
|
187
|
+
|
188
|
+
end
|
189
|
+
|
190
|
+
PostgreSQLAdapter::OID.register_type('geometry', SpatialOID.new(nil))
|
191
|
+
PostgreSQLAdapter::OID.register_type('geography', SpatialOID.new(::RGeo::Geographic.method(:spherical_factory)))
|
192
|
+
|
193
|
+
|
194
|
+
# This is a hack to ActiveRecord::ModelSchema. We have to "decorate" the decorate_columns
|
195
|
+
# method to apply class-specific customizations to spatial type casting.
|
196
|
+
|
197
|
+
module DecorateColumnsModification # :nodoc:
|
198
|
+
|
199
|
+
def decorate_columns(columns_hash_)
|
200
|
+
columns_hash_ = super(columns_hash_)
|
201
|
+
return unless columns_hash_
|
202
|
+
canonical_columns_ = self.columns_hash
|
203
|
+
columns_hash_.each do |name_, col_|
|
204
|
+
if col_.is_a?(SpatialOID) && (canonical_ = canonical_columns_[name_]) && canonical_.spatial?
|
205
|
+
columns_hash_[name_] = canonical_
|
206
|
+
end
|
207
|
+
end
|
208
|
+
columns_hash_
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
::ActiveRecord::Base.extend(DecorateColumnsModification)
|
214
|
+
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
@@ -0,0 +1,140 @@
|
|
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 ConnectionAdapters # :nodoc:
|
40
|
+
|
41
|
+
module PostGISAdapter # :nodoc:
|
42
|
+
|
43
|
+
|
44
|
+
class SpatialTableDefinition < ConnectionAdapters::PostgreSQLAdapter::TableDefinition # :nodoc:
|
45
|
+
|
46
|
+
def column(name_, type_, options_={})
|
47
|
+
if (info_ = @base.spatial_column_constructor(type_.to_sym))
|
48
|
+
type_ = options_[:type] || info_[:type] || type_
|
49
|
+
if type_.to_s == 'geometry' &&
|
50
|
+
(options_[:no_constraints] ||
|
51
|
+
options_[:limit].is_a?(::Hash) && options_[:limit][:no_constraints])
|
52
|
+
then
|
53
|
+
options_.delete(:limit)
|
54
|
+
else
|
55
|
+
options_[:type] = type_
|
56
|
+
type_ = :spatial
|
57
|
+
end
|
58
|
+
end
|
59
|
+
super(name_, type_, options_)
|
60
|
+
if type_ == :spatial
|
61
|
+
col_ = self[name_]
|
62
|
+
col_.extend(SpatialColumnDefinitionMethods) unless col_.respond_to?(:geographic?)
|
63
|
+
options_.merge!(col_.limit) if col_.limit.is_a?(::Hash)
|
64
|
+
col_.set_spatial_type(options_[:type])
|
65
|
+
col_.set_geographic(options_[:geographic])
|
66
|
+
col_.set_srid(options_[:srid])
|
67
|
+
col_.set_has_z(options_[:has_z])
|
68
|
+
col_.set_has_m(options_[:has_m])
|
69
|
+
end
|
70
|
+
self
|
71
|
+
end
|
72
|
+
|
73
|
+
def to_sql
|
74
|
+
@columns.find_all{ |c_| !c_.respond_to?(:geographic?) || c_.geographic? }.map{ |c_| c_.to_sql } * ', '
|
75
|
+
end
|
76
|
+
|
77
|
+
def non_geographic_spatial_columns
|
78
|
+
@columns.find_all{ |c_| c_.respond_to?(:geographic?) && !c_.geographic? }
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
module SpatialColumnDefinitionMethods # :nodoc:
|
85
|
+
|
86
|
+
def spatial_type
|
87
|
+
@spatial_type
|
88
|
+
end
|
89
|
+
|
90
|
+
def geographic?
|
91
|
+
@geographic
|
92
|
+
end
|
93
|
+
|
94
|
+
def srid
|
95
|
+
@srid ? @srid.to_i : (geographic? ? 4326 : -1)
|
96
|
+
end
|
97
|
+
|
98
|
+
def has_z?
|
99
|
+
@has_z
|
100
|
+
end
|
101
|
+
|
102
|
+
def has_m?
|
103
|
+
@has_m
|
104
|
+
end
|
105
|
+
|
106
|
+
def set_geographic(value_)
|
107
|
+
@geographic = value_ ? true : false
|
108
|
+
end
|
109
|
+
|
110
|
+
def set_spatial_type(value_)
|
111
|
+
@spatial_type = value_.to_s
|
112
|
+
end
|
113
|
+
|
114
|
+
def set_srid(value_)
|
115
|
+
@srid = value_
|
116
|
+
end
|
117
|
+
|
118
|
+
def set_has_z(value_)
|
119
|
+
@has_z = value_ ? true : false
|
120
|
+
end
|
121
|
+
|
122
|
+
def set_has_m(value_)
|
123
|
+
@has_m = value_ ? true : false
|
124
|
+
end
|
125
|
+
|
126
|
+
def sql_type
|
127
|
+
type_ = spatial_type.upcase.gsub('_', '')
|
128
|
+
type_ << 'Z' if has_z?
|
129
|
+
type_ << 'M' if has_m?
|
130
|
+
"GEOGRAPHY(#{type_},#{srid})"
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# -----------------------------------------------------------------------------
|
2
2
|
#
|
3
|
-
#
|
3
|
+
# PostGIS adapter for Rails 3.x
|
4
4
|
#
|
5
5
|
# -----------------------------------------------------------------------------
|
6
6
|
# Copyright 2010-2012 Daniel Azuma
|
@@ -35,30 +35,5 @@
|
|
35
35
|
|
36
36
|
|
37
37
|
require 'rails/railtie'
|
38
|
-
|
39
|
-
|
40
|
-
# :stopdoc:
|
41
|
-
|
42
|
-
module ActiveRecord
|
43
|
-
|
44
|
-
module ConnectionAdapters
|
45
|
-
|
46
|
-
module PostGISAdapter
|
47
|
-
|
48
|
-
|
49
|
-
class Railtie < ::Rails::Railtie
|
50
|
-
|
51
|
-
rake_tasks do
|
52
|
-
load ::File.expand_path('databases.rake', ::File.dirname(__FILE__))
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
63
|
-
|
64
|
-
# :startdoc:
|
38
|
+
require 'active_record/connection_adapters/postgis_adapter'
|
39
|
+
load 'active_record/connection_adapters/postgis_adapter/shared/railtie'
|
data/lib/active_record/connection_adapters/postgis_adapter/{arel_tosql.rb → shared/arel_tosql.rb}
RENAMED
@@ -34,12 +34,10 @@
|
|
34
34
|
;
|
35
35
|
|
36
36
|
|
37
|
-
# :
|
37
|
+
module Arel # :nodoc:
|
38
|
+
module Visitors # :nodoc:
|
38
39
|
|
39
|
-
|
40
|
-
module Visitors
|
41
|
-
|
42
|
-
class PostGIS < PostgreSQL
|
40
|
+
class PostGIS < PostgreSQL # :nodoc:
|
43
41
|
|
44
42
|
FUNC_MAP = {
|
45
43
|
'st_wkttosql' => 'ST_GeomFromEWKT',
|
@@ -59,5 +57,3 @@ module Arel
|
|
59
57
|
|
60
58
|
end
|
61
59
|
end
|
62
|
-
|
63
|
-
# :startdoc:
|
@@ -0,0 +1,133 @@
|
|
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 ConnectionAdapters # :nodoc:
|
40
|
+
|
41
|
+
|
42
|
+
# Extend JDBC's PostgreSQLAdapter implementation for compatibility with
|
43
|
+
# ActiveRecord's default PostgreSQLAdapter.
|
44
|
+
|
45
|
+
class PostgreSQLAdapter # :nodoc:
|
46
|
+
|
47
|
+
|
48
|
+
# Add `query` method for compatibility
|
49
|
+
|
50
|
+
def query(*args)
|
51
|
+
select_rows(*args)
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
# Backport from master, so PostGIS adapater will work with current stable
|
56
|
+
# activerecord-jdbc-adapter gem.
|
57
|
+
#
|
58
|
+
# https://github.com/jruby/activerecord-jdbc-adapter/pull/200
|
59
|
+
|
60
|
+
unless method_defined?(:schema_search_path=)
|
61
|
+
def schema_search_path=(schema_csv)
|
62
|
+
if schema_csv
|
63
|
+
execute "SET search_path TO #{schema_csv}"
|
64
|
+
@schema_search_path = schema_csv
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
# Backport from master, so PostGIS adapater will work with current stable
|
71
|
+
# activerecord-jdbc-adapter gem.
|
72
|
+
#
|
73
|
+
# https://github.com/jruby/activerecord-jdbc-adapter/pull/200
|
74
|
+
|
75
|
+
unless method_defined?(:schema_search_path)
|
76
|
+
# Returns the active schema search path.
|
77
|
+
def schema_search_path
|
78
|
+
@schema_search_path ||= exec_query('SHOW search_path', 'SCHEMA')[0]['search_path']
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
# For ActiveRecord 3.1 compatibility: Add the "postgis" adapter to the
|
84
|
+
# matcher of jdbc-like adapters.
|
85
|
+
|
86
|
+
def self.visitor_for(pool)
|
87
|
+
config = pool.spec.config
|
88
|
+
adapter = config[:adapter]
|
89
|
+
adapter_spec = config[:adapter_spec] || self
|
90
|
+
if adapter =~ /^(jdbc|jndi|postgis)$/
|
91
|
+
adapter_spec.arel2_visitors(config).values.first.new(pool)
|
92
|
+
else
|
93
|
+
adapter_spec.arel2_visitors(config)[adapter].new(pool)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
module PostGISAdapter # :nodoc:
|
102
|
+
|
103
|
+
|
104
|
+
# Based on the default <tt>postgresql_connection</tt> definition from
|
105
|
+
# activerecord-jdbc-adapter
|
106
|
+
|
107
|
+
def self.create_jdbc_connection(context_, config_)
|
108
|
+
begin
|
109
|
+
require 'jdbc/postgres'
|
110
|
+
::Jdbc::Postgres.load_driver(:require) if defined?(::Jdbc::Postgres.load_driver)
|
111
|
+
rescue LoadError # assuming driver.jar is on the class-path
|
112
|
+
end
|
113
|
+
require "arjdbc/postgresql"
|
114
|
+
config_[:username] ||= ::Java::JavaLang::System.get_property("user.name")
|
115
|
+
config_[:host] ||= "localhost"
|
116
|
+
config_[:port] ||= 5432
|
117
|
+
config_[:url] ||= "jdbc:postgresql://#{config_[:host]}:#{config_[:port]}/#{config_[:database]}"
|
118
|
+
config_[:url] << config_[:pg_params] if config_[:pg_params]
|
119
|
+
config_[:driver] ||= defined?(::Jdbc::Postgres.driver_name) ? ::Jdbc::Postgres.driver_name : 'org.postgresql.Driver'
|
120
|
+
config_[:adapter_class] = ::ActiveRecord::ConnectionAdapters::PostGISAdapter::MainAdapter
|
121
|
+
config_[:adapter_spec] = ::ArJdbc::PostgreSQL
|
122
|
+
conn_ = context_.jdbc_connection(config_)
|
123
|
+
conn_.execute("SET SEARCH_PATH TO #{config_[:schema_search_path]}") if config_[:schema_search_path]
|
124
|
+
conn_
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|