Skip to content

Commit 336b376

Browse files
committed
Make Postgres point type correspond to ruby array with two floats inside
1 parent c2e2031 commit 336b376

6 files changed

Lines changed: 72 additions & 8 deletions

File tree

‎activerecord/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Rails 4.0.0 (unreleased) ##
22

3+
* PostgreSQL geometric type point is supported by ActiveRecord. Fixes #7324.
4+
5+
*Martin Schuerrer*
6+
37
* Add an `add_index` override in Postgresql adapter and MySQL adapter
48
to allow custom index type support. Fixes #6101.
59

‎activerecord/lib/active_record/connection_adapters/postgresql/cast.rb‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ module ActiveRecord
22
module ConnectionAdapters
33
class PostgreSQLColumn < Column
44
module Cast
5+
def point_to_string(point)
6+
"(#{point[0]},#{point[1]})"
7+
end
8+
9+
def string_to_point(string)
10+
if string[0] == '(' && string[-1] == ')'
11+
string = string[1...-1]
12+
end
13+
string.split(',').map{ |v| Float(v) }
14+
end
15+
516
def string_to_time(string)
617
return string unless String === string
718

‎activerecord/lib/active_record/connection_adapters/postgresql/oid.rb‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ def type_cast(value)
6363
end
6464
end
6565

66+
class Point < Type
67+
def type_cast(value)
68+
if String === value
69+
ConnectionAdapters::PostgreSQLColumn.string_to_point value
70+
else
71+
value
72+
end
73+
end
74+
end
75+
6676
class Array < Type
6777
attr_reader :subtype
6878
def initialize(subtype)
@@ -330,6 +340,7 @@ def self.registered_type?(name)
330340
register_type 'time', OID::Time.new
331341

332342
register_type 'path', OID::Identity.new
343+
register_type 'point', OID::Point.new
333344
register_type 'polygon', OID::Identity.new
334345
register_type 'circle', OID::Identity.new
335346
register_type 'hstore', OID::Hstore.new

‎activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ def quote(value, column = nil) #:nodoc:
2828
super
2929
end
3030
when Array
31-
if column.array
32-
"'#{PostgreSQLColumn.array_to_string(value, column, self)}'"
31+
case sql_type
32+
when 'point' then super(PostgreSQLColumn.point_to_string(value))
3333
else
34-
super
34+
if column.array
35+
"'#{PostgreSQLColumn.array_to_string(value, column, self)}'"
36+
else
37+
super
38+
end
3539
end
3640
when Hash
3741
case sql_type
@@ -92,8 +96,12 @@ def type_cast(value, column, array_member = false)
9296
super(value, column)
9397
end
9498
when Array
95-
return super(value, column) unless column.array
96-
PostgreSQLColumn.array_to_string(value, column, self)
99+
case column.sql_type
100+
when 'point' then PostgreSQLColumn.point_to_string(value)
101+
else
102+
return super(value, column) unless column.array
103+
PostgreSQLColumn.array_to_string(value, column, self)
104+
end
97105
when String
98106
return super(value, column) unless 'bytea' == column.sql_type
99107
{ :value => value, :format => 1 }

‎activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,14 @@ def initialize_type_map
711711

712712
# populate composite types
713713
nodes.find_all { |row| OID::TYPE_MAP.key? row['typelem'].to_i }.each do |row|
714-
vector = OID::Vector.new row['typdelim'], OID::TYPE_MAP[row['typelem'].to_i]
714+
if OID.registered_type? row['typname']
715+
# this composite type is explicitly registered
716+
vector = OID::NAMES[row['typname']]
717+
else
718+
# use the default for composite types
719+
vector = OID::Vector.new row['typdelim'], OID::TYPE_MAP[row['typelem'].to_i]
720+
end
721+
715722
OID::TYPE_MAP[row['oid'].to_i] = vector
716723
end
717724

‎activerecord/test/cases/base_test.rb‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ def test_geometric_content
840840
# Reload and check that we have all the geometric attributes.
841841
h = Geometric.find(g.id)
842842

843-
assert_equal '(5,6.1)', h.a_point
843+
assert_equal [5.0, 6.1], h.a_point
844844
assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
845845
assert_equal '(5.5,7),(2,3)', h.a_box # reordered to store upper right corner then bottom left corner
846846
assert_equal '[(2,3),(5.5,7),(8.5,11)]', h.a_path
@@ -869,7 +869,7 @@ def test_geometric_content
869869
# Reload and check that we have all the geometric attributes.
870870
h = Geometric.find(g.id)
871871

872-
assert_equal '(5,6.1)', h.a_point
872+
assert_equal [5.0, 6.1], h.a_point
873873
assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
874874
assert_equal '(5.5,7),(2,3)', h.a_box # reordered to store upper right corner then bottom left corner
875875
assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_path
@@ -880,6 +880,29 @@ def test_geometric_content
880880
objs = Geometric.find_by_sql ["select isclosed(a_path) from geometrics where id = ?", g.id]
881881

882882
assert_equal true, objs[0].isclosed
883+
884+
# test native ruby formats when defining the geometric types
885+
g = Geometric.new(
886+
:a_point => [5.0, 6.1],
887+
#:a_line => '((2.0, 3), (5.5, 7.0))' # line type is currently unsupported in postgresql
888+
:a_line_segment => '((2.0, 3), (5.5, 7.0))',
889+
:a_box => '(2.0, 3), (5.5, 7.0)',
890+
:a_path => '((2.0, 3), (5.5, 7.0), (8.5, 11.0))', # ( ) is a closed path
891+
:a_polygon => '2.0, 3, 5.5, 7.0, 8.5, 11.0',
892+
:a_circle => '((5.3, 10.4), 2)'
893+
)
894+
895+
assert g.save
896+
897+
# Reload and check that we have all the geometric attributes.
898+
h = Geometric.find(g.id)
899+
900+
assert_equal [5.0, 6.1], h.a_point
901+
assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
902+
assert_equal '(5.5,7),(2,3)', h.a_box # reordered to store upper right corner then bottom left corner
903+
assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_path
904+
assert_equal '((2,3),(5.5,7),(8.5,11))', h.a_polygon
905+
assert_equal '<(5.3,10.4),2>', h.a_circle
883906
end
884907
end
885908

0 commit comments

Comments
 (0)