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;
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 : ''
query => ($msg) ? $msg : '',
fts => $fts,
);
my $db_query = PlayBot::utils::db::get->new();
......
......@@ -20,6 +20,12 @@ has 'chan' => (
required => 1
);
has 'fts' => (
is => 'ro',
isa => 'Bool',
required => 1,
);
has 'is_global' => (
is => 'ro',
isa => 'Bool',
......
......@@ -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) {
......
......@@ -149,27 +149,35 @@ sub _init {
sub _prepare_request {
my ($self, $query) = @_;
my @words_param;
my $req;
my @args;
my $words_sql;
my @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 ?";
}
}
if ($query->id >= 0) {
$req = 'select p.id, p.sender, p.title, p.url, p.duration, p.external_id, p.type';
$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);
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter