3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.platform.db.migration.def;
22 import javax.annotation.CheckForNull;
23 import javax.annotation.Nullable;
24 import javax.annotation.concurrent.Immutable;
25 import org.sonar.db.dialect.Dialect;
26 import org.sonar.db.dialect.MsSql;
27 import org.sonar.db.dialect.Oracle;
29 import static java.lang.String.format;
30 import static java.util.Objects.requireNonNull;
31 import static org.sonar.server.platform.db.migration.def.Validations.validateColumnName;
34 * Used to define VARCHAR column
37 public class VarcharColumnDef extends AbstractColumnDef {
38 public static final int MAX_SIZE = 4_000;
40 * @deprecated use {@link #UUID_SIZE} instead
43 public static final int UUID_VARCHAR_SIZE = 50;
44 public static final int UUID_SIZE = 40;
46 public static final int DESCRIPTION_SECTION_KEY_SIZE = 50;
48 * UUID length of the USERS table is not using the standard UUID length.
49 * 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.
50 * @see <a https://jira.sonarsource.com/browse/SONAR-10597>SONAR-10597</a>
52 public static final int USER_UUID_SIZE = 255;
54 private final int columnSize;
55 private final boolean ignoreOracleUnit;
57 private VarcharColumnDef(Builder builder) {
58 super(builder.columnName, builder.isNullable, builder.defaultValue);
59 this.columnSize = builder.columnSize;
60 this.ignoreOracleUnit = builder.ignoreOracleUnit;
63 public static Builder newVarcharColumnDefBuilder() {
67 public static Builder newVarcharColumnDefBuilder(String column) {
68 return newVarcharColumnDefBuilder().setColumnName(column);
71 public int getColumnSize() {
76 public String generateSqlType(Dialect dialect) {
77 switch (dialect.getId()) {
79 return format("NVARCHAR (%d)", columnSize);
81 return format("VARCHAR2 (%d%s)", columnSize, ignoreOracleUnit ? "" : " CHAR");
83 return format("VARCHAR (%d)", columnSize);
87 public static class Builder {
89 private Integer columnSize;
91 private String columnName;
93 private String defaultValue = null;
94 private boolean isNullable = true;
95 private boolean ignoreOracleUnit = false;
97 public Builder setColumnName(String columnName) {
98 this.columnName = validateColumnName(columnName);
102 public Builder setLimit(int limit) {
103 this.columnSize = limit;
107 public Builder setIsNullable(boolean isNullable) {
108 this.isNullable = isNullable;
112 public Builder setDefaultValue(@Nullable String s) {
113 this.defaultValue = s;
118 * In order to not depend on value of runtime variable NLS_LENGTH_SEMANTICS, unit of length
119 * is enforced to CHAR so that we're sure that type can't be BYTE.
120 * Unit is ignored for the columns created before SonarQube 6.3 (except for issues.message which
121 * has been fixed in migration 1151 of SonarQube 5.6. See SONAR-7493).
123 * In most cases this method should not be called with parameter {@code true} after
126 * @param b whether unit of length is hardcoded to CHAR.
128 public Builder setIgnoreOracleUnit(boolean b) {
129 this.ignoreOracleUnit = b;
133 public VarcharColumnDef build() {
134 validateColumnName(columnName);
135 requireNonNull(columnSize, "Limit cannot be null");
136 return new VarcharColumnDef(this);