Permalink
Browse files

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

…а в java код для будущей оптимизации
  • Loading branch information...
1 parent ea9c9dc commit 562baac3e078e5bde0172ee5c80d82a1cb434cd0 @maxcom maxcom committed May 4, 2012
Showing with 46 additions and 0 deletions.
  1. +1 −0 sql/main.xml
  2. +32 −0 sql/updates/2012-05-04-comdel-to-java.xml
  3. +13 −0 src/main/java/ru/org/linux/comment/CommentDao.java
View
1 sql/main.xml
@@ -20,5 +20,6 @@
<include file="sql/updates/2012-03-27-add-value-to-event_type.xml"/>
<include file="sql/updates/2012-03-30-add-value-to-event_type-9.1.xml"/>
<include file="sql/updates/2012-04-25-sticky-index.xml"/>
+ <include file="sql/updates/2012-05-04-comdel-to-java.xml"/>
</databaseChangeLog>
View
32 sql/updates/2012-05-04-comdel-to-java.xml
@@ -0,0 +1,32 @@
+<databaseChangeLog
+ xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
+ http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
+
+ <changeSet id="2012050401" author="Maxim Valyanskiy">
+ <sql splitStatements="false">
+ CREATE OR REPLACE FUNCTION msgdel() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+ DECLARE
+ grid int;
+ stat_1 int;
+ stat int;
+ thetopic topics%ROWTYPE;
+ thecomment comments%ROWTYPE;
+ BEGIN
+ SELECT * INTO thetopic FROM topics WHERE id = NEW.msgid;
+ IF FOUND THEN
+ SELECT groupid INTO grid FROM topics WHERE id = NEW.msgid;
+ UPDATE groups SET stat1=stat1-1 WHERE id = grid;
+ UPDATE topics SET lastmod=CURRENT_TIMESTAMP WHERE id = NEW.msgid;
+ UPDATE comments SET topic_deleted=true WHERE topic = NEW.msgid;
+ END IF;
+ RETURN NULL;
+ END;
+ $$;
+ </sql>
+ </changeSet>
+
+</databaseChangeLog>
View
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
if (deleteCount > 0) {
deleteInfoDao.insert(msgid, user, reason, scoreBonus);
+ updateStatsAfterDelete(msgid);
+
if (scoreBonus != 0) {
jdbcTemplate.update(updateScore, scoreBonus, msgid);
}
@@ -210,6 +212,17 @@ private boolean doDeleteComment(int msgid, String reason, User user, int scoreBo
}
}
+ private void updateStatsAfterDelete(int commentId) {
+ int topicId = jdbcTemplate.queryForInt("SELECT topic FROM comments WHERE id=?", commentId);
+ jdbcTemplate.update("UPDATE topics SET stat1=stat1-1, lastmod=CURRENT_TIMESTAMP WHERE id = ?", topicId);
+ jdbcTemplate.update("UPDATE topics SET stat2=stat1 WHERE id=? AND stat2 > stat1", topicId);
+ jdbcTemplate.update("UPDATE topics SET stat3=stat1 WHERE id=? AND stat3 > stat1", topicId);
+ jdbcTemplate.update("UPDATE topics SET stat4=stat1 WHERE id=? AND stat4 > stat1", topicId);
+
+ int groupId = jdbcTemplate.queryForInt("SELECT groupid FROM topics WHERE id = ?", topicId);
+ jdbcTemplate.update("UPDATE groups SET stat1=stat1-1 WHERE id = ?", groupId);
+ }
+
public List<Integer> deleteReplys(int msgid, User user, boolean score) {
return deleteReplys(msgid, user, score, 0);
}

0 comments on commit 562baac

Please sign in to comment.