diff --git a/PlayBot/commands/get.pm b/PlayBot/commands/get.pm index 53c920dca09716bc4530d3c8473c8f75f9fb096c..568610b0af6338f0f727bfa6a57fa394770d4d94 100644 --- a/PlayBot/commands/get.pm +++ b/PlayBot/commands/get.pm @@ -14,11 +14,12 @@ our $irc; our $log; sub exec { - my ($chan, $msg, $chan_src) = @_; + my ($fts, $chan, $msg, $chan_src) = @_; my $query = PlayBot::commands::get::query->new( - chan => $chan, - query => ($msg) ? $msg : '' + chan => $chan, + query => ($msg) ? $msg : '', + fts => $fts, ); my $db_query = PlayBot::utils::db::get->new(); diff --git a/PlayBot/commands/get/query.pm b/PlayBot/commands/get/query.pm index 91549ce30481e4b35ffaee680b950ef504fa0aca..55458eea91e7f4901533b841e656c0aa8c7e9941 100644 --- a/PlayBot/commands/get/query.pm +++ b/PlayBot/commands/get/query.pm @@ -20,6 +20,12 @@ has 'chan' => ( required => 1 ); +has 'fts' => ( + is => 'ro', + isa => 'Bool', + required => 1, +); + has 'is_global' => ( is => 'ro', isa => 'Bool', diff --git a/PlayBot/commands/parser.pm b/PlayBot/commands/parser.pm index 4cb9ddb33280b7d9b06a9d54b45e872bc4e862b3..1d0c917bcc8ec5776dd7dab8a5aad92028667cd5 100644 --- a/PlayBot/commands/parser.pm +++ b/PlayBot/commands/parser.pm @@ -94,9 +94,11 @@ sub exec { $irc->yield(privmsg => $chan => $insultes[rand @insultes]); }; } - elsif ($msg =~ /^( *!get)( +.*)?$/) { + elsif ($msg =~ /^ *!(get|fts)( +.*)?$/) { my $query = $2; - my @args = ($chan, $query); + my $fts = $1 eq 'fts'; + my @args = ($fts, $chan, $query); + my $id = PlayBot::commands::get::exec(@args); if ($id) { diff --git a/PlayBot/utils/db/get.pm b/PlayBot/utils/db/get.pm index 6fd1a6847bd2661293c20be37e7d8fdd787db21a..3da527d3b188e9129dd82bcc632729cf5423b386 100644 --- a/PlayBot/utils/db/get.pm +++ b/PlayBot/utils/db/get.pm @@ -149,18 +149,24 @@ sub _init { sub _prepare_request { my ($self, $query) = @_; - my @words_param; my $req; my @args; + my $words_sql; + my @words_param; - foreach (@{$query->words}) { - unshift @words_param, '%'.$_.'%'; + if ($query->fts) { + @words_param = (join(' ', @{$query->words})); + $words_sql = 'match (sender, title) against (?)'; } + else { + foreach (@{$query->words}) { + unshift @words_param, '%'.$_.'%'; + } - my $words_sql; - foreach (@{$query->words}) { - $words_sql .= ' and ' if ($words_sql); - $words_sql .= "concat(p.sender, ' ', p.title) like ?"; + foreach (@{$query->words}) { + $words_sql .= ' and ' if ($words_sql); + $words_sql .= "concat(p.sender, ' ', p.title) like ?"; + } } if ($query->id >= 0) { @@ -168,8 +174,10 @@ sub _prepare_request { $req .= ' from playbot p where id = ?'; @args = ($query->id); + return ($req, @args); } - elsif (@{$query->tags}) { + + if (@{$query->tags}) { my @where; foreach my $tag (@{$query->tags}) { @@ -183,7 +191,7 @@ sub _prepare_request { $req .= ' from playbot p where '.$where; $req .= ' and '.$words_sql if ($words_sql); $req .= ' and p.playlist is false'; - $req .= ' group by p.id order by rand()'; + $req .= ' group by p.id'; @args = (@{$query->tags}, @words_param); } @@ -193,7 +201,7 @@ sub _prepare_request { $req .= ' where '.$where; $req .= ' and '.$words_sql if ($words_sql); $req .= ' and p.playlist is false'; - $req .= ' and pc.chan = ? group by p.id order by rand()'; + $req .= ' and pc.chan = ? group by p.id'; @args = (@{$query->tags}, @words_param, $query->chan); } @@ -204,7 +212,7 @@ sub _prepare_request { $req .= ' from playbot p where'; $req .= ' '.$words_sql.' and' if ($words_sql); $req .= ' p.playlist is false'; - $req .= ' group by p.id order by rand()'; + $req .= ' group by p.id'; @args = (@words_param); } @@ -214,10 +222,20 @@ sub _prepare_request { $req .= ' where pc.chan = ?'; $req .= ' and '.$words_sql if ($words_sql); $req .= ' and p.playlist is false'; - $req .= ' group by p.id order by rand()'; + $req .= ' group by p.id'; @args = ($query->chan, @words_param); } + + } + + + if ($query->fts and $words_sql) { + $req .= ' order by '.$words_sql.' desc'; + push @args, @words_param; + } + else { + $req .= ' order by rand()'; } return ($req, @args);