Skip to content

Instantly share code, notes, and snippets.

@suya55
Created January 20, 2017 10:03
Show Gist options
  • Save suya55/b3d8d94d8bad80333672ccd8e63e1baf to your computer and use it in GitHub Desktop.
Save suya55/b3d8d94d8bad80333672ccd8e63e1baf to your computer and use it in GitHub Desktop.
create_on_duplicate_key_update
module ActiveRecord
# = Active Record Persistence
module Persistence
extend ActiveSupport::Concern
def create_on_duplicate_key_update!(keys)
#keys = duplicate_keys.is_a? Array ? duplicate_keys : [duplicate_keys] #TODO: 배열이 아닐때? 코드짜기.
keys.collect! { |k| k.to_s }
klass = self.class
attributes_with_values = arel_attributes_with_values_for_create(attribute_names)
column_hash = klass.connection.schema_cache.columns_hash klass.table_name
db_columns_with_values = []
attributes_with_values.map { |attr, value|
real_column = column_hash[attr.name]
db_columns_with_values << [real_column, value] if value.present?
if (value.blank? && ['created_at', 'updated_at'].include?(real_column.name))
now = DateTime.now
db_columns_with_values << [real_column, now]
self.send("#{attr.name}=",now)
end
}
sql = "INSERT INTO #{self.class.quoted_table_name} (#{db_columns_with_values.collect{|c| klass.connection.quote_column_name(c.first.name)}.join(', ')})"
sql << " VALUES (#{db_columns_with_values.collect{|c| klass.connection.quote(c.last)}.join(', ')}) "
sql << " ON DUPLICATE KEY UPDATE "
sql << sql_for_on_duplicate_key_update(keys,db_columns_with_values)
klass.connection.execute sql
end
private
def sql_for_on_duplicate_key_update(keys,db_columns_with_values)
connection = self.class.connection
results = []
db_columns_with_values.map do |column, value|
results << "#{self.class.quoted_table_name}.#{connection.quote_column_name(column.name)}=#{connection.quote(value, column)}" if (!keys.include?(column.name) && column.name != 'created_at')
end
results.join(',')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment