Ticket #6542: sql_creation_injection_points.diff
| File sql_creation_injection_points.diff, 4.5 kB (added by ymendel, 10 months ago) |
|---|
-
base.rb
old new 1461 1461 end 1462 1462 end 1463 1463 1464 def create_scope_to_object(object) #:nodoc: 1465 scope(:create).each { |att,value| object.send("#{att}=", value) } if scoped?(:create) 1466 end 1467 1464 1468 def thread_safe_scoped_methods #:nodoc: 1465 1469 scoped_methods = (Thread.current[:scoped_methods] ||= {}) 1466 1470 scoped_methods[self] ||= [] … … 1653 1657 @new_record = true 1654 1658 ensure_proper_type 1655 1659 self.attributes = attributes unless attributes.nil? 1656 self.class.send(: scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create)1660 self.class.send(:create_scope_to_object, self) 1657 1661 result = yield self if block_given? 1658 1662 callback(:after_initialize) if respond_to_without_attributes?(:after_initialize) 1659 1663 result … … 1711 1715 # be made (since they can't be persisted). 1712 1716 def destroy 1713 1717 unless new_record? 1714 connection.delete <<-end_sql, "#{self.class.name} Destroy" 1715 DELETE FROM #{self.class.table_name} 1716 WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quoted_id} 1717 end_sql 1718 connection.delete(sql_for_destroy, "#{self.class.name} Destroy") 1718 1719 end 1719 1720 1720 1721 freeze 1721 1722 end 1722 1723 1724 def sql_for_destroy #:nodoc: 1725 "DELETE FROM #{self.class.table_name} #{sql_for_destroy_conditions}" 1726 end 1727 1728 def sql_for_destroy_conditions #:nodoc: 1729 "WHERE #{self.class.primary_key} = #{quoted_id}" 1730 end 1731 1723 1732 # Returns a clone of the record that hasn't been assigned an id yet and 1724 1733 # is treated as a new record. Note that this is a "shallow" clone: 1725 1734 # it copies the object's attributes only, not its associations. … … 1951 1960 # Updates the associated record with values matching those of the instance attributes. 1952 1961 # Returns the number of affected rows. 1953 1962 def update 1954 connection.update( 1955 "UPDATE #{self.class.table_name} " + 1956 "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " + 1957 "WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quote_value(id)}", 1958 "#{self.class.name} Update" 1959 ) 1963 connection.update(sql_for_update, "#{self.class.name} Update") 1960 1964 end 1961 1965 1966 def sql_for_update #:nodoc: 1967 "UPDATE #{self.class.table_name} #{sql_for_update_values} #{sql_for_update_conditions}" 1968 end 1969 1970 def sql_for_update_values #:nodoc: 1971 "SET #{quoted_comma_pair_list(connection, attributes_for_update)}" 1972 end 1973 1974 def attributes_for_update #:nodoc: 1975 attributes_with_quotes(false) 1976 end 1977 1978 def sql_for_update_conditions #:nodoc: 1979 "WHERE #{self.class.primary_key} = #{quote_value(id)}" 1980 end 1981 1962 1982 # Creates a record with values matching those of the instance attributes 1963 1983 # and returns its id. 1964 1984 def create … … 1966 1986 self.id = connection.next_sequence_value(self.class.sequence_name) 1967 1987 end 1968 1988 1969 self.id = connection.insert( 1970 "INSERT INTO #{self.class.table_name} " + 1971 "(#{quoted_column_names.join(', ')}) " + 1972 "VALUES(#{attributes_with_quotes.values.join(', ')})", 1973 "#{self.class.name} Create", 1989 self.id = connection.insert(sql_for_create, "#{self.class.name} Create", 1974 1990 self.class.primary_key, self.id, self.class.sequence_name 1975 1991 ) 1976 1992 … … 1978 1994 id 1979 1995 end 1980 1996 1997 def sql_for_create #:nodoc: 1998 "INSERT INTO #{self.class.table_name} " + 1999 "(#{columns_for_create.join(', ')}) " + 2000 "VALUES(#{attributes_for_create.values.join(', ')})" 2001 end 2002 1981 2003 # Sets the attribute used for single table inheritance to this class name if this is not the ActiveRecord descendent. 1982 2004 # Considering the hierarchy Reply < Message < ActiveRecord, this makes it possible to do Reply.new without having to 1983 2005 # set Reply[Reply.inheritance_column] = "Reply" yourself. No such attribute would be set for objects of the … … 2117 2139 end 2118 2140 end 2119 2141 2142 alias_method :columns_for_create, :quoted_column_names 2143 alias_method :attributes_for_create, :attributes_with_quotes 2144 2120 2145 def quote_columns(quoter, hash) 2121 2146 hash.inject({}) do |quoted, (name, value)| 2122 2147 quoted[quoter.quote_column_name(name)] = value