/*
 * Copyright (c) 2013 - 2015 xxyy (Philipp Nowak; devnull@nowak-at.net). All rights reserved.
 *
 * Any usage, including, but not limited to, compiling, running, redistributing, printing,
 *  copying and reverse-engineering is strictly prohibited without explicit written permission
 *  from the original author and may result in legal steps being taken.
 *
 * See the included LICENSE file (core/src/main/resources) or email xxyy98+xyclicense@gmail.com for details.
 */

package li.l1t.common.sql;

import org.jetbrains.annotations.Nullable;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Stores a {@link java.sql.PreparedStatement} and {@link java.sql.ResultSet} for an already
 * executed SQL query.
 *
 * @author <a href="http://xxyy.github.io/">xxyy</a>
 * @since 12/02/14
 */
public class QueryResult implements li.l1t.common.sql.sane.result.QueryResult {
    @Nullable
    private PreparedStatement preparedStatement;
    @Nullable
    private ResultSet resultSet;
    /**
     * Return value of {@link java.sql.PreparedStatement#executeUpdate()}. Set to -1 if the
     * statement was a query.
     */
    private int updateReturn;

    public QueryResult(PreparedStatement statement, int updateReturn) {
        this(statement, null, updateReturn);
    }

    public QueryResult(PreparedStatement statement, ResultSet rs) {
        this(statement, rs, -1);
    }

    public QueryResult(@Nullable PreparedStatement preparedStatement, @Nullable ResultSet resultSet, int updateReturn) {
        this.preparedStatement = preparedStatement;
        this.resultSet = resultSet;
        this.updateReturn = updateReturn;
    }

    @Override
    public ResultSet rs() {
        return resultSet;
    }

    public QueryResult assertHasResultSet() {
        if (resultSet == null) {
            throw new IllegalStateException("QueryResult does not have ResultSet when required!");
        }

        return this;
    }

    public QueryResult vouchForResultSet() throws SQLException {
        if (resultSet == null) {
            throw new SQLException("No ResultSet associated with QueryResult when asked to vouch!");
        }

        return this;
    }

    @Override
    public void close() throws SQLException {
        if (preparedStatement == null) {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            return;
        }
        preparedStatement.close();
        preparedStatement = null;
        resultSet = null;
    }

    @Override
    public void tryClose() {
        try {
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @Nullable
    public PreparedStatement getStatement() {
        return this.preparedStatement;
    }

    @Nullable
    public ResultSet getResultSet() {
        return this.resultSet;
    }

    public int getUpdateReturn() {
        return this.updateReturn;
    }
}

@@ -496,16 +496,50 @@
 n.sql;%0A%0A
+import li.l1t.common.util.Closer;%0A
 import o
@@ -573,16 +573,16 @@
 llable;%0A
-
 %0Aimport 
@@ -906,49 +906,21 @@
 nts 
-li.l1t.common.sql.sane.result.QueryResult
+AutoCloseable
  %7B%0A 
@@ -1683,38 +1683,24 @@
 urn;%0A    %7D%0A%0A
-    @Override%0A
     public R
@@ -2240,36 +2240,16 @@
  close()
- throws SQLException
  %7B%0A     
@@ -2329,32 +2329,45 @@
                 
+Closer.close(
 resultSet.close(
@@ -2359,23 +2359,16 @@
 esultSet
-.close(
 );%0A     
@@ -2440,32 +2440,45 @@
       %7D%0A        
+Closer.close(
 preparedStatemen
@@ -2478,23 +2478,16 @@
 tatement
-.close(
 );%0A     
@@ -2544,38 +2544,24 @@
 ull;%0A    %7D%0A%0A
-    @Override%0A
     public v
@@ -2690,38 +2690,24 @@
    %7D%0A    %7D%0A%0A
-    @Override%0A
     @Nullabl

Make old QueryResult no longer implement new interface; Prepares #608