Merge pull request #38 from wycats/module_cleanup
Clean up module usage.
This commit is contained in:
commit
2539a71574
|
@ -2,5 +2,5 @@ require "active_record/connection_adapters/mysql2_adapter"
|
||||||
require "activerecord-import/adapters/mysql_adapter"
|
require "activerecord-import/adapters/mysql_adapter"
|
||||||
|
|
||||||
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
|
||||||
include ActiveRecord::Import::MysqlAdapter::InstanceMethods
|
include ActiveRecord::Import::MysqlAdapter
|
||||||
end
|
end
|
|
@ -2,5 +2,5 @@ require "active_record/connection_adapters/mysql_adapter"
|
||||||
require "activerecord-import/adapters/mysql_adapter"
|
require "activerecord-import/adapters/mysql_adapter"
|
||||||
|
|
||||||
class ActiveRecord::ConnectionAdapters::MysqlAdapter
|
class ActiveRecord::ConnectionAdapters::MysqlAdapter
|
||||||
include ActiveRecord::Import::MysqlAdapter::InstanceMethods
|
include ActiveRecord::Import::MysqlAdapter
|
||||||
end
|
end
|
|
@ -2,6 +2,6 @@ require "active_record/connection_adapters/postgresql_adapter"
|
||||||
require "activerecord-import/adapters/postgresql_adapter"
|
require "activerecord-import/adapters/postgresql_adapter"
|
||||||
|
|
||||||
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
|
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
|
||||||
include ActiveRecord::Import::PostgreSQLAdapter::InstanceMethods
|
include ActiveRecord::Import::PostgreSQLAdapter
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,6 @@ require "active_record/connection_adapters/sqlite3_adapter"
|
||||||
require "activerecord-import/adapters/sqlite3_adapter"
|
require "activerecord-import/adapters/sqlite3_adapter"
|
||||||
|
|
||||||
class ActiveRecord::ConnectionAdapters::Sqlite3Adapter
|
class ActiveRecord::ConnectionAdapters::Sqlite3Adapter
|
||||||
include ActiveRecord::Import::Sqlite3Adapter::InstanceMethods
|
include ActiveRecord::Import::Sqlite3Adapter
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,59 +1,53 @@
|
||||||
module ActiveRecord::Import::MysqlAdapter
|
module ActiveRecord::Import::MysqlAdapter
|
||||||
module InstanceMethods
|
include ActiveRecord::Import::ImportSupport
|
||||||
def self.included(klass)
|
include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
|
||||||
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
|
# Returns the maximum number of bytes that the server will allow
|
||||||
# in a single packet
|
# in a single packet
|
||||||
def max_allowed_packet # :nodoc:
|
def max_allowed_packet # :nodoc:
|
||||||
result = execute( "SHOW VARIABLES like 'max_allowed_packet';" )
|
result = execute( "SHOW VARIABLES like 'max_allowed_packet';" )
|
||||||
# original Mysql gem responds to #fetch_row while Mysql2 responds to #first
|
# 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 = result.respond_to?(:fetch_row) ? result.fetch_row[1] : result.first[1]
|
||||||
val.to_i
|
val.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a generated ON DUPLICATE KEY UPDATE statement given the passed
|
# Returns a generated ON DUPLICATE KEY UPDATE statement given the passed
|
||||||
# in +args+.
|
# in +args+.
|
||||||
def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:
|
def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:
|
||||||
sql = ' ON DUPLICATE KEY UPDATE '
|
sql = ' ON DUPLICATE KEY UPDATE '
|
||||||
arg = args.first
|
arg = args.first
|
||||||
if arg.is_a?( Array )
|
if arg.is_a?( Array )
|
||||||
sql << sql_for_on_duplicate_key_update_as_array( table_name, arg )
|
sql << sql_for_on_duplicate_key_update_as_array( table_name, arg )
|
||||||
elsif arg.is_a?( Hash )
|
elsif arg.is_a?( Hash )
|
||||||
sql << sql_for_on_duplicate_key_update_as_hash( table_name, arg )
|
sql << sql_for_on_duplicate_key_update_as_hash( table_name, arg )
|
||||||
elsif arg.is_a?( String )
|
elsif arg.is_a?( String )
|
||||||
sql << arg
|
sql << arg
|
||||||
else
|
else
|
||||||
raise ArgumentError.new( "Expected Array or Hash" )
|
raise ArgumentError.new( "Expected Array or Hash" )
|
||||||
end
|
|
||||||
sql
|
|
||||||
end
|
end
|
||||||
|
sql
|
||||||
|
end
|
||||||
|
|
||||||
def sql_for_on_duplicate_key_update_as_array( table_name, arr ) # :nodoc:
|
def sql_for_on_duplicate_key_update_as_array( table_name, arr ) # :nodoc:
|
||||||
results = arr.map do |column|
|
results = arr.map do |column|
|
||||||
qc = quote_column_name( column )
|
qc = quote_column_name( column )
|
||||||
"#{table_name}.#{qc}=VALUES(#{qc})"
|
"#{table_name}.#{qc}=VALUES(#{qc})"
|
||||||
end
|
|
||||||
results.join( ',' )
|
|
||||||
end
|
end
|
||||||
|
results.join( ',' )
|
||||||
|
end
|
||||||
|
|
||||||
def sql_for_on_duplicate_key_update_as_hash( table_name, hsh ) # :nodoc:
|
def sql_for_on_duplicate_key_update_as_hash( table_name, hsh ) # :nodoc:
|
||||||
sql = ' ON DUPLICATE KEY UPDATE '
|
sql = ' ON DUPLICATE KEY UPDATE '
|
||||||
results = hsh.map do |column1, column2|
|
results = hsh.map do |column1, column2|
|
||||||
qc1 = quote_column_name( column1 )
|
qc1 = quote_column_name( column1 )
|
||||||
qc2 = quote_column_name( column2 )
|
qc2 = quote_column_name( column2 )
|
||||||
"#{table_name}.#{qc1}=VALUES( #{qc2} )"
|
"#{table_name}.#{qc1}=VALUES( #{qc2} )"
|
||||||
end
|
|
||||||
results.join( ',')
|
|
||||||
end
|
end
|
||||||
|
results.join( ',')
|
||||||
|
end
|
||||||
|
|
||||||
#return true if the statement is a duplicate key record error
|
#return true if the statement is a duplicate key record error
|
||||||
def duplicate_key_update_error?(exception)# :nodoc:
|
def duplicate_key_update_error?(exception)# :nodoc:
|
||||||
exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('Duplicate entry')
|
exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('Duplicate entry')
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -1,13 +1,7 @@
|
||||||
module ActiveRecord::Import::PostgreSQLAdapter
|
module ActiveRecord::Import::PostgreSQLAdapter
|
||||||
module InstanceMethods
|
include ActiveRecord::Import::ImportSupport
|
||||||
def self.included(klass)
|
|
||||||
klass.instance_eval do
|
|
||||||
include ActiveRecord::Import::ImportSupport
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def next_value_for_sequence(sequence_name)
|
def next_value_for_sequence(sequence_name)
|
||||||
%{nextval('#{sequence_name}')}
|
%{nextval('#{sequence_name}')}
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
module ActiveRecord::Import::Sqlite3Adapter
|
module ActiveRecord::Import::Sqlite3Adapter
|
||||||
module InstanceMethods
|
def next_value_for_sequence(sequence_name)
|
||||||
def next_value_for_sequence(sequence_name)
|
%{nextval('#{sequence_name}')}
|
||||||
%{nextval('#{sequence_name}')}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue