Moved all adapter specific code into adapters/ directory and updated all adapters to utilize the same "require_adapter" method of loading specific-database adapters.
* Adds back in SQLite3 and PostgreSQL support issue-url: http://github.com/zdennis/activerecord-import/issues#issue/1 [contributes #1]
This commit is contained in:
parent
4fcfa7fd14
commit
e0dff4c055
|
@ -0,0 +1,47 @@
|
|||
require "active_record/connection_adapters/mysql_adapter"
|
||||
|
||||
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
|
||||
|
||||
end
|
|
@ -0,0 +1,11 @@
|
|||
require "active_record/connection_adapters/sqlite3_adapter"
|
||||
|
||||
module ActiveRecord # :nodoc:
|
||||
module ConnectionAdapters # :nodoc:
|
||||
class Sqlite3Adapter # :nodoc:
|
||||
def next_value_for_sequence(sequence_name)
|
||||
%{nextval('#{sequence_name}')}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,9 +3,10 @@ require "active_record"
|
|||
require "active_record/version"
|
||||
|
||||
module ActiveRecord::Extensions
|
||||
AdapterPath = File.join File.expand_path(File.dirname(__FILE__)), "/../active_record/adapters"
|
||||
AdapterPath = File.join File.expand_path(File.dirname(__FILE__)), "/active_record/adapters"
|
||||
|
||||
def self.require_adapter(adapter)
|
||||
require File.join(AdapterPath,"/abstract_adapter")
|
||||
require File.join(AdapterPath,"/#{adapter}_adapter")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,53 +1,2 @@
|
|||
require "active_record/connection_adapters/mysql_adapter"
|
||||
require File.join File.dirname(__FILE__), "base"
|
||||
|
||||
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
|
||||
ActiveRecord::Extensions.require_adapter "mysql"
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
require "active_record/connection_adapters/sqlite3_adapter"
|
||||
require File.join File.dirname(__FILE__), "base"
|
||||
ActiveRecord::Extensions.require_adapter "sqlite3"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
||||
|
||||
describe "ActiveRecord::ConnectionADapter::AbstractAdapter" do
|
||||
describe "ActiveRecord::ConnectionAdapter::AbstractAdapter" do
|
||||
context "#get_insert_value_sets - computing insert value sets" do
|
||||
let(:adapter){ ActiveRecord::ConnectionAdapters::AbstractAdapter }
|
||||
let(:base_sql){ "INSERT INTO atable (a,b,c)" }
|
||||
|
|
Loading…
Reference in a new issue