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

buckelieg.fn.db.AbstractQuery Maven / Gradle / Ivy

/*
 * Copyright 2016- Anatoly Kutyakov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package buckelieg.fn.db;

import javax.annotation.Nonnull;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.function.Consumer;

import static buckelieg.fn.db.Utils.cutComments;
import static buckelieg.fn.db.Utils.newSQLRuntimeException;
import static java.lang.Math.max;
import static java.util.Objects.requireNonNull;

@SuppressWarnings("unchecked")
abstract class AbstractQuery implements Query {

    protected S statement;
    protected final String query;
    private final String sqlString;
    protected final Connection connection;
    protected final boolean autoCommit;
    protected boolean skipWarnings = true;

    AbstractQuery(Connection connection, String query, Object... params) {
        try {
            this.query = cutComments(requireNonNull(query, "SQL query must be provided"));
            this.connection = connection;
            this.autoCommit = connection.getAutoCommit();
            this.statement = prepareStatement(connection, this.query, params);
            this.sqlString = asSQL(this.query, params);
        } catch (SQLException e) {
            throw newSQLRuntimeException(e);
        }
    }

    @Override
    public void close() {
        jdbcTry(statement::close); // by JDBC spec: subsequently closes all result sets opened by this statement
    }

    final  Q setTimeout(int timeout) {
        return setStatementParameter(statement -> statement.setQueryTimeout(max(timeout, 0)));
    }

    final  Q setPoolable(boolean poolable) {
        return setStatementParameter(statement -> statement.setPoolable(poolable));
    }

    final  Q setEscapeProcessing(boolean escapeProcessing) {
        return setStatementParameter(statement -> statement.setEscapeProcessing(escapeProcessing));
    }

    final  Q setSkipWarnings(boolean skipWarnings) {
        this.skipWarnings = skipWarnings;
        return (Q) this;
    }

    final  Q log(Consumer printer) {
        requireNonNull(printer, "Printer must be provided").accept(sqlString);
        return (Q) this;
    }

    final  O jdbcTry(TrySupplier supplier) {
        O result = null;
        try {
            result = supplier.get();
        } catch (SQLException e) {
            close();
            throw newSQLRuntimeException(e);
        } catch (AbstractMethodError ame) {
            // ignore this possible vendor-specific JDBC driver's error.
        }
        return result;
    }

    final void jdbcTry(TryAction action) {
        try {
            requireNonNull(action, "Action must be provided").doTry();
        } catch (SQLException e) {
            close();
            throw newSQLRuntimeException(e);
        }
    }

    final  O withStatement(TryFunction action) {
        try {
            O result = action.apply(statement);
            if(!skipWarnings && statement.getWarnings() != null) {
                throw statement.getWarnings();
            }
            return result;
        } catch (SQLException e) {
            close();
            throw newSQLRuntimeException(e);
        }
    }

    final  Q setStatementParameter(TryConsumer action) {
        jdbcTry(() -> action.accept(statement));
        return (Q) this;
    }

    abstract S prepareStatement(Connection connection, String query, Object... params) throws SQLException;

    String asSQL(String query, Object... params) {
        return Utils.asSQL(query, params);
    }

    @Nonnull
    @Override
    public final String asSQL() {
        return sqlString;
    }

    @Override
    public String toString() {
        return asSQL();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy