]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8025 add LOB storage clause to CreateTableBuilder for Oracle
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 12 Sep 2016 15:40:17 +0000 (17:40 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 16 Sep 2016 10:22:11 +0000 (12:22 +0200)
sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java
sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderDbTesterTest.java
sonar-db/src/test/java/org/sonar/db/version/CreateTableBuilderTest.java

index 46c498c99a4c3533d67c5635d539899b90a1fff8..b5677eb782f656314f2aebc121e573a9a45f0fb7 100644 (file)
@@ -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<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)");
+    }
+  }
+
 }
index ff9ea5186f7bd33140d9b7932b943bad662ee3b6..dc55da2bc037da67580af575f72d6c0c06f57701 100644 (file)
@@ -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();
 
index 160d6c8fd676bdcb435792a8dfc7ccca676de442..44a19c84cbc46ebb2f503f2c5b3cb36e131694de 100644 (file)
@@ -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<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";
   }