aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/cast.rb13
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/column.rb4
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb52
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb13
-rw-r--r--activerecord/test/cases/adapters/postgresql/quoting_test.rb2
-rw-r--r--activerecord/test/cases/adapters/postgresql/range_test.rb10
6 files changed, 40 insertions, 54 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
deleted file mode 100644
index 5b3e88fb08..0000000000
--- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-module ActiveRecord
- module ConnectionAdapters
- module PostgreSQL
- module Cast # :nodoc:
- def range_to_string(object) # :nodoc:
- from = object.begin.respond_to?(:infinite?) && object.begin.infinite? ? '' : object.begin
- to = object.end.respond_to?(:infinite?) && object.end.infinite? ? '' : object.end
- "[#{from},#{to}#{object.exclude_end? ? ')' : ']'}"
- end
- end
- end
- end
-end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
index 847fd4dded..37e5c3859c 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
@@ -1,11 +1,7 @@
-require 'active_record/connection_adapters/postgresql/cast'
-
module ActiveRecord
module ConnectionAdapters
# PostgreSQL-specific extensions to column definitions in a table.
class PostgreSQLColumn < Column #:nodoc:
- extend PostgreSQL::Cast
-
attr_accessor :array
def initialize(name, default, cast_type, sql_type = nil, null = true, default_function = nil)
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb
index 991cdd0913..ae967d5167 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb
@@ -10,28 +10,10 @@ module ActiveRecord
@type = type
end
- def extract_bounds(value)
- from, to = value[1..-2].split(',')
- {
- from: (value[1] == ',' || from == '-infinity') ? @subtype.infinity(negative: true) : from,
- to: (value[-2] == ',' || to == 'infinity') ? @subtype.infinity : to,
- exclude_start: (value[0] == '('),
- exclude_end: (value[-1] == ')')
- }
- end
-
- def infinity?(value)
- value.respond_to?(:infinite?) && value.infinite?
- end
-
def type_cast_for_schema(value)
value.inspect.gsub('Infinity', '::Float::INFINITY')
end
- def type_cast_single(value)
- infinity?(value) ? value : @subtype.type_cast_from_database(value)
- end
-
def cast_value(value)
return if value == 'empty'
return value if value.is_a?(::Range)
@@ -53,6 +35,40 @@ This is not reliable and will be removed in the future.
end
::Range.new(from, to, extracted[:exclude_end])
end
+
+ def type_cast_for_database(value)
+ if value.is_a?(::Range)
+ from = type_cast_single_for_database(value.begin)
+ to = type_cast_single_for_database(value.end)
+ "[#{from},#{to}#{value.exclude_end? ? ')' : ']'}"
+ else
+ super
+ end
+ end
+
+ private
+
+ def type_cast_single(value)
+ infinity?(value) ? value : @subtype.type_cast_from_database(value)
+ end
+
+ def type_cast_single_for_database(value)
+ infinity?(value) ? '' : @subtype.type_cast_for_database(value)
+ end
+
+ def extract_bounds(value)
+ from, to = value[1..-2].split(',')
+ {
+ from: (value[1] == ',' || from == '-infinity') ? @subtype.infinity(negative: true) : from,
+ to: (value[-2] == ',' || to == 'infinity') ? @subtype.infinity : to,
+ exclude_start: (value[0] == '('),
+ exclude_end: (value[-1] == ')')
+ }
+ end
+
+ def infinity?(value)
+ value.respond_to?(:infinite?) && value.infinite?
+ end
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
index 39b36731cd..be598f997f 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb
@@ -21,13 +21,6 @@ module ActiveRecord
sql_type = type_to_sql(column.type, column.limit, column.precision, column.scale)
case value
- when Range
- if /range$/ =~ sql_type
- escaped = quote_string(PostgreSQLColumn.range_to_string(value))
- "'#{escaped}'::#{sql_type}"
- else
- super
- end
when Float
if value.infinite? || value.nan?
"'#{value.to_s}'"
@@ -54,12 +47,6 @@ module ActiveRecord
return super unless column
case value
- when Range
- if /range$/ =~ column.sql_type
- PostgreSQLColumn.range_to_string(value)
- else
- super
- end
when NilClass
if column.array
value
diff --git a/activerecord/test/cases/adapters/postgresql/quoting_test.rb b/activerecord/test/cases/adapters/postgresql/quoting_test.rb
index 59c274c759..11d5173d37 100644
--- a/activerecord/test/cases/adapters/postgresql/quoting_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/quoting_test.rb
@@ -61,7 +61,7 @@ module ActiveRecord
def test_quote_range
range = "1,2]'; SELECT * FROM users; --".."a"
c = PostgreSQLColumn.new(nil, nil, OID::Range.new(Type::Integer.new, :int8range))
- assert_equal "'[1,2]''; SELECT * FROM users; --,a]'::int8range", @conn.quote(range, c)
+ assert_equal "'[1,0]'", @conn.quote(range, c)
end
def test_quote_bit_string
diff --git a/activerecord/test/cases/adapters/postgresql/range_test.rb b/activerecord/test/cases/adapters/postgresql/range_test.rb
index 02d0a9b483..d812cd01c4 100644
--- a/activerecord/test/cases/adapters/postgresql/range_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/range_test.rb
@@ -271,12 +271,12 @@ _SQL
end
def test_ranges_correctly_escape_input
- e = assert_raises(ActiveRecord::StatementInvalid) do
- range = "1,2]'; SELECT * FROM users; --".."a"
- PostgresqlRange.update_all(int8_range: range)
- end
+ range = "-1,2]'; DROP TABLE postgresql_ranges; --".."a"
+ PostgresqlRange.update_all(int8_range: range)
- assert e.message.starts_with?("PG::InvalidTextRepresentation")
+ assert_nothing_raised do
+ PostgresqlRange.first
+ end
end
private