001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.plugins.dbcleaner.purges; 021 022 import javax.persistence.Query; 023 024 import org.slf4j.Logger; 025 import org.slf4j.LoggerFactory; 026 import org.sonar.api.database.DatabaseSession; 027 import org.sonar.plugins.dbcleaner.api.Purge; 028 import org.sonar.plugins.dbcleaner.api.PurgeContext; 029 030 /** 031 * Purge Review that are attached to projects that have been deleted. 032 * 033 * @since 2.8 034 */ 035 public final class PurgeOrphanReviews extends Purge { 036 037 private static final Logger LOG = LoggerFactory.getLogger(PurgeOrphanReviews.class); 038 039 public PurgeOrphanReviews(DatabaseSession session) { 040 super(session); 041 } 042 043 public void purge(PurgeContext context) { 044 DatabaseSession session = getSession(); 045 046 // delete reviews 047 Query query = session.createNativeQuery(getDeleteReviewsSqlRequest()); 048 int rowDeleted = query.executeUpdate(); 049 LOG.debug("- {} reviews deleted.", rowDeleted); 050 051 // and delete review comments 052 query = session.createNativeQuery(getDeleteReviewCommentsSqlRequest()); 053 rowDeleted = query.executeUpdate(); 054 LOG.debug("- {} review comments deleted.", rowDeleted); 055 056 session.commit(); 057 } 058 059 protected String getDeleteReviewsSqlRequest() { 060 return "DELETE FROM reviews WHERE project_id not in (SELECT id FROM projects WHERE scope = 'PRJ' and qualifier = 'TRK')"; 061 } 062 063 protected String getDeleteReviewCommentsSqlRequest() { 064 return "DELETE FROM review_comments WHERE review_id not in (SELECT id FROM reviews)"; 065 } 066 }