You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeleteUnescapedActivities.java 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.db.migrations.v451;
  21. import java.sql.SQLException;
  22. import javax.annotation.Nullable;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.sonar.core.persistence.Database;
  25. import org.sonar.server.db.migrations.BaseDataChange;
  26. import org.sonar.server.db.migrations.MassUpdate;
  27. import org.sonar.server.db.migrations.Select;
  28. import org.sonar.server.db.migrations.SqlStatement;
  29. /**
  30. * See http://jira.sonarsource.com/browse/SONAR-5758
  31. *
  32. * @since 4.5.1
  33. */
  34. public class DeleteUnescapedActivities extends BaseDataChange {
  35. public DeleteUnescapedActivities(Database db) {
  36. super(db);
  37. }
  38. @Override
  39. public void execute(Context context) throws SQLException {
  40. MassUpdate massUpdate = context.prepareMassUpdate();
  41. massUpdate.select("select id,data_field from activities where log_type='QPROFILE'");
  42. massUpdate.update("delete from activities where id=?");
  43. massUpdate.execute(new MassUpdate.Handler() {
  44. @Override
  45. public boolean handle(Select.Row row, SqlStatement update) throws SQLException {
  46. String csv = row.getNullableString(2);
  47. if (isUnescaped(csv)) {
  48. update.setLong(1, row.getNullableLong(1));
  49. return true;
  50. }
  51. return false;
  52. }
  53. });
  54. }
  55. static boolean isUnescaped(@Nullable String csv) {
  56. if (csv != null) {
  57. String[] splits = StringUtils.split(csv, ';');
  58. for (String split : splits) {
  59. if (StringUtils.countMatches(split, "=") != 1) {
  60. return true;
  61. }
  62. }
  63. }
  64. return false;
  65. }
  66. }