From e0dff4c055a94ecc4e9a4ef812a6508a9fafa3ab Mon Sep 17 00:00:00 2001 From: Zach Dennis Date: Tue, 10 Aug 2010 20:46:32 -0400 Subject: [PATCH] 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] --- .../active_record/adapters/mysql_adapter.rb | 47 ++++++++++++++++ .../active_record/adapters/sqlite3_adapter.rb | 11 ++++ lib/activerecord-import/base.rb | 3 +- lib/activerecord-import/mysql.rb | 53 +------------------ lib/activerecord-import/sqlite3.rb | 4 +- test/active_record/connection_adapter_test.rb | 2 +- 6 files changed, 64 insertions(+), 56 deletions(-) create mode 100644 lib/activerecord-import/active_record/adapters/mysql_adapter.rb create mode 100644 lib/activerecord-import/active_record/adapters/sqlite3_adapter.rb diff --git a/lib/activerecord-import/active_record/adapters/mysql_adapter.rb b/lib/activerecord-import/active_record/adapters/mysql_adapter.rb new file mode 100644 index 0000000..02327c1 --- /dev/null +++ b/lib/activerecord-import/active_record/adapters/mysql_adapter.rb @@ -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 diff --git a/lib/activerecord-import/active_record/adapters/sqlite3_adapter.rb b/lib/activerecord-import/active_record/adapters/sqlite3_adapter.rb new file mode 100644 index 0000000..95373f1 --- /dev/null +++ b/lib/activerecord-import/active_record/adapters/sqlite3_adapter.rb @@ -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 diff --git a/lib/activerecord-import/base.rb b/lib/activerecord-import/base.rb index c32216f..ff69138 100644 --- a/lib/activerecord-import/base.rb +++ b/lib/activerecord-import/base.rb @@ -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 diff --git a/lib/activerecord-import/mysql.rb b/lib/activerecord-import/mysql.rb index 0053069..dbabc80 100644 --- a/lib/activerecord-import/mysql.rb +++ b/lib/activerecord-import/mysql.rb @@ -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" diff --git a/lib/activerecord-import/sqlite3.rb b/lib/activerecord-import/sqlite3.rb index 9c4fc82..6db45f6 100644 --- a/lib/activerecord-import/sqlite3.rb +++ b/lib/activerecord-import/sqlite3.rb @@ -1,2 +1,2 @@ -require "active_record/connection_adapters/sqlite3_adapter" -require File.join File.dirname(__FILE__), "base" \ No newline at end of file +require File.join File.dirname(__FILE__), "base" +ActiveRecord::Extensions.require_adapter "sqlite3" diff --git a/test/active_record/connection_adapter_test.rb b/test/active_record/connection_adapter_test.rb index a50dc61..ad3234c 100644 --- a/test/active_record/connection_adapter_test.rb +++ b/test/active_record/connection_adapter_test.rb @@ -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)" }