3 * Copyright (C) 2009-2017 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 private final int columnSize;
47 private final boolean ignoreOracleUnit;
49 private VarcharColumnDef(Builder builder) {
50 super(builder.columnName, builder.isNullable, builder.defaultValue);
51 this.columnSize = builder.columnSize;
52 this.ignoreOracleUnit = builder.ignoreOracleUnit;
55 public static Builder newVarcharColumnDefBuilder() {
59 public int getColumnSize() {
64 public String generateSqlType(Dialect dialect) {
65 switch (dialect.getId()) {
67 return format("NVARCHAR (%d)", columnSize);
69 return format("VARCHAR (%d%s)", columnSize, ignoreOracleUnit ? "" : " CHAR");
71 return format("VARCHAR (%d)", columnSize);
75 public static class Builder {
77 private Integer columnSize;
79 private String columnName;
81 private String defaultValue = null;
82 private boolean isNullable = true;
83 private boolean ignoreOracleUnit = false;
85 public Builder setColumnName(String columnName) {
86 this.columnName = validateColumnName(columnName);
90 public Builder setLimit(int limit) {
91 this.columnSize = limit;
95 public Builder setIsNullable(boolean isNullable) {
96 this.isNullable = isNullable;
100 public Builder setDefaultValue(@Nullable String s) {
101 this.defaultValue = s;
106 * In order to not depend on value of runtime variable NLS_LENGTH_SEMANTICS, unit of length
107 * is enforced to CHAR so that we're sure that type can't be BYTE.
108 * Unit is ignored for the columns created before SonarQube 6.3 (except for issues.message which
109 * has been fixed in migration 1151 of SonarQube 5.6. See SONAR-7493).
111 * In most cases this method should not be called with parameter {@code true} after
114 * @param b whether unit of length is hardcoded to CHAR.
116 public Builder setIgnoreOracleUnit(boolean b) {
117 this.ignoreOracleUnit = b;
121 public VarcharColumnDef build() {
122 validateColumnName(columnName);
123 requireNonNull(columnSize, "Limit cannot be null");
124 return new VarcharColumnDef(this);