forked from zdennis/activerecord-import
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up module usage. There's no need to use InstanceMethods -- norm…
…al Ruby modules and includes will work fine here.
- Loading branch information
Showing
7 changed files
with
57 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,53 @@ | ||
module ActiveRecord::Import::MysqlAdapter | ||
module InstanceMethods | ||
def self.included(klass) | ||
klass.instance_eval do | ||
include ActiveRecord::Import::ImportSupport | ||
include ActiveRecord::Import::OnDuplicateKeyUpdateSupport | ||
end | ||
end | ||
|
||
# Returns the maximum number of bytes that the server will allow | ||
# in a single packet | ||
def max_allowed_packet # :nodoc: | ||
result = execute( "SHOW VARIABLES like 'max_allowed_packet';" ) | ||
# original Mysql gem responds to #fetch_row while Mysql2 responds to #first | ||
val = result.respond_to?(:fetch_row) ? result.fetch_row[1] : result.first[1] | ||
val.to_i | ||
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 | ||
include ActiveRecord::Import::ImportSupport | ||
include ActiveRecord::Import::OnDuplicateKeyUpdateSupport | ||
|
||
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 | ||
# Returns the maximum number of bytes that the server will allow | ||
# in a single packet | ||
def max_allowed_packet # :nodoc: | ||
result = execute( "SHOW VARIABLES like 'max_allowed_packet';" ) | ||
# original Mysql gem responds to #fetch_row while Mysql2 responds to #first | ||
val = result.respond_to?(:fetch_row) ? result.fetch_row[1] : result.first[1] | ||
val.to_i | ||
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( ',') | ||
# 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 | ||
|
||
#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') | ||
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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
module ActiveRecord::Import::PostgreSQLAdapter | ||
module InstanceMethods | ||
def self.included(klass) | ||
klass.instance_eval do | ||
include ActiveRecord::Import::ImportSupport | ||
end | ||
end | ||
include ActiveRecord::Import::ImportSupport | ||
|
||
def next_value_for_sequence(sequence_name) | ||
%{nextval('#{sequence_name}')} | ||
end | ||
def next_value_for_sequence(sequence_name) | ||
%{nextval('#{sequence_name}')} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
module ActiveRecord::Import::Sqlite3Adapter | ||
module InstanceMethods | ||
def next_value_for_sequence(sequence_name) | ||
%{nextval('#{sequence_name}')} | ||
end | ||
def next_value_for_sequence(sequence_name) | ||
%{nextval('#{sequence_name}')} | ||
end | ||
end |