anchor.diff:  <% reference_index = 0 -%>
       final String sql =
         "INSERT INTO <%= entity.sync.master_entity.sql.qualified_table_name %>(<%= entity.sync.master_entity.attribute_by_name(:MasterSynchronized).sql.quoted_column_name %>, <%= entity.sync.master_entity.attribute_by_name(:CreatedAt).sql.quoted_column_name %>, <%= entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name %>, <%= attr_to_sync.collect{|a| a.sql.quoted_column_name }.join(', ') %>) " +
  -      " SELECT 0, ?, T.<%= entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name %>, <%= attr_to_sync.collect{|a| a.reference? ? (reference_index += 1; "R#{reference_index}.#{a.referenced_entity.primary_key.sql.quoted_column_name}") : "T.#{a.sql.quoted_column_name}" }.join(', ') %> " +
  +      " SELECT 0, ?, T.<%= entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name %>, <%= attr_to_sync.collect{|a|
  +if a.reference?
  +  reference_index += 1
  +  col_name = entity.sync.master_entity.attribute_by_name(a.name).referenced_entity.primary_key.sql.quoted_column_name
  +  ref_prefix = "R#{reference_index}"
  +  if a.referenced_entity.abstract?
  +    s = a.referenced_entity.subtypes.select{|a| !a.abstract? }.size
  +    cols = (0...(s)).collect do |index|
  +      "#{ref_prefix}#{index}.#{col_name}"
  +    end.join(", ")
  +    "COALESCE(#{cols})"
  +  else
  +    "#{ref_prefix}.#{col_name}"
  +  end
  +else
  +  "T.#{entity.sync.sync_temp_entity.attribute_by_name(a.name).sql.quoted_column_name}"
  +end
  +  }.join(', ') %> " +
         " FROM <%= entity.sync.sync_temp_entity.sql.qualified_table_name %> T " +
         " LEFT JOIN <%= entity.sync.master_entity.sql.qualified_table_name %> M ON " +
         "     M.<%= entity.sync.master_entity.attribute_by_name(:DeletedAt).sql.quoted_column_name %> IS NULL AND " +
  
positive.code: <% reference_index = 0 -%>
      final String sql =
        "UPDATE M " +
        " SET <%= entity.sync.master_entity.attribute_by_name(:MasterSynchronized).sql.quoted_column_name %> = 0, <%= attr_to_sync.collect{|a| "#{entity.sync.master_entity.attribute_by_name(a.name).sql.quoted_column_name} = #{a.reference? ? (reference_index += 1; "R#{reference_index}.#{entity.sync.master_entity.attribute_by_name(a.name).referenced_entity.primary_key.sql.quoted_column_name}") : "T.#{entity.sync.sync_temp_entity.attribute_by_name(a.name).sql.quoted_column_name}"}" }.join(', ') %> " +
        " FROM <%= entity.sync.master_entity.sql.qualified_table_name %> M " +
        " JOIN <%= entity.sync.sync_temp_entity.sql.qualified_table_name %> T ON M.<%= entity.sync.master_entity.attribute_by_name(:MappingSource).sql.quoted_column_name %> = T.<%= entity.sync.sync_temp_entity.attribute_by_name(:MappingSource).sql.quoted_column_name %> AND M.<%= entity.sync.master_entity.attribute_by_name(:MappingID).sql.quoted_column_name %> = T.<%= entity.sync.sync_temp_entity.attribute_by_name(:MappingID).sql.quoted_column_name %> " +
  <% reference_index = 0 -%>
  
negative.code: <%
    reference_index = 0
    attr_to_sync.each do |a|
     reference_value = if a.reference?
        reference_index += 1
        col_name = entity.sync.master_entity.attribute_by_name(a.name).referenced_entity.primary_key.sql.quoted_column_name
        ref_prefix = "R#{reference_index}"
        if a.referenced_entity.abstract?
          s = a.referenced_entity.subtypes.select{|a| !a.abstract? }.size
          cols = (0...(s)).collect do |index|
            "#{ref_prefix}#{index}.#{col_name}"
          end.join(", ")
          "COALESCE(#{cols})"
        else
          "#{ref_prefix}.#{col_name}"
        end
      else
        "T.#{entity.sync.sync_temp_entity.attribute_by_name(a.name).sql.quoted_column_name}"
      end
  
      if a.nullable?
  -%>
        "     ( (<%= reference_value %> IS NULL AND M.<%= entity.sync.master_entity.attribute_by_name(a.name).sql.quoted_column_name %> IS NOT NULL) OR (<%= reference_value %> IS NOT NULL AND M.<%= entity.sync.master_entity.attribute_by_name(a.name).sql.quoted_column_name %> IS NULL) OR (<%= reference_value %> IS NOT NULL AND <%= reference_value %> != M.<%= entity.sync.master_entity.attribute_by_name(a.name).sql.quoted_column_name %>) ) OR " +
  