Sha256: e81c2a99f72d5347cd728bae3b8b0f9e750c619cd6055fa0a904f4bac86afdc6

Contents?: true

Size: 1.66 KB

Versions: 9

Compression:

Stored size: 1.66 KB

Contents

module ActiveRecord::Extensions::ConnectionAdapters::MysqlAdapter # :nodoc:

  include ActiveRecord::Extensions::Import::ImportSupport  
  include ActiveRecord::Extensions::Import::OnDuplicateKeyUpdateSupport
    
  # Returns a generated ON DUPLICATE KEY UPDATE statement given the passed
  # in +args+. 
  def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:
    sql = ' ON DUPLICATE KEY UPDATE '
    arg = args.first
    if arg.is_a?( Array )
      sql << sql_for_on_duplicate_key_update_as_array( table_name, arg )
    elsif arg.is_a?( Hash )
      sql << sql_for_on_duplicate_key_update_as_hash( table_name, arg )
    elsif arg.is_a?( String )
      sql << arg
    else
      raise ArgumentError.new( "Expected Array or Hash" )
    end
    sql
  end

  def sql_for_on_duplicate_key_update_as_array( table_name, arr )  # :nodoc:
    results = arr.map do |column|
      qc = quote_column_name( column )
      "#{table_name}.#{qc}=VALUES(#{qc})"        
    end
    results.join( ',' )
  end
  
  def sql_for_on_duplicate_key_update_as_hash( table_name, hsh ) # :nodoc:
    sql = ' ON DUPLICATE KEY UPDATE '
    results = hsh.map do |column1, column2|
      qc1 = quote_column_name( column1 )
      qc2 = quote_column_name( column2 )
      "#{table_name}.#{qc1}=VALUES( #{qc2} )"
    end
    results.join( ',')
  end

  #return true if the statement is a duplicate key record error
  def duplicate_key_update_error?(exception)# :nodoc:
    exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('Duplicate entry')
  end

end

ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
  include ActiveRecord::Extensions::ConnectionAdapters::MysqlAdapter
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
ar-extensions-0.9.5 lib/ar-extensions/import/mysql.rb
ar-extensions-0.9.4 lib/ar-extensions/import/mysql.rb
ghazel-ar-extensions-0.9.3.1 lib/ar-extensions/import/mysql.rb
ghazel-ar-extensions-0.9.3 lib/ar-extensions/import/mysql.rb
ar-extensions-0.9.3 lib/ar-extensions/import/mysql.rb
Empact-ar-extensions-0.9.2 lib/ar-extensions/import/mysql.rb
ar-extensions-0.9.2 lib/ar-extensions/import/mysql.rb
ar-extensions-0.9.1 lib/ar-extensions/import/mysql.rb
ar-extensions-0.9.0 lib/ar-extensions/import/mysql.rb