Skip to content

Commit

Permalink
Merge pull request #117 from srt32/st-main-adapter-refactor
Browse files Browse the repository at this point in the history
Remove implementation of no_constraints in main_adapter
  • Loading branch information
teeparham committed May 28, 2014
2 parents 98f08b1 + 0da2f93 commit bf8656e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ def create_table(table_name, options = {}, &block)
has_z = col.has_z?
has_m = col.has_m?
type = "#{type}M" if has_m && !has_z
dimensions_ = 2
dimensions_ += 1 if has_z
dimensions_ += 1 if has_m
dimensions_ = set_dimensions(has_m, has_z)
execute("SELECT AddGeometryColumn('#{quote_string(table_name)}', '#{quote_string(col.name.to_s)}', #{col.srid}, '#{quote_string(type)}', #{dimensions_})")
end
end
Expand All @@ -127,32 +125,7 @@ def add_column(table_name, column_name, type, options = {})
table_name = table_name.to_s
column_name = column_name.to_s
if (info = spatial_column_constructor(type.to_sym))
limit = options[:limit]
if type.to_s == 'geometry' &&
(options[:no_constraints] || limit.is_a?(::Hash) && limit[:no_constraints])
then
options.delete(:limit)
super
else
options.merge!(limit) if limit.is_a?(::Hash)
type = (options[:type] || info[:type] || type).to_s.gsub('_', '').upcase
has_z = options[:has_z]
has_m = options[:has_m]
srid = (options[:srid] || PostGISAdapter::DEFAULT_SRID).to_i
if options[:geographic]
type << 'Z' if has_z
type << 'M' if has_m
execute("ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} GEOGRAPHY(#{type},#{srid})")
change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
change_column_null(table_name, column_name, false, options[:default]) if options[:null] == false
else
type = "#{type}M" if has_m && !has_z
dimensions = 2
dimensions += 1 if has_z
dimensions += 1 if has_m
execute("SELECT AddGeometryColumn('#{quote_string(table_name)}', '#{quote_string(column_name)}', #{srid}, '#{quote_string(type)}', #{dimensions})")
end
end
add_spatial_column(column_name, table_name, info, type, options)
else
super
end
Expand Down Expand Up @@ -204,6 +177,26 @@ def spatial_column_info(table_name)

private

def add_spatial_column(column_name, table_name, info, type, options)
limit = options[:limit]
options.merge!(limit) if limit.is_a?(::Hash)
type = (options[:type] || info[:type] || type).to_s.gsub('_', '').upcase
has_z = options[:has_z]
has_m = options[:has_m]
srid = (options[:srid] || PostGISAdapter::DEFAULT_SRID).to_i
if options[:geographic]
type << 'Z' if has_z
type << 'M' if has_m
execute("ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} GEOGRAPHY(#{type},#{srid})")
change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
change_column_null(table_name, column_name, false, options[:default]) if options[:null] == false
else
type = "#{type}M" if has_m && !has_z
dimensions = set_dimensions(has_m, has_z)
execute("SELECT AddGeometryColumn('#{quote_string(table_name)}', '#{quote_string(column_name)}', #{srid}, '#{quote_string(type)}', #{dimensions})")
end
end

def column_type_map
if defined?(type_map) # ActiveRecord 4.1+
type_map
Expand All @@ -212,6 +205,13 @@ def column_type_map
end
end

def set_dimensions(has_m, has_z)
dimensions = 2
dimensions += 1 if has_z
dimensions += 1 if has_m
dimensions
end

end
end
end
Expand Down
56 changes: 0 additions & 56 deletions test/ddl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,6 @@ def test_create_simple_geometry
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
end

# no_constraints no longer supported in PostGIS 2.0
def _test_create_no_constraints_geometry
klass = create_ar_class
klass.connection.create_table(:spatial_test) do |t|
t.column 'geom', :geometry, :limit => {:no_constraints => true}
end
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
col = klass.columns.last
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
assert_equal(false, col.geographic?)
assert_equal(false, col.has_spatial_constraints?)
assert_nil(col.srid)
assert(klass.cached_attributes.include?('geom'))
klass.connection.drop_table(:spatial_test)
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
end

def test_create_simple_geography
klass = create_ar_class
klass.connection.create_table(:spatial_test) do |t|
Expand Down Expand Up @@ -100,29 +83,6 @@ def test_add_geometry_column
assert_equal(false, cols_[-1].has_spatial_constraints?)
end

# no_constraints no longer supported in PostGIS 2.0
def _test_add_no_constraints_geometry_column
klass = create_ar_class
klass.connection.create_table(:spatial_test) do |t|
t.column('latlon', :geometry)
end
klass.connection.change_table(:spatial_test) do |t|
t.column('geom2', :geometry, :no_constraints => true)
t.column('name', :string)
end
assert_equal(1, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
cols_ = klass.columns
assert_equal(::RGeo::Feature::Geometry, cols_[-3].geometric_type)
assert_equal(0, cols_[-3].srid)
assert_equal(true, cols_[-3].has_spatial_constraints?)
assert_equal(::RGeo::Feature::Geometry, cols_[-2].geometric_type)
assert_nil(cols_[-2].srid)
assert_equal(false, cols_[-2].geographic?)
assert_equal(false, cols_[-2].has_spatial_constraints?)
assert_nil(cols_[-1].geometric_type)
assert_equal(false, cols_[-1].has_spatial_constraints?)
end

def test_add_geography_column
klass = create_ar_class
klass.connection.create_table(:spatial_test) do |t|
Expand Down Expand Up @@ -197,22 +157,6 @@ def test_create_simple_geometry_using_shortcut
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
end

# no_constraints no longer supported in PostGIS 2.0
def _test_create_no_constraints_geometry_using_shortcut
klass = create_ar_class
klass.connection.create_table(:spatial_test) do |t|
t.spatial 'geom', :no_constraints => true
end
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
col = klass.columns.last
assert_equal(::RGeo::Feature::Geometry, col.geometric_type)
assert_equal(false, col.geographic?)
assert_nil(col.srid)
assert(klass.cached_attributes.include?('geom'))
klass.connection.drop_table(:spatial_test)
assert_equal(0, klass.connection.select_value("SELECT COUNT(*) FROM geometry_columns WHERE f_table_name='spatial_test'").to_i)
end

def test_create_simple_geography_using_shortcut
klass = create_ar_class
klass.connection.create_table(:spatial_test) do |t|
Expand Down

0 comments on commit bf8656e

Please sign in to comment.