on SQL Server onlytags/8.0
@@ -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); | |||
} |
@@ -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() { |
@@ -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; | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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"); | |||
} | |||
} |