From ff001c531c3b03bc43fa4d8ae78896c7f6815950 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 7 Dec 2016 10:45:16 +0100 Subject: [PATCH] SONAR-5471 support timestamp type --- .../sonar/db/version/TimestampColumnDef.java | 85 ++++++++++++++++++ .../db/version/TimestampColumnDefTest.java | 90 +++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 sonar-db/src/main/java/org/sonar/db/version/TimestampColumnDef.java create mode 100644 sonar-db/src/test/java/org/sonar/db/version/TimestampColumnDefTest.java diff --git a/sonar-db/src/main/java/org/sonar/db/version/TimestampColumnDef.java b/sonar-db/src/main/java/org/sonar/db/version/TimestampColumnDef.java new file mode 100644 index 00000000000..62685d482e9 --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/TimestampColumnDef.java @@ -0,0 +1,85 @@ +/* + * 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 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 org.sonar.db.version.Validations.validateColumnName; + +/** + * Used to define TIMESTAMP columns. + * + * @deprecated implemented for compatibility with old tables, but {@link BigIntegerColumnDef} + * must be used for storing datetimes as bigints (no problems regarding timezone + * nor MySQL precision). + */ +@Deprecated +public class TimestampColumnDef extends AbstractColumnDef { + + private TimestampColumnDef(Builder builder) { + super(builder.columnName, builder.isNullable, null); + } + + public static Builder newTimestampColumnDefBuilder() { + return new Builder(); + } + + @Override + public String generateSqlType(Dialect dialect) { + switch (dialect.getId()) { + case MsSql.ID: + case MySql.ID: + return "DATETIME"; + case Oracle.ID: + return "TIMESTAMP (6)"; + case H2.ID: + case PostgreSql.ID: + return "TIMESTAMP"; + default: + throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); + } + } + + public static class Builder { + private String columnName; + private boolean isNullable = true; + + public Builder setColumnName(String columnName) { + this.columnName = validateColumnName(columnName); + return this; + } + + public Builder setIsNullable(boolean b) { + this.isNullable = b; + return this; + } + + public TimestampColumnDef build() { + validateColumnName(columnName); + return new TimestampColumnDef(this); + } + } + +} diff --git a/sonar-db/src/test/java/org/sonar/db/version/TimestampColumnDefTest.java b/sonar-db/src/test/java/org/sonar/db/version/TimestampColumnDefTest.java new file mode 100644 index 00000000000..1d68c91c9c4 --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/version/TimestampColumnDefTest.java @@ -0,0 +1,90 @@ +/* + * 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 org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +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 org.assertj.core.api.Assertions.assertThat; +import static org.sonar.db.version.TimestampColumnDef.newTimestampColumnDefBuilder; + +public class TimestampColumnDefTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void build_column_def() throws Exception { + TimestampColumnDef def = newTimestampColumnDefBuilder() + .setColumnName("created_at") + .setIsNullable(false) + .build(); + + assertThat(def.getName()).isEqualTo("created_at"); + assertThat(def.isNullable()).isFalse(); + assertThat(def.getDefaultValue()).isNull(); + } + + @Test + public void build_column_def_with_only_required_attributes() throws Exception { + TimestampColumnDef def = newTimestampColumnDefBuilder() + .setColumnName("created_at") + .build(); + + assertThat(def.getName()).isEqualTo("created_at"); + assertThat(def.isNullable()).isTrue(); + assertThat(def.getDefaultValue()).isNull(); + } + + @Test + public void generate_sql_type() throws Exception { + TimestampColumnDef def = newTimestampColumnDefBuilder() + .setColumnName("created_at") + .build(); + + assertThat(def.generateSqlType(new H2())).isEqualTo("TIMESTAMP"); + assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("TIMESTAMP"); + assertThat(def.generateSqlType(new MsSql())).isEqualTo("DATETIME"); + assertThat(def.generateSqlType(new MySql())).isEqualTo("DATETIME"); + assertThat(def.generateSqlType(new Oracle())).isEqualTo("TIMESTAMP (6)"); + } + + @Test + public void fail_with_NPE_if_name_is_null() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("Column name cannot be null"); + + newTimestampColumnDefBuilder().setColumnName(null); + } + + @Test + public void fail_with_NPE_if_no_name() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("Column name cannot be null"); + + newTimestampColumnDefBuilder().build(); + } +} -- 2.39.5