Replace useless queries by JOIN statements

master
Daniel Berteaud 10 years ago
parent f69cc901de
commit 47224076e0
  1. 31
      vroom.pl

@ -384,23 +384,19 @@ helper get_participant_list => sub {
helper set_peer_role => sub { helper set_peer_role => sub {
my $self = shift; my $self = shift;
my ($data) = @_; my ($data) = @_;
my $res = $self->get_room_by_name($data->{room});
if (!$res->{ok}){
return $res;
}
my $room = $res->{data};
# Check if this ID isn't the one from another peer first # Check if this ID isn't the one from another peer first
my $sth = eval { my $sth = eval {
$self->db->prepare('SELECT COUNT(`id`) $self->db->prepare('SELECT COUNT(`p`.`id`)
FROM `room_participants` FROM `room_participants` `p`
WHERE `peer_id`=? LEFT JOIN `rooms` `r` ON `p`.`room_id`=`r`.`id`
AND `participant`!=? WHERE `p`.`peer_id`=?
AND `room_id`=?'); AND `p`.`participant`!=?
AND `r`.`name`=?');
}; };
if ($@){ if ($@){
return {msg => $@}; return {msg => $@};
} }
$sth->execute($data->{peer_id},$data->{name},$room->{id}); $sth->execute($data->{peer_id},$data->{name},$data->{room});
if ($sth->err){ if ($sth->err){
return {msg => "DB Error: " . $sth->errstr . " (code " . $sth->err . ")"}; return {msg => "DB Error: " . $sth->errstr . " (code " . $sth->err . ")"};
} }
@ -411,11 +407,12 @@ helper set_peer_role => sub {
return {msg => 'ERROR_UNAUTHORIZED'}; return {msg => 'ERROR_UNAUTHORIZED'};
} }
$sth = eval { $sth = eval {
$self->db->prepare('UPDATE `room_participants` $self->db->prepare('UPDATE `room_participants` `p`
SET `peer_id`=?, LEFT JOIN `rooms` `r` ON `p`.`room_id`=`r`.`id`
`role`=? SET `p`.`peer_id`=?,
WHERE `participant`=? `p`.`role`=?
AND `room_id`=?'); WHERE `p`.`participant`=?
AND `r`.`name`=?');
}; };
if ($@){ if ($@){
return {msg => $@}; return {msg => $@};
@ -424,7 +421,7 @@ helper set_peer_role => sub {
$data->{peer_id}, $data->{peer_id},
$data->{role}, $data->{role},
$data->{name}, $data->{name},
$room->{id} $data->{room}
); );
if ($sth->err){ if ($sth->err){
return {msg => "DB Error: " . $sth->errstr . " (code " . $sth->err . ")"}; return {msg => "DB Error: " . $sth->errstr . " (code " . $sth->err . ")"};

Loading…
Cancel
Save