Providing support for the Mysql2 adapter.
This commit is contained in:
parent
f61eabda19
commit
b6c72f8456
11 changed files with 192 additions and 160 deletions
|
@ -0,0 +1,6 @@
|
|||
require "active_record/connection_adapters/mysql2_adapter"
|
||||
require "activerecord-import/active_record/adapters/mysql_base"
|
||||
|
||||
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
||||
include ActiveRecord::ConnectionAdapters::MysqlBase
|
||||
end
|
|
@ -1,47 +1,6 @@
|
|||
require "active_record/connection_adapters/mysql_adapter"
|
||||
require "activerecord-import/active_record/adapters/mysql_base"
|
||||
|
||||
class ActiveRecord::ConnectionAdapters::MysqlAdapter
|
||||
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
|
||||
|
||||
include ActiveRecord::ConnectionAdapters::MysqlBase
|
||||
end
|
||||
|
|
48
lib/activerecord-import/active_record/adapters/mysql_base.rb
Normal file
48
lib/activerecord-import/active_record/adapters/mysql_base.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
module ActiveRecord::ConnectionAdapters::MysqlBase
|
||||
def self.included(klass)
|
||||
klass.instance_eval do
|
||||
include ActiveRecord::Extensions::Import::ImportSupport
|
||||
include ActiveRecord::Extensions::Import::OnDuplicateKeyUpdateSupport
|
||||
end
|
||||
end
|
||||
|
||||
# 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
|
2
lib/activerecord-import/mysql2.rb
Normal file
2
lib/activerecord-import/mysql2.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
require File.join File.dirname(__FILE__), "base"
|
||||
ActiveRecord::Extensions.require_adapter "mysql2"
|
Loading…
Add table
Add a link
Reference in a new issue