import java.util.List;
import java.util.Locale;
import javax.annotation.CheckForNull;
+import org.sonar.core.util.stream.Collectors;
import org.sonar.db.dialect.Dialect;
+import org.sonar.db.dialect.Oracle;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
addPkColumns(res);
addColumns(res, dialect, columnDefs);
addPkConstraint(res);
- res.append(")");
+ res.append(')');
+ addLOBStorageClause(res, dialect, columnDefs);
return res.toString();
}
}
}
+ private void addLOBStorageClause(StringBuilder res, Dialect dialect, List<ColumnDef> columnDefs) {
+ if (!Oracle.ID.equals(dialect.getId())) {
+ return;
+ }
+
+ List<ColumnDef> clobColumnDefs = columnDefs.stream()
+ .filter(columnDef -> columnDef instanceof ClobColumnDef || columnDef instanceof BlobColumnDef)
+ .collect(Collectors.toList());
+ if (!clobColumnDefs.isEmpty()) {
+ res.append(" LOB (");
+ appendColumnNames(res, clobColumnDefs);
+ res.append(')');
+ res.append(" STORE AS SECUREFILE (RETENTION NONE NOCACHE NOLOGGING)");
+ }
+ }
+
}
import org.sonar.db.DbTester;
import static org.sonar.db.version.BigIntegerColumnDef.newBigIntegerColumnDefBuilder;
+import static org.sonar.db.version.BlobColumnDef.newBlobColumnDefBuilder;
import static org.sonar.db.version.BooleanColumnDef.newBooleanColumnDefBuilder;
import static org.sonar.db.version.ClobColumnDef.newClobColumnDefBuilder;
import static org.sonar.db.version.DecimalColumnDef.newDecimalColumnDefBuilder;
.addColumn(new TinyIntColumnDef.Builder().setColumnName("tiny_col_2").setIsNullable(false).build())
.addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col_1").setLimit(40).build())
.addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col_2").setLimit(40).setIsNullable(false).build())
+ .addColumn(newBlobColumnDefBuilder().setColumnName("blob_col_1").build())
+ .addColumn(newBlobColumnDefBuilder().setColumnName("blob_col_2").setIsNullable(false).build())
.build()
.iterator().next();
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.sonar.db.version.BigIntegerColumnDef.newBigIntegerColumnDefBuilder;
+import static org.sonar.db.version.BlobColumnDef.newBlobColumnDefBuilder;
import static org.sonar.db.version.BooleanColumnDef.newBooleanColumnDefBuilder;
+import static org.sonar.db.version.ClobColumnDef.newClobColumnDefBuilder;
import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;
public class CreateTableBuilderTest {
private static final H2 H2_DIALECT = new H2();
- private static final Dialect[] ALL_DIALECTS = {H2_DIALECT, new MySql(), new MsSql(), new PostgreSql(), new Oracle()};
+ private static final Oracle ORACLE = new Oracle();
+ private static final Dialect[] ALL_DIALECTS = {H2_DIALECT, new MySql(), new MsSql(), new PostgreSql(), ORACLE};
private static final String TABLE_NAME = "table_42";
@Rule
});
}
+ @Test
+ public void builds_adds_LOB_storage_clause_on_Oracle_for_CLOB_column() {
+ List<String> stmts = new CreateTableBuilder(ORACLE, TABLE_NAME)
+ .addColumn(newClobColumnDefBuilder().setColumnName("clob_1").setIsNullable(false).build())
+ .build();
+ assertThat(stmts).hasSize(1);
+
+ assertThat(stmts.iterator().next()).isEqualTo(
+ "CREATE TABLE " + TABLE_NAME + " (" +
+ "clob_1 CLOB NOT NULL)" +
+ " LOB (clob_1) STORE AS SECUREFILE (RETENTION NONE NOCACHE NOLOGGING)");
+ }
+
+ @Test
+ public void builds_adds_LOB_storage_clause_on_Oracle_for_BLOB_column() {
+ List<String> stmts = new CreateTableBuilder(ORACLE, TABLE_NAME)
+ .addColumn(newBlobColumnDefBuilder().setColumnName("blob_1").setIsNullable(false).build())
+ .build();
+ assertThat(stmts).hasSize(1);
+
+ assertThat(stmts.iterator().next()).isEqualTo(
+ "CREATE TABLE " + TABLE_NAME + " (" +
+ "blob_1 BLOB NOT NULL)" +
+ " LOB (blob_1) STORE AS SECUREFILE (RETENTION NONE NOCACHE NOLOGGING)");
+ }
+
+ @Test
+ public void build_does_not_add_LOB_storage_clause_for_CLOB_column_for_other_than_Oracle() {
+ Arrays.stream(ALL_DIALECTS)
+ .filter(dialect -> dialect != ORACLE)
+ .forEach(dialect -> {
+ List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME)
+ .addColumn(newClobColumnDefBuilder().setColumnName("clob_1").setIsNullable(false).build())
+ .build();
+ assertThat(stmts).hasSize(1);
+
+ assertThat(stmts.iterator().next()).doesNotContain("STORE AS SECUREFILE");
+ });
+ }
+
+ @Test
+ public void build_does_not_add_LOB_storage_clause_for_BLOB_column_for_other_than_Oracle() {
+ Arrays.stream(ALL_DIALECTS)
+ .filter(dialect -> dialect != ORACLE)
+ .forEach(dialect -> {
+ List<String> stmts = new CreateTableBuilder(dialect, TABLE_NAME)
+ .addColumn(newBlobColumnDefBuilder().setColumnName("blob_1").setIsNullable(false).build())
+ .build();
+ assertThat(stmts).hasSize(1);
+
+ assertThat(stmts.iterator().next()).doesNotContain("STORE AS SECUREFILE");
+ });
+ }
+
private static String bigIntSqlType(Dialect dialect) {
return Oracle.ID.equals(dialect.getId()) ? "NUMBER (38)" : "BIGINT";
}