Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Allow database source to use different types of joins #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/etl/control/source/database_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class DatabaseSource < Source
# Other options:
# * <tt>:join</tt>: Optional join part for the query (ignored unless
# specified)
# * <tt>:join_type</tt>: Optional join clause type for a query with a
# join part (defaults to 'JOIN')
# * <tt>:select</tt>: Optional select part for the query (defaults to
# '*')
# * <tt>:group</tt>: Optional group by part for the query (ignored
Expand Down Expand Up @@ -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] || '*'
Expand Down Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion test/source_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -228,4 +252,4 @@ def definition
}
]
end
end
end