Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add geometric type for postgresql adapter. Closes #2233.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2498 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit e30699f66034405de0eaaad12066c2c7d266762f 1 parent 24b9d2f
Marcel Molina authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add geometric type for postgresql adapter. #2233 [akaspick@gmail.com]
+
* Add option (true by default) to generate reader methods for each attribute of a record to avoid the overhead of calling method missing. In partial fullfilment of #1236. [skaes@web.de]
* Add convenience predicate methods on Column class. In partial fullfilment of #1236. [skaes@web.de]
View
2  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -354,6 +354,8 @@ def translate_field_type(field_type)
when /^timestamp/i then 'datetime'
when /^real|^money/i then 'float'
when /^interval/i then 'string'
+ # geometric types (the line type is currently not implemented in postgresql)
+ when /^point|lseg|box|path|polygon|circle/i then 'string'
when /^bytea/i then 'binary'
else field_type # Pass through standard types.
end
View
62 activerecord/test/base_test.rb
@@ -718,6 +718,68 @@ def test_default
assert_equal 'a varchar field', default.char2
assert_equal 'a text field', default.char3
end
+
+ class Geometric < ActiveRecord::Base; end
+ def test_geometric_content
+
+ # accepted format notes:
+ # ()'s aren't required
+ # values can be a mix of float or integer
+
+ g = Geometric.new(
+ :a_point => '(5.0, 6.1)',
+ #:a_line => '((2.0, 3), (5.5, 7.0))' # line type is currently unsupported in postgresql
+ :a_line_segment => '(2.0, 3), (5.5, 7.0)',
+ :a_box => '2.0, 3, 5.5, 7.0',
+ :a_path => '[(2.0, 3), (5.5, 7.0), (8.5, 11.0)]', # [ ] is an open path
+ :a_polygon => '((2.0, 3), (5.5, 7.0), (8.5, 11.0))',
+ :a_circle => '<(5.3, 10.4), 2>'
+ )
+
+ assert g.save
+
+ # Reload and check that we have all the geometric attributes.
+ h = Geometric.find(g.id)
+
+ assert_equal '(5,6.1)', h.a_point
+ assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
+ assert_equal '(5.5,7),(2,3)', h.a_box # reordered to store upper right corner then bottom left corner
+ assert_equal '[(2,3),(5.5,7),(8.5,11)]', h.a_path
+ assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_polygon
+ assert_equal '<(5.3,10.4),2>', h.a_circle
+
+ # use a geometric function to test for an open path
+ objs = Geometric.find_by_sql ["select isopen(a_path) from geometrics where id = ?", g.id]
+ assert_equal objs[0].isopen, 't'
+
+ # test alternate formats when defining the geometric types
+
+ g = Geometric.new(
+ :a_point => '5.0, 6.1',
+ #:a_line => '((2.0, 3), (5.5, 7.0))' # line type is currently unsupported in postgresql
+ :a_line_segment => '((2.0, 3), (5.5, 7.0))',
+ :a_box => '(2.0, 3), (5.5, 7.0)',
+ :a_path => '((2.0, 3), (5.5, 7.0), (8.5, 11.0))', # ( ) is a closed path
+ :a_polygon => '2.0, 3, 5.5, 7.0, 8.5, 11.0',
+ :a_circle => '((5.3, 10.4), 2)'
+ )
+
+ assert g.save
+
+ # Reload and check that we have all the geometric attributes.
+ h = Geometric.find(g.id)
+
+ assert_equal '(5,6.1)', h.a_point
+ assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
+ assert_equal '(5.5,7),(2,3)', h.a_box # reordered to store upper right corner then bottom left corner
+ assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_path
+ assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_polygon
+ assert_equal '<(5.3,10.4),2>', h.a_circle
+
+ # use a geometric function to test for an closed path
+ objs = Geometric.find_by_sql ["select isclosed(a_path) from geometrics where id = ?", g.id]
+ assert_equal objs[0].isclosed, 't'
+ end
end
def test_auto_id
View
1  activerecord/test/fixtures/db_definitions/postgresql.drop.sql
@@ -25,3 +25,4 @@ DROP TABLE categories_posts;
DROP TABLE defaults;
DROP TABLE fk_test_has_fk;
DROP TABLE fk_test_has_pk;
+DROP TABLE geometrics;
View
11 activerecord/test/fixtures/db_definitions/postgresql.sql
@@ -207,3 +207,14 @@ CREATE TABLE fk_test_has_fk (
id INTEGER NOT NULL PRIMARY KEY,
fk_id INTEGER NOT NULL REFERENCES fk_test_has_fk(id)
);
+
+CREATE TABLE geometrics (
+ id serial primary key,
+ a_point point,
+ -- a_line line, (the line type is currently not implemented in postgresql)
+ a_line_segment lseg,
+ a_box box,
+ a_path path,
+ a_polygon polygon,
+ a_circle circle
+);
Please sign in to comment.
Something went wrong with that request. Please try again.