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.

VarcharColumnDef.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.def;
  21. import javax.annotation.CheckForNull;
  22. import javax.annotation.Nullable;
  23. import javax.annotation.concurrent.Immutable;
  24. import org.sonar.db.dialect.Dialect;
  25. import org.sonar.db.dialect.MsSql;
  26. import org.sonar.db.dialect.Oracle;
  27. import static java.lang.String.format;
  28. import static java.util.Objects.requireNonNull;
  29. import static org.sonar.server.platform.db.migration.def.Validations.validateColumnName;
  30. /**
  31. * Used to define VARCHAR column
  32. */
  33. @Immutable
  34. public class VarcharColumnDef extends AbstractColumnDef {
  35. public static final int MAX_SIZE = 4_000;
  36. public static final int UUID_SIZE = 40;
  37. public static final int DESCRIPTION_SECTION_KEY_SIZE = 50;
  38. /**
  39. * UUID length of the USERS table is not using the standard UUID length.
  40. * The reason of this is because when the UUID column was introduced in the USERS table, existing rows were fed with the login, which has a length of 255.
  41. * @see <a https://jira.sonarsource.com/browse/SONAR-10597>SONAR-10597</a>
  42. */
  43. public static final int USER_UUID_SIZE = 255;
  44. private final int columnSize;
  45. private final boolean ignoreOracleUnit;
  46. private VarcharColumnDef(Builder builder) {
  47. super(builder.columnName, builder.isNullable, builder.defaultValue);
  48. this.columnSize = builder.columnSize;
  49. this.ignoreOracleUnit = builder.ignoreOracleUnit;
  50. }
  51. public static Builder newVarcharColumnDefBuilder() {
  52. return new Builder();
  53. }
  54. public static Builder newVarcharColumnDefBuilder(String column) {
  55. return newVarcharColumnDefBuilder().setColumnName(column);
  56. }
  57. public int getColumnSize() {
  58. return columnSize;
  59. }
  60. @Override
  61. public String generateSqlType(Dialect dialect) {
  62. switch (dialect.getId()) {
  63. case MsSql.ID:
  64. return format("NVARCHAR (%d)", columnSize);
  65. case Oracle.ID:
  66. return format("VARCHAR2 (%d%s)", columnSize, ignoreOracleUnit ? "" : " CHAR");
  67. default:
  68. return format("VARCHAR (%d)", columnSize);
  69. }
  70. }
  71. public static class Builder {
  72. @CheckForNull
  73. private Integer columnSize;
  74. @CheckForNull
  75. private String columnName;
  76. @CheckForNull
  77. private String defaultValue = null;
  78. private boolean isNullable = true;
  79. private boolean ignoreOracleUnit = false;
  80. public Builder setColumnName(String columnName) {
  81. this.columnName = validateColumnName(columnName);
  82. return this;
  83. }
  84. public Builder setLimit(int limit) {
  85. this.columnSize = limit;
  86. return this;
  87. }
  88. public Builder setIsNullable(boolean isNullable) {
  89. this.isNullable = isNullable;
  90. return this;
  91. }
  92. public Builder setDefaultValue(@Nullable String s) {
  93. this.defaultValue = s;
  94. return this;
  95. }
  96. /**
  97. * In order to not depend on value of runtime variable NLS_LENGTH_SEMANTICS, unit of length
  98. * is enforced to CHAR so that we're sure that type can't be BYTE.
  99. * Unit is ignored for the columns created before SonarQube 6.3 (except for issues.message which
  100. * has been fixed in migration 1151 of SonarQube 5.6. See SONAR-7493).
  101. *
  102. * In most cases this method should not be called with parameter {@code true} after
  103. * version 6.3.
  104. *
  105. * @param b whether unit of length is hardcoded to CHAR.
  106. */
  107. public Builder setIgnoreOracleUnit(boolean b) {
  108. this.ignoreOracleUnit = b;
  109. return this;
  110. }
  111. public VarcharColumnDef build() {
  112. validateColumnName(columnName);
  113. requireNonNull(columnSize, "Limit cannot be null");
  114. return new VarcharColumnDef(this);
  115. }
  116. }
  117. }