Skip to content

Commit f17fa8e

Browse files
committed
pg, inline casting methods into OID::Type objects.
This inlines casting for the most obvious types. The rest will follow eventually. I need to put some tests in place, to make sure that the inlining is not causing regressions. /cc @sgrif
1 parent 4c66ab2 commit f17fa8e

File tree

4 files changed

+25
-44
lines changed

4 files changed

+25
-44
lines changed

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ def point_to_string(point) # :nodoc:
66
"(#{point[0]},#{point[1]})"
77
end
88

9-
def string_to_bit(value) # :nodoc:
10-
case value
11-
when /^0x/i
12-
value[2..-1].hex.to_s(2) # Hexadecimal notation
13-
else
14-
value # Bit-string notation
15-
end
16-
end
17-
189
def hstore_to_string(object, array_member = false) # :nodoc:
1910
if Hash === object
2011
string = object.map { |k, v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(',')
@@ -76,28 +67,6 @@ def string_to_json(string) # :nodoc:
7667
end
7768
end
7869

79-
def string_to_cidr(string) # :nodoc:
80-
if string.nil?
81-
nil
82-
elsif String === string
83-
begin
84-
IPAddr.new(string)
85-
rescue ArgumentError
86-
nil
87-
end
88-
else
89-
string
90-
end
91-
end
92-
93-
def cidr_to_string(object) # :nodoc:
94-
if IPAddr === object
95-
"#{object.to_s}/#{object.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
96-
else
97-
object
98-
end
99-
end
100-
10170
def string_to_array(string, oid) # :nodoc:
10271
parse_pg_array(string).map {|val| type_cast_array(oid, val)}
10372
end

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ module OID # :nodoc:
55
class Bit < Type::String
66
def type_cast(value)
77
if ::String === value
8-
ConnectionAdapters::PostgreSQLColumn.string_to_bit value
8+
case value
9+
when /^0x/i
10+
value[2..-1].hex.to_s(2) # Hexadecimal notation
11+
else
12+
value # Bit-string notation
13+
end
914
else
1015
value
1116
end

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,26 @@ def type_cast_for_schema(value)
1818
end
1919
end
2020

21+
def type_cast_for_database(value)
22+
if IPAddr === value
23+
"#{value.to_s}/#{value.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
24+
else
25+
value
26+
end
27+
end
28+
2129
def cast_value(value)
22-
ConnectionAdapters::PostgreSQLColumn.string_to_cidr value
30+
if value.nil?
31+
nil
32+
elsif String === value
33+
begin
34+
IPAddr.new(value)
35+
rescue ArgumentError
36+
nil
37+
end
38+
else
39+
value
40+
end
2341
end
2442
end
2543
end

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ def quote(value, column = nil) #:nodoc:
4444
when 'json' then super(PostgreSQLColumn.json_to_string(value), column)
4545
else super
4646
end
47-
when IPAddr
48-
case sql_type
49-
when 'inet', 'cidr' then super(PostgreSQLColumn.cidr_to_string(value), column)
50-
else super
51-
end
5247
when Float
5348
if value.infinite? && column.type == :datetime
5449
"'#{value.to_s.downcase}'"
@@ -125,12 +120,6 @@ def type_cast(value, column, array_member = false)
125120
when 'json' then PostgreSQLColumn.json_to_string(value)
126121
else super(value, column)
127122
end
128-
when IPAddr
129-
if %w(inet cidr).include? column.sql_type
130-
PostgreSQLColumn.cidr_to_string(value)
131-
else
132-
super(value, column)
133-
end
134123
else
135124
super(value, column)
136125
end

0 commit comments

Comments
 (0)