aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/main
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-12-07 10:44:50 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2016-12-12 13:05:19 +0100
commit0690781a55308bdec3dca95f8cefa2d2838636b8 (patch)
tree91cf321430172622327497ffa72d5d6af48a58da /sonar-db/src/main
parent1b16981bab6f9309b80fa08dde67aef9a2b3dc1d (diff)
downloadsonarqube-0690781a55308bdec3dca95f8cefa2d2838636b8.tar.gz
sonarqube-0690781a55308bdec3dca95f8cefa2d2838636b8.zip
SONAR-5471 support default value in boolean and varchar columns
Diffstat (limited to 'sonar-db/src/main')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java13
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java13
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java4
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java11
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java7
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java3
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/CreateTableBuilder.java17
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java2
-rw-r--r--sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java11
13 files changed, 74 insertions, 15 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java
index 3d1c3d5539d..d9aae31fb29 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/AbstractColumnDef.java
@@ -19,13 +19,19 @@
*/
package org.sonar.db.version;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
public abstract class AbstractColumnDef implements ColumnDef {
private final String columnName;
private final boolean isNullable;
+ @CheckForNull
+ private final Object defaultValue;
- public AbstractColumnDef(String columnName, boolean isNullable) {
+ public AbstractColumnDef(String columnName, boolean isNullable, @Nullable Object defaultValue) {
this.columnName = columnName;
this.isNullable = isNullable;
+ this.defaultValue = defaultValue;
}
@Override
@@ -37,4 +43,9 @@ public abstract class AbstractColumnDef implements ColumnDef {
public boolean isNullable() {
return isNullable;
}
+
+ @Override
+ public Object getDefaultValue() {
+ return defaultValue;
+ }
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java
index f61a30dd3df..3806cca6c4a 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/AddColumnsBuilder.java
@@ -25,6 +25,7 @@ import org.sonar.db.dialect.MsSql;
import org.sonar.db.dialect.PostgreSql;
import static com.google.common.collect.Lists.newArrayList;
+import static java.lang.String.format;
import static org.sonar.db.version.Validations.validateTableName;
/**
@@ -80,6 +81,18 @@ public class AddColumnsBuilder {
private void addColumn(StringBuilder sql, ColumnDef columnDef) {
sql.append(columnDef.getName()).append(" ").append(columnDef.generateSqlType(dialect));
+ Object defaultValue = columnDef.getDefaultValue();
+ if (defaultValue != null) {
+ sql.append(" DEFAULT ");
+ // TODO remove duplication with CreateTableBuilder
+ 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);
+ }
+ }
sql.append(columnDef.isNullable() ? " NULL" : " NOT NULL");
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java b/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java
index da9765e428f..81e571d6b27 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/AlterColumnsBuilder.java
@@ -28,6 +28,7 @@ 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.collect.Lists.newArrayList;
/**
@@ -54,6 +55,8 @@ public class AlterColumnsBuilder {
}
public AlterColumnsBuilder updateColumn(ColumnDef columnDef) {
+ // limitation of Oracle, only attribute changes must be defined in ALTER.
+ checkArgument(columnDef.getDefaultValue()==null, "Default value is not supported on alter of column '%s'", columnDef.getName());
columnDefs.add(columnDef);
return this;
}
@@ -137,6 +140,7 @@ public class AlterColumnsBuilder {
if (addNotNullableProperty) {
sql.append(columnDef.isNullable() ? " NULL" : " NOT NULL");
}
+
}
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java
index dc62060e71c..c5eea2195ae 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/BigIntegerColumnDef.java
@@ -28,7 +28,7 @@ import static org.sonar.db.version.Validations.validateColumnName;
public class BigIntegerColumnDef extends AbstractColumnDef {
private BigIntegerColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
}
public static Builder newBigIntegerColumnDefBuilder() {
diff --git a/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java
index 31e51fb9897..9d94c404d30 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/BlobColumnDef.java
@@ -31,7 +31,7 @@ import static org.sonar.db.version.Validations.validateColumnName;
public class BlobColumnDef extends AbstractColumnDef {
public BlobColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
}
@Override
diff --git a/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java
index c2cd7d17789..6503b89127e 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/BooleanColumnDef.java
@@ -19,6 +19,7 @@
*/
package org.sonar.db.version;
+import javax.annotation.Nullable;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.H2;
import org.sonar.db.dialect.MsSql;
@@ -32,8 +33,9 @@ import static org.sonar.db.version.Validations.validateColumnName;
* Used to define VARCHAR column
*/
public class BooleanColumnDef extends AbstractColumnDef {
+
private BooleanColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, builder.defaultValue);
}
public static Builder newBooleanColumnDefBuilder() {
@@ -59,7 +61,7 @@ public class BooleanColumnDef extends AbstractColumnDef {
public static class Builder {
private String columnName;
-
+ private Boolean defaultValue = null;
private boolean isNullable = true;
public Builder setColumnName(String columnName) {
@@ -72,6 +74,11 @@ public class BooleanColumnDef extends AbstractColumnDef {
return this;
}
+ public Builder setDefaultValue(@Nullable Boolean b) {
+ this.defaultValue = b;
+ return this;
+ }
+
public BooleanColumnDef build() {
validateColumnName(columnName);
return new BooleanColumnDef(this);
diff --git a/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java
index a57f0043a2e..93e342bafe5 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/ClobColumnDef.java
@@ -19,7 +19,6 @@
*/
package org.sonar.db.version;
-import javax.annotation.CheckForNull;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.H2;
import org.sonar.db.dialect.MsSql;
@@ -31,13 +30,11 @@ import static org.sonar.db.version.Validations.validateColumnName;
/**
* Used to define CLOB columns
- *
- * Warning, for the moment it's only supporting MsSQL
*/
public class ClobColumnDef extends AbstractColumnDef {
private ClobColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
}
public static Builder newClobColumnDefBuilder() {
@@ -63,9 +60,7 @@ public class ClobColumnDef extends AbstractColumnDef {
}
public static class Builder {
- @CheckForNull
private String columnName;
-
private boolean isNullable = true;
public Builder setColumnName(String columnName) {
diff --git a/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java
index 91d987e1838..7fc8e4bd013 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/ColumnDef.java
@@ -19,6 +19,7 @@
*/
package org.sonar.db.version;
+import javax.annotation.CheckForNull;
import org.sonar.db.dialect.Dialect;
public interface ColumnDef {
@@ -29,4 +30,6 @@ public interface ColumnDef {
String generateSqlType(Dialect dialect);
+ @CheckForNull
+ Object getDefaultValue();
}
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 84b34fda5b4..f771b94e07a 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
@@ -39,6 +39,7 @@ 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;
@@ -134,6 +135,7 @@ public class CreateTableBuilder {
res.append(columnDef.getName());
res.append(' ');
appendDataType(res, dialect, columnDef);
+ appendDefaultValue(res, columnDef);
appendNullConstraint(res, columnDef);
appendColumnFlags(res, dialect, columnDef);
if (columnDefIterator.hasNext()) {
@@ -169,6 +171,21 @@ public class CreateTableBuilder {
}
}
+ 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)) {
diff --git a/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java
index ba4c46358ef..69c466958de 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/DecimalColumnDef.java
@@ -38,7 +38,7 @@ public class DecimalColumnDef extends AbstractColumnDef {
private final int scale;
private DecimalColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
this.precision = builder.precision;
this.scale = builder.scale;
}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java
index 8b41a8a4ed8..5b2098572fe 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/IntegerColumnDef.java
@@ -32,7 +32,7 @@ import static org.sonar.db.version.Validations.validateColumnName;
public class IntegerColumnDef extends AbstractColumnDef {
private IntegerColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
}
public static Builder newIntegerColumnDefBuilder() {
diff --git a/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java
index 81fe6d06eb4..caf02b08d2b 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/TinyIntColumnDef.java
@@ -35,7 +35,7 @@ import static org.sonar.db.version.Validations.validateColumnName;
public class TinyIntColumnDef extends AbstractColumnDef {
private TinyIntColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, null);
}
@Override
diff --git a/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java
index cfab9db27e5..319f6869004 100644
--- a/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java
+++ b/sonar-db/src/main/java/org/sonar/db/version/VarcharColumnDef.java
@@ -20,6 +20,7 @@
package org.sonar.db.version;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.MsSql;
@@ -42,7 +43,7 @@ public class VarcharColumnDef extends AbstractColumnDef {
private final int columnSize;
private VarcharColumnDef(Builder builder) {
- super(builder.columnName, builder.isNullable);
+ super(builder.columnName, builder.isNullable, builder.defaultValue);
this.columnSize = builder.columnSize;
}
@@ -71,6 +72,9 @@ public class VarcharColumnDef extends AbstractColumnDef {
private boolean isNullable = true;
+ @CheckForNull
+ public String defaultValue = null;
+
public Builder setColumnName(String columnName) {
this.columnName = validateColumnName(columnName);
return this;
@@ -86,6 +90,11 @@ public class VarcharColumnDef extends AbstractColumnDef {
return this;
}
+ public Builder setDefaultValue(@Nullable String s) {
+ this.defaultValue = s;
+ return this;
+ }
+
public VarcharColumnDef build() {
validateColumnName(columnName);
requireNonNull(columnSize, "Limit cannot be null");