3 * Copyright (C) 2009-2022 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;
47 * UUID length of the USERS table is not using the standard UUID length.
48 * 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.
49 * @see <a https://jira.sonarsource.com/browse/SONAR-10597>SONAR-10597</a>
51 public static final int USER_UUID_SIZE = 255;
53 private final int columnSize;
54 private final boolean ignoreOracleUnit;
56 private VarcharColumnDef(Builder builder) {
57 super(builder.columnName, builder.isNullable, builder.defaultValue);
58 this.columnSize = builder.columnSize;
59 this.ignoreOracleUnit = builder.ignoreOracleUnit;
62 public static Builder newVarcharColumnDefBuilder() {
66 public int getColumnSize() {
71 public String generateSqlType(Dialect dialect) {
72 switch (dialect.getId()) {
74 return format("NVARCHAR (%d)", columnSize);
76 return format("VARCHAR2 (%d%s)", columnSize, ignoreOracleUnit ? "" : " CHAR");
78 return format("VARCHAR (%d)", columnSize);
82 public static class Builder {
84 private Integer columnSize;
86 private String columnName;
88 private String defaultValue = null;
89 private boolean isNullable = true;
90 private boolean ignoreOracleUnit = false;
92 public Builder setColumnName(String columnName) {
93 this.columnName = validateColumnName(columnName);
97 public Builder setLimit(int limit) {
98 this.columnSize = limit;
102 public Builder setIsNullable(boolean isNullable) {
103 this.isNullable = isNullable;
107 public Builder setDefaultValue(@Nullable String s) {
108 this.defaultValue = s;
113 * In order to not depend on value of runtime variable NLS_LENGTH_SEMANTICS, unit of length
114 * is enforced to CHAR so that we're sure that type can't be BYTE.
115 * Unit is ignored for the columns created before SonarQube 6.3 (except for issues.message which
116 * has been fixed in migration 1151 of SonarQube 5.6. See SONAR-7493).
118 * In most cases this method should not be called with parameter {@code true} after
121 * @param b whether unit of length is hardcoded to CHAR.
123 public Builder setIgnoreOracleUnit(boolean b) {
124 this.ignoreOracleUnit = b;
128 public VarcharColumnDef build() {
129 validateColumnName(columnName);
130 requireNonNull(columnSize, "Limit cannot be null");
131 return new VarcharColumnDef(this);