##// END OF EJS Templates
SQL error with MySQL (#19657)....
Jean-Philippe Lang -
r14251:8eb1e94f4d18
parent child
Show More
@@ -1,168 +1,173
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Enumeration < ActiveRecord::Base
19 19 include Redmine::SubclassFactory
20 20
21 21 default_scope lambda {order(:position)}
22 22
23 23 belongs_to :project
24 24
25 25 acts_as_list :scope => 'type = \'#{type}\' AND #{parent_id ? "parent_id = #{parent_id}" : "parent_id IS NULL"}'
26 26 acts_as_customizable
27 27 acts_as_tree
28 28
29 29 before_destroy :check_integrity
30 30 before_save :check_default
31 31
32 32 attr_protected :type
33 33
34 34 validates_presence_of :name
35 35 validates_uniqueness_of :name, :scope => [:type, :project_id]
36 36 validates_length_of :name, :maximum => 30
37 37
38 38 scope :shared, lambda { where(:project_id => nil) }
39 39 scope :sorted, lambda { order(:position) }
40 40 scope :active, lambda { where(:active => true) }
41 41 scope :system, lambda { where(:project_id => nil) }
42 42 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
43 43
44 44 def self.default
45 45 # Creates a fake default scope so Enumeration.default will check
46 46 # it's type. STI subclasses will automatically add their own
47 47 # types to the finder.
48 48 if self.descends_from_active_record?
49 49 where(:is_default => true, :type => 'Enumeration').first
50 50 else
51 51 # STI classes are
52 52 where(:is_default => true).first
53 53 end
54 54 end
55 55
56 56 # Overloaded on concrete classes
57 57 def option_name
58 58 nil
59 59 end
60 60
61 61 def check_default
62 62 if is_default? && is_default_changed?
63 63 Enumeration.where({:type => type}).update_all({:is_default => false})
64 64 end
65 65 end
66 66
67 67 # Overloaded on concrete classes
68 68 def objects_count
69 69 0
70 70 end
71 71
72 72 def in_use?
73 73 self.objects_count != 0
74 74 end
75 75
76 76 # Is this enumeration overriding a system level enumeration?
77 77 def is_override?
78 78 !self.parent.nil?
79 79 end
80 80
81 81 alias :destroy_without_reassign :destroy
82 82
83 83 # Destroy the enumeration
84 84 # If a enumeration is specified, objects are reassigned
85 85 def destroy(reassign_to = nil)
86 86 if reassign_to && reassign_to.is_a?(Enumeration)
87 87 self.transfer_relations(reassign_to)
88 88 end
89 89 destroy_without_reassign
90 90 end
91 91
92 92 def <=>(enumeration)
93 93 position <=> enumeration.position
94 94 end
95 95
96 96 def to_s; name end
97 97
98 98 # Returns the Subclasses of Enumeration. Each Subclass needs to be
99 99 # required in development mode.
100 100 #
101 101 # Note: subclasses is protected in ActiveRecord
102 102 def self.get_subclasses
103 103 subclasses
104 104 end
105 105
106 106 # Does the +new+ Hash override the previous Enumeration?
107 107 def self.overriding_change?(new, previous)
108 108 if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
109 109 return false
110 110 else
111 111 return true
112 112 end
113 113 end
114 114
115 115 # Does the +new+ Hash have the same custom values as the previous Enumeration?
116 116 def self.same_custom_values?(new, previous)
117 117 previous.custom_field_values.each do |custom_value|
118 118 if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
119 119 return false
120 120 end
121 121 end
122 122
123 123 return true
124 124 end
125 125
126 126 # Are the new and previous fields equal?
127 127 def self.same_active_state?(new, previous)
128 128 new = (new == "1" ? true : false)
129 129 return new == previous
130 130 end
131 131
132 132 # Overrides acts_as_list reset_positions_in_list so that enumeration overrides
133 133 # get the same position as the overriden enumeration
134 134 def reset_positions_in_list
135 135 super
136 self.class.
137 where("parent_id IS NOT NULL").
138 update_all("position = (SELECT MIN(position) FROM #{self.class.table_name} p WHERE p.id = #{self.class.table_name}.parent_id)")
136 # TODO: no database specific statement
137 if Redmine::Database.mysql?
138 self.class.connection.execute("UPDATE #{self.class.table_name} c JOIN #{self.class.table_name} p on p.id = c.parent_id SET c.position = p.position")
139 else
140 self.class.
141 where("parent_id IS NOT NULL").
142 update_all("position = (SELECT MIN(position) FROM #{self.class.table_name} p WHERE p.id = #{self.class.table_name}.parent_id)")
143 end
139 144 end
140 145
141 146 private
142 147 def check_integrity
143 148 raise "Cannot delete enumeration" if self.in_use?
144 149 end
145 150
146 151 # Overrides acts_as_list add_to_list_bottom so that enumeration overrides
147 152 # get the same position as the overriden enumeration
148 153 def add_to_list_bottom
149 154 if parent
150 155 self[position_column] = parent.position
151 156 else
152 157 super
153 158 end
154 159 end
155 160
156 161 # Overrides acts_as_list remove_from_list so that enumeration overrides
157 162 # get the same position as the overriden enumeration
158 163 def remove_from_list
159 164 if parent_id.blank?
160 165 super
161 166 end
162 167 end
163 168 end
164 169
165 170 # Force load the subclasses in development mode
166 171 require_dependency 'time_entry_activity'
167 172 require_dependency 'document_category'
168 173 require_dependency 'issue_priority'
@@ -1,68 +1,73
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 # Helper module to get information about the Redmine database
20 20 module Database
21 21 class << self
22 22 # Returns true if the database is PostgreSQL
23 23 def postgresql?
24 24 (ActiveRecord::Base.connection.adapter_name =~ /postgresql/i).present?
25 25 end
26 26
27 27 # Returns the PostgreSQL version or nil if another DBMS is used
28 28 def postgresql_version
29 29 postgresql? ? ActiveRecord::Base.connection.send(:postgresql_version) : nil
30 30 end
31 31
32 32 # Returns true if the database is a PostgreSQL >=9.0 database with the unaccent extension installed
33 33 def postgresql_unaccent?
34 34 if postgresql?
35 35 return @postgresql_unaccent unless @postgresql_unaccent.nil?
36 36 begin
37 37 sql = "SELECT name FROM pg_available_extensions WHERE installed_version IS NOT NULL and name = 'unaccent'"
38 38 @postgresql_unaccent = postgresql_version >= 90000 && ActiveRecord::Base.connection.select_value(sql).present?
39 39 rescue
40 40 false
41 41 end
42 42 else
43 43 false
44 44 end
45 45 end
46 46
47 # Returns true if the database is MySQL
48 def mysql?
49 (ActiveRecord::Base.connection.adapter_name =~ /mysql/i).present?
50 end
51
47 52 # Returns a SQL statement for case/accent (if possible) insensitive match
48 53 def like(left, right, options={})
49 54 neg = (options[:match] == false ? 'NOT ' : '')
50 55
51 56 if postgresql?
52 57 if postgresql_unaccent?
53 58 "unaccent(#{left}) #{neg}ILIKE unaccent(#{right})"
54 59 else
55 60 "#{left} #{neg}ILIKE #{right}"
56 61 end
57 62 else
58 63 "#{left} #{neg}LIKE #{right}"
59 64 end
60 65 end
61 66
62 67 # Resets database information
63 68 def reset
64 69 @postgresql_unaccent = nil
65 70 end
66 71 end
67 72 end
68 73 end
General Comments 0
You need to be logged in to leave comments. Login now