Module: ActiveRecord::Extensions::ConnectionAdapters::MysqlAdapter
- Defined in:
- lib/ar-extensions/import/mysql_generic.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#sql_for_on_duplicate_key_update(table_name, *args) ⇒ Object
Returns a generated ON DUPLICATE KEY UPDATE statement given the passed in
args
. -
#sql_for_on_duplicate_key_update_as_array(table_name, arr) ⇒ Object
:nodoc:.
-
#sql_for_on_duplicate_key_update_as_hash(table_name, hsh) ⇒ Object
:nodoc:.
Methods included from Import::OnDuplicateKeyUpdateSupport
#supports_on_duplicate_key_update?
Methods included from Import::ImportSupport
Instance Method Details
#sql_for_on_duplicate_key_update(table_name, *args) ⇒ Object
Returns a generated ON DUPLICATE KEY UPDATE statement given the passed in args
.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ar-extensions/import/mysql_generic.rb', line 8 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 |
#sql_for_on_duplicate_key_update_as_array(table_name, arr) ⇒ Object
:nodoc:
23 24 25 26 27 28 29 |
# File 'lib/ar-extensions/import/mysql_generic.rb', line 23 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 |
#sql_for_on_duplicate_key_update_as_hash(table_name, hsh) ⇒ Object
:nodoc:
31 32 33 34 35 36 37 38 39 |
# File 'lib/ar-extensions/import/mysql_generic.rb', line 31 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 |