From: Sébastien Lesaint Date: Mon, 12 Sep 2016 15:40:17 +0000 (+0200) Subject: SONAR-8025 add LOB storage clause to CreateTableBuilder for Oracle X-Git-Tag: 6.1-RC1~46 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2c8754e8bc9ac0faeb43055ed6fcb150befb8f55;p=sonarqube.git SONAR-8025 add LOB storage clause to CreateTableBuilder for Oracle --- diff --git a/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java index 46c498c99a4..b5677eb782f 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java +++ b/sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java @@ -25,7 +25,9 @@ import java.util.Iterator; 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; @@ -71,7 +73,8 @@ public class CreateTableBuilder { addPkColumns(res); addColumns(res, dialect, columnDefs); addPkConstraint(res); - res.append(")"); + res.append(')'); + addLOBStorageClause(res, dialect, columnDefs); return res.toString(); } @@ -136,4 +139,20 @@ public class CreateTableBuilder { } } + private void addLOBStorageClause(StringBuilder res, Dialect dialect, List columnDefs) { + if (!Oracle.ID.equals(dialect.getId())) { + return; + } + + List 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)"); + } + } + } diff --git a/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderDbTesterTest.java b/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderDbTesterTest.java index ff9ea5186f7..dc55da2bc03 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderDbTesterTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderDbTesterTest.java @@ -25,6 +25,7 @@ import org.sonar.api.utils.System2; 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; @@ -49,6 +50,8 @@ public class CreateTableBuilderDbTesterTest { .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(); diff --git a/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java b/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java index 160d6c8fd67..44a19c84cbc 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java @@ -34,12 +34,15 @@ import org.sonar.db.dialect.PostgreSql; 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 @@ -244,6 +247,60 @@ public class CreateTableBuilderTest { }); } + @Test + public void builds_adds_LOB_storage_clause_on_Oracle_for_CLOB_column() { + List 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 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 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 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"; }