Skip to content
This repository
Browse code

обновление статистики после удаления комментария перенесено из тригер…

…а в java код для будущей оптимизации
  • Loading branch information...
commit 562baac3e078e5bde0172ee5c80d82a1cb434cd0 1 parent ea9c9dc
Maxim Valyanskiy maxcom authored
1  sql/main.xml
@@ -20,5 +20,6 @@
20 20 <include file="sql/updates/2012-03-27-add-value-to-event_type.xml"/>
21 21 <include file="sql/updates/2012-03-30-add-value-to-event_type-9.1.xml"/>
22 22 <include file="sql/updates/2012-04-25-sticky-index.xml"/>
  23 + <include file="sql/updates/2012-05-04-comdel-to-java.xml"/>
23 24
24 25 </databaseChangeLog>
32 sql/updates/2012-05-04-comdel-to-java.xml
... ... @@ -0,0 +1,32 @@
  1 +<databaseChangeLog
  2 + xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
  5 + http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
  6 +
  7 + <changeSet id="2012050401" author="Maxim Valyanskiy">
  8 + <sql splitStatements="false">
  9 + CREATE OR REPLACE FUNCTION msgdel() RETURNS trigger
  10 + LANGUAGE plpgsql
  11 + AS $$
  12 + DECLARE
  13 + grid int;
  14 + stat_1 int;
  15 + stat int;
  16 + thetopic topics%ROWTYPE;
  17 + thecomment comments%ROWTYPE;
  18 + BEGIN
  19 + SELECT * INTO thetopic FROM topics WHERE id = NEW.msgid;
  20 + IF FOUND THEN
  21 + SELECT groupid INTO grid FROM topics WHERE id = NEW.msgid;
  22 + UPDATE groups SET stat1=stat1-1 WHERE id = grid;
  23 + UPDATE topics SET lastmod=CURRENT_TIMESTAMP WHERE id = NEW.msgid;
  24 + UPDATE comments SET topic_deleted=true WHERE topic = NEW.msgid;
  25 + END IF;
  26 + RETURN NULL;
  27 + END;
  28 + $$;
  29 + </sql>
  30 + </changeSet>
  31 +
  32 +</databaseChangeLog>
13 src/main/java/ru/org/linux/comment/CommentDao.java
@@ -197,6 +197,8 @@ private boolean doDeleteComment(int msgid, String reason, User user, int scoreBo
197 197 if (deleteCount > 0) {
198 198 deleteInfoDao.insert(msgid, user, reason, scoreBonus);
199 199
  200 + updateStatsAfterDelete(msgid);
  201 +
200 202 if (scoreBonus != 0) {
201 203 jdbcTemplate.update(updateScore, scoreBonus, msgid);
202 204 }
@@ -210,6 +212,17 @@ private boolean doDeleteComment(int msgid, String reason, User user, int scoreBo
210 212 }
211 213 }
212 214
  215 + private void updateStatsAfterDelete(int commentId) {
  216 + int topicId = jdbcTemplate.queryForInt("SELECT topic FROM comments WHERE id=?", commentId);
  217 + jdbcTemplate.update("UPDATE topics SET stat1=stat1-1, lastmod=CURRENT_TIMESTAMP WHERE id = ?", topicId);
  218 + jdbcTemplate.update("UPDATE topics SET stat2=stat1 WHERE id=? AND stat2 > stat1", topicId);
  219 + jdbcTemplate.update("UPDATE topics SET stat3=stat1 WHERE id=? AND stat3 > stat1", topicId);
  220 + jdbcTemplate.update("UPDATE topics SET stat4=stat1 WHERE id=? AND stat4 > stat1", topicId);
  221 +
  222 + int groupId = jdbcTemplate.queryForInt("SELECT groupid FROM topics WHERE id = ?", topicId);
  223 + jdbcTemplate.update("UPDATE groups SET stat1=stat1-1 WHERE id = ?", groupId);
  224 + }
  225 +
213 226 public List<Integer> deleteReplys(int msgid, User user, boolean score) {
214 227 return deleteReplys(msgid, user, score, 0);
215 228 }

0 comments on commit 562baac

Please sign in to comment.
Something went wrong with that request. Please try again.