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.

DropColumnsBuilder.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.platform.db.migration.sql;
  21. import java.util.Arrays;
  22. import java.util.Collections;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import org.sonar.db.dialect.Dialect;
  26. import org.sonar.db.dialect.H2;
  27. import org.sonar.db.dialect.MsSql;
  28. import org.sonar.db.dialect.Oracle;
  29. import org.sonar.db.dialect.PostgreSql;
  30. /**
  31. * Generate a SQL query to drop multiple columns from a table
  32. */
  33. public class DropColumnsBuilder {
  34. private static final String ALTER_TABLE = "ALTER TABLE ";
  35. private final Dialect dialect;
  36. private final String tableName;
  37. private final String[] columns;
  38. public DropColumnsBuilder(Dialect dialect, String tableName, String... columns) {
  39. this.tableName = tableName;
  40. this.dialect = dialect;
  41. this.columns = columns;
  42. }
  43. public List<String> build() {
  44. switch (dialect.getId()) {
  45. case PostgreSql.ID:
  46. StringBuilder sql = new StringBuilder().append(ALTER_TABLE).append(tableName).append(" ");
  47. dropColumns(sql, "DROP COLUMN ", columns);
  48. return Collections.singletonList(sql.toString());
  49. case MsSql.ID:
  50. return Collections.singletonList(getMsSQLStatement(columns));
  51. case Oracle.ID:
  52. return Collections.singletonList(getOracleStatement());
  53. case H2.ID:
  54. return Arrays.stream(columns).map(this::getMsSQLStatement).toList();
  55. default:
  56. throw new IllegalStateException(String.format("Unsupported database '%s'", dialect.getId()));
  57. }
  58. }
  59. private String getOracleStatement() {
  60. StringBuilder sql = new StringBuilder().append(ALTER_TABLE).append(tableName).append(" ");
  61. sql.append("SET UNUSED (");
  62. dropColumns(sql, "", columns);
  63. sql.append(")");
  64. return sql.toString();
  65. }
  66. private String getMsSQLStatement(String... columnNames) {
  67. StringBuilder sql = new StringBuilder().append(ALTER_TABLE).append(tableName).append(" ");
  68. sql.append("DROP COLUMN ");
  69. dropColumns(sql, "", columnNames);
  70. return sql.toString();
  71. }
  72. private static void dropColumns(StringBuilder sql, String columnPrefix, String... columnNames) {
  73. Iterator<String> columnNamesIterator = Arrays.stream(columnNames).iterator();
  74. while (columnNamesIterator.hasNext()) {
  75. sql.append(columnPrefix);
  76. sql.append(columnNamesIterator.next());
  77. if (columnNamesIterator.hasNext()) {
  78. sql.append(", ");
  79. }
  80. }
  81. }
  82. }