aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java
blob: f771b94e07a74ba5d2e360f67ac9147afcb534c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.db.version;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import org.sonar.core.util.stream.Collectors;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.H2;
import org.sonar.db.dialect.MsSql;
import org.sonar.db.dialect.MySql;
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Stream.of;
import static org.sonar.db.version.Validations.validateConstraintName;
import static org.sonar.db.version.Validations.validateTableName;

public class CreateTableBuilder {

  private final Dialect dialect;
  private final String tableName;
  private final List<ColumnDef> columnDefs = new ArrayList<>();
  private final List<ColumnDef> pkColumnDefs = new ArrayList<>(2);
  private final Multimap<ColumnDef, ColumnFlag> flagsByColumn = HashMultimap.create(1, 1);
  @CheckForNull
  private String pkConstraintName;

  public CreateTableBuilder(Dialect dialect, String tableName) {
    this.dialect = requireNonNull(dialect, "dialect can't be null");
    this.tableName = validateTableName(tableName);
  }

  public List<String> build() {
    checkState(!columnDefs.isEmpty() || !pkColumnDefs.isEmpty(), "at least one column must be specified");

    return Stream.concat(of(createTableStatement()), createOracleAutoIncrementStatements())
      .collect(Collectors.toList());
  }

  public CreateTableBuilder addColumn(ColumnDef columnDef) {
    columnDefs.add(requireNonNull(columnDef, "column def can't be null"));
    return this;
  }

  public CreateTableBuilder addPkColumn(ColumnDef columnDef, ColumnFlag... flags) {
    pkColumnDefs.add(requireNonNull(columnDef, "column def can't be null"));
    addFlags(columnDef, flags);
    return this;
  }

  private void addFlags(ColumnDef columnDef, ColumnFlag[] flags) {
    Arrays.stream(flags)
      .forEach(flag -> {
        requireNonNull(flag, "flag can't be null");
        if (flag == ColumnFlag.AUTO_INCREMENT) {
          validateColumnDefForAutoIncrement(columnDef);
        }
        flagsByColumn.put(columnDef, flag);
      });
  }

  private void validateColumnDefForAutoIncrement(ColumnDef columnDef) {
    checkArgument("id".equals(columnDef.getName()),
      "Auto increment column name must be id");
    checkArgument(columnDef instanceof BigIntegerColumnDef
      || columnDef instanceof IntegerColumnDef,
      "Auto increment column must either be BigInteger or Integer");
    checkArgument(!columnDef.isNullable(),
      "Auto increment column can't be nullable");
    checkState(pkColumnDefs.stream().filter(this::isAutoIncrement).count() == 0,
      "There can't be more than one auto increment column");
  }

  public CreateTableBuilder withPkConstraintName(String pkConstraintName) {
    this.pkConstraintName = validateConstraintName(pkConstraintName);
    return this;
  }

  private String createTableStatement() {
    StringBuilder res = new StringBuilder("CREATE TABLE ");
    res.append(tableName);
    res.append(" (");
    appendPkColumns(res);
    appendColumns(res, dialect, columnDefs);
    appendPkConstraint(res);
    res.append(')');
    appendCollationClause(res, dialect);
    return res.toString();
  }

  private void appendPkColumns(StringBuilder res) {
    appendColumns(res, dialect, pkColumnDefs);
    if (!pkColumnDefs.isEmpty() && !columnDefs.isEmpty()) {
      res.append(',');
    }
  }

  private void appendColumns(StringBuilder res, Dialect dialect, List<ColumnDef> columnDefs) {
    if (columnDefs.isEmpty()) {
      return;
    }
    Iterator<ColumnDef> columnDefIterator = columnDefs.iterator();
    while (columnDefIterator.hasNext()) {
      ColumnDef columnDef = columnDefIterator.next();
      res.append(columnDef.getName());
      res.append(' ');
      appendDataType(res, dialect, columnDef);
      appendDefaultValue(res, columnDef);
      appendNullConstraint(res, columnDef);
      appendColumnFlags(res, dialect, columnDef);
      if (columnDefIterator.hasNext()) {
        res.append(',');
      }
    }
  }

  private void appendDataType(StringBuilder res, Dialect dialect, ColumnDef columnDef) {
    if (PostgreSql.ID.equals(dialect.getId()) && isAutoIncrement(columnDef)) {
      if (columnDef instanceof BigIntegerColumnDef) {
        res.append("BIGSERIAL");
      } else if (columnDef instanceof IntegerColumnDef) {
        res.append("SERIAL");
      } else {
        throw new IllegalStateException("Column with autoincrement is neither BigInteger nor Integer");
      }
    } else {
      res.append(columnDef.generateSqlType(dialect));
    }
  }

  private boolean isAutoIncrement(ColumnDef columnDef) {
    Collection<ColumnFlag> columnFlags = this.flagsByColumn.get(columnDef);
    return columnFlags != null && columnFlags.contains(ColumnFlag.AUTO_INCREMENT);
  }

  private static void appendNullConstraint(StringBuilder res, ColumnDef columnDef) {
    if (columnDef.isNullable()) {
      res.append(" NULL");
    } else {
      res.append(" NOT NULL");
    }
  }

  private void appendDefaultValue(StringBuilder sql, ColumnDef columnDef) {
    Object defaultValue = columnDef.getDefaultValue();
    if (defaultValue != null) {
      sql.append(" DEFAULT ");

      if (defaultValue instanceof String) {
        sql.append(format("'%s'", defaultValue));
      } else if (defaultValue instanceof Boolean) {
        sql.append((boolean) defaultValue ? dialect.getTrueSqlValue() : dialect.getFalseSqlValue());
      } else {
        sql.append(defaultValue);
      }
    }
  }

  private void appendColumnFlags(StringBuilder res, Dialect dialect, ColumnDef columnDef) {
    Collection<ColumnFlag> columnFlags = this.flagsByColumn.get(columnDef);
    if (columnFlags != null && columnFlags.contains(ColumnFlag.AUTO_INCREMENT)) {
      switch (dialect.getId()) {
        case Oracle.ID:
          // no auto increment on Oracle, must use a sequence
          break;
        case PostgreSql.ID:
          // no specific clause on PostgreSQL but a specific type
          break;
        case MsSql.ID:
          res.append(" IDENTITY (0,1)");
          break;
        case MySql.ID:
          res.append(" AUTO_INCREMENT");
          break;
        case H2.ID:
          res.append(" AUTO_INCREMENT (0,1)");
          break;
        default:
          throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId());
      }
    }
  }

  private void appendPkConstraint(StringBuilder res) {
    if (pkColumnDefs.isEmpty()) {
      return;
    }
    res.append(", ");
    res.append("CONSTRAINT ");
    appendPkConstraintName(res);
    res.append(" PRIMARY KEY ");
    res.append('(');
    appendColumnNames(res, pkColumnDefs);
    res.append(')');
  }

  private void appendPkConstraintName(StringBuilder res) {
    if (pkConstraintName == null) {
      res.append("pk_").append(tableName);
    } else {
      res.append(pkConstraintName.toLowerCase(Locale.ENGLISH));
    }
  }

  private static void appendColumnNames(StringBuilder res, List<ColumnDef> columnDefs) {
    Iterator<ColumnDef> columnDefIterator = columnDefs.iterator();
    while (columnDefIterator.hasNext()) {
      res.append(columnDefIterator.next().getName());
      if (columnDefIterator.hasNext()) {
        res.append(',');
      }
    }
  }

  private static void appendCollationClause(StringBuilder res, Dialect dialect) {
    if (MySql.ID.equals(dialect.getId())) {
      res.append(" ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin");
    }
  }

  private Stream<String> createOracleAutoIncrementStatements() {
    if (!Oracle.ID.equals(dialect.getId())) {
      return Stream.empty();
    }
    return pkColumnDefs.stream()
      .filter(this::isAutoIncrement)
      .flatMap(columnDef -> of(createSequenceFor(tableName), createTriggerFor(tableName)));
  }

  private static String createSequenceFor(String tableName) {
    return "CREATE SEQUENCE " + tableName + "_seq START WITH 1 INCREMENT BY 1";
  }

  private static String createTriggerFor(String tableName) {
    return "CREATE OR REPLACE TRIGGER " + tableName + "_idt" +
      " BEFORE INSERT ON " + tableName +
      " FOR EACH ROW" +
      " BEGIN" +
      " IF :new.id IS null THEN" +
      " SELECT " + tableName + "_seq.nextval INTO :new.id FROM dual;" +
      " END IF;" +
      " END;";
  }

  public enum ColumnFlag {
    AUTO_INCREMENT
  }

}