]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12251 use setNull(Types.DOUBLE) instead of setNull(Types.DECIMAL)
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 5 Jul 2019 09:51:50 +0000 (11:51 +0200)
committerSonarTech <sonartech@sonarsource.com>
Thu, 17 Oct 2019 13:24:47 +0000 (15:24 +0200)
on SQL Server only

server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/BaseSqlStatement.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DataChange.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MassUpdate.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/SelectImpl.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/UpsertImpl.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/UpsertImplTest.java

index 1253f4aa7275ad91fc7dccacff7181f45ccd62ee..e3a3dbcd2e7f7dcd84087b67af0f3d09948a51a5 100644 (file)
@@ -26,11 +26,15 @@ import java.sql.Types;
 import java.util.Date;
 import javax.annotation.Nullable;
 import org.apache.commons.dbutils.DbUtils;
+import org.sonar.db.dialect.Dialect;
+import org.sonar.db.dialect.MsSql;
 
 class BaseSqlStatement<CHILD extends SqlStatement> implements SqlStatement<CHILD> {
+  private final Dialect dialect;
   protected PreparedStatement pstmt;
 
-  protected BaseSqlStatement(PreparedStatement pstmt) {
+  protected BaseSqlStatement(Dialect dialect, PreparedStatement pstmt) {
+    this.dialect = dialect;
     this.pstmt = pstmt;
   }
 
@@ -95,7 +99,7 @@ class BaseSqlStatement<CHILD extends SqlStatement> implements SqlStatement<CHILD
   @SuppressWarnings("unchecked")
   public CHILD setDouble(int columnIndex, @Nullable Double value) throws SQLException {
     if (value == null) {
-      pstmt.setNull(columnIndex, Types.DECIMAL);
+      pstmt.setNull(columnIndex, MsSql.ID.equals(dialect.getId()) ? Types.DOUBLE : Types.DECIMAL);
     } else {
       pstmt.setDouble(columnIndex, value);
     }
index a6beab641fc40f1bed45ee360a565e6dc18a0d20..292b06ae53501705bc14973e5513cf0ca21eb023 100644 (file)
@@ -83,7 +83,7 @@ public abstract class DataChange implements MigrationStep {
     }
 
     public Upsert prepareUpsert(String sql) throws SQLException {
-      return UpsertImpl.create(writeConnection, sql);
+      return UpsertImpl.create(db, writeConnection, sql);
     }
 
     public MassUpdate prepareMassUpdate() {
index b9f9bc402f16f1d17dac7d82ade83195e38a0456..388ba621f241e6bb932a2420227e3e9ec0758f1f 100644 (file)
@@ -73,7 +73,7 @@ public class MassUpdate {
   }
 
   public Upsert update(String sql) throws SQLException {
-    UpsertImpl upsert = UpsertImpl.create(writeConnection, sql);
+    UpsertImpl upsert = UpsertImpl.create(db, writeConnection, sql);
     this.updates.add(upsert);
     return upsert;
   }
index 4d0ef0f7916e2e5e9005db355f381d1b5a5d434a..a079c08d4f9e969393d485c3675c4077bfdaffc0 100644 (file)
@@ -27,11 +27,12 @@ import java.util.ArrayList;
 import java.util.List;
 import org.apache.commons.dbutils.DbUtils;
 import org.sonar.db.Database;
+import org.sonar.db.dialect.Dialect;
 
 public class SelectImpl extends BaseSqlStatement<Select> implements Select {
 
-  private SelectImpl(PreparedStatement pstmt) {
-    super(pstmt);
+  private SelectImpl(Dialect dialect, PreparedStatement pstmt) {
+    super(dialect, pstmt);
   }
 
   @Override
@@ -93,6 +94,6 @@ public class SelectImpl extends BaseSqlStatement<Select> implements Select {
     // TODO use DbClient#newScrollingSelectStatement()
     PreparedStatement pstmt = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
     pstmt.setFetchSize(db.getDialect().getScrollDefaultFetchSize());
-    return new SelectImpl(pstmt);
+    return new SelectImpl(db.getDialect(), pstmt);
   }
 }
index 97007636427c10d1607221a1b2498729e852d22f..f1c4bd5525ebdd712febc3920e6c13b91c97233f 100644 (file)
@@ -22,6 +22,8 @@ package org.sonar.server.platform.db.migration.step;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.Dialect;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
@@ -32,8 +34,8 @@ public class UpsertImpl extends BaseSqlStatement<Upsert> implements Upsert {
   private int maxBatchSize = MAX_BATCH_SIZE;
   private long batchCount = 0L;
 
-  private UpsertImpl(PreparedStatement pstmt) {
-    super(pstmt);
+  private UpsertImpl(Dialect dialect, PreparedStatement pstmt) {
+    super(dialect, pstmt);
   }
 
   @Override
@@ -80,7 +82,7 @@ public class UpsertImpl extends BaseSqlStatement<Upsert> implements Upsert {
     return this;
   }
 
-  public static UpsertImpl create(Connection connection, String sql) throws SQLException {
-    return new UpsertImpl(connection.prepareStatement(sql));
+  public static UpsertImpl create(Database db, Connection connection, String sql) throws SQLException {
+    return new UpsertImpl(db.getDialect(), connection.prepareStatement(sql));
   }
 }
index 4e64063e643edf01e25f371dbd35750440da0aab..e4eb9e35641b2a6489f39aa5c15f603b2a5ff88b 100644 (file)
@@ -23,9 +23,12 @@ import java.sql.Connection;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.Dialect;
 
 import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class UpsertImplTest {
 
@@ -68,6 +71,8 @@ public class UpsertImplTest {
   }
 
   private UpsertImpl create() throws Exception {
-    return UpsertImpl.create(mock(Connection.class), "sql");
+    Database database = mock(Database.class);
+    when(database.getDialect()).thenReturn(mock(Dialect.class));
+    return UpsertImpl.create(database, mock(Connection.class), "sql");
   }
 }