]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8445 move DataChange subclasses from sonar-db to sonar-db-migration
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 15 Dec 2016 10:11:00 +0000 (11:11 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 16 Dec 2016 13:35:27 +0000 (14:35 +0100)
56 files changed:
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/BaseSqlStatement.java [new file with mode: 0644]
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 [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/Select.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/SelectImpl.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/SqlStatement.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/Upsert.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/UpsertImpl.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v561/UpdateUsersExternalIdentityWhenEmpty.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/CleanEventsWithoutAnalysisUuid.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/CleanEventsWithoutSnapshotId.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/CleanMeasuresWithNullAnalysisUuid.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInProjects.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInResourceIndex.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInSnapshots.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/CleanUsurperRootComponents.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/DeleteOrphanDuplicationsIndexRowsWithoutComponentOrAnalysis.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/DeleteOrphanMeasuresWithoutComponent.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/DropTreesOfSnapshots.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/FixProjectUuidOfDeveloperProjects.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateAnalysisUuidColumnOnCeActivity.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateAnalysisUuidOnEvents.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateAnalysisUuidOnMeasures.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateComponentUuidAndAnalysisUuidOfDuplicationsIndex.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateComponentUuidColumnsOfSnapshots.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateComponentUuidOfMeasures.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateLastUsedColumnOfRulesProfiles.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateProfileKeyOfActivities.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateUserUpdatedAtOfRulesProfiles.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnOnSnapshots.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfProjects.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfResourceIndex.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateUuidPathColumnOnProjects.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/RemoveUsersPasswordWhenNotLocal.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v61/CopyActivitiesToQprofileChanges.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v61/DeleteProjectDashboards.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v61/PopulateTableProperties2.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v61/RemoveViewsDefinitionFromProperties.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v62/CreateDefaultOrganization.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v62/DeletePermissionShareDashboard.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v62/PopulateIsRootColumnOnTableUsers.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v62/PopulateOrganizationUuidOfGroupRoles.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v62/PopulateOrganizationUuidOfGroups.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v62/PopulateOrganizationUuidOfPermissionTemplates.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v62/PopulateOrganizationUuidOfUserRoles.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v62/UpdateQualityGateConditionsOnCoverage.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/PopulateUuidColumnOfEvents.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/step/DataChangeTest.java
server/sonar-server/src/test/resources/org/sonar/server/computation/task/projectanalysis/filemove/FileMoveDetectionStepTest/v1/DeleteOrphanDuplicationsIndexRowsWithoutComponent.java
sonar-db/src/main/java/org/sonar/db/version/BaseSqlStatement.java [deleted file]
sonar-db/src/main/java/org/sonar/db/version/MassUpdate.java [deleted file]
sonar-db/src/main/java/org/sonar/db/version/Select.java [deleted file]
sonar-db/src/main/java/org/sonar/db/version/SelectImpl.java [deleted file]
sonar-db/src/main/java/org/sonar/db/version/SqlStatement.java [deleted file]
sonar-db/src/main/java/org/sonar/db/version/Upsert.java [deleted file]
sonar-db/src/main/java/org/sonar/db/version/UpsertImpl.java [deleted file]

diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/BaseSqlStatement.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/BaseSqlStatement.java
new file mode 100644 (file)
index 0000000..622bc1d
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * 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.server.platform.db.migration.step;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.sql.Types;
+import java.util.Date;
+import javax.annotation.Nullable;
+import org.apache.commons.dbutils.DbUtils;
+
+class BaseSqlStatement<CHILD extends SqlStatement> implements SqlStatement<CHILD> {
+  protected PreparedStatement pstmt;
+
+  protected BaseSqlStatement(PreparedStatement pstmt) {
+    this.pstmt = pstmt;
+  }
+
+  @Override
+  public void close() {
+    DbUtils.closeQuietly(pstmt);
+    pstmt = null;
+  }
+
+  @Override
+  public CHILD setString(int columnIndex, @Nullable String value) throws SQLException {
+    pstmt.setString(columnIndex, value);
+    return (CHILD) this;
+  }
+
+  @Override
+  public CHILD setBytes(int columnIndex, @Nullable byte[] value) throws SQLException {
+    pstmt.setBytes(columnIndex, value);
+    return (CHILD) this;
+  }
+
+  @Override
+  public CHILD setInt(int columnIndex, @Nullable Integer value) throws SQLException {
+    if (value == null) {
+      pstmt.setNull(columnIndex, Types.INTEGER);
+    } else {
+      pstmt.setInt(columnIndex, value);
+    }
+    return (CHILD) this;
+  }
+
+  @Override
+  public CHILD setLong(int columnIndex, @Nullable Long value) throws SQLException {
+    if (value == null) {
+      pstmt.setNull(columnIndex, Types.BIGINT);
+    } else {
+      pstmt.setLong(columnIndex, value);
+    }
+    return (CHILD) this;
+  }
+
+  @Override
+  public CHILD setBoolean(int columnIndex, @Nullable Boolean value) throws SQLException {
+    if (value == null) {
+      pstmt.setNull(columnIndex, Types.BOOLEAN);
+    } else {
+      pstmt.setBoolean(columnIndex, value);
+    }
+    return (CHILD) this;
+  }
+
+  @Override
+  public CHILD setDouble(int columnIndex, @Nullable Double value) throws SQLException {
+    if (value == null) {
+      pstmt.setNull(columnIndex, Types.DECIMAL);
+    } else {
+      pstmt.setDouble(columnIndex, value);
+    }
+    return (CHILD) this;
+  }
+
+  @Override
+  public CHILD setDate(int columnIndex, @Nullable Date value) throws SQLException {
+    if (value == null) {
+      pstmt.setNull(columnIndex, Types.TIMESTAMP);
+    } else {
+      pstmt.setTimestamp(columnIndex, new Timestamp(value.getTime()));
+    }
+    return (CHILD) this;
+  }
+}
index 7f254c051f7c78e79ace791d09db4b44e342146b..398d2b865607326a3e7e4806d9df49498b023a33 100644 (file)
@@ -22,11 +22,6 @@ package org.sonar.server.platform.db.migration.step;
 import java.sql.Connection;
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SelectImpl;
-import org.sonar.db.version.Upsert;
-import org.sonar.db.version.UpsertImpl;
 
 public abstract class DataChange implements MigrationStep {
 
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MassUpdate.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/MassUpdate.java
new file mode 100644 (file)
index 0000000..20c808b
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * 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.server.platform.db.migration.step;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
+import org.sonar.core.util.ProgressLogger;
+import org.sonar.db.Database;
+
+import static com.google.common.base.Preconditions.checkState;
+
+public class MassUpdate {
+
+  @FunctionalInterface
+  public interface Handler {
+    /**
+     * Convert some column values of a given row.
+     *
+     * @return true if the row must be updated, else false. If false, then the update parameter must not be touched.
+     */
+    boolean handle(Select.Row row, SqlStatement update) throws SQLException;
+  }
+
+  @FunctionalInterface
+  public interface MultiHandler {
+    /**
+     * Convert some column values of a given row.
+     *
+     * @param updateIndex 0-based
+     * @return true if the row must be updated, else false. If false, then the update parameter must not be touched.
+     */
+    boolean handle(Select.Row row, SqlStatement update, int updateIndex) throws SQLException;
+  }
+
+  private final Database db;
+  private final Connection readConnection;
+  private final Connection writeConnection;
+  private final AtomicLong counter = new AtomicLong(0L);
+  private final ProgressLogger progress = ProgressLogger.create(getClass(), counter);
+
+  private Select select;
+  private List<UpsertImpl> updates = new ArrayList<>(1);
+
+  public MassUpdate(Database db, Connection readConnection, Connection writeConnection) {
+    this.db = db;
+    this.readConnection = readConnection;
+    this.writeConnection = writeConnection;
+  }
+
+  public SqlStatement select(String sql) throws SQLException {
+    this.select = SelectImpl.create(db, readConnection, sql);
+    return this.select;
+  }
+
+  public MassUpdate update(String sql) throws SQLException {
+    this.updates.add(UpsertImpl.create(writeConnection, sql));
+    return this;
+  }
+
+  public MassUpdate rowPluralName(String s) {
+    this.progress.setPluralLabel(s);
+    return this;
+  }
+
+  public void execute(Handler handler) throws SQLException {
+    checkState(select != null && !updates.isEmpty(), "SELECT or UPDATE requests are not defined");
+    checkState(updates.size() == 1, "There should be only one update when using a " + Handler.class.getName());
+
+    progress.start();
+    try {
+      select.scroll(row -> callSingleHandler(handler, updates.iterator().next(), row));
+      closeUpdates();
+
+      // log the total number of processed rows
+      progress.log();
+    } finally {
+      progress.stop();
+    }
+  }
+
+  public void execute(MultiHandler handler) throws SQLException {
+    checkState(select != null && !updates.isEmpty(), "SELECT or UPDATE(s) requests are not defined");
+
+    progress.start();
+    try {
+      select.scroll(row -> callMultiHandler(handler, updates, row));
+      closeUpdates();
+
+      // log the total number of processed rows
+      progress.log();
+    } finally {
+      progress.stop();
+    }
+  }
+
+  private void callSingleHandler(Handler handler, Upsert update, Select.Row row) throws SQLException {
+    if (handler.handle(row, update)) {
+      update.addBatch();
+    }
+    counter.getAndIncrement();
+  }
+
+  private void callMultiHandler(MultiHandler handler, List<UpsertImpl> updates, Select.Row row) throws SQLException {
+    int i = 0;
+    for (UpsertImpl update : updates) {
+      if (handler.handle(row, update, i)) {
+        update.addBatch();
+      }
+      i++;
+    }
+    counter.getAndIncrement();
+  }
+
+  private void closeUpdates() throws SQLException {
+    for (UpsertImpl update : updates) {
+      if (update.getBatchCount() > 0L) {
+        update.execute().commit();
+      }
+      update.close();
+    }
+  }
+
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/Select.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/Select.java
new file mode 100644 (file)
index 0000000..94fb86e
--- /dev/null
@@ -0,0 +1,169 @@
+/*
+ * 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.server.platform.db.migration.step;
+
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Timestamp;
+import java.util.Date;
+import java.util.List;
+import javax.annotation.CheckForNull;
+
+public interface Select extends SqlStatement<Select> {
+
+  class Row {
+    private final ResultSet rs;
+
+    Row(ResultSet rs) {
+      this.rs = rs;
+    }
+
+    @CheckForNull
+    public Long getNullableLong(int columnIndex) throws SQLException {
+      long l = rs.getLong(columnIndex);
+      return rs.wasNull() ? null : l;
+    }
+
+    public long getLong(int columnIndex) throws SQLException {
+      return rs.getLong(columnIndex);
+    }
+
+    @CheckForNull
+    public Double getNullableDouble(int columnIndex) throws SQLException {
+      double d = rs.getDouble(columnIndex);
+      return rs.wasNull() ? null : d;
+    }
+
+    public double getDouble(int columnIndex) throws SQLException {
+      return rs.getDouble(columnIndex);
+    }
+
+    @CheckForNull
+    public Integer getNullableInt(int columnIndex) throws SQLException {
+      int i = rs.getInt(columnIndex);
+      return rs.wasNull() ? null : i;
+    }
+
+    public int getInt(int columnIndex) throws SQLException {
+      return rs.getInt(columnIndex);
+    }
+
+    @CheckForNull
+    public Boolean getNullableBoolean(int columnIndex) throws SQLException {
+      boolean b = rs.getBoolean(columnIndex);
+      return rs.wasNull() ? null : b;
+    }
+
+    public boolean getBoolean(int columnIndex) throws SQLException {
+      return rs.getBoolean(columnIndex);
+    }
+
+    @CheckForNull
+    public String getNullableString(int columnIndex) throws SQLException {
+      String s = rs.getString(columnIndex);
+      return rs.wasNull() ? null : s;
+    }
+
+    public String getString(int columnIndex) throws SQLException {
+      return rs.getString(columnIndex);
+    }
+
+    @CheckForNull
+    public Date getNullableDate(int columnIndex) throws SQLException {
+      Timestamp t = rs.getTimestamp(columnIndex);
+      return rs.wasNull() ? null : t;
+    }
+
+    public Date getDate(int columnIndex) throws SQLException {
+      return rs.getTimestamp(columnIndex);
+    }
+
+    @CheckForNull
+    public byte[] getNullableBytes(int columnIndex) throws SQLException {
+      byte[] b = rs.getBytes(columnIndex);
+      return rs.wasNull() ? null : b;
+    }
+
+    public byte[] getBytes(int columnIndex) throws SQLException {
+      return rs.getBytes(columnIndex);
+    }
+
+    @Override
+    public String toString() {
+      try {
+        ResultSetMetaData rsMetaData = rs.getMetaData();
+        StringBuilder sb = new StringBuilder();
+        for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
+          if (i > 1) {
+            sb.append(",");
+          }
+          sb.append(rsMetaData.getColumnLabel(i).toLowerCase());
+          sb.append("=");
+          sb.append(rs.getObject(i));
+        }
+        return sb.toString();
+      } catch (Exception e) {
+        return "Unavailable: " + e.getMessage();
+      }
+    }
+  }
+
+  @FunctionalInterface
+  interface RowReader<T> {
+    T read(Row row) throws SQLException;
+  }
+
+  class LongReader implements RowReader<Long> {
+    private LongReader() {
+    }
+
+    @Override
+    public Long read(Row row) throws SQLException {
+      return row.getNullableLong(1);
+    }
+  }
+
+  RowReader<Long> LONG_READER = new LongReader();
+
+  class StringReader implements RowReader<String> {
+    private StringReader() {
+    }
+
+    @Override
+    public String read(Row row) throws SQLException {
+      return row.getNullableString(1);
+    }
+  }
+
+  RowReader<String> STRING_READER = new StringReader();
+
+  @FunctionalInterface
+  interface RowHandler {
+    void handle(Row row) throws SQLException;
+  }
+
+  <T> List<T> list(RowReader<T> reader) throws SQLException;
+
+  @CheckForNull
+  <T> T get(RowReader<T> reader) throws SQLException;
+
+  void scroll(RowHandler handler) throws SQLException;
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/SelectImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/SelectImpl.java
new file mode 100644 (file)
index 0000000..c710ef8
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * 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.server.platform.db.migration.step;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.dbutils.DbUtils;
+import org.sonar.db.Database;
+
+public class SelectImpl extends BaseSqlStatement<Select>implements Select {
+
+  private SelectImpl(PreparedStatement pstmt) {
+    super(pstmt);
+  }
+
+  @Override
+  public <T> List<T> list(Select.RowReader<T> reader) throws SQLException {
+    ResultSet rs = pstmt.executeQuery();
+    Select.Row row = new Select.Row(rs);
+    try {
+      List<T> rows = new ArrayList<>();
+      while (rs.next()) {
+        rows.add(reader.read(row));
+      }
+      return rows;
+    } catch (Exception e) {
+      throw newExceptionWithRowDetails(row, e);
+    } finally {
+      DbUtils.closeQuietly(rs);
+      close();
+    }
+  }
+
+  @Override
+  public <T> T get(Select.RowReader<T> reader) throws SQLException {
+    ResultSet rs = pstmt.executeQuery();
+    Select.Row row = new Select.Row(rs);
+    try {
+      if (rs.next()) {
+        return reader.read(row);
+      }
+      return null;
+    } catch (Exception e) {
+      throw newExceptionWithRowDetails(row, e);
+    } finally {
+      DbUtils.closeQuietly(rs);
+      close();
+    }
+  }
+
+  @Override
+  public void scroll(Select.RowHandler handler) throws SQLException {
+    ResultSet rs = pstmt.executeQuery();
+    Select.Row row = new Select.Row(rs);
+    try {
+      while (rs.next()) {
+        handler.handle(row);
+      }
+    } catch (Exception e) {
+      throw newExceptionWithRowDetails(row, e);
+    } finally {
+      DbUtils.closeQuietly(rs);
+      close();
+    }
+  }
+
+  private static IllegalStateException newExceptionWithRowDetails(Select.Row row, Exception e) {
+    return new IllegalStateException("Error during processing of row: [" + row + "]", e);
+  }
+
+  public static SelectImpl create(Database db, Connection connection, String sql) throws SQLException {
+    // 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);
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/SqlStatement.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/SqlStatement.java
new file mode 100644 (file)
index 0000000..c091490
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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.server.platform.db.migration.step;
+
+import java.sql.SQLException;
+import java.util.Date;
+import javax.annotation.Nullable;
+
+public interface SqlStatement<CHILD extends SqlStatement> extends AutoCloseable {
+  CHILD setBoolean(int columnIndex, @Nullable Boolean value) throws SQLException;
+
+  CHILD setDate(int columnIndex, @Nullable Date value) throws SQLException;
+
+  CHILD setDouble(int columnIndex, @Nullable Double value) throws SQLException;
+
+  CHILD setInt(int columnIndex, @Nullable Integer value) throws SQLException;
+
+  CHILD setLong(int columnIndex, @Nullable Long value) throws SQLException;
+
+  CHILD setString(int columnIndex, @Nullable String value) throws SQLException;
+
+  CHILD setBytes(int columnIndex, @Nullable byte[] data) throws SQLException;
+
+  @Override
+  void close();
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/Upsert.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/Upsert.java
new file mode 100644 (file)
index 0000000..c22ea03
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * 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.server.platform.db.migration.step;
+
+import java.sql.SQLException;
+
+/**
+ * INSERT, UPDATE or DELETE
+ */
+public interface Upsert extends SqlStatement<Upsert> {
+  Upsert addBatch() throws SQLException;
+
+  Upsert execute() throws SQLException;
+
+  Upsert commit() throws SQLException;
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/UpsertImpl.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/UpsertImpl.java
new file mode 100644 (file)
index 0000000..2fbff98
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * 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.server.platform.db.migration.step;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import org.sonar.db.BatchSession;
+
+public class UpsertImpl extends BaseSqlStatement<Upsert> implements Upsert {
+
+  private long batchCount = 0L;
+
+  private UpsertImpl(PreparedStatement pstmt) {
+    super(pstmt);
+  }
+
+  @Override
+  public Upsert addBatch() throws SQLException {
+    pstmt.addBatch();
+    pstmt.clearParameters();
+    batchCount++;
+    if (batchCount % BatchSession.MAX_BATCH_SIZE == 0L) {
+      pstmt.executeBatch();
+      pstmt.getConnection().commit();
+    }
+    return this;
+  }
+
+  @Override
+  public Upsert execute() throws SQLException {
+    if (batchCount == 0L) {
+      pstmt.execute();
+    } else {
+      pstmt.executeBatch();
+    }
+    return this;
+  }
+
+  public long getBatchCount() {
+    return batchCount;
+  }
+
+  @Override
+  public Upsert commit() throws SQLException {
+    pstmt.getConnection().commit();
+    return this;
+  }
+
+  public static UpsertImpl create(Connection connection, String sql) throws SQLException {
+    return new UpsertImpl(connection.prepareStatement(sql));
+  }
+}
index a95fbca667c176c8c392e64d07e7db7c4b6a806e..f6779b443205fe15d8388095c9a1618d288e4267 100644 (file)
@@ -22,9 +22,9 @@ package org.sonar.server.platform.db.migration.version.v561;
 import java.sql.SQLException;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 /**
index 6a66c53427858fa68c19b29037616c2e99f6b1cc..534f41aae6fc7823537b489f43a6472aa9d692d2 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class CleanEventsWithoutAnalysisUuid extends DataChange {
index 5bf9cc47ebace6693b25d5bdc5a163d4e8cc05eb..c2b8f4b8cf97cac4513e4076730bde51ae693124 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class CleanEventsWithoutSnapshotId extends DataChange {
index 8e51247db5d85c184934e4f28277cff02171e786..7ce57f98d4637e9914f66d59128d625180bed11b 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class CleanMeasuresWithNullAnalysisUuid extends DataChange {
index fd2d4b3721373770f6d82454ce70acb76b258256..0b81094f7838c6f83c0a1539594f7df723d7f07d 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class CleanOrphanRowsInProjects extends DataChange {
index c098ccf20455d5fdff33e7023e1aa456e3bc00c5..106b6cce1ed3d0c7ae6440dc34d7974d7bcbc584 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class CleanOrphanRowsInResourceIndex extends DataChange {
index 749c2f584dfa447f3e8208ad393b3c6dfa9856aa..e0d868a167ca20753cc705edfdc23848e701e633 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class CleanOrphanRowsInSnapshots extends DataChange {
index 6b8889d42eab4d43a35509dce3f6a8d9fd496eb2..7a79f186459621f786254cb3cb2c8f7c76e68dbc 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class CleanUsurperRootComponents extends DataChange {
index 1691c3845146aa288ab76cafde3742e07344f3cf..583eaa34bf86273256074346af7528a647d19817 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class DeleteOrphanDuplicationsIndexRowsWithoutComponentOrAnalysis extends DataChange {
index 1812edb8e2c6dbbb754bd69d585441fea4b57c5f..e1a2b5c75a0990bbb9b2309404b68a1aec17049e 100644 (file)
@@ -21,7 +21,7 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class DeleteOrphanMeasuresWithoutComponent extends DataChange {
index f961ccdfe0af272e495b8205dd0440a1dd8f30b4..afad81f2c7741fd6c2abb68af0ad08b3217a71e7 100644 (file)
@@ -21,9 +21,9 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class DropTreesOfSnapshots extends DataChange {
index 432399598b44f28dff55839627af67266c93182d..62696cbfbaeadf509c42651d8020c50757a44359 100644 (file)
@@ -21,9 +21,9 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class FixProjectUuidOfDeveloperProjects extends DataChange {
index ab09e587eeaa199907edef19ae334b8de258d980..7d973dcd17315476e756bc8a2fb09f5a971b8d78 100644 (file)
@@ -21,9 +21,9 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateAnalysisUuidColumnOnCeActivity extends DataChange {
index 17cf6102e024d83f39de67025b05c092a3f6e2d9..8db948b54fbf02667c9411cb34b42425684af05d 100644 (file)
@@ -21,9 +21,9 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateAnalysisUuidOnEvents extends DataChange {
index 9f0d0d47ea02e3d559c968844e81d773e25342cf..ba1a0ff770656dfb4921389947c4676914b4ed60 100644 (file)
@@ -21,9 +21,9 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateAnalysisUuidOnMeasures extends DataChange {
index 2061f671f95fcaedf21d4fdf32e9801808fd76ef..6f83b4a9fa10af36dd40563f696b85574a7180b0 100644 (file)
@@ -21,9 +21,9 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateComponentUuidAndAnalysisUuidOfDuplicationsIndex extends DataChange {
index 97d5532e0a38464dda900d1ff5e8a0207ca27e20..4a95e445f6dceeab540f418d1a06fb71c04dac22 100644 (file)
@@ -23,9 +23,9 @@ import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.Map;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateComponentUuidColumnsOfSnapshots extends DataChange {
index a0a1075eac7a38b100d261438bb2b09388310edd..6270b528ec5cfeb38ec1c9b1d44a2dfef90f4744 100644 (file)
@@ -21,9 +21,9 @@ package org.sonar.server.platform.db.migration.version.v60;
 
 import java.sql.SQLException;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateComponentUuidOfMeasures extends DataChange {
index d245196aaa5393b7ffcf59896d83f0292a87916c..a3bc75c3ed542cf9340b937295b1569c93f4748a 100644 (file)
@@ -26,9 +26,9 @@ import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateLastUsedColumnOfRulesProfiles extends DataChange {
index c241b04f2a16b1c05a898916dacdec251fccc3c5..3a8c90ad10dee98c18494527f50a4804e9c7765f 100644 (file)
@@ -24,9 +24,9 @@ import java.sql.SQLException;
 import java.util.Map;
 import org.sonar.api.utils.KeyValueFormat;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 import static org.apache.commons.lang.StringUtils.isBlank;
index 747c9a601a768cab4525959ef45e7cf8d4908981..7d14b96003c16c1a67ae0189ec0bbd8fce0f1c82 100644 (file)
@@ -28,9 +28,9 @@ import java.util.List;
 import java.util.Map;
 import javax.annotation.CheckForNull;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateUserUpdatedAtOfRulesProfiles extends DataChange {
index ad8de24bb88b28bc9788b99317300ec8712a0727..4c570c9422e960320674b36894dc235290c9ac79 100644 (file)
@@ -22,9 +22,9 @@ package org.sonar.server.platform.db.migration.version.v60;
 import java.sql.SQLException;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateUuidColumnOnSnapshots extends DataChange {
index d2e4baea0e163d58409aec84daf5ca98d5a87a56..fe17c2c6571152005baa43aa75aaabc2964a9c05 100644 (file)
@@ -25,9 +25,9 @@ import java.util.Map;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateUuidColumnsOfProjects extends DataChange {
index 96f5f7a5d88249b1d400d3304b84e0f2339a8f2f..3b1f85105e59abfd6ce026394facc631fac7d688 100644 (file)
@@ -23,9 +23,9 @@ import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.Map;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateUuidColumnsOfResourceIndex extends DataChange {
index 6fdb03d80dd3a9b1b1371e24157a78aafc9a684b..7239ce9c71f2e4284f661cacbdf0736f4dd873c6 100644 (file)
@@ -32,9 +32,9 @@ import javax.annotation.Nullable;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 import static java.util.stream.Collectors.toCollection;
index edca86281aebc398fba667b0bfd8986cf1c65ff1..b4d5e0ef79d9ccda6cf755aeb2b4554c78ddadd0 100644 (file)
@@ -23,7 +23,7 @@ package org.sonar.server.platform.db.migration.version.v60;
 import java.sql.SQLException;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class RemoveUsersPasswordWhenNotLocal extends DataChange {
index 9e7ff005cc615f00b598c846cd1e34d9a535ea6c..f11be1b1e4fd04b5a16690b4d553c94fedb4700b 100644 (file)
@@ -23,7 +23,7 @@ import java.sql.SQLException;
 import java.util.Date;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 
 public class CopyActivitiesToQprofileChanges extends DataChange {
 
index 36c5b432be52da34159266b5920c62a73a48e781..66e3b7875f5dfccf80ad2002c566f6e53d035887 100644 (file)
@@ -23,7 +23,7 @@ package org.sonar.server.platform.db.migration.version.v61;
 import java.sql.SQLException;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 
 public class DeleteProjectDashboards extends DataChange {
 
index ee19605605b4363f2b1dff2eefc57ea6799a8d98..d4da59764acd9e24ba7546ce77bdfba9ff9ff379 100644 (file)
@@ -23,9 +23,9 @@ import java.sql.SQLException;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 
 public class PopulateTableProperties2 extends DataChange {
   private final System2 system2;
index 6ae55b299a5adaf3d25e6c9a67eedde9420bce2f..aee82cccff42a031e8dc2e4deed3ee63ec7ab0ac 100644 (file)
@@ -24,7 +24,7 @@ import javax.annotation.Nullable;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.Upsert;
+import org.sonar.server.platform.db.migration.step.Upsert;
 
 public class RemoveViewsDefinitionFromProperties extends DataChange {
 
index 9ef1a1f08ed24ea473eff31158dca2f8bf739f80..41dfe5fee7b293a7766037c7d745eaabfab7dd24 100644 (file)
@@ -24,7 +24,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.Select;
+import org.sonar.server.platform.db.migration.step.Select;
 
 public class CreateDefaultOrganization extends DataChange {
   private static final String KEY_DEFAULT_ORGANIZATION = "default-organization";
index 47549ae026957fa9d72a0cca8323ca5961af4e6c..37f0cc3ea9858a5a7745eaae2dcf61a74af803ab 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.server.platform.db.migration.version.v62;
 import java.sql.SQLException;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.MassUpdate;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
 
 public class DeletePermissionShareDashboard extends DataChange {
 
index 3b78bb379ece9e330a820e706c924b4d26439ffb..5176cde16ac777016600a211a9e652fd9a66de9e 100644 (file)
@@ -22,9 +22,9 @@ package org.sonar.server.platform.db.migration.version.v62;
 import java.sql.SQLException;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 
 /**
  * All users with "admin" role, either directly or via a group, are made root. All others are made non root.
index ccced6405660dab2b1276cd46e4641032a7770de..e50fe91f9e740b863e7f3a4b02168d5453ff62d9 100644 (file)
@@ -22,8 +22,8 @@ package org.sonar.server.platform.db.migration.version.v62;
 import java.sql.SQLException;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
 
 import static com.google.common.base.Preconditions.checkState;
 
index 57ab86d21b440c127ce70402a2738078b2146b8e..edbe5bdf73d08c4e1c254f31d4fbadd3f9dbc992 100644 (file)
@@ -24,8 +24,8 @@ import java.sql.SQLException;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
 
 import static com.google.common.base.Preconditions.checkState;
 
index e3005863b10d1f2ce70be8eebb01f01e86cb5b00..0e0d753cac001cf0370f9964523b79e490b37124 100644 (file)
@@ -24,8 +24,8 @@ import java.sql.SQLException;
 import org.sonar.api.utils.System2;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
 
 import static com.google.common.base.Preconditions.checkState;
 
index a0f5a8b52227798d2308198cfa82024cfd1a1a50..2c4f2194ca55f0225acbf99944bc30a441ad5c1b 100644 (file)
@@ -22,8 +22,8 @@ package org.sonar.server.platform.db.migration.version.v62;
 import java.sql.SQLException;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
 
 import static com.google.common.base.Preconditions.checkState;
 
index 09882948183b09505e87f6ff4d9c20427d672044..fb1b224e6f37643a0e2fd3173efdfc183e6918e0 100644 (file)
@@ -32,7 +32,7 @@ import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.db.Database;
 import org.sonar.server.platform.db.migration.step.DataChange;
-import org.sonar.db.version.Select;
+import org.sonar.server.platform.db.migration.step.Select;
 
 import static java.util.Objects.requireNonNull;
 import static org.sonar.core.util.stream.Collectors.uniqueIndex;
index f4cecbffbd6bf6a6856d0566f4e4869499209150..75321aa77c1714859c11c9ee92bd5bbf32faf4eb 100644 (file)
@@ -23,9 +23,9 @@ package org.sonar.server.platform.db.migration.version.v63;
 import java.sql.SQLException;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Database;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.SqlStatement;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+import org.sonar.server.platform.db.migration.step.Select;
+import org.sonar.server.platform.db.migration.step.SqlStatement;
 import org.sonar.server.platform.db.migration.step.DataChange;
 
 public class PopulateUuidColumnOfEvents extends DataChange {
index f34646f2f1c1dc0bb347253cd91ec072ef242c07..10ce712605f937324fc6684f2bd771f0c87218ea 100644 (file)
@@ -30,12 +30,8 @@ import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.System2;
 import org.sonar.db.BatchSession;
 import org.sonar.db.DbTester;
-import org.sonar.db.version.MassUpdate;
-import org.sonar.db.version.Select;
-import org.sonar.db.version.Select.Row;
-import org.sonar.db.version.Select.RowReader;
-import org.sonar.db.version.SqlStatement;
-import org.sonar.db.version.Upsert;
+import org.sonar.server.platform.db.migration.step.Select.Row;
+import org.sonar.server.platform.db.migration.step.Select.RowReader;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.fail;
diff --git a/sonar-db/src/main/java/org/sonar/db/version/BaseSqlStatement.java b/sonar-db/src/main/java/org/sonar/db/version/BaseSqlStatement.java
deleted file mode 100644 (file)
index 6b465a7..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * 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 java.sql.PreparedStatement;
-import java.sql.SQLException;
-import java.sql.Timestamp;
-import java.sql.Types;
-import java.util.Date;
-import javax.annotation.Nullable;
-import org.apache.commons.dbutils.DbUtils;
-
-class BaseSqlStatement<CHILD extends SqlStatement> implements SqlStatement<CHILD> {
-  protected PreparedStatement pstmt;
-
-  protected BaseSqlStatement(PreparedStatement pstmt) {
-    this.pstmt = pstmt;
-  }
-
-  @Override
-  public void close() {
-    DbUtils.closeQuietly(pstmt);
-    pstmt = null;
-  }
-
-  @Override
-  public CHILD setString(int columnIndex, @Nullable String value) throws SQLException {
-    pstmt.setString(columnIndex, value);
-    return (CHILD) this;
-  }
-
-  @Override
-  public CHILD setBytes(int columnIndex, @Nullable byte[] value) throws SQLException {
-    pstmt.setBytes(columnIndex, value);
-    return (CHILD) this;
-  }
-
-  @Override
-  public CHILD setInt(int columnIndex, @Nullable Integer value) throws SQLException {
-    if (value == null) {
-      pstmt.setNull(columnIndex, Types.INTEGER);
-    } else {
-      pstmt.setInt(columnIndex, value);
-    }
-    return (CHILD) this;
-  }
-
-  @Override
-  public CHILD setLong(int columnIndex, @Nullable Long value) throws SQLException {
-    if (value == null) {
-      pstmt.setNull(columnIndex, Types.BIGINT);
-    } else {
-      pstmt.setLong(columnIndex, value);
-    }
-    return (CHILD) this;
-  }
-
-  @Override
-  public CHILD setBoolean(int columnIndex, @Nullable Boolean value) throws SQLException {
-    if (value == null) {
-      pstmt.setNull(columnIndex, Types.BOOLEAN);
-    } else {
-      pstmt.setBoolean(columnIndex, value);
-    }
-    return (CHILD) this;
-  }
-
-  @Override
-  public CHILD setDouble(int columnIndex, @Nullable Double value) throws SQLException {
-    if (value == null) {
-      pstmt.setNull(columnIndex, Types.DECIMAL);
-    } else {
-      pstmt.setDouble(columnIndex, value);
-    }
-    return (CHILD) this;
-  }
-
-  @Override
-  public CHILD setDate(int columnIndex, @Nullable Date value) throws SQLException {
-    if (value == null) {
-      pstmt.setNull(columnIndex, Types.TIMESTAMP);
-    } else {
-      pstmt.setTimestamp(columnIndex, new Timestamp(value.getTime()));
-    }
-    return (CHILD) this;
-  }
-}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/MassUpdate.java b/sonar-db/src/main/java/org/sonar/db/version/MassUpdate.java
deleted file mode 100644 (file)
index 5218fb7..0000000
+++ /dev/null
@@ -1,143 +0,0 @@
-/*
- * 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 java.sql.Connection;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
-import org.sonar.core.util.ProgressLogger;
-import org.sonar.db.Database;
-
-import static com.google.common.base.Preconditions.checkState;
-
-public class MassUpdate {
-
-  @FunctionalInterface
-  public interface Handler {
-    /**
-     * Convert some column values of a given row.
-     *
-     * @return true if the row must be updated, else false. If false, then the update parameter must not be touched.
-     */
-    boolean handle(Select.Row row, SqlStatement update) throws SQLException;
-  }
-
-  @FunctionalInterface
-  public interface MultiHandler {
-    /**
-     * Convert some column values of a given row.
-     *
-     * @param updateIndex 0-based
-     * @return true if the row must be updated, else false. If false, then the update parameter must not be touched.
-     */
-    boolean handle(Select.Row row, SqlStatement update, int updateIndex) throws SQLException;
-  }
-
-  private final Database db;
-  private final Connection readConnection;
-  private final Connection writeConnection;
-  private final AtomicLong counter = new AtomicLong(0L);
-  private final ProgressLogger progress = ProgressLogger.create(getClass(), counter);
-
-  private Select select;
-  private List<UpsertImpl> updates = new ArrayList<>(1);
-
-  public MassUpdate(Database db, Connection readConnection, Connection writeConnection) {
-    this.db = db;
-    this.readConnection = readConnection;
-    this.writeConnection = writeConnection;
-  }
-
-  public SqlStatement select(String sql) throws SQLException {
-    this.select = SelectImpl.create(db, readConnection, sql);
-    return this.select;
-  }
-
-  public MassUpdate update(String sql) throws SQLException {
-    this.updates.add(UpsertImpl.create(writeConnection, sql));
-    return this;
-  }
-
-  public MassUpdate rowPluralName(String s) {
-    this.progress.setPluralLabel(s);
-    return this;
-  }
-
-  public void execute(Handler handler) throws SQLException {
-    checkState(select != null && !updates.isEmpty(), "SELECT or UPDATE requests are not defined");
-    checkState(updates.size() == 1, "There should be only one update when using a " + Handler.class.getName());
-
-    progress.start();
-    try {
-      select.scroll(row -> callSingleHandler(handler, updates.iterator().next(), row));
-      closeUpdates();
-
-      // log the total number of processed rows
-      progress.log();
-    } finally {
-      progress.stop();
-    }
-  }
-
-  public void execute(MultiHandler handler) throws SQLException {
-    checkState(select != null && !updates.isEmpty(), "SELECT or UPDATE(s) requests are not defined");
-
-    progress.start();
-    try {
-      select.scroll(row -> callMultiHandler(handler, updates, row));
-      closeUpdates();
-
-      // log the total number of processed rows
-      progress.log();
-    } finally {
-      progress.stop();
-    }
-  }
-
-  private void callSingleHandler(Handler handler, Upsert update, Select.Row row) throws SQLException {
-    if (handler.handle(row, update)) {
-      update.addBatch();
-    }
-    counter.getAndIncrement();
-  }
-
-  private void callMultiHandler(MultiHandler handler, List<UpsertImpl> updates, Select.Row row) throws SQLException {
-    int i = 0;
-    for (UpsertImpl update : updates) {
-      if (handler.handle(row, update, i)) {
-        update.addBatch();
-      }
-      i++;
-    }
-    counter.getAndIncrement();
-  }
-
-  private void closeUpdates() throws SQLException {
-    for (UpsertImpl update : updates) {
-      if (update.getBatchCount() > 0L) {
-        update.execute().commit();
-      }
-      update.close();
-    }
-  }
-
-}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/Select.java b/sonar-db/src/main/java/org/sonar/db/version/Select.java
deleted file mode 100644 (file)
index 0071439..0000000
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
- * 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 java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.sql.Timestamp;
-import java.util.Date;
-import java.util.List;
-import javax.annotation.CheckForNull;
-
-public interface Select extends SqlStatement<Select> {
-
-  class Row {
-    private final ResultSet rs;
-
-    Row(ResultSet rs) {
-      this.rs = rs;
-    }
-
-    @CheckForNull
-    public Long getNullableLong(int columnIndex) throws SQLException {
-      long l = rs.getLong(columnIndex);
-      return rs.wasNull() ? null : l;
-    }
-
-    public long getLong(int columnIndex) throws SQLException {
-      return rs.getLong(columnIndex);
-    }
-
-    @CheckForNull
-    public Double getNullableDouble(int columnIndex) throws SQLException {
-      double d = rs.getDouble(columnIndex);
-      return rs.wasNull() ? null : d;
-    }
-
-    public double getDouble(int columnIndex) throws SQLException {
-      return rs.getDouble(columnIndex);
-    }
-
-    @CheckForNull
-    public Integer getNullableInt(int columnIndex) throws SQLException {
-      int i = rs.getInt(columnIndex);
-      return rs.wasNull() ? null : i;
-    }
-
-    public int getInt(int columnIndex) throws SQLException {
-      return rs.getInt(columnIndex);
-    }
-
-    @CheckForNull
-    public Boolean getNullableBoolean(int columnIndex) throws SQLException {
-      boolean b = rs.getBoolean(columnIndex);
-      return rs.wasNull() ? null : b;
-    }
-
-    public boolean getBoolean(int columnIndex) throws SQLException {
-      return rs.getBoolean(columnIndex);
-    }
-
-    @CheckForNull
-    public String getNullableString(int columnIndex) throws SQLException {
-      String s = rs.getString(columnIndex);
-      return rs.wasNull() ? null : s;
-    }
-
-    public String getString(int columnIndex) throws SQLException {
-      return rs.getString(columnIndex);
-    }
-
-    @CheckForNull
-    public Date getNullableDate(int columnIndex) throws SQLException {
-      Timestamp t = rs.getTimestamp(columnIndex);
-      return rs.wasNull() ? null : t;
-    }
-
-    public Date getDate(int columnIndex) throws SQLException {
-      return rs.getTimestamp(columnIndex);
-    }
-
-    @CheckForNull
-    public byte[] getNullableBytes(int columnIndex) throws SQLException {
-      byte[] b = rs.getBytes(columnIndex);
-      return rs.wasNull() ? null : b;
-    }
-
-    public byte[] getBytes(int columnIndex) throws SQLException {
-      return rs.getBytes(columnIndex);
-    }
-
-    @Override
-    public String toString() {
-      try {
-        ResultSetMetaData rsMetaData = rs.getMetaData();
-        StringBuilder sb = new StringBuilder();
-        for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
-          if (i > 1) {
-            sb.append(",");
-          }
-          sb.append(rsMetaData.getColumnLabel(i).toLowerCase());
-          sb.append("=");
-          sb.append(rs.getObject(i));
-        }
-        return sb.toString();
-      } catch (Exception e) {
-        return "Unavailable: " + e.getMessage();
-      }
-    }
-  }
-
-  @FunctionalInterface
-  interface RowReader<T> {
-    T read(Row row) throws SQLException;
-  }
-
-  class LongReader implements RowReader<Long> {
-    private LongReader() {
-    }
-
-    @Override
-    public Long read(Row row) throws SQLException {
-      return row.getNullableLong(1);
-    }
-  }
-
-  RowReader<Long> LONG_READER = new LongReader();
-
-  class StringReader implements RowReader<String> {
-    private StringReader() {
-    }
-
-    @Override
-    public String read(Row row) throws SQLException {
-      return row.getNullableString(1);
-    }
-  }
-
-  RowReader<String> STRING_READER = new StringReader();
-
-  @FunctionalInterface
-  interface RowHandler {
-    void handle(Row row) throws SQLException;
-  }
-
-  <T> List<T> list(RowReader<T> reader) throws SQLException;
-
-  @CheckForNull
-  <T> T get(RowReader<T> reader) throws SQLException;
-
-  void scroll(RowHandler handler) throws SQLException;
-}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/SelectImpl.java b/sonar-db/src/main/java/org/sonar/db/version/SelectImpl.java
deleted file mode 100644 (file)
index ade8124..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * 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 java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.List;
-import org.apache.commons.dbutils.DbUtils;
-import org.sonar.db.Database;
-
-public class SelectImpl extends BaseSqlStatement<Select>implements Select {
-
-  private SelectImpl(PreparedStatement pstmt) {
-    super(pstmt);
-  }
-
-  @Override
-  public <T> List<T> list(Select.RowReader<T> reader) throws SQLException {
-    ResultSet rs = pstmt.executeQuery();
-    Select.Row row = new Select.Row(rs);
-    try {
-      List<T> rows = new ArrayList<>();
-      while (rs.next()) {
-        rows.add(reader.read(row));
-      }
-      return rows;
-    } catch (Exception e) {
-      throw newExceptionWithRowDetails(row, e);
-    } finally {
-      DbUtils.closeQuietly(rs);
-      close();
-    }
-  }
-
-  @Override
-  public <T> T get(Select.RowReader<T> reader) throws SQLException {
-    ResultSet rs = pstmt.executeQuery();
-    Select.Row row = new Select.Row(rs);
-    try {
-      if (rs.next()) {
-        return reader.read(row);
-      }
-      return null;
-    } catch (Exception e) {
-      throw newExceptionWithRowDetails(row, e);
-    } finally {
-      DbUtils.closeQuietly(rs);
-      close();
-    }
-  }
-
-  @Override
-  public void scroll(Select.RowHandler handler) throws SQLException {
-    ResultSet rs = pstmt.executeQuery();
-    Select.Row row = new Select.Row(rs);
-    try {
-      while (rs.next()) {
-        handler.handle(row);
-      }
-    } catch (Exception e) {
-      throw newExceptionWithRowDetails(row, e);
-    } finally {
-      DbUtils.closeQuietly(rs);
-      close();
-    }
-  }
-
-  private static IllegalStateException newExceptionWithRowDetails(Select.Row row, Exception e) {
-    return new IllegalStateException("Error during processing of row: [" + row + "]", e);
-  }
-
-  public static SelectImpl create(Database db, Connection connection, String sql) throws SQLException {
-    // 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);
-  }
-}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/SqlStatement.java b/sonar-db/src/main/java/org/sonar/db/version/SqlStatement.java
deleted file mode 100644 (file)
index 4158aee..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * 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 java.sql.SQLException;
-import java.util.Date;
-import javax.annotation.Nullable;
-
-public interface SqlStatement<CHILD extends SqlStatement> extends AutoCloseable {
-  CHILD setBoolean(int columnIndex, @Nullable Boolean value) throws SQLException;
-
-  CHILD setDate(int columnIndex, @Nullable Date value) throws SQLException;
-
-  CHILD setDouble(int columnIndex, @Nullable Double value) throws SQLException;
-
-  CHILD setInt(int columnIndex, @Nullable Integer value) throws SQLException;
-
-  CHILD setLong(int columnIndex, @Nullable Long value) throws SQLException;
-
-  CHILD setString(int columnIndex, @Nullable String value) throws SQLException;
-
-  CHILD setBytes(int columnIndex, @Nullable byte[] data) throws SQLException;
-
-  @Override
-  void close();
-}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/Upsert.java b/sonar-db/src/main/java/org/sonar/db/version/Upsert.java
deleted file mode 100644 (file)
index 45b54f3..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * 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 java.sql.SQLException;
-
-/**
- * INSERT, UPDATE or DELETE
- */
-public interface Upsert extends SqlStatement<Upsert> {
-  Upsert addBatch() throws SQLException;
-
-  Upsert execute() throws SQLException;
-
-  Upsert commit() throws SQLException;
-}
diff --git a/sonar-db/src/main/java/org/sonar/db/version/UpsertImpl.java b/sonar-db/src/main/java/org/sonar/db/version/UpsertImpl.java
deleted file mode 100644 (file)
index ace1a52..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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 java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.SQLException;
-import org.sonar.db.BatchSession;
-
-public class UpsertImpl extends BaseSqlStatement<Upsert> implements Upsert {
-
-  private long batchCount = 0L;
-
-  private UpsertImpl(PreparedStatement pstmt) {
-    super(pstmt);
-  }
-
-  @Override
-  public Upsert addBatch() throws SQLException {
-    pstmt.addBatch();
-    pstmt.clearParameters();
-    batchCount++;
-    if (batchCount % BatchSession.MAX_BATCH_SIZE == 0L) {
-      pstmt.executeBatch();
-      pstmt.getConnection().commit();
-    }
-    return this;
-  }
-
-  @Override
-  public Upsert execute() throws SQLException {
-    if (batchCount == 0L) {
-      pstmt.execute();
-    } else {
-      pstmt.executeBatch();
-    }
-    return this;
-  }
-
-  public long getBatchCount() {
-    return batchCount;
-  }
-
-  @Override
-  public Upsert commit() throws SQLException {
-    pstmt.getConnection().commit();
-    return this;
-  }
-
-  public static UpsertImpl create(Connection connection, String sql) throws SQLException {
-    return new UpsertImpl(connection.prepareStatement(sql));
-  }
-}