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;
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 int getColumnSize() {
72 public String generateSqlType(Dialect dialect) {
73 switch (dialect.getId()) {
75 return format("NVARCHAR (%d)", columnSize);
77 return format("VARCHAR2 (%d%s)", columnSize, ignoreOracleUnit ? "" : " CHAR");
79 return format("VARCHAR (%d)", columnSize);
83 public static class Builder {
85 private Integer columnSize;
87 private String columnName;
89 private String defaultValue = null;
90 private boolean isNullable = true;
91 private boolean ignoreOracleUnit = false;
93 public Builder setColumnName(String columnName) {
94 this.columnName = validateColumnName(columnName);
98 public Builder setLimit(int limit) {
99 this.columnSize = limit;
103 public Builder setIsNullable(boolean isNullable) {
104 this.isNullable = isNullable;
108 public Builder setDefaultValue(@Nullable String s) {
109 this.defaultValue = s;
114 * In order to not depend on value of runtime variable NLS_LENGTH_SEMANTICS, unit of length
115 * is enforced to CHAR so that we're sure that type can't be BYTE.
116 * Unit is ignored for the columns created before SonarQube 6.3 (except for issues.message which
117 * has been fixed in migration 1151 of SonarQube 5.6. See SONAR-7493).
119 * In most cases this method should not be called with parameter {@code true} after
122 * @param b whether unit of length is hardcoded to CHAR.
124 public Builder setIgnoreOracleUnit(boolean b) {
125 this.ignoreOracleUnit = b;
129 public VarcharColumnDef build() {
130 validateColumnName(columnName);
131 requireNonNull(columnSize, "Limit cannot be null");
132 return new VarcharColumnDef(this);