All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tarantool.jdbc.SQLStatement Maven / Gradle / Ivy

There is a newer version: 1.9.4
Show newest version
package org.tarantool.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.Statement;

@SuppressWarnings("Since15")
public class SQLStatement implements Statement {
    protected final SQLConnection connection;
    private SQLResultSet resultSet;
    private int updateCount;
    private int maxRows;

    protected SQLStatement(SQLConnection sqlConnection) {
        this.connection = sqlConnection;
    }

    @Override
    public ResultSet executeQuery(String sql) throws SQLException {
        discardLastResults();
        return connection.executeQuery(sql);
    }

    @Override
    public int executeUpdate(String sql) throws SQLException {
        discardLastResults();
        return connection.executeUpdate(sql);
    }

    @Override
    public void close() throws SQLException {

    }

    @Override
    public int getMaxFieldSize() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public void setMaxFieldSize(int max) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public int getMaxRows() throws SQLException {
        return maxRows;
    }

    @Override
    public void setMaxRows(int max) throws SQLException {
       maxRows = max;
       if(resultSet!=null) {
           resultSet.maxRows = maxRows;
       }
    }

    @Override
    public void setEscapeProcessing(boolean enable) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public int getQueryTimeout() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public void setQueryTimeout(int seconds) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public void cancel() throws SQLException {

    }

    @Override
    public SQLWarning getWarnings() throws SQLException {
        return null;
    }

    @Override
    public void clearWarnings() throws SQLException {

    }

    @Override
    public void setCursorName(String name) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public boolean execute(String sql) throws SQLException {
        discardLastResults();
        return handleResult(connection.execute(sql));
    }

    @Override
    public ResultSet getResultSet() throws SQLException {
        try {
            return resultSet;
        } finally {
            resultSet = null;
        }
    }

    @Override
    public int getUpdateCount() throws SQLException {
        try {
            return updateCount;
        } finally {
            updateCount = -1;
        }
    }

    @Override
    public boolean getMoreResults() throws SQLException {
        return false;
    }

    @Override
    public void setFetchDirection(int direction) throws SQLException {
        if (direction != ResultSet.FETCH_FORWARD) {
            throw new SQLFeatureNotSupportedException();
        }
    }

    @Override
    public int getFetchDirection() throws SQLException {
        return ResultSet.FETCH_FORWARD;
    }

    @Override
    public void setFetchSize(int rows) throws SQLException {
    }

    @Override
    public int getFetchSize() throws SQLException {
        return 0;
    }

    @Override
    public int getResultSetConcurrency() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public int getResultSetType() throws SQLException {
        return ResultSet.TYPE_FORWARD_ONLY;
    }

    @Override
    public void addBatch(String sql) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public void clearBatch() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public int[] executeBatch() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public Connection getConnection() throws SQLException {
        return connection;
    }

    @Override
    public boolean getMoreResults(int current) throws SQLException {
        return false;
    }

    @Override
    public ResultSet getGeneratedKeys() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public int executeUpdate(String sql, String[] columnNames) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public boolean execute(String sql, int[] columnIndexes) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public boolean execute(String sql, String[] columnNames) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public int getResultSetHoldability() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public boolean isClosed() throws SQLException {
        return connection.isClosed();
    }

    @Override
    public void setPoolable(boolean poolable) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public boolean isPoolable() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public void closeOnCompletion() throws SQLException {

    }

    @Override
    public boolean isCloseOnCompletion() throws SQLException {
        return false;
    }

    @Override
    public  T unwrap(Class iface) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override
    public boolean isWrapperFor(Class iface) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    /**
     * Clears the results of the most recent execution.
     */
    protected void discardLastResults() {
        updateCount = -1;
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (Exception ignored) {
                // No-op.
            }
            resultSet = null;
        }
    }

    /**
     * Sets the internals according to the result of last execution.
     *
     * @param result The result of SQL statement execution.
     * @return {@code true}, if the result is a ResultSet object.
     */
    protected boolean handleResult(Object result) {
        if (result instanceof SQLResultSet) {
            resultSet = (SQLResultSet) result;
            resultSet.maxRows = maxRows;
            updateCount = -1;
            return true;
        } else {
            resultSet = null;
            updateCount = (Integer) result;
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy