aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb42
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb49
2 files changed, 77 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index 11256ab3d9..dea7542f03 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -175,21 +175,13 @@ module ActiveRecord
# See also #ids.
#
def pluck(*column_names)
- if loaded? && (column_names.map(&:to_s) - @klass.attribute_names - @klass.attribute_aliases.keys).empty?
- return records.pluck(*column_names)
- end
+ _pluck(column_names, @klass.allow_unsafe_raw_sql == :enabled)
+ end
- if has_include?(column_names.first)
- relation = apply_join_dependency
- relation.pluck(*column_names)
- else
- relation = spawn
- relation.select_values = column_names.map { |cn|
- @klass.has_attribute?(cn) || @klass.attribute_alias?(cn) ? arel_attribute(cn) : cn
- }
- result = skip_query_cache_if_necessary { klass.connection.select_all(relation.arel, nil) }
- result.cast_values(klass.attribute_types)
- end
+ # Same as #pluck but allows raw SQL regardless of `allow_unsafe_raw_sql`
+ # config setting.
+ def unsafe_raw_pluck(*column_names)
+ _pluck(column_names, true)
end
# Pluck all the ID's for the relation using the table's primary key
@@ -202,6 +194,28 @@ module ActiveRecord
private
+ def _pluck(column_names, unsafe_raw)
+ unrecognized = column_names.reject do |cn|
+ @klass.respond_to_attribute?(cn)
+ end
+
+ if loaded? && unrecognized.none?
+ records.pluck(*column_names)
+ elsif has_include?(column_names.first)
+ relation = apply_join_dependency
+ relation.pluck(*column_names)
+ elsif unsafe_raw || unrecognized.none?
+ relation = spawn
+ relation.select_values = column_names.map { |cn|
+ @klass.respond_to_attribute?(cn) ? arel_attribute(cn) : cn
+ }
+ result = skip_query_cache_if_necessary { klass.connection.select_all(relation.arel, nil) }
+ result.cast_values(klass.attribute_types)
+ else
+ raise ArgumentError, "Invalid column name: #{unrecognized}"
+ end
+ end
+
def has_include?(column_name)
eager_loading? || (includes_values.present? && column_name && column_name != :all)
end
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 897ff5c8af..63b1d8e154 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -295,7 +295,22 @@ module ActiveRecord
spawn.order!(*args)
end
+ # Same as #order but allows raw SQL regardless of `allow_unsafe_raw_sql`
+ # config setting.
+ def unsafe_raw_order(*args) # :nodoc:
+ check_if_method_has_arguments!(:order, args)
+ spawn.unsafe_raw_order!(*args)
+ end
+
+ # Same as #order but operates on relation in-place instead of copying.
def order!(*args) # :nodoc:
+ restrict_order_args(args) unless klass.allow_unsafe_raw_sql == :enabled
+ unsafe_raw_order!(*args)
+ end
+
+ # Same as #order! but allows raw SQL regardless of `allow_unsafe_raw_sql`
+ # config setting.
+ def unsafe_raw_order!(*args) # :nodoc:
preprocess_order_args(args)
self.order_values += args
@@ -316,7 +331,22 @@ module ActiveRecord
spawn.reorder!(*args)
end
+ # Same as #reorder but allows raw SQL regardless of `allow_unsafe_raw_sql`
+ # config setting.
+ def unsafe_raw_reorder(*args) # :nodoc:
+ check_if_method_has_arguments!(:reorder, args)
+ spawn.unsafe_raw_reorder!(*args)
+ end
+
+ # Same as #reorder but operates on relation in-place instead of copying.
def reorder!(*args) # :nodoc:
+ restrict_order_args(args) unless klass.allow_unsafe_raw_sql == :enabled
+ unsafe_raw_reorder!
+ end
+
+ # Same as #reorder! but allows raw SQL regardless of `allow_unsafe_raw_sql`
+ # config setting.
+ def unsafe_raw_reorder!(*args) # :nodoc:
preprocess_order_args(args)
self.reordering_value = true
@@ -1139,6 +1169,25 @@ module ActiveRecord
end.flatten!
end
+ # Only allow column names and directions as arguments to #order and
+ # #reorder. Other arguments will cause an ArugmentError to be raised.
+ def restrict_order_args(args)
+ args = args.dup
+ orderings = args.extract_options!
+ columns = args | orderings.keys
+
+ unrecognized = columns.reject { |c| klass.respond_to_attribute?(c) }
+ if unrecognized.any?
+ raise ArgumentError, "Invalid order column: #{unrecognized}"
+ end
+
+ # TODO: find a better list of modifiers.
+ unrecognized = orderings.values.reject { |d| VALID_DIRECTIONS.include?(d.to_s) }
+ if unrecognized.any?
+ raise ArgumentError, "Invalid order direction: #{unrecognized}"
+ end
+ end
+
# Checks to make sure that the arguments are not blank. Note that if some
# blank-like object were initially passed into the query method, then this
# method will not raise an error.