Skip to content
Extraits de code Groupes Projets
Valider 758de748 rédigé par Alexandre MORIGNOT's avatar Alexandre MORIGNOT
Parcourir les fichiers

Merge branch 'full_text_search' into 'master'

Add full text search with !fts

See merge request !7
parents ec48296f 8f7c6914
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!7Add full text search with !fts
...@@ -14,11 +14,12 @@ our $irc; ...@@ -14,11 +14,12 @@ our $irc;
our $log; our $log;
sub exec { sub exec {
my ($chan, $msg, $chan_src) = @_; my ($fts, $chan, $msg, $chan_src) = @_;
my $query = PlayBot::commands::get::query->new( my $query = PlayBot::commands::get::query->new(
chan => $chan, chan => $chan,
query => ($msg) ? $msg : '' query => ($msg) ? $msg : '',
fts => $fts,
); );
my $db_query = PlayBot::utils::db::get->new(); my $db_query = PlayBot::utils::db::get->new();
......
...@@ -20,6 +20,12 @@ has 'chan' => ( ...@@ -20,6 +20,12 @@ has 'chan' => (
required => 1 required => 1
); );
has 'fts' => (
is => 'ro',
isa => 'Bool',
required => 1,
);
has 'is_global' => ( has 'is_global' => (
is => 'ro', is => 'ro',
isa => 'Bool', isa => 'Bool',
......
...@@ -94,9 +94,11 @@ sub exec { ...@@ -94,9 +94,11 @@ sub exec {
$irc->yield(privmsg => $chan => $insultes[rand @insultes]); $irc->yield(privmsg => $chan => $insultes[rand @insultes]);
}; };
} }
elsif ($msg =~ /^( *!get)( +.*)?$/) { elsif ($msg =~ /^ *!(get|fts)( +.*)?$/) {
my $query = $2; my $query = $2;
my @args = ($chan, $query); my $fts = $1 eq 'fts';
my @args = ($fts, $chan, $query);
my $id = PlayBot::commands::get::exec(@args); my $id = PlayBot::commands::get::exec(@args);
if ($id) { if ($id) {
......
...@@ -149,18 +149,24 @@ sub _init { ...@@ -149,18 +149,24 @@ sub _init {
sub _prepare_request { sub _prepare_request {
my ($self, $query) = @_; my ($self, $query) = @_;
my @words_param;
my $req; my $req;
my @args; my @args;
my $words_sql;
my @words_param;
foreach (@{$query->words}) { if ($query->fts) {
unshift @words_param, '%'.$_.'%'; @words_param = (join(' ', @{$query->words}));
$words_sql = 'match (sender, title) against (?)';
} }
else {
foreach (@{$query->words}) {
unshift @words_param, '%'.$_.'%';
}
my $words_sql; foreach (@{$query->words}) {
foreach (@{$query->words}) { $words_sql .= ' and ' if ($words_sql);
$words_sql .= ' and ' if ($words_sql); $words_sql .= "concat(p.sender, ' ', p.title) like ?";
$words_sql .= "concat(p.sender, ' ', p.title) like ?"; }
} }
if ($query->id >= 0) { if ($query->id >= 0) {
...@@ -168,8 +174,10 @@ sub _prepare_request { ...@@ -168,8 +174,10 @@ sub _prepare_request {
$req .= ' from playbot p where id = ?'; $req .= ' from playbot p where id = ?';
@args = ($query->id); @args = ($query->id);
return ($req, @args);
} }
elsif (@{$query->tags}) {
if (@{$query->tags}) {
my @where; my @where;
foreach my $tag (@{$query->tags}) { foreach my $tag (@{$query->tags}) {
...@@ -183,7 +191,7 @@ sub _prepare_request { ...@@ -183,7 +191,7 @@ sub _prepare_request {
$req .= ' from playbot p where '.$where; $req .= ' from playbot p where '.$where;
$req .= ' and '.$words_sql if ($words_sql); $req .= ' and '.$words_sql if ($words_sql);
$req .= ' and p.playlist is false'; $req .= ' and p.playlist is false';
$req .= ' group by p.id order by rand()'; $req .= ' group by p.id';
@args = (@{$query->tags}, @words_param); @args = (@{$query->tags}, @words_param);
} }
...@@ -193,7 +201,7 @@ sub _prepare_request { ...@@ -193,7 +201,7 @@ sub _prepare_request {
$req .= ' where '.$where; $req .= ' where '.$where;
$req .= ' and '.$words_sql if ($words_sql); $req .= ' and '.$words_sql if ($words_sql);
$req .= ' and p.playlist is false'; $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); @args = (@{$query->tags}, @words_param, $query->chan);
} }
...@@ -204,7 +212,7 @@ sub _prepare_request { ...@@ -204,7 +212,7 @@ sub _prepare_request {
$req .= ' from playbot p where'; $req .= ' from playbot p where';
$req .= ' '.$words_sql.' and' if ($words_sql); $req .= ' '.$words_sql.' and' if ($words_sql);
$req .= ' p.playlist is false'; $req .= ' p.playlist is false';
$req .= ' group by p.id order by rand()'; $req .= ' group by p.id';
@args = (@words_param); @args = (@words_param);
} }
...@@ -214,10 +222,20 @@ sub _prepare_request { ...@@ -214,10 +222,20 @@ sub _prepare_request {
$req .= ' where pc.chan = ?'; $req .= ' where pc.chan = ?';
$req .= ' and '.$words_sql if ($words_sql); $req .= ' and '.$words_sql if ($words_sql);
$req .= ' and p.playlist is false'; $req .= ' and p.playlist is false';
$req .= ' group by p.id order by rand()'; $req .= ' group by p.id';
@args = ($query->chan, @words_param); @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); return ($req, @args);
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter