@@ -648,7 +648,7 class Query < ActiveRecord::Base | |||
|
648 | 648 | if value.any? |
|
649 | 649 | case type_for(field) |
|
650 | 650 | when :date, :date_past |
|
651 | sql = date_clause(db_table, db_field, parse_date(value.first), parse_date(value.first)) | |
|
651 | sql = date_clause(db_table, db_field, parse_date(value.first), parse_date(value.first), is_custom_filter) | |
|
652 | 652 | when :integer |
|
653 | 653 | if is_custom_filter |
|
654 | 654 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) = #{value.first.to_i})" |
@@ -683,7 +683,7 class Query < ActiveRecord::Base | |||
|
683 | 683 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter |
|
684 | 684 | when ">=" |
|
685 | 685 | if [:date, :date_past].include?(type_for(field)) |
|
686 | sql = date_clause(db_table, db_field, parse_date(value.first), nil) | |
|
686 | sql = date_clause(db_table, db_field, parse_date(value.first), nil, is_custom_filter) | |
|
687 | 687 | else |
|
688 | 688 | if is_custom_filter |
|
689 | 689 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) >= #{value.first.to_f})" |
@@ -693,7 +693,7 class Query < ActiveRecord::Base | |||
|
693 | 693 | end |
|
694 | 694 | when "<=" |
|
695 | 695 | if [:date, :date_past].include?(type_for(field)) |
|
696 | sql = date_clause(db_table, db_field, nil, parse_date(value.first)) | |
|
696 | sql = date_clause(db_table, db_field, nil, parse_date(value.first), is_custom_filter) | |
|
697 | 697 | else |
|
698 | 698 | if is_custom_filter |
|
699 | 699 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) <= #{value.first.to_f})" |
@@ -703,7 +703,7 class Query < ActiveRecord::Base | |||
|
703 | 703 | end |
|
704 | 704 | when "><" |
|
705 | 705 | if [:date, :date_past].include?(type_for(field)) |
|
706 | sql = date_clause(db_table, db_field, parse_date(value[0]), parse_date(value[1])) | |
|
706 | sql = date_clause(db_table, db_field, parse_date(value[0]), parse_date(value[1]), is_custom_filter) | |
|
707 | 707 | else |
|
708 | 708 | if is_custom_filter |
|
709 | 709 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})" |
@@ -717,64 +717,64 class Query < ActiveRecord::Base | |||
|
717 | 717 | sql = "#{queried_table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{self.class.connection.quoted_true})" if field == "status_id" |
|
718 | 718 | when "><t-" |
|
719 | 719 | # between today - n days and today |
|
720 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0) | |
|
720 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0, is_custom_filter) | |
|
721 | 721 | when ">t-" |
|
722 | 722 | # >= today - n days |
|
723 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, nil) | |
|
723 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, nil, is_custom_filter) | |
|
724 | 724 | when "<t-" |
|
725 | 725 | # <= today - n days |
|
726 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i) | |
|
726 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i, is_custom_filter) | |
|
727 | 727 | when "t-" |
|
728 | 728 | # = n days in past |
|
729 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i) | |
|
729 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i, is_custom_filter) | |
|
730 | 730 | when "><t+" |
|
731 | 731 | # between today and today + n days |
|
732 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i) | |
|
732 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i, is_custom_filter) | |
|
733 | 733 | when ">t+" |
|
734 | 734 | # >= today + n days |
|
735 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil) | |
|
735 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil, is_custom_filter) | |
|
736 | 736 | when "<t+" |
|
737 | 737 | # <= today + n days |
|
738 | sql = relative_date_clause(db_table, db_field, nil, value.first.to_i) | |
|
738 | sql = relative_date_clause(db_table, db_field, nil, value.first.to_i, is_custom_filter) | |
|
739 | 739 | when "t+" |
|
740 | 740 | # = today + n days |
|
741 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i) | |
|
741 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i, is_custom_filter) | |
|
742 | 742 | when "t" |
|
743 | 743 | # = today |
|
744 | sql = relative_date_clause(db_table, db_field, 0, 0) | |
|
744 | sql = relative_date_clause(db_table, db_field, 0, 0, is_custom_filter) | |
|
745 | 745 | when "ld" |
|
746 | 746 | # = yesterday |
|
747 | sql = relative_date_clause(db_table, db_field, -1, -1) | |
|
747 | sql = relative_date_clause(db_table, db_field, -1, -1, is_custom_filter) | |
|
748 | 748 | when "w" |
|
749 | 749 | # = this week |
|
750 | 750 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
751 | 751 | day_of_week = Date.today.cwday |
|
752 | 752 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
753 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6) | |
|
753 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6, is_custom_filter) | |
|
754 | 754 | when "lw" |
|
755 | 755 | # = last week |
|
756 | 756 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
757 | 757 | day_of_week = Date.today.cwday |
|
758 | 758 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
759 | sql = relative_date_clause(db_table, db_field, - days_ago - 7, - days_ago - 1) | |
|
759 | sql = relative_date_clause(db_table, db_field, - days_ago - 7, - days_ago - 1, is_custom_filter) | |
|
760 | 760 | when "l2w" |
|
761 | 761 | # = last 2 weeks |
|
762 | 762 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
763 | 763 | day_of_week = Date.today.cwday |
|
764 | 764 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
765 | sql = relative_date_clause(db_table, db_field, - days_ago - 14, - days_ago - 1) | |
|
765 | sql = relative_date_clause(db_table, db_field, - days_ago - 14, - days_ago - 1, is_custom_filter) | |
|
766 | 766 | when "m" |
|
767 | 767 | # = this month |
|
768 | 768 | date = Date.today |
|
769 | sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month) | |
|
769 | sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month, is_custom_filter) | |
|
770 | 770 | when "lm" |
|
771 | 771 | # = last month |
|
772 | 772 | date = Date.today.prev_month |
|
773 | sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month) | |
|
773 | sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month, is_custom_filter) | |
|
774 | 774 | when "y" |
|
775 | 775 | # = this year |
|
776 | 776 | date = Date.today |
|
777 | sql = date_clause(db_table, db_field, date.beginning_of_year, date.end_of_year) | |
|
777 | sql = date_clause(db_table, db_field, date.beginning_of_year, date.end_of_year, is_custom_filter) | |
|
778 | 778 | when "~" |
|
779 | 779 | sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{self.class.connection.quote_string(value.first.to_s.downcase)}%'" |
|
780 | 780 | when "!~" |
@@ -829,8 +829,18 class Query < ActiveRecord::Base | |||
|
829 | 829 | end |
|
830 | 830 | end |
|
831 | 831 | |
|
832 | def quoted_time(time, is_custom_filter) | |
|
833 | if is_custom_filter | |
|
834 | # Custom field values are stored as strings in the DB | |
|
835 | # using this format that does not depend on DB date representation | |
|
836 | time.strftime("%Y-%m-%d %H:%M:%S") | |
|
837 | else | |
|
838 | self.class.connection.quoted_date(time) | |
|
839 | end | |
|
840 | end | |
|
841 | ||
|
832 | 842 | # Returns a SQL clause for a date or datetime field. |
|
833 | def date_clause(table, field, from, to) | |
|
843 | def date_clause(table, field, from, to, is_custom_filter) | |
|
834 | 844 | s = [] |
|
835 | 845 | if from |
|
836 | 846 | if from.is_a?(Date) |
@@ -841,7 +851,7 class Query < ActiveRecord::Base | |||
|
841 | 851 | if self.class.default_timezone == :utc |
|
842 | 852 | from = from.utc |
|
843 | 853 | end |
|
844 |
s << ("#{table}.#{field} > '%s'" % [ |
|
|
854 | s << ("#{table}.#{field} > '%s'" % [quoted_time(from, is_custom_filter)]) | |
|
845 | 855 | end |
|
846 | 856 | if to |
|
847 | 857 | if to.is_a?(Date) |
@@ -850,14 +860,14 class Query < ActiveRecord::Base | |||
|
850 | 860 | if self.class.default_timezone == :utc |
|
851 | 861 | to = to.utc |
|
852 | 862 | end |
|
853 |
s << ("#{table}.#{field} <= '%s'" % [ |
|
|
863 | s << ("#{table}.#{field} <= '%s'" % [quoted_time(to, is_custom_filter)]) | |
|
854 | 864 | end |
|
855 | 865 | s.join(' AND ') |
|
856 | 866 | end |
|
857 | 867 | |
|
858 | 868 | # Returns a SQL clause for a date or datetime field using relative dates. |
|
859 | def relative_date_clause(table, field, days_from, days_to) | |
|
860 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil)) | |
|
869 | def relative_date_clause(table, field, days_from, days_to, is_custom_filter) | |
|
870 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil), is_custom_filter) | |
|
861 | 871 | end |
|
862 | 872 | |
|
863 | 873 | # Returns a Date or Time from the given filter value |
General Comments 0
You need to be logged in to leave comments.
Login now