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

cool.scx.jdbc.result_handler.SingleValueHandler Maven / Gradle / Ivy

There is a newer version: 2.7.4
Show newest version
package cool.scx.jdbc.result_handler;

import cool.scx.jdbc.dialect.Dialect;

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

/**
 * a
 *
 * @param  t
 * @author scx567888
 * @version 0.0.1
 */
final class SingleValueHandler implements ResultHandler {

    /**
     * 列索引
     */
    private final int columnIndex;

    /**
     * 列名
     */
    private final String columnName;

    /**
     * 所需 class
     */
    private final Class clazz;

    /**
     * 使用名称还是索引
     */
    private final boolean useName;

    public SingleValueHandler() {
        this(1, null);
    }

    public SingleValueHandler(Class clazz) {
        this(1, clazz);
    }

    public SingleValueHandler(int columnIndex) {
        this(columnIndex, null);
    }

    public SingleValueHandler(String columnName) {
        this(columnName, null);
    }

    public SingleValueHandler(int columnIndex, Class clazz) {
        this(columnIndex, null, clazz, false);
    }

    public SingleValueHandler(String columnName, Class clazz) {
        this(1, columnName, clazz, true);
    }

    private SingleValueHandler(int columnIndex, String columnName, Class clazz, boolean useName) {
        this.columnIndex = columnIndex;
        this.columnName = columnName;
        this.clazz = clazz;
        this.useName = useName;
    }

    @Override
    @SuppressWarnings("unchecked")
    public T apply(ResultSet rs, Dialect dialect) throws SQLException {
        if (rs.next()) {
            if (this.clazz == null) {
                return (T) (this.useName ? rs.getObject(this.columnName) : rs.getObject(this.columnIndex));
            } else {
                return this.useName ? rs.getObject(this.columnName, clazz) : rs.getObject(this.columnIndex, clazz);
            }
        } else {
            return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy