From: hylom Date: Thu, 2 Feb 2017 16:05:06 +0000 (+0900) Subject: Model::Moderations.pm: rewrite SQL in select_with_m2 method X-Git-Tag: v0.1.1~963 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=5bbcf1f12e948b7ace125aa1e1ed8b4dc5a34d46;p=newslash%2Fnewslash.git Model::Moderations.pm: rewrite SQL in select_with_m2 method --- diff --git a/src/newslash_web/lib/Newslash/Model/Moderations.pm b/src/newslash_web/lib/Newslash/Model/Moderations.pm index b47ed040..46ffea59 100644 --- a/src/newslash_web/lib/Newslash/Model/Moderations.pm +++ b/src/newslash_web/lib/Newslash/Model/Moderations.pm @@ -51,42 +51,22 @@ sub select { sub select_with_m2 { my ($self, $cid, $uid) = @_; my $sql = <<"EOSQL"; -SELECT moderatorlog.*, metamodlog.val AS m2val, metamodlog.uid AS m2uid from moderatorlog - LEFT JOIN metamodlog ON moderatorlog.id = metamodlog.mmid - WHERE moderatorlog.cid = ? - AND moderatorlog.active = 1 +SELECT moderatorlog.*, m2.val AS m2val, m2.uid AS m2uid FROM moderatorlog + LEFT JOIN + (SELECT metamodlog.* FROM moderatorlog + LEFT JOIN metamodlog ON moderatorlog.id = metamodlog.mmid + WHERE moderatorlog.cid = ? AND metamodlog.uid = ?) AS m2 + ON moderatorlog.id = m2.mmid + WHERE moderatorlog.cid = ? + AND moderatorlog.active = 1 EOSQL - #AND (metamodlog.uid = ? OR metamodlog.uid IS NULL) my $dbh = $self->connect_db; my $sth = $dbh->prepare($sql); - my $rs = $sth->execute($cid); + my $rs = $sth->execute($cid, $uid, $cid); my $mods = $sth->fetchall_arrayref({}); $self->disconnect_db; - return if !$mods; - - # query result contains non-given user's metamoderation, and moderation data is duplicated. - # so, remove those datas. - # TODO: are there more smart ways? - my %eliminated; - for my $mod (@$mods) { - $eliminated{$mod->{id}} = $mod; - } - for my $mod (@$mods) { - if ($mod->{m2uid} == $uid) { - $eliminated{$mod->{id}} = $mod; - } - } - for my $mod (values %eliminated) { - if ($mod->{m2uid} != $uid) { - delete $mod->{m2uid}; - delete $mod->{m2val}; - } - } - my @rs = values %eliminated; - - return \@rs; - + return $mods; } #========================================================================