Skip to content

Commit

Permalink
added join option for select()
Browse files Browse the repository at this point in the history
  • Loading branch information
xaicron committed Apr 26, 2012
1 parent 21a137a commit 73e5ec0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/SQL/Format.pm
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,11 @@ sub select {
my $suffix = delete $opts->{suffix};
my $format = "$prefix %c FROM %t";
my @args = ($cols, $table);

if (my $join = delete $opts->{join}) {
$format .= ' %j';
push @args, $join;
}
if (keys %{ $where || {} }) {
$format .= ' WHERE %w';
push @args, $where;
Expand Down Expand Up @@ -1104,6 +1109,8 @@ Default value is C<< '' >>
=item $opts->{having}
=item $opts->{join}
See also C<< %o >> format.
=back
Expand Down
23 changes: 23 additions & 0 deletions t/30_select.t
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,27 @@ $test->(
},
);

$test->(
desc => 'join',
input => [
{ foo => 'f' },
[qw/f.id b.name/],
{ 'f.created_at' => { '>' => \['UNIX_TIMESTAMP() - ?', 60 * 10] } },
{
order_by => 'f.id',
limit => 10,
offset => 20,
join => {
type => 'left',
table => { bar => 'b' },
condition => [qw/id/],
},
},
],
expects => {
stmt => 'SELECT `f`.`id`, `b`.`name` FROM `foo` `f` LEFT JOIN `bar` `b` USING (`id`) WHERE (`f`.`created_at` > UNIX_TIMESTAMP() - ?) ORDER BY `f`.`id` LIMIT 10 OFFSET 20',
bind => [qw/600/],
},
);

done_testing;

0 comments on commit 73e5ec0

Please sign in to comment.