-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from rgeo/spatial_column_info
Do not query database for non-spatial tables
- Loading branch information
Showing
5 changed files
with
79 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
lib/active_record/connection_adapters/postgis_adapter/spatial_column_info.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module ActiveRecord # :nodoc: | ||
module ConnectionAdapters # :nodoc: | ||
module PostGISAdapter | ||
# Do spatial sql queries for column info and memoize that info. | ||
class SpatialColumnInfo | ||
def initialize(adapter, table_name) | ||
@adapter = adapter | ||
@table_name = table_name | ||
end | ||
|
||
def all | ||
info = @adapter.query("SELECT f_geometry_column,coord_dimension,srid,type FROM geometry_columns WHERE f_table_name='#{@table_name}'") | ||
result = {} | ||
info.each do |row| | ||
name = row[0] | ||
type = row[3] | ||
dimension = row[1].to_i | ||
has_m = !!(type =~ /m$/i) | ||
type.sub!(/m$/, '') | ||
has_z = dimension > 3 || dimension == 3 && !has_m | ||
result[name] = { | ||
dimension: dimension, | ||
has_m: has_m, | ||
has_z: has_z, | ||
name: name, | ||
srid: row[2].to_i, | ||
type: type, | ||
} | ||
end | ||
result | ||
end | ||
|
||
# will not query the database for non-spatial columns/tables | ||
def get(column_name, type) | ||
return nil unless type =~ /geometry/i | ||
@spatial_column_info ||= all | ||
@spatial_column_info[column_name] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters