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.

AlterColumnsBuilder.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.ArrayList;
  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.Oracle;
  27. import org.sonar.db.dialect.PostgreSql;
  28. import org.sonar.server.platform.db.migration.def.ColumnDef;
  29. import static com.google.common.base.Preconditions.checkArgument;
  30. import static com.google.common.collect.Lists.newArrayList;
  31. import static org.sonar.server.platform.db.migration.def.Validations.validateTableName;
  32. /**
  33. * Generate SQL queries to update multiple columns of a single table.
  34. *
  35. * Note that this operation will not be re-entrant on:
  36. * <ul>
  37. * <li>Oracle 11G (may raise {@code ORA-01442: column to be modified to NOT NULL is already NOT NULL} or
  38. * {@code ORA-01451: column to be modified to NULL cannot be modified to NULL})</li>
  39. * </ul>
  40. */
  41. public class AlterColumnsBuilder {
  42. private static final String ALTER_TABLE = "ALTER TABLE ";
  43. private static final String ALTER_COLUMN = "ALTER COLUMN ";
  44. private final Dialect dialect;
  45. private final String tableName;
  46. private final List<ColumnDef> columnDefs = newArrayList();
  47. public AlterColumnsBuilder(Dialect dialect, String tableName) {
  48. this.dialect = dialect;
  49. this.tableName = validateTableName(tableName);
  50. }
  51. public AlterColumnsBuilder updateColumn(ColumnDef columnDef) {
  52. // limitation of Oracle, only attribute changes must be defined in ALTER.
  53. checkArgument(columnDef.getDefaultValue()==null, "Default value is not supported on alter of column '%s'", columnDef.getName());
  54. columnDefs.add(columnDef);
  55. return this;
  56. }
  57. public List<String> build() {
  58. if (columnDefs.isEmpty()) {
  59. throw new IllegalStateException("No column has been defined");
  60. }
  61. switch (dialect.getId()) {
  62. case PostgreSql.ID:
  63. return createPostgresQuery();
  64. case Oracle.ID:
  65. return createOracleQuery();
  66. default:
  67. return createMsSqlAndH2Queries();
  68. }
  69. }
  70. private List<String> createPostgresQuery() {
  71. StringBuilder sql = new StringBuilder(ALTER_TABLE + tableName + " ");
  72. for (Iterator<ColumnDef> columnDefIterator = columnDefs.iterator(); columnDefIterator.hasNext();) {
  73. ColumnDef columnDef = columnDefIterator.next();
  74. sql.append(ALTER_COLUMN);
  75. addColumn(sql, columnDef, "TYPE ", false);
  76. sql.append(", ");
  77. sql.append(ALTER_COLUMN);
  78. sql.append(columnDef.getName());
  79. sql.append(' ').append(columnDef.isNullable() ? "DROP" : "SET").append(" NOT NULL");
  80. if (columnDefIterator.hasNext()) {
  81. sql.append(", ");
  82. }
  83. }
  84. return Collections.singletonList(sql.toString());
  85. }
  86. private List<String> createOracleQuery() {
  87. List<String> sqls = new ArrayList<>();
  88. for (ColumnDef columnDef : columnDefs) {
  89. StringBuilder sql = new StringBuilder(ALTER_TABLE + tableName + " ").append("MODIFY (");
  90. addColumn(sql, columnDef, "", true);
  91. sql.append(")");
  92. sqls.add(sql.toString());
  93. }
  94. return sqls;
  95. }
  96. private List<String> createMsSqlAndH2Queries() {
  97. List<String> sqls = new ArrayList<>();
  98. for (ColumnDef columnDef : columnDefs) {
  99. StringBuilder defaultQuery = new StringBuilder(ALTER_TABLE + tableName + " ");
  100. defaultQuery.append(ALTER_COLUMN);
  101. addColumn(defaultQuery, columnDef, "", true);
  102. sqls.add(defaultQuery.toString());
  103. }
  104. return sqls;
  105. }
  106. private void addColumn(StringBuilder sql, ColumnDef columnDef, String typePrefix, boolean addNotNullableProperty) {
  107. sql.append(columnDef.getName())
  108. .append(" ")
  109. .append(typePrefix)
  110. .append(columnDef.generateSqlType(dialect));
  111. if (addNotNullableProperty) {
  112. sql.append(columnDef.isNullable() ? " NULL" : " NOT NULL");
  113. }
  114. }
  115. }