Skip to content

Commit

Permalink
type handling by index_hint option moves to SQL::Maker::Select::add_i…
Browse files Browse the repository at this point in the history
…ndex_hint
  • Loading branch information
soh335 committed Apr 2, 2014
1 parent 0b13e36 commit 6a18b53
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
13 changes: 2 additions & 11 deletions lib/SQL/Maker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,7 @@ sub select_query {
}
}
if (my $o = $opt->{index_hint}) {
if (ref $o eq 'ARRAY') {
# ['foo', 'bar']
$stmt->add_index_hint($table, { list => $o });
} elsif (ref $o eq 'HASH') {
# { type => '...', list => ['foo'] }
$stmt->add_index_hint($table, $o);
} else {
# just 'foo'
$stmt->add_index_hint($table, { list => [$o] });
}
$stmt->add_index_hint($table, $o);
}

$stmt->limit( $opt->{limit} ) if $opt->{limit};
Expand Down Expand Up @@ -493,7 +484,7 @@ You can write it as follows:
=item C<< $opt->{index_hint} >>
This option adds an INDEX HINT like as 'USE INDEX' clause via L<SQL::Maker::Select>.
This option adds an INDEX HINT like as 'USE INDEX' clause for MySQL via L<SQL::Maker::Select>.
You can write it as follows:
Expand Down
21 changes: 19 additions & 2 deletions lib/SQL/Maker/Select.pm
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,23 @@ sub add_join {
sub add_index_hint {
my ($self, $table, $hint) = @_;

my ($type, $list);

if (ref $hint eq 'HASH') {
# { type => '...', list => ['foo'] }
$type = $hint->{type} || 'USE';
$list = ref($hint->{list}) eq 'ARRAY' ? $hint->{list} : [ $hint->{list} ];
} else {
# ['foo, 'bar'] or just 'foo'
$type = 'USE';
$list = ref($hint) eq 'ARRAY' ? $hint : [ $hint ];
}

$self->{index_hint}->{$table} = {
type => $hint->{type} || 'USE',
list => ref($hint->{list}) eq 'ARRAY' ? $hint->{list} : [ $hint->{list} ],
type => $type,
list => $list,
};

return $self;
}

Expand Down Expand Up @@ -423,6 +436,10 @@ it falls back to plain JOIN.
=item C<< $stmt->add_index_hint(foo => {type => 'USE', list => ['index_hint']}); >>
=item C<< $stmt->add_index_hint(foo => 'index_hint'); >>
=item C<< $stmt->add_index_hint(foo => ['index_hint']); >>
my $stmt = SQL::Maker::Select->new();
$stmt->add_select('name');
$stmt->add_from('user');
Expand Down
16 changes: 16 additions & 0 deletions t/select/01_statement.t
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,22 @@ subtest 'index hint' => sub {
$stmt->add_index_hint('baz' => { type => 'USE', list => ['index_hint']});
is($stmt->as_sql, "SELECT foo FROM baz USE INDEX (index_hint)", "we can turn on USE INDEX");
};

subtest 'hint as scalar' => sub {
my $stmt = ns( quote_char => q{}, name_sep => q{.}, new_line => q{ } );
$stmt->add_select(foo => 'foo');
$stmt->add_from( qw(baz) );
$stmt->add_index_hint('baz' => 'index_hint');
is($stmt->as_sql, "SELECT foo FROM baz USE INDEX (index_hint)", "we can turn on USE INDEX");
};

subtest 'hint as array ref' => sub {
my $stmt = ns( quote_char => q{}, name_sep => q{.}, new_line => q{ } );
$stmt->add_select(foo => 'foo');
$stmt->add_from( qw(baz) );
$stmt->add_index_hint('baz' => ['index_hint']);
is($stmt->as_sql, "SELECT foo FROM baz USE INDEX (index_hint)", "we can turn on USE INDEX");
};
};

subtest 'index hint with joins' => sub {
Expand Down

0 comments on commit 6a18b53

Please sign in to comment.