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

io.github.mjcro.mosaic.handlers.sql.Mapper Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package io.github.mjcro.mosaic.handlers.sql;

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

/**
 * Defines behavior single data type should be read from {@link ResultSet}
 * and be written using {@link PreparedStatement} placeholders.
 */
public interface Mapper {
    /**
     * @return Table common name.
     */
    String getCommonName();

    /**
     * @return Column names.
     */
    String[] getColumnNames();

    /**
     * Set placeholder values into given statement.
     *
     * @param stmt   Statement to set placeholders into.
     * @param offset Starting offset.
     * @param value  Source value.
     * @throws SQLException On error.
     */
    void setPlaceholdersValue(PreparedStatement stmt, int offset, Object value) throws SQLException;

    /**
     * Reads value from result set.
     *
     * @param resultSet Result set with data.
     * @param offset    Starting offset.
     * @return Value.
     * @throws SQLException On error.
     */
    Object readObjectValue(ResultSet resultSet, int offset) throws SQLException;

    /**
     * Constructs new mapper with new common table name.
     *
     * @param commonName New common table name.
     * @return Mapper.
     */
    default Mapper withCommonName(String commonName) {
        if (commonName == null) {
            throw new NullPointerException("commonName");
        } else if (commonName.equals(getCommonName())) {
            return this;
        } else if (this instanceof CommonNameMapperDecorator) {
            return ((CommonNameMapperDecorator) this).getDecorated().withCommonName(commonName);
        } else {
            return new CommonNameMapperDecorator(this, commonName);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy