-
-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not query database for non-spatial tables #125
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module ActiveRecord # :nodoc: | ||
module ConnectionAdapters # :nodoc: | ||
module PostGISAdapter | ||
# Do spatial sql queries for column info and memoize that info. | ||
class SpatialColumnInfo | ||
attr_accessor :adapter, :table_name | ||
|
||
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could these two lines (38, 39) become the following?
What are we gaining from the memoization? Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind the above comment. I see what's happening. Nice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that's the whole point of the PR. 📝 😺 |
||
end | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could these
attr_accessor
's becomeattr_reader
s? Either way, I believe they could also be privatized as they are not used by anything outside of the class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed them in the latest commit.