diff --git a/lib/etl/control/source/database_source.rb b/lib/etl/control/source/database_source.rb index c53c782..1b01951 100644 --- a/lib/etl/control/source/database_source.rb +++ b/lib/etl/control/source/database_source.rb @@ -28,6 +28,8 @@ class DatabaseSource < Source # Other options: # * :join: Optional join part for the query (ignored unless # specified) + # * :join_type: Optional join clause type for a query with a + # join part (defaults to 'JOIN') # * :select: Optional select part for the query (defaults to # '*') # * :group: Optional group by part for the query (ignored @@ -62,6 +64,11 @@ def join configuration[:join] end + # Get the type of join for the query, defaults to JOIN + def join_type + configuration[:join_type] || 'JOIN' + end + # Get the select part of the query, defaults to '*' def select configuration[:select] || '*' @@ -172,7 +179,7 @@ def write_local(file) def query return @query if @query q = "SELECT #{select} FROM #{@table}" - q << " JOIN #{join}" if join + q << " #{join_type} #{join}" if join conditions = [] if new_records_only diff --git a/test/source_test.rb b/test/source_test.rb index ee2e4ae..c05b729 100644 --- a/test/source_test.rb +++ b/test/source_test.rb @@ -109,6 +109,30 @@ class SourceTest < Test::Unit::TestCase @source = ETL::Control::DatabaseSource.new(control, configuration, definition) end + context 'with a join clause' do + setup do + Person.destroy_all + Person.create!( :first_name => 'Bob', :last_name => 'Jones', :ssn => '123456789') + Person.create!( :first_name => 'Carol', :last_name => 'Jones') + Person.create!( :first_name => 'Bob', :last_name => 'Smith') + end + + should 'perform an inner join by default' do + size = build_source( :store_locally => true, + :mysqlstream => false, + :join => 'people p2 on p2.last_name = people.last_name and p2.id != people.id').to_a.size + assert_equal 2, size + end + + should 'allow other types of joins' do + size = build_source( :store_locally => true, + :mysqlstream => false, + :join => 'people p2 on p2.last_name = people.last_name and p2.id != people.id', + :join_type => 'left outer join').to_a.size + assert_equal 3, size + end + end + context "with a specified LIMIT `n`" do setup do ETL::Engine.limit = @limit @@ -228,4 +252,4 @@ def definition } ] end -end \ No newline at end of file +end