You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SelectImpl.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.db.migrations;
  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import org.apache.commons.dbutils.DbUtils;
  28. import org.sonar.db.Database;
  29. class SelectImpl extends BaseSqlStatement<Select> implements Select {
  30. private SelectImpl(PreparedStatement pstmt) {
  31. super(pstmt);
  32. }
  33. @Override
  34. public <T> List<T> list(Select.RowReader<T> reader) throws SQLException {
  35. ResultSet rs = pstmt.executeQuery();
  36. Select.Row row = new Select.Row(rs);
  37. try {
  38. List<T> rows = new ArrayList<>();
  39. while (rs.next()) {
  40. rows.add(reader.read(row));
  41. }
  42. return rows;
  43. } catch (Exception e) {
  44. throw newExceptionWithRowDetails(row, e);
  45. } finally {
  46. DbUtils.closeQuietly(rs);
  47. close();
  48. }
  49. }
  50. @Override
  51. public <T> T get(Select.RowReader<T> reader) throws SQLException {
  52. ResultSet rs = pstmt.executeQuery();
  53. Select.Row row = new Select.Row(rs);
  54. try {
  55. if (rs.next()) {
  56. return reader.read(row);
  57. }
  58. return null;
  59. } catch (Exception e) {
  60. throw newExceptionWithRowDetails(row, e);
  61. } finally {
  62. DbUtils.closeQuietly(rs);
  63. close();
  64. }
  65. }
  66. @Override
  67. public void scroll(Select.RowHandler handler) throws SQLException {
  68. ResultSet rs = pstmt.executeQuery();
  69. Select.Row row = new Select.Row(rs);
  70. try {
  71. while (rs.next()) {
  72. handler.handle(row);
  73. }
  74. } catch (Exception e) {
  75. throw newExceptionWithRowDetails(row, e);
  76. } finally {
  77. DbUtils.closeQuietly(rs);
  78. close();
  79. }
  80. }
  81. private IllegalStateException newExceptionWithRowDetails(Select.Row row, Exception e) {
  82. return new IllegalStateException("Error during processing of row: [" + row + "]", e);
  83. }
  84. static SelectImpl create(Database db, Connection connection, String sql) throws SQLException {
  85. // TODO use DbClient#newScrollingSelectStatement()
  86. PreparedStatement pstmt = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
  87. pstmt.setFetchSize(db.getDialect().getScrollDefaultFetchSize());
  88. return new SelectImpl(pstmt);
  89. }
  90. }