summaryrefslogtreecommitdiffstats
path: root/app/models/custom_field.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2013-01-03 12:28:50 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2013-01-03 12:28:50 +0000
commitd82159bcf544bb66e301599ae0cada1ce73f2335 (patch)
treec15e3ebbcdd217b94ec4f668e69ceb041990154e /app/models/custom_field.rb
parent049eaeb776b51710a1a2efe061a0850626445938 (diff)
downloadredmine-d82159bcf544bb66e301599ae0cada1ce73f2335.tar.gz
redmine-d82159bcf544bb66e301599ae0cada1ce73f2335.zip
Use joins instead of sub-queries in group by/sort by when using custom fields (#12713).
Sub-queries in group by are not supported by SQLServer. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11102 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/custom_field.rb')
-rw-r--r--app/models/custom_field.rb34
1 files changed, 22 insertions, 12 deletions
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index 40582ff73..a9c42d4bb 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -180,18 +180,12 @@ class CustomField < ActiveRecord::Base
case field_format
when 'string', 'text', 'list', 'date', 'bool'
# COALESCE is here to make sure that blank and NULL values are sorted equally
- "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
- " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
- " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
- " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
+ "COALESCE(#{join_alias}.value, '')"
when 'int', 'float'
# Make the database cast values into numeric
# Postgresql will raise an error if a value can not be casted!
# CustomValue validations should ensure that it doesn't occur
- "(SELECT CAST(cv_sort.value AS decimal(30,3)) FROM #{CustomValue.table_name} cv_sort" +
- " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
- " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
- " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
+ "CAST(#{join_alias}.value AS decimal(30,3))"
when 'user', 'version'
value_class.fields_for_order_statement(value_join_alias)
else
@@ -207,10 +201,7 @@ class CustomField < ActiveRecord::Base
when 'list', 'date', 'bool', 'int'
order_statement
when 'user', 'version'
- "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
- " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
- " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
- " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
+ "COALESCE(#{join_alias}.value, '')"
else
nil
end
@@ -230,6 +221,25 @@ class CustomField < ActiveRecord::Base
" AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" +
" LEFT OUTER JOIN #{value_class.table_name} #{value_join_alias}" +
" ON CAST(#{join_alias}.value as decimal(30,0)) = #{value_join_alias}.id"
+ when 'int', 'float'
+ "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
+ " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
+ " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
+ " AND #{join_alias}.custom_field_id = #{id}" +
+ " AND #{join_alias}.value <> ''" +
+ " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
+ " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
+ " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
+ " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)"
+ when 'string', 'text', 'list', 'date', 'bool'
+ "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
+ " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
+ " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
+ " AND #{join_alias}.custom_field_id = #{id}" +
+ " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
+ " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
+ " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
+ " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)"
else
nil
end