Ticket #1391: single_file.diff
| File single_file.diff, 8.5 kB (added by wishdev@gmail.com, 3 years ago) |
|---|
-
activerecord/lib/active_record/validations.rb
old new 469 473 470 474 if scope = configuration[:scope] 471 475 validates_each(attr_names,configuration) do |record, attr_name, value| 472 record.errors.add(attr_name, configuration[:message]) if record.class.find_first(record.new_record? ? ["#{attr_name} = ? AND #{scope} = ?", record.send(attr_name), record.send(scope)] : ["#{attr_name} = ? AND #{record.class.primary_key} <> ? AND #{scope} = ?", record.send(attr_name), record.send(:id), record.send(scope)]) 473 end 476 if record.class.find_first(record.new_record? ? 477 ["#{attr_name} " + 478 (record.send(attr_name).nil? ? "IS" : "=") + 479 " ? AND #{scope} " + 480 (record.send(scope).nil? ? "IS" : "=") + 481 " ?", 482 record.send(attr_name), record.send(scope)] : 483 ["#{attr_name} " + 484 (record.send(attr_name).nil? ? "IS" : "=") + 485 " ? AND #{record.class.primary_key} " + 486 (record.send(:id).nil? ? "IS NOT" : "<>") + 487 " ? AND #{scope} " + 488 (record.send(scope).nil? ? "IS" : "=") + 489 " ?", 490 record.send(attr_name), record.send(:id), record.send(scope)]) 491 record.errors.add(attr_name, configuration[:message]) 492 end 493 end 474 494 else 475 495 validates_each(attr_names,configuration) do |record, attr_name, value| 476 496 record.errors.add(attr_name, configuration[:message]) if record.class.find_first(record.new_record? ? ["#{attr_name} = ?", record.send(attr_name)] : ["#{attr_name} = ? AND #{record.class.primary_key} <> ?", record.send(attr_name), record.send(:id) ] ) -
activerecord/lib/active_record/associations/association_proxy.rb
old new 51 51 end 52 52 53 53 def quoted_record_ids(records) 54 records.map { |record| record.quoted_id }.join(',') 54 ary = records.map { |record| record.quoted_id }.reject { |key| key == "NULL" } 55 [ ary.join(','), ary.length != records.length ] 55 56 end 56 57 57 58 def interpolate_sql_options!(options, *keys) -
activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
old new 139 139 records.each { |record| @owner.connection.execute(interpolate_sql(sql, record)) } 140 140 else 141 141 ids = quoted_record_ids(records) 142 sql = "DELETE FROM #{@join_table} WHERE #{@association_class_primary_key_name} = #{@owner.quoted_id} AND #{@association_foreign_key} IN (#{ids})" 143 @owner.connection.execute(sql) 144 end 145 end 142 sql = "DELETE FROM #{@join_table} WHERE " 143 sql << "#{@association_class_primary_key_name} " 144 if @owner.quoted_id == "NULL" 145 sql << "IS NULL" 146 else 147 sql << "= #{@owner.quoted_id}" 148 end 149 sql << " AND (" 150 null_ids = ids[1] 151 if (null_ids) 152 sql << "#{@association_foreign_key} IS NULL" 153 if ids[0].length > 0 154 sql << " OR " 155 end 156 end 157 if ids[0].length > 0 158 sql << "#{@association_foreign_key} IN (#{ids[0]})" 159 end 160 sql << ")" 161 @owner.connection.execute(sql) 162 end 163 end 146 164 147 165 def construct_sql 148 166 interpolate_sql_options!(@options, :finder_sql) -
activerecord/lib/active_record/associations/has_one_association.rb
old new 46 46 end 47 47 48 48 def construct_sql 49 @finder_sql = "#{@association_class.table_name}.#{@association_class_primary_key_name} =#{@owner.quoted_id}#{@options[:conditions] ? " AND " + @options[:conditions] : ""}"49 @finder_sql = "#{@association_class.table_name}.#{@association_class_primary_key_name} #{@owner.quoted_id == "NULL" ? "IS " : " = "}#{@owner.quoted_id}#{@options[:conditions] ? " AND " + @options[:conditions] : ""}" 50 50 end 51 51 end 52 52 end -
activerecord/lib/active_record/associations/has_many_association.rb
old new 122 122 if @options[:dependent] 123 123 records.each { |r| r.destroy } 124 124 else 125 ids = quoted_record_ids(records) 126 @association_class.update_all( 127 "#{@association_class_primary_key_name} = NULL", 128 "#{@association_class_primary_key_name} = #{@owner.quoted_id} AND #{@association_class.primary_key} IN (#{ids})" 129 ) 130 end 131 end 125 ids = quoted_record_ids(records) 126 conditions = "#{@association_class_primary_key_name} " 127 if @owner.quoted_id == "NULL" 128 conditions << "IS NULL" 129 else 130 conditions << "= #{@owner.quoted_id}" 131 end 132 conditions << " AND (" 133 null_ids = ids[1] 134 if (null_ids) 135 conditions << "#{@association_class.primary_key} IS NULL" 136 if ids[0].length > 0 137 conditions << " OR " 138 end 139 end 140 if ids[0].length > 0 141 conditions << "#{@association_class.primary_key} IN (#{ids[0]})" 142 end 143 conditions << ")" 144 @association_class.update_all("#{@association_class_primary_key_name} = NULL", conditions) 145 end 146 end 132 147 133 148 def target_obsolete? 134 149 false … … 138 153 if @options[:finder_sql] 139 154 @finder_sql = interpolate_sql(@options[:finder_sql]) 140 155 else 141 @finder_sql = "#{@association_class.table_name}.#{@association_class_primary_key_name} = #{@owner.quoted_id}" 142 @finder_sql << " AND #{interpolate_sql(@conditions)}" if @conditions 156 @finder_sql = "#{@association_class_primary_key_name} = #{@owner.quoted_id}" 157 @finder_sql = "#{@association_class.table_name}.#{@association_class_primary_key_name}" 158 if @owner.quoted_id == "NULL" 159 @finder_sql << " IS NULL" 160 else 161 @finder_sql << " = #{@owner.quoted_id}" 162 end 163 @finder_sql << " AND #{interpolate_sql(@conditions)}" if @conditions 143 164 end 144 165 145 166 if @options[:counter_sql] -
activerecord/lib/active_record/base.rb
old new 970 975 # be made (since they can't be persisted). 971 976 def destroy 972 977 unless new_record? 973 connection.delete( 974 "DELETE FROM #{self.class.table_name} " + 975 "WHERE #{self.class.primary_key} = #{quote(id)}", 976 "#{self.class.name} Destroy" 977 ) 978 sql = "DELETE FROM #{self.class.table_name} " 979 quoted_id = quote(id) 980 if (quoted_id == "NULL") 981 sql << "WHERE #{self.class.primary_key} IS NULL" 982 else 983 sql << "WHERE #{self.class.primary_key} = #{quote(id)}" 984 end 985 connection.delete(sql, "#{self.class.name} Destroy") 978 986 end 979 987 980 988 freeze … … 1144 1152 1145 1153 # Updates the associated record with values matching those of the instant attributes. 1146 1154 def update 1147 connection.update( 1148 "UPDATE #{self.class.table_name} " + 1149 "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " + 1150 "WHERE #{self.class.primary_key} = #{quote(id)}", 1151 "#{self.class.name} Update" 1152 ) 1155 quoted_id = quote(id) 1156 sql = "UPDATE #{self.class.table_name} " + 1157 "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " + 1158 "WHERE #{self.class.primary_key}#{quoted_id != "NULL" ? " = " : " IS "}#{quote(id)}" 1159 connection.update(sql, "#{self.class.name} Update") 1153 1160 end 1154 1161 1155 1162 # Creates a new record with values matching those of the instant attributes.