lib/arel/engines/sql/relations/writes.rb in arel-0.1.2 vs lib/arel/engines/sql/relations/writes.rb in arel-0.2.pre

- old
+ new

@@ -2,22 +2,33 @@ class Deletion < Compound def to_sql build_query \ "DELETE", "FROM #{table_sql}", - ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), + ("WHERE #{wheres.collect(&:to_sql).join(' AND ')}" unless wheres.blank? ), ("LIMIT #{taken}" unless taken.blank? ) end end class Insert < Compound def to_sql insertion_attributes_values_sql = if record.is_a?(Value) record.value else - build_query "(#{record.keys.collect { |key| engine.quote_column_name(key.name) }.join(', ')})", - "VALUES (#{record.collect { |key, value| key.format(value) }.join(', ')})" + attributes = record.keys.sort_by do |attribute| + attribute.name.to_s + end + + first = attributes.collect do |key| + engine.quote_column_name(key.name) + end.join(', ') + + second = attributes.collect do |key| + key.format(record[key]) + end.join(', ') + + build_query "(#{first})", "VALUES (#{second})" end build_query \ "INSERT", "INTO #{table_sql}", @@ -35,20 +46,25 @@ protected def assignment_sql if assignments.respond_to?(:collect) - assignments.collect do |attribute, value| + attributes = assignments.keys.sort_by do |attribute| + attribute.name.to_s + end + + attributes.map do |attribute| + value = assignments[attribute] "#{engine.quote_column_name(attribute.name)} = #{attribute.format(value)}" - end.join(",\n") + end.join(", ") else assignments.value end end def build_update_conditions_sql conditions = "" - conditions << " WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? + conditions << " WHERE #{wheres.collect(&:to_sql).join(' AND ')}" unless wheres.blank? conditions << " ORDER BY #{order_clauses.join(', ')}" unless orders.blank? unless taken.blank? conditions << " LIMIT #{taken}"